fix (ldap): support only updating the bind-dn and/or bind-password (#3511)

* fix (ldap): support only updating the bind-dn and/or bind-password

* chore: changelog entry for PR #3511
This commit is contained in:
Jim authored and GitHub committed 2023-07-27 15:41:37 -04:00
1 parent da7e8f7c1c
commit f8ea8c9744
9 files changed
+211 -41

No files matched your search

-10
View File
@@ -127,16 +127,6 @@ func TestNewAuthMethod(t *testing.T) {
wantErrCode: errors.InvalidParameter,
wantErrContains: `https scheme in url "https://alice.com" is not either ldap or ldaps`,
},
{
name: "opt-error",
ctx: testCtx,
scopeId: "global",
urls: TestConvertToUrls(t, "ldaps://alice.com"),
opts: []Option{WithBindCredential(testCtx, "dn", "")},
wantErr: true,
wantErrCode: errors.InvalidParameter,
wantErrContains: "ldap.WithBindCredential: missing password",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
-4
View File
@@ -250,10 +250,6 @@ func WithBindCredential(ctx context.Context, dn, password string) Option {
switch {
case dn == "" && password == "":
return errors.New(ctx, errors.InvalidParameter, op, "missing both dn and password")
case dn != "" && password == "":
return errors.New(ctx, errors.InvalidParameter, op, "missing password")
case dn == "" && password != "":
return errors.New(ctx, errors.InvalidParameter, op, "missing dn")
}
o.withBindDn = dn
o.withBindPassword = password
+6 -6
View File
@@ -184,18 +184,18 @@ func Test_getOpts(t *testing.T) {
testOpts.withBindPassword = "password"
assert.Equal(opts, testOpts)
})
t.Run("WithBindCredential-missing-dn", func(t *testing.T) {
t.Run("WithBindCredential-only-password", func(t *testing.T) {
assert := assert.New(t)
opts, err := getOpts(WithBindCredential(testCtx, "", "password"))
require.Error(t, err)
require.NoError(t, err)
assert.Empty(opts.withBindDn)
assert.Empty(opts.withBindPassword)
assert.NotEmpty(opts.withBindPassword)
})
t.Run("WithBindCredential-missing-password", func(t *testing.T) {
t.Run("WithBindCredential-only-dn", func(t *testing.T) {
assert := assert.New(t)
opts, err := getOpts(WithBindCredential(testCtx, "dn", ""))
require.Error(t, err)
assert.Empty(opts.withBindDn)
require.NoError(t, err)
assert.NotEmpty(opts.withBindDn)
assert.Empty(opts.withBindPassword)
})
t.Run("WithBindCredential-missing-dn-and-password", func(t *testing.T) {
@@ -297,7 +297,7 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, am *AuthMethod, versi
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
}
if err := bc.encrypt(ctx, dbWrapper); err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("error wrapping updated bind credential"))
}
addBindCred = bc
}
@@ -494,6 +494,62 @@ func TestRepository_UpdateAuthMethod(t *testing.T) {
return am
},
},
{
name: "update-just-binddn",
ctx: testCtx,
repo: testRepo,
setup: func() *AuthMethod {
return TestAuthMethod(t,
testConn, databaseWrapper,
org.PublicId,
[]string{"ldaps://ldap1", "ldap://ldap2"},
WithBindCredential(testCtx, "orig-bind-dn", "orig-bind-password"),
)
},
updateWith: func(orig *AuthMethod) *AuthMethod {
am := AllocAuthMethod()
am.PublicId = orig.PublicId
am.BindDn = "bind-dn"
return &am
},
fieldMasks: []string{
BindDnField,
},
version: 1,
want: func(orig, updateWith *AuthMethod) *AuthMethod {
am := orig.clone()
am.BindDn = updateWith.BindDn
return am
},
},
{
name: "update-just-bind-password",
ctx: testCtx,
repo: testRepo,
setup: func() *AuthMethod {
return TestAuthMethod(t,
testConn, databaseWrapper,
org.PublicId,
[]string{"ldaps://ldap1", "ldap://ldap2"},
WithBindCredential(testCtx, "orig-bind-dn", "orig-bind-password"),
)
},
updateWith: func(orig *AuthMethod) *AuthMethod {
am := AllocAuthMethod()
am.PublicId = orig.PublicId
am.BindPassword = "bind-password"
return &am
},
fieldMasks: []string{
BindPasswordField,
},
version: 1,
want: func(orig, updateWith *AuthMethod) *AuthMethod {
am := orig.clone()
am.BindPassword = updateWith.BindPassword
return am
},
},
{
name: "missing-auth-method",
ctx: testCtx,