Skip to content

Commit

Permalink
Fix handling of username_as_alias during LDAP authentication
Browse files Browse the repository at this point in the history
There is a bug that was introduced in the LDAP authentication method by hashicorp#11000.
It was thought to be backward compatible but has broken a number of users. Later
a new parameter `username_as_alias` was introduced in hashicorp#14324
to make it possible for operators to restore the previous behavior.
The way it is currently working is not completely backward compatible thought
because when username_as_alias is set, a call to GetUserAliasAttributeValue() will
first be made, then this value is completely discarded in pathLogin() and replaced
by the username as expected.

This is an issue because it makes useless calls to the LDAP server and will break
backward compatibility if one of the constraints in GetUserAliasAttributeValue()
is not respected, even though the resulting value will be discarded anyway.

In order to maintain backward compatibility here we have to only call
GetUserAliasAttributeValue() if necessary.

Since this change of behavior was introduced in 1.9, this fix will need to be
backported to the 1.9, 1.10 and 1.11 branches.
  • Loading branch information
remilapeyre committed May 19, 2022
1 parent 73b49f4 commit 5d6f77a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
17 changes: 10 additions & 7 deletions builtin/credential/ldap/backend.go
Expand Up @@ -56,7 +56,7 @@ type backend struct {
*framework.Backend
}

func (b *backend) Login(ctx context.Context, req *logical.Request, username string, password string) (string, []string, *logical.Response, []string, error) {
func (b *backend) Login(ctx context.Context, req *logical.Request, username string, password string, usernameAsAlias bool) (string, []string, *logical.Response, []string, error) {
cfg, err := b.Config(ctx, req)
if err != nil {
return "", nil, nil, nil, err
Expand Down Expand Up @@ -195,12 +195,15 @@ func (b *backend) Login(ctx context.Context, req *logical.Request, username stri
// Policies from each group may overlap
policies = strutil.RemoveDuplicates(policies, true)

entityAliasAttribute, err := ldapClient.GetUserAliasAttributeValue(cfg.ConfigEntry, c, username)
if err != nil {
return "", nil, logical.ErrorResponse(err.Error()), nil, nil
}
if entityAliasAttribute == "" {
return "", nil, logical.ErrorResponse("missing entity alias attribute value"), nil, nil
entityAliasAttribute := username
if !usernameAsAlias {
entityAliasAttribute, err = ldapClient.GetUserAliasAttributeValue(cfg.ConfigEntry, c, username)
if err != nil {
return "", nil, logical.ErrorResponse(err.Error()), nil, nil
}
if entityAliasAttribute == "" {
return "", nil, logical.ErrorResponse("missing entity alias attribute value"), nil, nil
}
}

return entityAliasAttribute, policies, ldapResponse, allGroups, nil
Expand Down
8 changes: 2 additions & 6 deletions builtin/credential/ldap/path_login.go
Expand Up @@ -73,7 +73,7 @@ func (b *backend) pathLogin(ctx context.Context, req *logical.Request, d *framew
username := d.Get("username").(string)
password := d.Get("password").(string)

effectiveUsername, policies, resp, groupNames, err := b.Login(ctx, req, username, password)
effectiveUsername, policies, resp, groupNames, err := b.Login(ctx, req, username, password, cfg.UsernameAsAlias)
// Handle an internal error
if err != nil {
return nil, err
Expand Down Expand Up @@ -103,10 +103,6 @@ func (b *backend) pathLogin(ctx context.Context, req *logical.Request, d *framew
},
}

if cfg.UsernameAsAlias {
auth.Alias.Name = username
}

cfg.PopulateTokenAuth(auth)

// Add in configured policies from mappings
Expand Down Expand Up @@ -139,7 +135,7 @@ func (b *backend) pathLoginRenew(ctx context.Context, req *logical.Request, d *f
username := req.Auth.Metadata["username"]
password := req.Auth.InternalData["password"].(string)

_, loginPolicies, resp, groupNames, err := b.Login(ctx, req, username, password)
_, loginPolicies, resp, groupNames, err := b.Login(ctx, req, username, password, cfg.UsernameAsAlias)
if err != nil || (resp != nil && resp.IsError()) {
return resp, err
}
Expand Down

0 comments on commit 5d6f77a

Please sign in to comment.