diff --git a/crowd/src/main/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtil.java b/crowd/src/main/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtil.java index dee118c4..cd86096c 100644 --- a/crowd/src/main/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtil.java +++ b/crowd/src/main/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtil.java @@ -6,8 +6,6 @@ import com.atlassian.crowd.directory.InternalDirectory; import com.atlassian.crowd.directory.MicrosoftActiveDirectory; import com.atlassian.crowd.directory.SynchronisableDirectoryProperties; -import com.atlassian.crowd.directory.ldap.LDAPPropertiesMapper; -import com.atlassian.crowd.directory.ldap.LdapSecureMode; import com.atlassian.crowd.directory.monitor.poller.PollerConfig; import com.atlassian.crowd.embedded.api.Directory; import com.atlassian.crowd.embedded.api.DirectoryType; @@ -38,6 +36,41 @@ public class DirectoryModelUtil { public static final String ATTRIBUTE_USE_NESTED_GROUPS = "useNestedGroups"; + static final String LDAP_URL_KEY = "ldap.url"; + static final String LDAP_SECURE_KEY = "ldap.secure"; + static final String LDAP_REFERRAL_KEY = "ldap.referral"; + static final String LDAP_BASEDN_KEY = "ldap.basedn"; + static final String LDAP_USERDN_KEY = "ldap.userdn"; + static final String LDAP_PASSWORD_KEY = "ldap.password"; + static final String LDAP_PAGEDRESULTS_KEY = "ldap.pagedresults"; + static final String LDAP_PAGEDRESULTS_SIZE = "ldap.pagedresults.size"; + static final String LDAP_NESTED_GROUPS_DISABLED = "ldap.nestedgroups.disabled"; + static final String LDAP_FILTER_EXPIRED_USERS = "ldap.filter.expiredUsers"; + static final String LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE = "ldap.usermembership.use"; + static final String LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE_FOR_GROUP_MEMBERSHIP = "ldap.usermembership.use.for.groups"; + static final String LDAP_RELAXED_DN_STANDARDISATION = "ldap.relaxed.dn.standardisation"; + static final String LDAP_SEARCH_TIMELIMIT = "ldap.search.timelimit"; + static final String LDAP_EXTERNAL_ID = "ldap.external.id"; + static final String LDAP_POOL_TYPE = "ldap.pool.type"; + static final String ROLES_DISABLED = "ldap.roles.disabled"; + static final String LOCAL_GROUPS = "ldap.local.groups"; + static final String GROUP_DN_ADDITION = "ldap.group.dn"; + static final String GROUP_DESCRIPTION_KEY = "ldap.group.description"; + static final String GROUP_NAME_KEY = "ldap.group.name"; + static final String GROUP_OBJECTCLASS_KEY = "ldap.group.objectclass"; + static final String GROUP_OBJECTFILTER_KEY = "ldap.group.filter"; + static final String GROUP_USERNAMES_KEY = "ldap.group.usernames"; + static final String USER_DN_ADDITION = "ldap.user.dn"; + static final String USER_EMAIL_KEY = "ldap.user.email"; + static final String USER_FIRSTNAME_KEY = "ldap.user.firstname"; + static final String USER_GROUP_KEY = "ldap.user.group"; + static final String USER_LASTNAME_KEY = "ldap.user.lastname"; + static final String USER_DISPLAYNAME_KEY = "ldap.user.displayname"; + static final String USER_OBJECTCLASS_KEY = "ldap.user.objectclass"; + static final String USER_OBJECTFILTER_KEY = "ldap.user.filter"; + static final String USER_USERNAME_KEY = "ldap.user.username"; + static final String USER_USERNAME_RDN_KEY = "ldap.user.username.rdn"; + private static final Set> SUPPORTED_DIRECTORY_BEAN_TYPES; static { @@ -103,41 +136,41 @@ public static DirectoryDelegatingModel toDirectoryDelegatingModel( @Nonnull final Directory directory) { DirectoryDelegatingModel.DirectoryDelegatingConnector connector = DirectoryDelegatingModel.DirectoryDelegatingConnector.builder() .type(toDirectoryDelegatingConnectorType(directory)) - .url(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_URL_KEY)) + .url(directory.getAttributes().get(LDAP_URL_KEY)) .ssl(toDirectoryDelegatingConnectorSslType(directory)) - .useNodeReferrals(toBoolean(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_REFERRAL_KEY))) - .nestedGroupsDisabled(toBoolean(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_NESTED_GROUPS_DISABLED))) + .useNodeReferrals(toBoolean(directory.getAttributes().get(LDAP_REFERRAL_KEY))) + .nestedGroupsDisabled(toBoolean(directory.getAttributes().get(LDAP_NESTED_GROUPS_DISABLED))) .synchronizeUsers(toBoolean(directory.getAttributes().get(DelegatedAuthenticationDirectory.ATTRIBUTE_CREATE_USER_ON_AUTH))) .synchronizeUserDetails(toBoolean(directory.getAttributes().get(DelegatedAuthenticationDirectory.ATTRIBUTE_UPDATE_USER_ON_AUTH))) .synchronizeGroupMemberships(toBoolean(directory.getAttributes().get(DelegatedAuthenticationDirectory.ATTRIBUTE_KEY_IMPORT_GROUPS))) - .useUserMembershipAttribute(toBoolean(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE))) - .usePagedResults(toBoolean(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_KEY))) - .pagedResultsSize(toLong(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_SIZE))) + .useUserMembershipAttribute(toBoolean(directory.getAttributes().get(LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE))) + .usePagedResults(toBoolean(directory.getAttributes().get(LDAP_PAGEDRESULTS_KEY))) + .pagedResultsSize(toLong(directory.getAttributes().get(LDAP_PAGEDRESULTS_SIZE))) .readTimeoutInMillis(toLong(directory.getAttributes().get(SynchronisableDirectoryProperties.READ_TIMEOUT_IN_MILLISECONDS))) - .searchTimeoutInMillis(toLong(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_SEARCH_TIMELIMIT))) + .searchTimeoutInMillis(toLong(directory.getAttributes().get(LDAP_SEARCH_TIMELIMIT))) .connectionTimeoutInMillis(toLong(directory.getAttributes().get(SynchronisableDirectoryProperties.CONNECTION_TIMEOUT_IN_MILLISECONDS))) - .baseDn(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_BASEDN_KEY)) - .username(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_USERDN_KEY)) + .baseDn(directory.getAttributes().get(LDAP_BASEDN_KEY)) + .username(directory.getAttributes().get(LDAP_USERDN_KEY)) .build(); DirectoryDelegatingModel.DirectoryDelegatingConfiguration configuration = DirectoryDelegatingModel.DirectoryDelegatingConfiguration.builder() - .userDn(directory.getAttributes().get(LDAPPropertiesMapper.USER_DN_ADDITION)) - .userObjectClass(directory.getAttributes().get(LDAPPropertiesMapper.USER_OBJECTCLASS_KEY)) - .userObjectFilter(directory.getAttributes().get(LDAPPropertiesMapper.USER_OBJECTFILTER_KEY)) - .userNameAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_USERNAME_KEY)) - .userNameRdnAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_USERNAME_RDN_KEY)) - .userFirstNameAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_FIRSTNAME_KEY)) - .userLastNameAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_LASTNAME_KEY)) - .userDisplayNameAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_DISPLAYNAME_KEY)) - .userEmailAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_EMAIL_KEY)) - .userGroupAttribute(directory.getAttributes().get(LDAPPropertiesMapper.USER_GROUP_KEY)) - .userUniqueIdAttribute(directory.getAttributes().get(LDAPPropertiesMapper.LDAP_EXTERNAL_ID)) - .groupDn(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_DN_ADDITION)) - .groupObjectClass(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_OBJECTCLASS_KEY)) - .groupObjectFilter(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_OBJECTFILTER_KEY)) - .groupNameAttribute(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_NAME_KEY)) - .groupDescriptionAttribute(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_DESCRIPTION_KEY)) - .groupMembersAttribute(directory.getAttributes().get(LDAPPropertiesMapper.GROUP_USERNAMES_KEY)) + .userDn(directory.getAttributes().get(USER_DN_ADDITION)) + .userObjectClass(directory.getAttributes().get(USER_OBJECTCLASS_KEY)) + .userObjectFilter(directory.getAttributes().get(USER_OBJECTFILTER_KEY)) + .userNameAttribute(directory.getAttributes().get(USER_USERNAME_KEY)) + .userNameRdnAttribute(directory.getAttributes().get(USER_USERNAME_RDN_KEY)) + .userFirstNameAttribute(directory.getAttributes().get(USER_FIRSTNAME_KEY)) + .userLastNameAttribute(directory.getAttributes().get(USER_LASTNAME_KEY)) + .userDisplayNameAttribute(directory.getAttributes().get(USER_DISPLAYNAME_KEY)) + .userEmailAttribute(directory.getAttributes().get(USER_EMAIL_KEY)) + .userGroupAttribute(directory.getAttributes().get(USER_GROUP_KEY)) + .userUniqueIdAttribute(directory.getAttributes().get(LDAP_EXTERNAL_ID)) + .groupDn(directory.getAttributes().get(GROUP_DN_ADDITION)) + .groupObjectClass(directory.getAttributes().get(GROUP_OBJECTCLASS_KEY)) + .groupObjectFilter(directory.getAttributes().get(GROUP_OBJECTFILTER_KEY)) + .groupNameAttribute(directory.getAttributes().get(GROUP_NAME_KEY)) + .groupDescriptionAttribute(directory.getAttributes().get(GROUP_DESCRIPTION_KEY)) + .groupMembersAttribute(directory.getAttributes().get(GROUP_USERNAMES_KEY)) .build(); DirectoryDelegatingModel.DirectoryDelegatingModelBuilder builder = DirectoryDelegatingModel.builder(); @@ -153,10 +186,15 @@ public static DirectoryDelegatingModel toDirectoryDelegatingModel( private static DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType toDirectoryDelegatingConnectorSslType( @Nonnull final Directory directory) { - final String ldapSecure = directory.getAttributes().get(LDAPPropertiesMapper.LDAP_SECURE_KEY); - // LdapSecureMode.fromString evaluates to the default value NONE ("false") if ldapSecure is null - final LdapSecureMode ldapSecureMode = LdapSecureMode.fromString(ldapSecure); - return DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.valueOf(ldapSecureMode.name().toUpperCase()); + final String ldapSecure = directory.getAttributes().get(LDAP_SECURE_KEY); + + if ("true".equalsIgnoreCase(ldapSecure)) { + return DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.LDAPS; + } else if ("starttls".equalsIgnoreCase(ldapSecure)) { + return DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.START_TLS; + } + + return DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.NONE; } @Nonnull @@ -346,14 +384,20 @@ private static String toDirectoryDelegatedConnectorTypeClass( } @Nullable - private static String toDirectoryDelegatingConnectorSecureModeName( + static String toDirectoryDelegatingConnectorSecureModeName( @Nullable final DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType sslType) { if (sslType == null) { return null; } - return LdapSecureMode.valueOf(sslType.name()).getName(); + if (sslType == DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.LDAPS) { + return "true"; + } else if (sslType == DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.START_TLS) { + return "starttls"; + } + + return "false"; } private static void setDirectoryAttributes( @@ -385,43 +429,43 @@ private static void setDirectoryAttributes( final DirectoryDelegatingModel.DirectoryDelegatingConnector connector = directoryDelegatingModel.getConnector(); if (connector != null) { setAttributeIfNotNull(attributes, DelegatedAuthenticationDirectory.ATTRIBUTE_LDAP_DIRECTORY_CLASS, toDirectoryDelegatedConnectorTypeClass(connector.getType())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_URL_KEY, connector.getUrl()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_SECURE_KEY, toDirectoryDelegatingConnectorSecureModeName(connector.getSsl())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_REFERRAL_KEY, fromBoolean(connector.getUseNodeReferrals())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_NESTED_GROUPS_DISABLED, fromBoolean(connector.getNestedGroupsDisabled())); + setAttributeIfNotNull(attributes, LDAP_URL_KEY, connector.getUrl()); + setAttributeIfNotNull(attributes, LDAP_SECURE_KEY, toDirectoryDelegatingConnectorSecureModeName(connector.getSsl())); + setAttributeIfNotNull(attributes, LDAP_REFERRAL_KEY, fromBoolean(connector.getUseNodeReferrals())); + setAttributeIfNotNull(attributes, LDAP_NESTED_GROUPS_DISABLED, fromBoolean(connector.getNestedGroupsDisabled())); setAttributeIfNotNull(attributes, DelegatedAuthenticationDirectory.ATTRIBUTE_CREATE_USER_ON_AUTH, fromBoolean(connector.getSynchronizeUsers())); setAttributeIfNotNull(attributes, DelegatedAuthenticationDirectory.ATTRIBUTE_UPDATE_USER_ON_AUTH, fromBoolean(connector.getSynchronizeUserDetails())); setAttributeIfNotNull(attributes, DelegatedAuthenticationDirectory.ATTRIBUTE_KEY_IMPORT_GROUPS, fromBoolean(connector.getSynchronizeGroupMemberships())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE, fromBoolean(connector.getUseUserMembershipAttribute())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_PAGEDRESULTS_KEY, fromBoolean(connector.getUsePagedResults())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_PAGEDRESULTS_SIZE, fromLong(connector.getPagedResultsSize())); + setAttributeIfNotNull(attributes, LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE, fromBoolean(connector.getUseUserMembershipAttribute())); + setAttributeIfNotNull(attributes, LDAP_PAGEDRESULTS_KEY, fromBoolean(connector.getUsePagedResults())); + setAttributeIfNotNull(attributes, LDAP_PAGEDRESULTS_SIZE, fromLong(connector.getPagedResultsSize())); setAttributeIfNotNull(attributes, SynchronisableDirectoryProperties.READ_TIMEOUT_IN_MILLISECONDS, fromLong(connector.getReadTimeoutInMillis())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_SEARCH_TIMELIMIT, fromLong(connector.getSearchTimeoutInMillis())); + setAttributeIfNotNull(attributes, LDAP_SEARCH_TIMELIMIT, fromLong(connector.getSearchTimeoutInMillis())); setAttributeIfNotNull(attributes, SynchronisableDirectoryProperties.CONNECTION_TIMEOUT_IN_MILLISECONDS, fromLong(connector.getConnectionTimeoutInMillis())); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_BASEDN_KEY, connector.getBaseDn()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_USERDN_KEY, connector.getUsername()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_PASSWORD_KEY, connector.getPassword()); + setAttributeIfNotNull(attributes, LDAP_BASEDN_KEY, connector.getBaseDn()); + setAttributeIfNotNull(attributes, LDAP_USERDN_KEY, connector.getUsername()); + setAttributeIfNotNull(attributes, LDAP_PASSWORD_KEY, connector.getPassword()); } final DirectoryDelegatingModel.DirectoryDelegatingConfiguration configuration = directoryDelegatingModel.getConfiguration(); if (configuration != null) { - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_DN_ADDITION, configuration.getUserDn()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_OBJECTCLASS_KEY, configuration.getUserObjectClass()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_OBJECTFILTER_KEY, configuration.getUserObjectFilter()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_USERNAME_KEY, configuration.getUserNameAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_USERNAME_RDN_KEY, configuration.getUserNameRdnAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_FIRSTNAME_KEY, configuration.getUserFirstNameAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_LASTNAME_KEY, configuration.getUserLastNameAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_DISPLAYNAME_KEY, configuration.getUserDisplayNameAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_EMAIL_KEY, configuration.getUserEmailAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.USER_GROUP_KEY, configuration.getUserGroupAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.LDAP_EXTERNAL_ID, configuration.getUserUniqueIdAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_DN_ADDITION, configuration.getGroupDn()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_OBJECTCLASS_KEY, configuration.getGroupObjectClass()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_OBJECTFILTER_KEY, configuration.getGroupObjectFilter()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_NAME_KEY, configuration.getGroupNameAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_DESCRIPTION_KEY, configuration.getGroupDescriptionAttribute()); - setAttributeIfNotNull(attributes, LDAPPropertiesMapper.GROUP_USERNAMES_KEY, configuration.getGroupMembersAttribute()); + setAttributeIfNotNull(attributes, USER_DN_ADDITION, configuration.getUserDn()); + setAttributeIfNotNull(attributes, USER_OBJECTCLASS_KEY, configuration.getUserObjectClass()); + setAttributeIfNotNull(attributes, USER_OBJECTFILTER_KEY, configuration.getUserObjectFilter()); + setAttributeIfNotNull(attributes, USER_USERNAME_KEY, configuration.getUserNameAttribute()); + setAttributeIfNotNull(attributes, USER_USERNAME_RDN_KEY, configuration.getUserNameRdnAttribute()); + setAttributeIfNotNull(attributes, USER_FIRSTNAME_KEY, configuration.getUserFirstNameAttribute()); + setAttributeIfNotNull(attributes, USER_LASTNAME_KEY, configuration.getUserLastNameAttribute()); + setAttributeIfNotNull(attributes, USER_DISPLAYNAME_KEY, configuration.getUserDisplayNameAttribute()); + setAttributeIfNotNull(attributes, USER_EMAIL_KEY, configuration.getUserEmailAttribute()); + setAttributeIfNotNull(attributes, USER_GROUP_KEY, configuration.getUserGroupAttribute()); + setAttributeIfNotNull(attributes, LDAP_EXTERNAL_ID, configuration.getUserUniqueIdAttribute()); + setAttributeIfNotNull(attributes, GROUP_DN_ADDITION, configuration.getGroupDn()); + setAttributeIfNotNull(attributes, GROUP_OBJECTCLASS_KEY, configuration.getGroupObjectClass()); + setAttributeIfNotNull(attributes, GROUP_OBJECTFILTER_KEY, configuration.getGroupObjectFilter()); + setAttributeIfNotNull(attributes, GROUP_NAME_KEY, configuration.getGroupNameAttribute()); + setAttributeIfNotNull(attributes, GROUP_DESCRIPTION_KEY, configuration.getGroupDescriptionAttribute()); + setAttributeIfNotNull(attributes, GROUP_USERNAMES_KEY, configuration.getGroupMembersAttribute()); } // Also set some defaults for directory delegating. @@ -430,12 +474,12 @@ private static void setDirectoryAttributes( attributes.putIfAbsent(DirectoryImpl.ATTRIBUTE_KEY_LOCAL_USER_STATUS, Boolean.toString(false)); attributes.putIfAbsent(DirectoryImpl.ATTRIBUTE_KEY_USE_PRIMARY_GROUP, Boolean.toString(false)); attributes.putIfAbsent(DirectoryProperties.CACHE_ENABLED, Boolean.toString(false)); - attributes.putIfAbsent(LDAPPropertiesMapper.LDAP_FILTER_EXPIRED_USERS, Boolean.toString(false)); - attributes.putIfAbsent(LDAPPropertiesMapper.LDAP_POOL_TYPE, "JNDI"); - attributes.putIfAbsent(LDAPPropertiesMapper.LDAP_RELAXED_DN_STANDARDISATION, Boolean.toString(false)); - attributes.putIfAbsent(LDAPPropertiesMapper.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE_FOR_GROUP_MEMBERSHIP, Boolean.toString(false)); - attributes.putIfAbsent(LDAPPropertiesMapper.LOCAL_GROUPS, Boolean.toString(false)); - attributes.putIfAbsent(LDAPPropertiesMapper.ROLES_DISABLED, Boolean.toString(true)); + attributes.putIfAbsent(LDAP_FILTER_EXPIRED_USERS, Boolean.toString(false)); + attributes.putIfAbsent(LDAP_POOL_TYPE, "JNDI"); + attributes.putIfAbsent(LDAP_RELAXED_DN_STANDARDISATION, Boolean.toString(false)); + attributes.putIfAbsent(LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE_FOR_GROUP_MEMBERSHIP, Boolean.toString(false)); + attributes.putIfAbsent(LOCAL_GROUPS, Boolean.toString(false)); + attributes.putIfAbsent(ROLES_DISABLED, Boolean.toString(true)); attributes.putIfAbsent(SynchronisableDirectoryProperties.INCREMENTAL_SYNC_ENABLED, Boolean.toString(false)); attributes.putIfAbsent(SynchronisableDirectoryProperties.CACHE_SYNCHRONISE_CRON, pollerConfig.getCronExpression()); attributes.putIfAbsent(SynchronisableDirectoryProperties.CACHE_SYNCHRONISE_INTERVAL, Long.toString(pollerConfig.getPollingIntervalInMin() * 60)); diff --git a/crowd/src/test/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtilTest.java b/crowd/src/test/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtilTest.java index f7b2276d..221c8b4f 100644 --- a/crowd/src/test/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtilTest.java +++ b/crowd/src/test/java/com/deftdevs/bootstrapi/crowd/model/util/DirectoryModelUtilTest.java @@ -2,8 +2,6 @@ import com.atlassian.crowd.directory.DelegatedAuthenticationDirectory; import com.atlassian.crowd.directory.SynchronisableDirectoryProperties; -import com.atlassian.crowd.directory.ldap.LDAPPropertiesMapper; -import com.atlassian.crowd.directory.ldap.LdapSecureMode; import com.atlassian.crowd.embedded.api.Directory; import com.atlassian.crowd.embedded.api.DirectoryType; import com.atlassian.crowd.embedded.api.MockDirectoryInternal; @@ -115,21 +113,21 @@ private void assertDirectoryDelegatingAttributesForConnectorMatch( final boolean firstParameterIsExpected) { AssertUtil.assertEquals(directory.getValue(DelegatedAuthenticationDirectory.ATTRIBUTE_LDAP_DIRECTORY_CLASS), DirectoryDelegatingConnectorTypeImplClass.MICROSOFT_ACTIVE_DIRECTORY.getImplClass(), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_URL_KEY), directoryDelegatingModel.getConnector().getUrl(), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_SECURE_KEY), LdapSecureMode.valueOf(directoryDelegatingModel.getConnector().getSsl().name()).getName(), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_REFERRAL_KEY), String.valueOf(directoryDelegatingModel.getConnector().getUseNodeReferrals()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_NESTED_GROUPS_DISABLED), String.valueOf(directoryDelegatingModel.getConnector().getNestedGroupsDisabled()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_URL_KEY), directoryDelegatingModel.getConnector().getUrl(), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_SECURE_KEY), DirectoryModelUtil.toDirectoryDelegatingConnectorSecureModeName(directoryDelegatingModel.getConnector().getSsl()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_REFERRAL_KEY), String.valueOf(directoryDelegatingModel.getConnector().getUseNodeReferrals()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_NESTED_GROUPS_DISABLED), String.valueOf(directoryDelegatingModel.getConnector().getNestedGroupsDisabled()), firstParameterIsExpected); AssertUtil.assertEquals(directory.getValue(DelegatedAuthenticationDirectory.ATTRIBUTE_CREATE_USER_ON_AUTH), String.valueOf(directoryDelegatingModel.getConnector().getSynchronizeUsers()), firstParameterIsExpected); AssertUtil.assertEquals(directory.getValue(DelegatedAuthenticationDirectory.ATTRIBUTE_UPDATE_USER_ON_AUTH), String.valueOf(directoryDelegatingModel.getConnector().getSynchronizeUserDetails()), firstParameterIsExpected); AssertUtil.assertEquals(directory.getValue(DelegatedAuthenticationDirectory.ATTRIBUTE_KEY_IMPORT_GROUPS), String.valueOf(directoryDelegatingModel.getConnector().getSynchronizeGroupMemberships()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE), String.valueOf(directoryDelegatingModel.getConnector().getUseUserMembershipAttribute()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_KEY), String.valueOf(directoryDelegatingModel.getConnector().getUsePagedResults()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_SIZE), String.valueOf(directoryDelegatingModel.getConnector().getPagedResultsSize()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE), String.valueOf(directoryDelegatingModel.getConnector().getUseUserMembershipAttribute()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_PAGEDRESULTS_KEY), String.valueOf(directoryDelegatingModel.getConnector().getUsePagedResults()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_PAGEDRESULTS_SIZE), String.valueOf(directoryDelegatingModel.getConnector().getPagedResultsSize()), firstParameterIsExpected); AssertUtil.assertEquals(directory.getValue(SynchronisableDirectoryProperties.READ_TIMEOUT_IN_MILLISECONDS), String.valueOf(directoryDelegatingModel.getConnector().getReadTimeoutInMillis()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_SEARCH_TIMELIMIT), String.valueOf(directoryDelegatingModel.getConnector().getSearchTimeoutInMillis()), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_SEARCH_TIMELIMIT), String.valueOf(directoryDelegatingModel.getConnector().getSearchTimeoutInMillis()), firstParameterIsExpected); AssertUtil.assertEquals(directory.getValue(SynchronisableDirectoryProperties.CONNECTION_TIMEOUT_IN_MILLISECONDS), String.valueOf(directoryDelegatingModel.getConnector().getConnectionTimeoutInMillis()), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_BASEDN_KEY), directoryDelegatingModel.getConnector().getBaseDn(), firstParameterIsExpected); - AssertUtil.assertEquals(directory.getValue(LDAPPropertiesMapper.LDAP_USERDN_KEY), directoryDelegatingModel.getConnector().getUsername(), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_BASEDN_KEY), directoryDelegatingModel.getConnector().getBaseDn(), firstParameterIsExpected); + AssertUtil.assertEquals(directory.getValue(DirectoryModelUtil.LDAP_USERDN_KEY), directoryDelegatingModel.getConnector().getUsername(), firstParameterIsExpected); } private void assertDirectoryDelegatingAttributesForConfigurationMatch( @@ -137,23 +135,23 @@ private void assertDirectoryDelegatingAttributesForConfigurationMatch( final DirectoryDelegatingModel directoryDelegatingModelExpected, final boolean firstParameterIsExpected) { - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_DN_ADDITION), directoryDelegatingModelExpected.getConfiguration().getUserDn(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_OBJECTCLASS_KEY), directoryDelegatingModelExpected.getConfiguration().getUserObjectClass(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_OBJECTFILTER_KEY), directoryDelegatingModelExpected.getConfiguration().getUserObjectFilter(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_USERNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserNameAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_USERNAME_RDN_KEY), directoryDelegatingModelExpected.getConfiguration().getUserNameRdnAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_FIRSTNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserFirstNameAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_LASTNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserLastNameAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_DISPLAYNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserDisplayNameAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_EMAIL_KEY), directoryDelegatingModelExpected.getConfiguration().getUserEmailAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.USER_GROUP_KEY), directoryDelegatingModelExpected.getConfiguration().getUserGroupAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.LDAP_EXTERNAL_ID), directoryDelegatingModelExpected.getConfiguration().getUserUniqueIdAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_DN_ADDITION), directoryDelegatingModelExpected.getConfiguration().getGroupDn(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_OBJECTCLASS_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupObjectClass(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_OBJECTFILTER_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupObjectFilter(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_NAME_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupNameAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_DESCRIPTION_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupDescriptionAttribute(), firstParameterIsExpected); - AssertUtil.assertEquals(directoryActual.getValue(LDAPPropertiesMapper.GROUP_USERNAMES_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupMembersAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_DN_ADDITION), directoryDelegatingModelExpected.getConfiguration().getUserDn(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_OBJECTCLASS_KEY), directoryDelegatingModelExpected.getConfiguration().getUserObjectClass(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_OBJECTFILTER_KEY), directoryDelegatingModelExpected.getConfiguration().getUserObjectFilter(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_USERNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserNameAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_USERNAME_RDN_KEY), directoryDelegatingModelExpected.getConfiguration().getUserNameRdnAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_FIRSTNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserFirstNameAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_LASTNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserLastNameAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_DISPLAYNAME_KEY), directoryDelegatingModelExpected.getConfiguration().getUserDisplayNameAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_EMAIL_KEY), directoryDelegatingModelExpected.getConfiguration().getUserEmailAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.USER_GROUP_KEY), directoryDelegatingModelExpected.getConfiguration().getUserGroupAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.LDAP_EXTERNAL_ID), directoryDelegatingModelExpected.getConfiguration().getUserUniqueIdAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_DN_ADDITION), directoryDelegatingModelExpected.getConfiguration().getGroupDn(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_OBJECTCLASS_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupObjectClass(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_OBJECTFILTER_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupObjectFilter(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_NAME_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupNameAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_DESCRIPTION_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupDescriptionAttribute(), firstParameterIsExpected); + AssertUtil.assertEquals(directoryActual.getValue(DirectoryModelUtil.GROUP_USERNAMES_KEY), directoryDelegatingModelExpected.getConfiguration().getGroupMembersAttribute(), firstParameterIsExpected); } private void assertDirectoryAllowedOperationsMatches( @@ -179,40 +177,40 @@ private Directory getDirectoryDelegating() { .setAllowedOperations(Collections.emptySet()) // Connector attributes .setAttribute(DelegatedAuthenticationDirectory.ATTRIBUTE_LDAP_DIRECTORY_CLASS, DirectoryDelegatingConnectorTypeImplClass.MICROSOFT_ACTIVE_DIRECTORY.getImplClass()) - .setAttribute(LDAPPropertiesMapper.LDAP_URL_KEY, "ldap://example.com:389") - .setAttribute(LDAPPropertiesMapper.LDAP_SECURE_KEY, LdapSecureMode.START_TLS.getName()) - .setAttribute(LDAPPropertiesMapper.LDAP_REFERRAL_KEY, String.valueOf(true)) - .setAttribute(LDAPPropertiesMapper.LDAP_NESTED_GROUPS_DISABLED, String.valueOf(false)) + .setAttribute(DirectoryModelUtil.LDAP_URL_KEY, "ldap://example.com:389") + .setAttribute(DirectoryModelUtil.LDAP_SECURE_KEY, "starttls") + .setAttribute(DirectoryModelUtil.LDAP_REFERRAL_KEY, String.valueOf(true)) + .setAttribute(DirectoryModelUtil.LDAP_NESTED_GROUPS_DISABLED, String.valueOf(false)) .setAttribute(DelegatedAuthenticationDirectory.ATTRIBUTE_CREATE_USER_ON_AUTH, String.valueOf(false)) .setAttribute(DelegatedAuthenticationDirectory.ATTRIBUTE_UPDATE_USER_ON_AUTH, String.valueOf(false)) .setAttribute(DelegatedAuthenticationDirectory.ATTRIBUTE_KEY_IMPORT_GROUPS, String.valueOf(false)) - .setAttribute(LDAPPropertiesMapper.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE, String.valueOf(false)) - .setAttribute(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_KEY, String.valueOf(true)) - .setAttribute(LDAPPropertiesMapper.LDAP_PAGEDRESULTS_SIZE, String.valueOf(999L)) + .setAttribute(DirectoryModelUtil.LDAP_USING_USER_MEMBERSHIP_ATTRIBUTE, String.valueOf(false)) + .setAttribute(DirectoryModelUtil.LDAP_PAGEDRESULTS_KEY, String.valueOf(true)) + .setAttribute(DirectoryModelUtil.LDAP_PAGEDRESULTS_SIZE, String.valueOf(999L)) .setAttribute(SynchronisableDirectoryProperties.READ_TIMEOUT_IN_MILLISECONDS, String.valueOf(123000L)) - .setAttribute(LDAPPropertiesMapper.LDAP_SEARCH_TIMELIMIT, String.valueOf(456000L)) + .setAttribute(DirectoryModelUtil.LDAP_SEARCH_TIMELIMIT, String.valueOf(456000L)) .setAttribute(SynchronisableDirectoryProperties.CONNECTION_TIMEOUT_IN_MILLISECONDS, String.valueOf(789000L)) - .setAttribute(LDAPPropertiesMapper.LDAP_BASEDN_KEY, "baseDn") - .setAttribute(LDAPPropertiesMapper.LDAP_USERDN_KEY, "userDn") - .setAttribute(LDAPPropertiesMapper.LDAP_PASSWORD_KEY, "password") + .setAttribute(DirectoryModelUtil.LDAP_BASEDN_KEY, "baseDn") + .setAttribute(DirectoryModelUtil.LDAP_USERDN_KEY, "userDn") + .setAttribute(DirectoryModelUtil.LDAP_PASSWORD_KEY, "password") // Configuration attributes - .setAttribute(LDAPPropertiesMapper.USER_DN_ADDITION, "userDnAddition") - .setAttribute(LDAPPropertiesMapper.USER_OBJECTCLASS_KEY, "userObjectClass") - .setAttribute(LDAPPropertiesMapper.USER_OBJECTFILTER_KEY, "userObjectFilter") - .setAttribute(LDAPPropertiesMapper.USER_USERNAME_KEY, "userName") - .setAttribute(LDAPPropertiesMapper.USER_USERNAME_RDN_KEY, "userNameRdn") - .setAttribute(LDAPPropertiesMapper.USER_FIRSTNAME_KEY, "userFirstName") - .setAttribute(LDAPPropertiesMapper.USER_LASTNAME_KEY, "userLastName") - .setAttribute(LDAPPropertiesMapper.USER_DISPLAYNAME_KEY, "userDisplayName") - .setAttribute(LDAPPropertiesMapper.USER_EMAIL_KEY, "userEmail") - .setAttribute(LDAPPropertiesMapper.USER_GROUP_KEY, "userGroup") - .setAttribute(LDAPPropertiesMapper.LDAP_EXTERNAL_ID, "userUniqueId") - .setAttribute(LDAPPropertiesMapper.GROUP_DN_ADDITION, "groupDnAddition") - .setAttribute(LDAPPropertiesMapper.GROUP_OBJECTCLASS_KEY, "groupObjectClass") - .setAttribute(LDAPPropertiesMapper.GROUP_OBJECTFILTER_KEY, "groupObjectFilter") - .setAttribute(LDAPPropertiesMapper.GROUP_NAME_KEY, "groupName") - .setAttribute(LDAPPropertiesMapper.GROUP_DESCRIPTION_KEY, "groupDescription") - .setAttribute(LDAPPropertiesMapper.GROUP_USERNAMES_KEY, "groupMembers") + .setAttribute(DirectoryModelUtil.USER_DN_ADDITION, "userDnAddition") + .setAttribute(DirectoryModelUtil.USER_OBJECTCLASS_KEY, "userObjectClass") + .setAttribute(DirectoryModelUtil.USER_OBJECTFILTER_KEY, "userObjectFilter") + .setAttribute(DirectoryModelUtil.USER_USERNAME_KEY, "userName") + .setAttribute(DirectoryModelUtil.USER_USERNAME_RDN_KEY, "userNameRdn") + .setAttribute(DirectoryModelUtil.USER_FIRSTNAME_KEY, "userFirstName") + .setAttribute(DirectoryModelUtil.USER_LASTNAME_KEY, "userLastName") + .setAttribute(DirectoryModelUtil.USER_DISPLAYNAME_KEY, "userDisplayName") + .setAttribute(DirectoryModelUtil.USER_EMAIL_KEY, "userEmail") + .setAttribute(DirectoryModelUtil.USER_GROUP_KEY, "userGroup") + .setAttribute(DirectoryModelUtil.LDAP_EXTERNAL_ID, "userUniqueId") + .setAttribute(DirectoryModelUtil.GROUP_DN_ADDITION, "groupDnAddition") + .setAttribute(DirectoryModelUtil.GROUP_OBJECTCLASS_KEY, "groupObjectClass") + .setAttribute(DirectoryModelUtil.GROUP_OBJECTFILTER_KEY, "groupObjectFilter") + .setAttribute(DirectoryModelUtil.GROUP_NAME_KEY, "groupName") + .setAttribute(DirectoryModelUtil.GROUP_DESCRIPTION_KEY, "groupDescription") + .setAttribute(DirectoryModelUtil.GROUP_USERNAMES_KEY, "groupMembers") ; return directoryBuilder.build(); diff --git a/crowd/src/test/java/it/com/deftdevs/bootstrapi/crowd/rest/DirectoryResourceFuncTest.java b/crowd/src/test/java/it/com/deftdevs/bootstrapi/crowd/rest/DirectoryResourceFuncTest.java new file mode 100644 index 00000000..c71bafbd --- /dev/null +++ b/crowd/src/test/java/it/com/deftdevs/bootstrapi/crowd/rest/DirectoryResourceFuncTest.java @@ -0,0 +1,124 @@ +package it.com.deftdevs.bootstrapi.crowd.rest; + +import com.deftdevs.bootstrapi.commons.constants.BootstrAPI; +import com.deftdevs.bootstrapi.commons.model.AbstractDirectoryModel; +import com.deftdevs.bootstrapi.commons.model.DirectoryDelegatingModel; +import com.fasterxml.jackson.databind.ObjectMapper; +import it.com.deftdevs.bootstrapi.commons.rest.HttpRequestHelper; +import org.junit.jupiter.api.Test; + +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.Response; +import java.net.http.HttpResponse; + +import static org.junit.jupiter.api.Assertions.*; + +public class DirectoryResourceFuncTest { + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Test + void testCreateGetAndDeleteDirectoryDelegating() throws Exception { + final DirectoryDelegatingModel directoryModel = getExampleDelegatingModel(); + + // Create + final HttpResponse createResponse = HttpRequestHelper.builder(BootstrAPI.DIRECTORY) + .request(HttpMethod.POST, directoryModel); + assertEquals(Response.Status.OK.getStatusCode(), createResponse.statusCode()); + + final DirectoryDelegatingModel createdDirectory = objectMapper.readValue( + createResponse.body(), DirectoryDelegatingModel.class); + assertNotNull(createdDirectory.getId()); + assertEquals(directoryModel.getName(), createdDirectory.getName()); + assertEquals(directoryModel.getActive(), createdDirectory.getActive()); + assertNotNull(createdDirectory.getConnector()); + assertEquals(directoryModel.getConnector().getUrl(), createdDirectory.getConnector().getUrl()); + assertEquals(directoryModel.getConnector().getSsl(), createdDirectory.getConnector().getSsl()); + assertEquals(directoryModel.getConnector().getBaseDn(), createdDirectory.getConnector().getBaseDn()); + + // Get + final HttpResponse getResponse = HttpRequestHelper.builder(BootstrAPI.DIRECTORY + "/" + createdDirectory.getId()) + .request(); + assertEquals(Response.Status.OK.getStatusCode(), getResponse.statusCode()); + + final AbstractDirectoryModel retrievedDirectory = objectMapper.readValue( + getResponse.body(), AbstractDirectoryModel.class); + assertInstanceOf(DirectoryDelegatingModel.class, retrievedDirectory); + assertEquals(createdDirectory.getId(), retrievedDirectory.getId()); + assertEquals(createdDirectory.getName(), retrievedDirectory.getName()); + + // Delete + final HttpResponse deleteResponse = HttpRequestHelper.builder(BootstrAPI.DIRECTORY + "/" + createdDirectory.getId()) + .request(HttpMethod.DELETE, null); + assertEquals(Response.Status.OK.getStatusCode(), deleteResponse.statusCode()); + + // Verify deleted + final HttpResponse getAfterDeleteResponse = HttpRequestHelper.builder(BootstrAPI.DIRECTORY + "/" + createdDirectory.getId()) + .request(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), getAfterDeleteResponse.statusCode()); + } + + @Test + void testCreateDirectoryDelegatingUnauthenticated() throws Exception { + final HttpResponse response = HttpRequestHelper.builder(BootstrAPI.DIRECTORY) + .username("wrong") + .password("password") + .request(HttpMethod.POST, getExampleDelegatingModel()); + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.statusCode()); + } + + @Test + void testCreateDirectoryDelegatingUnauthorized() throws Exception { + final HttpResponse response = HttpRequestHelper.builder(BootstrAPI.DIRECTORY) + .username("user") + .password("user") + .request(HttpMethod.POST, getExampleDelegatingModel()); + assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.statusCode()); + } + + private DirectoryDelegatingModel getExampleDelegatingModel() { + return DirectoryDelegatingModel.builder() + .name("Test Delegating Directory") + .active(true) + .description("Integration test directory") + .connector(DirectoryDelegatingModel.DirectoryDelegatingConnector.builder() + .type(DirectoryDelegatingModel.DirectoryDelegatingConnector.ConnectorType.MICROSOFT_ACTIVE_DIRECTORY) + .url("ldaps://test.example.com:636") + .ssl(DirectoryDelegatingModel.DirectoryDelegatingConnector.SslType.LDAPS) + .useNodeReferrals(false) + .nestedGroupsDisabled(true) + .synchronizeUsers(false) + .synchronizeUserDetails(false) + .synchronizeGroupMemberships(false) + .useUserMembershipAttribute(false) + .usePagedResults(true) + .pagedResultsSize(999L) + .readTimeoutInMillis(120000L) + .searchTimeoutInMillis(60000L) + .connectionTimeoutInMillis(10000L) + .baseDn("DC=test,DC=example,DC=com") + .username("domain\\testuser") + .password("testpassword") + .build()) + .configuration(DirectoryDelegatingModel.DirectoryDelegatingConfiguration.builder() + .userDn("") + .userObjectClass("user") + .userObjectFilter("(objectClass=user)") + .userNameAttribute("sAMAccountName") + .userNameRdnAttribute("cn") + .userFirstNameAttribute("givenName") + .userLastNameAttribute("sn") + .userDisplayNameAttribute("displayName") + .userEmailAttribute("email") + .userGroupAttribute("memberOf") + .userUniqueIdAttribute("userID") + .groupDn("") + .groupObjectClass("group") + .groupObjectFilter("(objectClass=group)") + .groupNameAttribute("gn") + .groupDescriptionAttribute("description") + .groupMembersAttribute("member") + .build()) + .build(); + } +}