2019-12-06 15:15:54 +07:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2020-12-03 12:23:52 +07:00
|
|
|
"errors"
|
2021-07-02 06:16:16 +07:00
|
|
|
"fmt"
|
2019-12-06 15:15:54 +07:00
|
|
|
"testing"
|
|
|
|
|
2020-03-19 11:22:46 +07:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2020-05-05 02:39:25 +07:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-21 02:34:53 +07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-06 15:15:54 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-13 18:12:50 +07:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-12-06 15:15:54 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCreateRawConnectionWhenSchemeIsLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2019-12-06 15:15:54 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2019-12-06 15:15:54 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2019-12-06 15:15:54 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connBind)
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
_, err := ldapClient.connect("cn=admin,dc=example,dc=com", "password")
|
2019-12-06 15:15:54 +07:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCreateTLSConnectionWhenSchemeIsLDAPS(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2019-12-06 15:15:54 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldaps://127.0.0.1:389"), gomock.Any()).
|
2019-12-06 15:15:54 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2019-12-06 15:15:54 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connBind)
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
_, err := ldapClient.connect("cn=admin,dc=example,dc=com", "password")
|
2019-12-06 15:15:54 +07:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
func TestEscapeSpecialCharsFromUserInput(t *testing.T) {
|
2020-01-21 02:34:53 +07:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// No escape.
|
2021-01-04 17:28:55 +07:00
|
|
|
assert.Equal(t, "xyz", ldapClient.ldapEscape("xyz"))
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// Escape.
|
2021-01-04 17:28:55 +07:00
|
|
|
assert.Equal(t, "test\\,abc", ldapClient.ldapEscape("test,abc"))
|
|
|
|
assert.Equal(t, "test\\5cabc", ldapClient.ldapEscape("test\\abc"))
|
|
|
|
assert.Equal(t, "test\\2aabc", ldapClient.ldapEscape("test*abc"))
|
|
|
|
assert.Equal(t, "test \\28abc\\29", ldapClient.ldapEscape("test (abc)"))
|
|
|
|
assert.Equal(t, "test\\#abc", ldapClient.ldapEscape("test#abc"))
|
|
|
|
assert.Equal(t, "test\\+abc", ldapClient.ldapEscape("test+abc"))
|
|
|
|
assert.Equal(t, "test\\<abc", ldapClient.ldapEscape("test<abc"))
|
|
|
|
assert.Equal(t, "test\\>abc", ldapClient.ldapEscape("test>abc"))
|
|
|
|
assert.Equal(t, "test\\;abc", ldapClient.ldapEscape("test;abc"))
|
|
|
|
assert.Equal(t, "test\\\"abc", ldapClient.ldapEscape("test\"abc"))
|
|
|
|
assert.Equal(t, "test\\=abc", ldapClient.ldapEscape("test=abc"))
|
|
|
|
assert.Equal(t, "test\\,\\5c\\28abc\\29", ldapClient.ldapEscape("test,\\(abc)"))
|
2020-03-31 05:36:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEscapeSpecialCharsInGroupsFilter(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
GroupsFilter: "(|(member={dn})(uid={username})(uid={input}))",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-03-31 05:36:04 +07:00
|
|
|
|
|
|
|
profile := ldapUserProfile{
|
2020-06-19 17:50:21 +07:00
|
|
|
DN: "cn=john (external),dc=example,dc=com",
|
|
|
|
Username: "john",
|
|
|
|
DisplayName: "John Doe",
|
|
|
|
Emails: []string{"john.doe@authelia.com"},
|
2020-03-31 05:36:04 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
filter, _ := ldapClient.resolveGroupsFilter("john", &profile)
|
2020-03-31 05:36:04 +07:00
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john))", filter)
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
filter, _ = ldapClient.resolveGroupsFilter("john#=(abc,def)", &profile)
|
2020-03-31 05:36:04 +07:00
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john\\#\\=\\28abc\\,def\\29))", filter)
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
type ExtendedSearchRequestMatcher struct {
|
|
|
|
filter string
|
|
|
|
baseDN string
|
|
|
|
scope int
|
|
|
|
derefAliases int
|
|
|
|
typesOnly bool
|
|
|
|
attributes []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExtendedSearchRequestMatcher(filter, base string, scope, derefAliases int, typesOnly bool, attributes []string) *ExtendedSearchRequestMatcher {
|
|
|
|
return &ExtendedSearchRequestMatcher{filter, base, scope, derefAliases, typesOnly, attributes}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExtendedSearchRequestMatcher) Matches(x interface{}) bool {
|
|
|
|
sr := x.(*ldap.SearchRequest)
|
|
|
|
|
|
|
|
if e.filter != sr.Filter || e.baseDN != sr.BaseDN || e.scope != sr.Scope || e.derefAliases != sr.DerefAliases ||
|
|
|
|
e.typesOnly != sr.TypesOnly || utils.IsStringSlicesDifferent(e.attributes, sr.Attributes) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExtendedSearchRequestMatcher) String() string {
|
|
|
|
return fmt.Sprintf("baseDN: %s, filter %s", e.baseDN, e.filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckLDAPServerExtensions(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-07-02 06:16:16 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-02 06:16:16 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
searchOIDs := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: ldapSupportedExtensionAttribute,
|
|
|
|
Values: []string{ldapOIDPasswdModifyExtension},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
gomock.InOrder(dialURL, connBind, searchOIDs, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-02 06:16:16 +07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, ldapClient.supportExtensionPasswdModify)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotEnablePasswdModifyExtension(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-07-02 06:16:16 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-02 06:16:16 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
searchOIDs := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: ldapSupportedExtensionAttribute,
|
|
|
|
Values: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
gomock.InOrder(dialURL, connBind, searchOIDs, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-02 06:16:16 +07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, ldapClient.supportExtensionPasswdModify)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnCheckServerConnectError(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-07-02 06:16:16 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-02 06:16:16 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, errors.New("could not connect"))
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-02 06:16:16 +07:00
|
|
|
assert.EqualError(t, err, "could not connect")
|
|
|
|
|
|
|
|
assert.False(t, ldapClient.supportExtensionPasswdModify)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnCheckServerSearchError(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-07-02 06:16:16 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-02 06:16:16 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
searchOIDs := mockConn.EXPECT().
|
2021-07-02 06:16:16 +07:00
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(nil, errors.New("could not perform the search"))
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
gomock.InOrder(dialURL, connBind, searchOIDs, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-02 06:16:16 +07:00
|
|
|
assert.EqualError(t, err, "could not perform the search")
|
|
|
|
|
|
|
|
assert.False(t, ldapClient.supportExtensionPasswdModify)
|
|
|
|
}
|
|
|
|
|
2020-01-21 02:34:53 +07:00
|
|
|
type SearchRequestMatcher struct {
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSearchRequestMatcher(expected string) *SearchRequestMatcher {
|
|
|
|
return &SearchRequestMatcher{expected}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srm *SearchRequestMatcher) Matches(x interface{}) bool {
|
|
|
|
sr := x.(*ldap.SearchRequest)
|
|
|
|
return sr.Filter == srm.expected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srm *SearchRequestMatcher) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldEscapeUserInput(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
// Here we ensure that the input has been correctly escaped.
|
2020-03-31 05:36:04 +07:00
|
|
|
Search(NewSearchRequestMatcher("(|(uid=john\\=abc)(mail=john\\=abc))")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
_, err := ldapClient.getUserProfile(mockConn, "john=abc")
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "user not found")
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCombineUsernameFilterAndUsersFilter(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
UsersFilter: "(&({username_attribute}={input})(&(objectCategory=person)(objectClass=user)))",
|
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
assert.True(t, ldapClient.usersFilterReplacementInput)
|
|
|
|
|
2020-01-21 02:34:53 +07:00
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
Search(NewSearchRequestMatcher("(&(uid=john)(&(objectCategory=person)(objectClass=user)))")).
|
2020-01-21 02:34:53 +07:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
_, err := ldapClient.getUserProfile(mockConn, "john")
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "user not found")
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
func createSearchResultWithAttributes(attributes ...*ldap.EntryAttribute) *ldap.SearchResult {
|
|
|
|
return &ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{Attributes: attributes},
|
2020-02-28 05:21:07 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSearchResultWithAttributeValues(values ...string) *ldap.SearchResult {
|
|
|
|
return createSearchResultWithAttributes(&ldap.EntryAttribute{
|
|
|
|
Values: values,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCrashWhenGroupsAreNotRetrievedFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(mockConn, nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
2021-07-13 18:12:50 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-06-19 17:50:21 +07:00
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2020-06-19 17:50:21 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connBind, searchProfile, searchGroups, connClose)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
2020-06-19 17:50:21 +07:00
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
2020-03-15 14:10:25 +07:00
|
|
|
assert.Equal(t, details.Username, "john")
|
2020-02-28 05:21:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCrashWhenEmailsAreNotRetrievedFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(mockConn, nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
|
2021-07-13 18:12:50 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connBind, searchProfile, searchGroups, connClose)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{"group1", "group2"})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{})
|
2020-03-15 14:10:25 +07:00
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnUsernameFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-03-15 14:10:25 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
2020-03-15 14:10:25 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
|
2021-07-13 18:12:50 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-06-19 17:50:21 +07:00
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2020-06-19 17:50:21 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connBind, searchProfile, searchGroups, connClose)
|
2020-03-15 14:10:25 +07:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{"group1", "group2"})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
2020-06-19 17:50:21 +07:00
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
2020-03-15 14:10:25 +07:00
|
|
|
assert.Equal(t, details.Username, "John")
|
2020-02-28 05:21:07 +07:00
|
|
|
}
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
func TestShouldUpdateUserPasswordPasswdModifyExtension(t *testing.T) {
|
2021-01-04 17:28:55 +07:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
2021-07-06 16:13:17 +07:00
|
|
|
pwdModifyRequest := ldap.NewPasswordModifyRequest(
|
|
|
|
"uid=test,dc=example,dc=com",
|
|
|
|
"",
|
|
|
|
"password",
|
|
|
|
)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURLOIDs := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
2021-07-06 16:13:17 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBindOIDs := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
searchOIDs := mockConn.EXPECT().
|
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: ldapSupportedExtensionAttribute,
|
|
|
|
Values: []string{ldapOIDPasswdModifyExtension},
|
2021-07-06 16:13:17 +07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-13 18:12:50 +07:00
|
|
|
},
|
|
|
|
}, nil)
|
2021-07-06 16:13:17 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connCloseOIDs := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
dialURL := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
connBind := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2021-07-13 18:12:50 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
2021-01-04 17:28:55 +07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-13 18:12:50 +07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
passwdModify := mockConn.EXPECT().
|
|
|
|
PasswordModify(pwdModifyRequest).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
gomock.InOrder(dialURLOIDs, connBindOIDs, searchOIDs, connCloseOIDs, dialURL, connBind, searchProfile, passwdModify, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-13 18:12:50 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ldapClient.UpdatePassword("john", "password")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdateUserPasswordActiveDirectory(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
Implementation: "activedirectory",
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "sAMAccountName",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayName",
|
|
|
|
UsersFilter: "cn={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-13 18:12:50 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
|
|
|
|
pwdEncoded, _ := utf16.NewEncoder().String("\"password\"")
|
|
|
|
|
|
|
|
modifyRequest := ldap.NewModifyRequest(
|
|
|
|
"cn=test,dc=example,dc=com",
|
|
|
|
nil,
|
2021-01-04 17:28:55 +07:00
|
|
|
)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
modifyRequest.Replace("unicodePwd", []string{pwdEncoded})
|
|
|
|
|
|
|
|
dialURLOIDs := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
connBindOIDs := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
searchOIDs := mockConn.EXPECT().
|
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: ldapSupportedExtensionAttribute,
|
|
|
|
Values: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
connCloseOIDs := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
dialURL := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
connBind := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "cn=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2021-07-13 18:12:50 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
passwdModify := mockConn.EXPECT().
|
|
|
|
Modify(modifyRequest).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
gomock.InOrder(dialURLOIDs, connBindOIDs, searchOIDs, connCloseOIDs, dialURL, connBind, searchProfile, passwdModify, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-13 18:12:50 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ldapClient.UpdatePassword("john", "password")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdateUserPasswordBasic(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := newLDAPUserProvider(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
Implementation: "custom",
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "uid=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayName",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-07-13 18:12:50 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
modifyRequest := ldap.NewModifyRequest(
|
|
|
|
"uid=test,dc=example,dc=com",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
modifyRequest.Replace("userPassword", []string{"password"})
|
|
|
|
|
|
|
|
dialURLOIDs := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
connBindOIDs := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
searchOIDs := mockConn.EXPECT().
|
|
|
|
Search(NewExtendedSearchRequestMatcher("(objectClass=*)", "", ldap.ScopeBaseObject, ldap.NeverDerefAliases, false, []string{ldapSupportedExtensionAttribute})).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: ldapSupportedExtensionAttribute,
|
|
|
|
Values: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
connCloseOIDs := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
dialURL := mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
connBind := mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
connClose := mockConn.EXPECT().Close()
|
|
|
|
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayName",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
passwdModify := mockConn.EXPECT().
|
|
|
|
Modify(modifyRequest).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
gomock.InOrder(dialURLOIDs, connBindOIDs, searchOIDs, connCloseOIDs, dialURL, connBind, searchProfile, passwdModify, connClose)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err := ldapClient.StartupCheck()
|
2021-07-06 16:13:17 +07:00
|
|
|
require.NoError(t, err)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2021-07-06 16:13:17 +07:00
|
|
|
err = ldapClient.UpdatePassword("john", "password")
|
2021-01-04 17:28:55 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckValidUserPassword(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=test,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
2021-07-13 18:12:50 +07:00
|
|
|
mockConn.EXPECT().Close().Times(2),
|
2021-01-04 17:28:55 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
valid, err := ldapClient.CheckUserPassword("john", "password")
|
|
|
|
|
|
|
|
assert.True(t, valid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckInvalidUserPassword(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=test,dc=example,dc=com"), gomock.Eq("password")).
|
2021-12-02 17:28:16 +07:00
|
|
|
Return(errors.New("invalid username or password")),
|
2021-07-13 18:12:50 +07:00
|
|
|
mockConn.EXPECT().Close(),
|
2021-01-04 17:28:55 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
valid, err := ldapClient.CheckUserPassword("john", "password")
|
|
|
|
|
|
|
|
assert.False(t, valid)
|
2021-12-02 17:28:16 +07:00
|
|
|
require.EqualError(t, err, "authentication failed. Cause: invalid username or password")
|
2021-01-04 17:28:55 +07:00
|
|
|
}
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
func TestShouldCallStartTLSWhenEnabled(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 12:23:52 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2020-12-03 12:23:52 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connStartTLS := mockConn.EXPECT().
|
2020-12-03 12:23:52 +07:00
|
|
|
StartTLS(ldapClient.tlsConfig)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
2020-12-03 12:23:52 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
2021-07-13 18:12:50 +07:00
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2020-12-03 12:23:52 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connStartTLS, connBind, searchProfile, searchGroups, connClose)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func TestShouldParseDynamicConfiguration(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-04-16 08:44:37 +07:00
|
|
|
UsersFilter: "(&(|({username_attribute}={input})({mail_attribute}={input})({display_name_attribute}={input}))(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!pwdLastSet=0))",
|
|
|
|
GroupsFilter: "(&(|(member={dn})(member={input})(member={username}))(objectClass=group))",
|
2021-01-04 17:28:55 +07:00
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
AdditionalGroupsDN: "ou=groups",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
assert.True(t, ldapClient.groupsFilterReplacementInput)
|
|
|
|
assert.True(t, ldapClient.groupsFilterReplacementUsername)
|
|
|
|
assert.True(t, ldapClient.groupsFilterReplacementDN)
|
|
|
|
|
|
|
|
assert.True(t, ldapClient.usersFilterReplacementInput)
|
|
|
|
|
|
|
|
assert.Equal(t, "(&(|(uid={input})(mail={input})(displayName={input}))(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!pwdLastSet=0))", ldapClient.configuration.UsersFilter)
|
2021-01-04 17:28:55 +07:00
|
|
|
assert.Equal(t, "(&(|(member={dn})(member={input})(member={username}))(objectClass=group))", ldapClient.configuration.GroupsFilter)
|
2021-04-12 08:10:50 +07:00
|
|
|
assert.Equal(t, "ou=users,dc=example,dc=com", ldapClient.usersBaseDN)
|
|
|
|
assert.Equal(t, "ou=groups,dc=example,dc=com", ldapClient.groupsBaseDN)
|
2021-01-04 17:28:55 +07:00
|
|
|
}
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
func TestShouldCallStartTLSWithInsecureSkipVerifyWhenSkipVerifyTrue(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
TLS: &schema.TLSConfig{
|
|
|
|
SkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
assert.False(t, ldapClient.groupsFilterReplacementInput)
|
|
|
|
assert.False(t, ldapClient.groupsFilterReplacementUsername)
|
|
|
|
assert.False(t, ldapClient.groupsFilterReplacementDN)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 12:23:52 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connBind := mockConn.EXPECT().
|
2020-12-03 12:23:52 +07:00
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connStartTLS := mockConn.EXPECT().
|
2020-12-03 12:23:52 +07:00
|
|
|
StartTLS(ldapClient.tlsConfig)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connClose := mockConn.EXPECT().Close()
|
2020-12-03 12:23:52 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
2021-07-13 18:12:50 +07:00
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
2021-08-05 11:17:07 +07:00
|
|
|
Name: "displayName",
|
2020-12-03 12:23:52 +07:00
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connStartTLS, connBind, searchProfile, searchGroups, connClose)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnLDAPSAlreadySecuredWhenStartTLSAttempted(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
ldapClient := newLDAPUserProvider(
|
2021-01-04 17:28:55 +07:00
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2021-08-05 11:17:07 +07:00
|
|
|
DisplayNameAttribute: "displayName",
|
2021-01-04 17:28:55 +07:00
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
TLS: &schema.TLSConfig{
|
|
|
|
SkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
2021-09-17 16:53:59 +07:00
|
|
|
false,
|
2021-01-04 17:28:55 +07:00
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
dialURL := mockFactory.EXPECT().
|
2021-01-04 17:28:55 +07:00
|
|
|
DialURL(gomock.Eq("ldaps://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 12:23:52 +07:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
connStartTLS := mockConn.EXPECT().
|
2020-12-03 12:23:52 +07:00
|
|
|
StartTLS(ldapClient.tlsConfig).
|
|
|
|
Return(errors.New("LDAP Result Code 200 \"Network Error\": ldap: already encrypted"))
|
|
|
|
|
2021-07-13 18:12:50 +07:00
|
|
|
gomock.InOrder(dialURL, connStartTLS)
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
_, err := ldapClient.GetDetails("john")
|
|
|
|
assert.EqualError(t, err, "LDAP Result Code 200 \"Network Error\": ldap: already encrypted")
|
|
|
|
}
|