From d8d6e3e400a786eb07da242591c7752a14c0ad96 Mon Sep 17 00:00:00 2001 From: Sulabh Upadhyay Date: Tue, 24 Nov 2015 14:38:00 +0530 Subject: [PATCH] LDAP-49 : Fix for Com4jException thrown by external users provider 1. AdConnectionHelper now connects to domain LDAP servers instead of forest GC ( Global Catalog) servers to get the users details. 2. More logging is added in AdConnectionHelper and WindowsAuthenticationHelper 3. More exception handling is added in AdConnectionHelper to catch exceptions thrown by Com4j. 4. Unit tests are also updated. Manual Testing of scenarios ( Compat mode and and non-compat mode windows authentication ) 1. Test on corpnet in which GC servers are available. 2. Testing on private active directory setup in which a. Two domains in same forest and AD global catalog is not present in both the domains. Windows auth for users from both domain to SonarQube server in one of the domain. b. Two domains in the same forest and AD global catalog is present for one of the domains. Windows auth for users from both domain to SonarQube server in one of the domain. c. Two domains ( sub-tree of third domain). Windows auth for users from both domain to SonarQube server in one of the domain. --- .../ldap/windows/AdConnectionHelper.java | 150 ++++++++++++------ .../windows/WindowsAuthenticationHelper.java | 19 ++- .../ldap/windows/AdConnectionHelperTest.java | 75 ++++++--- .../WindowsAuthenticationHelperTest.java | 2 +- 4 files changed, 170 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/sonar/plugins/ldap/windows/AdConnectionHelper.java b/src/main/java/org/sonar/plugins/ldap/windows/AdConnectionHelper.java index a7d6847..294fb39 100644 --- a/src/main/java/org/sonar/plugins/ldap/windows/AdConnectionHelper.java +++ b/src/main/java/org/sonar/plugins/ldap/windows/AdConnectionHelper.java @@ -19,7 +19,9 @@ */ package org.sonar.plugins.ldap.windows; +import com.google.common.annotations.VisibleForTesting; import com4j.ComException; +import com4j.ExecutionException; import com4j.typelibs.activeDirectory.IADs; import com4j.typelibs.ado20.Field; import com4j.typelibs.ado20.Fields; @@ -48,7 +50,7 @@ public class AdConnectionHelper { public static final String ADS_OBJECT_PROVIDER_STR = "ADsDSOObject"; /** - * Root of the directory data tree on a diretory server + * Root of the directory data tree on a directory server */ public static final String ROOT_DSE = "RootDSE"; @@ -111,8 +113,8 @@ public Map getUserDetails(final String domainName, final String Map userDetails = new HashMap<>(); _Connection connection = null; try { - String defaultNamingContext = getDefaultNamingContext(domainName); - if (defaultNamingContext == null) { + String activeDirectoryBindString = getActiveDirectoryBindString(domainName); + if (activeDirectoryBindString == null) { return userDetails; } @@ -121,7 +123,7 @@ public Map getUserDetails(final String domainName, final String return userDetails; } - userDetails = getUserDetailsFromAd(connection, defaultNamingContext, domainName, userName, requestedDetails); + userDetails = getUserDetailsFromAd(connection, activeDirectoryBindString, domainName, userName, requestedDetails); } finally { if (connection != null) { connection.close(); @@ -143,8 +145,8 @@ public Collection getUserGroupsInDomain(final String domainName, final S _Connection connection = null; try { - String defaultNamingContext = getDefaultNamingContext(domainName); - if (defaultNamingContext == null) { + String activeDirectoryBindString = getActiveDirectoryBindString(domainName); + if (activeDirectoryBindString == null) { return userGroups; } @@ -153,12 +155,12 @@ public Collection getUserGroupsInDomain(final String domainName, final S return userGroups; } - String userNameDn = getUserDistinguishedName(connection, defaultNamingContext, domainName, userName); + String userNameDn = getUserDistinguishedName(connection, activeDirectoryBindString, domainName, userName); if (StringUtils.isBlank(userNameDn)) { return userGroups; } - Collection adUserGroups = getUserGroupsFromAd(connection, defaultNamingContext, domainName, userNameDn, + Collection adUserGroups = getUserGroupsFromAd(connection, activeDirectoryBindString, domainName, userNameDn, requestedGroupIdAttribute); userGroups.addAll(adUserGroups); @@ -174,21 +176,35 @@ public Collection getUserGroupsInDomain(final String domainName, final S } /** - * Returns the default naming context of the active directory for given domain - * - * @return {@link String} Default naming context. + * Returns the bind string of one of the available active directory controllers in the given domain. */ - String getDefaultNamingContext(String domainName) { - String defaultNamingContext = null; - IADs rootDse; + @VisibleForTesting + String getActiveDirectoryBindString(String domainName) { + String connectionUrl = null; + + LOG.debug("Getting active directory bind string for domain: {}", domainName); + IADs rootDse = null; try { - rootDse = com4jWrapper.getObject(IADs.class, String.format("GC://%s/%s", domainName, ROOT_DSE), null); - defaultNamingContext = (String) rootDse.get(DEFAULT_NAMING_CONTEXT_STR); - } catch (ComException comException) { - LOG.debug("Unable to get {} for domain: {}: {}", DEFAULT_NAMING_CONTEXT_STR, domainName, comException.getMessage()); + rootDse = getRootDse(domainName); + if (rootDse != null) { + String defaultNamingContext = (String) getRootDseAttribute(rootDse, DEFAULT_NAMING_CONTEXT_STR); + if (StringUtils.isNotBlank(defaultNamingContext)) { + connectionUrl = String.format("LDAP://%s/%s", domainName, defaultNamingContext); + } + } + } finally { + if (rootDse != null) { + rootDse.dispose(); + } } - return defaultNamingContext; + if (StringUtils.isNotBlank(connectionUrl)) { + LOG.debug("Active directory bind string for the domain {}: {}", domainName, connectionUrl); + } else { + LOG.debug("Unable to get the active directory bind string for the domain {}", domainName); + } + + return connectionUrl; } /** @@ -196,18 +212,19 @@ String getDefaultNamingContext(String domainName) { * * @return {@link _Connection} */ + @VisibleForTesting _Connection getActiveDirectoryConnection() { _Connection connection = com4jWrapper.createConnection(); if (connection != null) { connection.provider(ADS_OBJECT_PROVIDER_STR); try { connection.open(DEFAULT_AD_CONNECTION_STR, "", "", -1); - } catch (ComException comException) { - LOG.error("Unable to get connection to active directory. {}", comException.getMessage()); + } catch (ComException | ExecutionException ex) { + LOG.error("Unable to get connection to the active directory. {}", ex.getMessage()); connection = null; } } else { - LOG.error("Unable to create connection to active directory."); + LOG.error("Unable to create connection to the active directory."); } return connection; @@ -218,6 +235,7 @@ _Connection getActiveDirectoryConnection() { * * @return {@link String} attributes value or null if the attribute is not found */ + @VisibleForTesting String getUserAttributeValue(final Fields userData, final String attributeName) { String attributeValue = null; try { @@ -226,7 +244,10 @@ String getUserAttributeValue(final Fields userData, final String attributeName) Object obj = field.value(); if (obj != null) { attributeValue = obj.toString(); + LOG.trace("Value of user attribute {}: {}", attributeName, attributeValue); } + } else { + LOG.debug("User attribute {} doesn't exist.", attributeName); } } catch (ComException comException) { LOG.debug("Unable to get {}. {}", attributeName, comException.getMessage()); @@ -235,39 +256,65 @@ String getUserAttributeValue(final Fields userData, final String attributeName) return attributeValue; } - private Map getUserDetailsFromAd(final _Connection connection, final String namingContext, String domainName, + private Object getRootDseAttribute(IADs rootDse, String attributeName) { + Object attributeValue = null; + try { + LOG.trace("Getting value of {} from {}", attributeName, ROOT_DSE); + attributeValue = rootDse.get(attributeName); + LOG.trace("Value of {} from {} : {}", attributeName, ROOT_DSE, attributeValue); + } catch (ComException comException) { + LOG.debug("Unable to get value of attribute {} from {}: {}", attributeName, ROOT_DSE, comException.getMessage()); + } + + return attributeValue; + } + + private IADs getRootDse(String domainName) { + IADs rootDse = null; + + String adBindString = String.format("LDAP://%s/%s", domainName, ROOT_DSE); + try { + rootDse = com4jWrapper.getObject(IADs.class, adBindString, null); + } catch (ComException | ExecutionException ex) { + // ExecutionException will be thrown if the server is unavailable + LOG.debug("Unable to get {} for the active directory bind string {}: {}", ROOT_DSE, adBindString, ex.getMessage()); + } + + return rootDse; + } + + private Map getUserDetailsFromAd(final _Connection connection, final String connectionUrl, String domainName, String userName, final Collection requestedDetails) { Map userDetails = new HashMap<>(); - String commandText = getUserDetailsCommandText(namingContext, userName, requestedDetails); - LOG.trace(commandText); + String commandText = getUserDetailsCommandText(connectionUrl, userName, requestedDetails); Collection> userDetailsRecords = executeQuery(connection, commandText, requestedDetails); if (userDetailsRecords.size() == 1) { userDetails = userDetailsRecords.iterator().next(); + } else { + LOG.debug("No details record for the user found: " + domainName + "\\" + userName); } return userDetails; } - private String getUserDistinguishedName(final _Connection connection, final String namingContext, final String domainName, + private String getUserDistinguishedName(final _Connection connection, final String connectionUrl, final String domainName, final String userName) { Collection requestedUserAttributes = new ArrayList<>(); requestedUserAttributes.add(DISTINGUISHED_NAME_STR); - Map userAttributes = getUserDetailsFromAd(connection, namingContext, domainName, userName, requestedUserAttributes); + Map userAttributes = getUserDetailsFromAd(connection, connectionUrl, domainName, userName, requestedUserAttributes); return userAttributes.get(DISTINGUISHED_NAME_STR); } - private Collection getUserGroupsFromAd(final _Connection connection, final String namingContext, String domainName, + private Collection getUserGroupsFromAd(final _Connection connection, final String connectionUrl, String domainName, final String userNameDn, final String requestedGroupIdAttribute) { Collection adUserGroups = new ArrayList<>(); - String commandText = getUserGroupsCommandText(namingContext, userNameDn, requestedGroupIdAttribute); - LOG.trace(commandText); - + String commandText = getUserGroupsCommandText(connectionUrl, userNameDn, requestedGroupIdAttribute); Collection requestedAttributes = new ArrayList<>(); requestedAttributes.add(requestedGroupIdAttribute); @@ -304,17 +351,21 @@ private Collection> executeQuery(final _Connection connectio private Collection> getDataFromRecordSet(final _Recordset recordSet, final Collection requestedDetails) { Collection> records = new ArrayList<>(); - while (!recordSet.eof()) { - Fields userData = recordSet.fields(); - if (userData != null) { - Map requestedDetailsMap = new HashMap<>(); - for (String requestedDetail : requestedDetails) { - String userAttributeValue = getUserAttributeValue(userData, requestedDetail); - requestedDetailsMap.put(requestedDetail, userAttributeValue); + try { + while (!recordSet.eof()) { + Fields userData = recordSet.fields(); + if (userData != null) { + Map requestedDetailsMap = new HashMap<>(); + for (String requestedDetail : requestedDetails) { + String userAttributeValue = getUserAttributeValue(userData, requestedDetail); + requestedDetailsMap.put(requestedDetail, userAttributeValue); + } + records.add(requestedDetailsMap); } - records.add(requestedDetailsMap); + recordSet.moveNext(); } - recordSet.moveNext(); + } catch (ComException comException) { + LOG.debug("Exception while getting data from the record-set : {} ", comException.getMessage()); } return records; @@ -326,10 +377,13 @@ private _Recordset executeCommand(final _Connection connection, final String com try { command = com4jWrapper.createCommand(connection, commandText); if (command != null) { + LOG.trace("Executing command: {}", commandText); recordSet = command.execute(null, com4jWrapper.getMissing(), -1); } else { - LOG.error("Unable to create the active directory command"); + LOG.error("Unable to create the active directory command {}", commandText); } + } catch (ComException comException) { + LOG.debug("Exception while executing the command : {} ", comException.getMessage()); } finally { if (command != null) { command.dispose(); @@ -340,27 +394,27 @@ private _Recordset executeCommand(final _Connection connection, final String com } /* - * User Details Command Text format ;(filter);requestedAttributes;scope - * e.g.;(sAMAccountName=userName);cn,mail;SubTree + * User Details Command Text format ;(filter);requestedAttributes;scope + * e.g.;(sAMAccountName=userName);cn,mail;SubTree */ - private static String getUserDetailsCommandText(final String namingContext, final String userName, + private String getUserDetailsCommandText(final String bindString, final String userName, final Collection requestedDetails) { /* Filter on sAMAccountName attribute */ String filter = String.format("(%s=%s)", SAMACCOUNTNAME_STR, userName); /* Requested user attributes */ String requestedAttributes = StringUtils.join(requestedDetails, ","); - return String.format(";%s;%s;SubTree", namingContext, filter, requestedAttributes); + return String.format("<%s>;%s;%s;SubTree", bindString, filter, requestedAttributes); } /* - * User Groups Command Text format ;(filter);requestedAttributes;scope + * User Groups Command Text format ;(filter);requestedAttributes;scope */ - private static String getUserGroupsCommandText(final String namingContext, final String userDn, + private String getUserGroupsCommandText(final String bindString, final String userDn, final String requestedDetail) { /* Filter on user dn attribute */ String filter = String.format("(&(objectClass=group)(member=%s))", userDn); - return String.format(";%s;%s;SubTree", namingContext, filter, requestedDetail); + return String.format("<%s>;%s;%s;SubTree", bindString, filter, requestedDetail); } } diff --git a/src/main/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelper.java b/src/main/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelper.java index 9a0cfdd..77f9eee 100644 --- a/src/main/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelper.java +++ b/src/main/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelper.java @@ -181,8 +181,21 @@ public UserDetails getSsoUserDetails(HttpServletRequest request) { @CheckForNull public UserDetails getUserDetails(String userName) { checkArgument(isNotEmpty(userName), "userName is null or empty."); + + LOG.debug("Getting details for user: {}", userName); + UserDetails userDetails = null; IWindowsAccount windowsAccount = getWindowsAccount(userName); - return windowsAccount != null ? getSsoUserDetails(windowsAccount) : null; + if (windowsAccount != null) { + userDetails = getUserDetails(windowsAccount); + } + + if (userDetails == null) { + LOG.debug("Unable to get details for user {}", userName); + } else { + LOG.debug("Details for user {}: {}", userName, userDetails); + } + + return userDetails; } /** @@ -208,10 +221,12 @@ public Collection getUserGroups(WindowsPrincipal windowsPrincipal) { } } + LOG.debug("Groups for the user {} : {}", windowsPrincipal.getName(), groups); + return groups; } - UserDetails getSsoUserDetails(IWindowsAccount windowsAccount) { + UserDetails getUserDetails(IWindowsAccount windowsAccount) { UserDetails userDetails = new UserDetails(); String windowsAccountName = getWindowsAccountName(new WindowsAccount(windowsAccount), diff --git a/src/test/java/org/sonar/plugins/ldap/windows/AdConnectionHelperTest.java b/src/test/java/org/sonar/plugins/ldap/windows/AdConnectionHelperTest.java index 40cc0c9..b56eaaa 100644 --- a/src/test/java/org/sonar/plugins/ldap/windows/AdConnectionHelperTest.java +++ b/src/test/java/org/sonar/plugins/ldap/windows/AdConnectionHelperTest.java @@ -53,6 +53,7 @@ public class AdConnectionHelperTest { private String userName; private String namingContext; private String userDistinguishedName; + private String adBindString; @Before public void init() { @@ -65,6 +66,7 @@ public void init() { userName = "userName"; namingContext = "dc=domain"; userDistinguishedName = "dn=User Distinguished Name"; + adBindString = "LDAP://" + domainName + "/" + namingContext; } @Test(expected = IllegalArgumentException.class) @@ -98,7 +100,7 @@ public void getUserDetailsRequestedAttributesEmptyArgumentCheck() { } @Test - public void getUserDetailsWhenGetDefaultNamingContextReturnsNull() { + public void getUserDetailsWhenGetConnectionUrlReturnsNull() { String testConnectionString = getTestConnectionString(domainName); when(com4jWrapper.getObject(IADs.class, testConnectionString, null)).thenThrow(mock(ComException.class)); @@ -120,27 +122,50 @@ public void getUserDetailsWhenGetActiveDirectoryReturnsNull() { } @Test - public void getUserDetailsWhenExecuteCommandReturnsNull() { + public void getUserDetailsWhenCom4JWrapperExecuteCommandReturnsNull() { setupTestDefaultNamingContext(domainName, namingContext); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserDetailsTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserDetailsTests); _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); when(com4jWrapper.createCommand(connection, commandText)).thenReturn(null); + when(connection.execute(null, com4jWrapper.getMissing(), -1)).thenReturn(null); assertThat(adConnectionHelper.getUserDetails(domainName, userName, userAttributesForGetUserDetailsTests)).isEmpty(); verify(com4jWrapper, times(1)).createCommand(connection, commandText); verify(com4jWrapper, times(1)).cleanUp(); } + @Test + public void getUserDetailsWhenCom4jWrapperExecuteCommandThrowsException() { + setupTestDefaultNamingContext(domainName, namingContext); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserDetailsTests); + + // ExecuteCommand returns null as executeCommand throws exception + _Connection connection = mock(_Connection.class); + when(com4jWrapper.createConnection()).thenReturn(connection); + + ComException comException = mock(ComException.class); + when(comException.getMessage()).thenReturn("Com4jComException: Exception"); + + _Command command = mock(_Command.class); + when(com4jWrapper.createCommand(connection, commandText)).thenReturn(command); + when(command.execute(null, com4jWrapper.getMissing(), -1)).thenThrow(comException); + + assertThat(adConnectionHelper.getUserDetails(domainName, userName, userAttributesForGetUserDetailsTests)).isEmpty(); + verify(com4jWrapper, times(1)).createCommand(connection, commandText); + verify(com4jWrapper, times(1)).cleanUp(); + verify(comException, times(1)).getMessage(); + } + @Test public void getUserDetailsWhenExecuteCommandReturnsNullRecordSet() { setupTestDefaultNamingContext(domainName, namingContext); _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserDetailsTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserDetailsTests); _Command command = mock(_Command.class); when(command.execute(null, com4jWrapper.getMissing(), -1)).thenReturn(null); @@ -158,7 +183,7 @@ public void getUserDetailsWhenRecordSetEofIsNotSet() { _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserDetailsTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserDetailsTests); RecordSetStub recordSet = getTestRecordSet(null); _Command command = mock(_Command.class); @@ -181,7 +206,7 @@ public void getUserDetailsNormalTest() { _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserDetailsTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserDetailsTests); Collection> fieldsRows = new ArrayList<>(); Map fieldsCollection = new HashMap<>(); @@ -260,7 +285,7 @@ public void getUserGroupsWhenExecuteCommandReturnsNull() { _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserGroupTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserGroupTests); when(com4jWrapper.createCommand(connection, commandText)).thenReturn(null); @@ -276,7 +301,7 @@ public void getUserGroupsWhenExecuteCommandFroUserDetailsReturnsNullRecordSet() _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserGroupTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserGroupTests); _Command command = mock(_Command.class); when(command.execute(null, com4jWrapper.getMissing(), -1)).thenReturn(null); @@ -294,7 +319,7 @@ public void getUserGroupsWhenUserDetailsRecordSetEofIsNotSet() { _Connection connection = mock(_Connection.class); when(com4jWrapper.createConnection()).thenReturn(connection); - String commandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserGroupTests); + String commandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserGroupTests); RecordSetStub recordSet = getTestRecordSet(null); _Command command = mock(_Command.class); @@ -324,7 +349,7 @@ public void getUserGroupsNormalTest() { userDetailsRows.add(userDetailsFieldsCollection); RecordSetStub userDetailsRecordSet = getTestRecordSet(userDetailsRows); - String userDetailsCommandText = getUserDetailsCommandText(namingContext, userName, userAttributesForGetUserGroupTests); + String userDetailsCommandText = getUserDetailsCommandText(adBindString, userName, userAttributesForGetUserGroupTests); _Command userDetailsCommand = mock(_Command.class); when(userDetailsCommand.execute(null, com4jWrapper.getMissing(), -1)).thenReturn(userDetailsRecordSet); @@ -345,7 +370,7 @@ public void getUserGroupsNormalTest() { expectedUserGroups.add("Group2"); RecordSetStub userGroupsRecordSet = getTestRecordSet(userGroupsRows); - String userGroupsCommandText = getUserGroupsCommandText(namingContext, userDistinguishedName, testRequestedGroupIdAttribute); + String userGroupsCommandText = getUserGroupsCommandText(adBindString, userDistinguishedName, testRequestedGroupIdAttribute); _Command userGroupsCommand = mock(_Command.class); when(userGroupsCommand.execute(null, com4jWrapper.getMissing(), -1)).thenReturn(userGroupsRecordSet); when(com4jWrapper.createCommand(connection, userGroupsCommandText)).thenReturn(userGroupsCommand); @@ -399,22 +424,22 @@ public void getActiveDirectoryConnectionNormalTest() { } @Test - public void getNamingContextTestCom4jGetObjectThrowsComException() { + public void getConnectionUrlTestCom4jGetObjectThrowsComException() { String testConnectionString = getTestConnectionString(domainName); ComException comException = mock(ComException.class); when(comException.getMessage()).thenReturn("ComException"); when(com4jWrapper.getObject(IADs.class, testConnectionString, null)).thenThrow(comException); - String namingContext = adConnectionHelper.getDefaultNamingContext(domainName); + String bindString = adConnectionHelper.getActiveDirectoryBindString(domainName); - assertThat(namingContext).isNull(); + assertThat(bindString).isNull(); verify(com4jWrapper, times(1)).getObject(IADs.class, testConnectionString, null); verify(comException, times(1)).getMessage(); } @Test - public void getNamingContextTestRootDseGetThrowsComException() { + public void getConnectionUrlTestRootDseGetThrowsComException() { String testConnectionString = getTestConnectionString(domainName); ComException comException = mock(ComException.class); when(comException.getMessage()).thenReturn("ComException"); @@ -422,18 +447,18 @@ public void getNamingContextTestRootDseGetThrowsComException() { when(iads.get(AdConnectionHelper.DEFAULT_NAMING_CONTEXT_STR)).thenThrow(comException); when(com4jWrapper.getObject(IADs.class, testConnectionString, null)).thenReturn(iads); - String namingContext = adConnectionHelper.getDefaultNamingContext(domainName); + String bindString = adConnectionHelper.getActiveDirectoryBindString(domainName); - assertThat(namingContext).isNull(); + assertThat(bindString).isNull(); verify(com4jWrapper, times(1)).getObject(IADs.class, testConnectionString, null); verify(comException, times(1)).getMessage(); } @Test - public void getNamingContextNormalTest() { + public void getConnectionUrlNormalTest() { setupTestDefaultNamingContext(domainName, namingContext); - assertThat(adConnectionHelper.getDefaultNamingContext(domainName)).isEqualTo(namingContext); + assertThat(adConnectionHelper.getActiveDirectoryBindString(domainName)).isEqualTo(adBindString); } @Test @@ -480,21 +505,21 @@ public void getUserAttributeValueNormalTest() { } private String getTestConnectionString(final String domainName) { - return "GC://" + domainName + "/" + AdConnectionHelper.ROOT_DSE; + return "LDAP://" + domainName + "/" + AdConnectionHelper.ROOT_DSE; } - private String getUserDetailsCommandText(final String namingContext, final String userName, final Collection requestedDetails) { + private String getUserDetailsCommandText(final String bindString, final String userName, final Collection requestedDetails) { /* Requested user attributes */ String requestedAttributes = StringUtils.join(requestedDetails, ","); - return String.format(";(%s=%s);%s;SubTree", namingContext, - AdConnectionHelper.SAMACCOUNTNAME_STR, userName, requestedAttributes); + return String.format("<%s>;(%s=%s);%s;SubTree", bindString, AdConnectionHelper.SAMACCOUNTNAME_STR, userName, + requestedAttributes); } - private String getUserGroupsCommandText(final String namingContext, final String userDistinguishedName, + private String getUserGroupsCommandText(final String bindString, final String userDistinguishedName, final String requestedGroupIdAttribute) { String filter = String.format("(&(objectClass=group)(member=%s))", userDistinguishedName); - return String.format(";%s;%s;SubTree", namingContext, filter, testRequestedGroupIdAttribute); + return String.format("<%s>;%s;%s;SubTree", bindString, filter, testRequestedGroupIdAttribute); } private Collection getRequestedUserAttributesForGetUserDetailsTests() { diff --git a/src/test/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelperTest.java b/src/test/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelperTest.java index ad354c2..33690e4 100644 --- a/src/test/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelperTest.java +++ b/src/test/java/org/sonar/plugins/ldap/windows/WindowsAuthenticationHelperTest.java @@ -450,7 +450,7 @@ private void runGetUserDetailsFromWindowsAccountTest(IWindowsAccount windowsAcco authenticationHelper = new WindowsAuthenticationHelper(windowsAuthSettings, windowsAuthProvider, adConnectionHelper); - assertThat(authenticationHelper.getSsoUserDetails(windowsAccount)).isEqualToComparingFieldByField(expectedUserDetails); + assertThat(authenticationHelper.getUserDetails(windowsAccount)).isEqualToComparingFieldByField(expectedUserDetails); } private void runGetUserGroupsTest(String domainName, String userName, Collection windowsAccounts,