Attempt to link SAML (all OAuth) users to LDAP by email
Zendesk: https://gitlab.zendesk.com/agent/tickets/78679
Currently, during the OAuth process, GitLab will attempt to link an LDAP user - first by querying LDAP for the OAuth 'UID' by LDAP UID and then by DN. This is because the OAuth process, especially in the case of SAML, may contain either the LDAP UID or DN. In this customer's case, the SAML response actually contains the user's email address rather than UID or DN. Currently, that means that the LDAP link fails and the user's groups aren't properly synced via LDAP group sync.
At a minimum we could add a third lookup so we had first lookup by UID, then DN, then email. Unfortunately, this means up to 3 queries each and everything time a user signs in. This logic is in Gitlab::OAuth::User#ldap_person
. There are a couple of ways I think we can reduce this:
- Turn the individual queries into a compound filter query. Instead of
(uid=foo)
and then(dn=foo)
, we could build an OR query like(|(uid=foo)(dn=foo)(email=foo))
and take the result. We may need to check if there are multiple values returned and have some order of precedence, but it is probably more performant. - Swap the order in the
Gitlab::SAML::User#gl_user
method. Currently,Gitlab::SAML::User#gl_user
first tries to link an LDAP user by querying LDAP, then looks internally for a user with a matching email address. We should look up the local user by email address first and then query LDAP only if the user we find doesn't have an LDAP identity. This will eliminate unnecessary queries to LDAP.