diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2022-12-20 14:24:36 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-12-23 20:02:51 +0000 |
commit | d023db004a914ab40a9d7caaaa40d901433a018b (patch) | |
tree | 5d6ef66155432dd2579b07ba217dd30476c9c0b0 /server/sonar-webserver-auth | |
parent | ba6ee4d6696aa45cdc4b684b7aaa49e71401f3e7 (diff) | |
download | sonarqube-d023db004a914ab40a9d7caaaa40d901433a018b.tar.gz sonarqube-d023db004a914ab40a9d7caaaa40d901433a018b.zip |
SONAR-17798 respect sonar.authenticator.ignoreStartupFailure property in LdapCredentialsAuthentication
Diffstat (limited to 'server/sonar-webserver-auth')
2 files changed, 11 insertions, 23 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LdapCredentialsAuthentication.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LdapCredentialsAuthentication.java index b41295d0562..962e63448f2 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LdapCredentialsAuthentication.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/LdapCredentialsAuthentication.java @@ -37,7 +37,6 @@ import org.sonar.auth.ldap.LdapRealm; import org.sonar.auth.ldap.LdapUserDetails; import org.sonar.auth.ldap.LdapUsersProvider; import org.sonar.db.user.UserDto; -import org.sonar.process.ProcessProperties; import org.sonar.server.authentication.event.AuthenticationEvent; import org.sonar.server.authentication.event.AuthenticationEvent.Source; import org.sonar.server.authentication.event.AuthenticationException; @@ -47,8 +46,6 @@ import static org.apache.commons.lang.StringUtils.trimToNull; public class LdapCredentialsAuthentication { - private static final String LDAP_SECURITY_REALM = "LDAP"; - private static final Logger LOG = Loggers.get(LdapCredentialsAuthentication.class); private final Configuration configuration; @@ -66,19 +63,10 @@ public class LdapCredentialsAuthentication { this.userRegistrar = userRegistrar; this.authenticationEvent = authenticationEvent; - String realmName = configuration.get(ProcessProperties.Property.SONAR_SECURITY_REALM.getKey()).orElse(null); - this.isLdapAuthActivated = LDAP_SECURITY_REALM.equals(realmName); - - if (isLdapAuthActivated) { - ldapRealm.init(); - this.ldapAuthenticator = ldapRealm.doGetAuthenticator(); - this.ldapUsersProvider = ldapRealm.getUsersProvider(); - this.ldapGroupsProvider = ldapRealm.getGroupsProvider(); - } else { - this.ldapAuthenticator = null; - this.ldapUsersProvider = null; - this.ldapGroupsProvider = null; - } + this.isLdapAuthActivated = ldapRealm.isLdapAuthActivated(); + this.ldapAuthenticator = ldapRealm.getAuthenticator(); + this.ldapUsersProvider = ldapRealm.getUsersProvider(); + this.ldapGroupsProvider = ldapRealm.getGroupsProvider(); } public Optional<UserDto> authenticate(Credentials credentials, HttpServletRequest request, AuthenticationEvent.Method method) { @@ -160,7 +148,7 @@ public class LdapCredentialsAuthentication { private final String key; private LdapIdentityProvider(String ldapServerKey) { - this.key = LDAP_SECURITY_REALM + "_" + ldapServerKey; + this.key = LdapRealm.LDAP_SECURITY_REALM + "_" + ldapServerKey; } @Override diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/LdapCredentialsAuthenticationTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/LdapCredentialsAuthenticationTest.java index d3c1edbaf4a..6ee4e7c724a 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/LdapCredentialsAuthenticationTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/LdapCredentialsAuthenticationTest.java @@ -103,18 +103,21 @@ public class LdapCredentialsAuthenticationTest { @Before public void setUp() throws Exception { settings.setProperty(ProcessProperties.Property.SONAR_SECURITY_REALM.getKey(), "LDAP"); - when(ldapRealm.doGetAuthenticator()).thenReturn(ldapAuthenticator); + settings.setProperty(ProcessProperties.Property.SONAR_AUTHENTICATOR_IGNORE_STARTUP_FAILURE.getKey(), "true"); + when(ldapRealm.getAuthenticator()).thenReturn(ldapAuthenticator); when(ldapRealm.getUsersProvider()).thenReturn(ldapUsersProvider); when(ldapRealm.getGroupsProvider()).thenReturn(ldapGroupsProvider); + when(ldapRealm.isLdapAuthActivated()).thenReturn(true); underTest = new LdapCredentialsAuthentication(settings.asConfig(), userRegistrar, authenticationEvent, ldapRealm); } @Test public void authenticate_with_null_group_provider() { reset(ldapRealm); - when(ldapRealm.doGetAuthenticator()).thenReturn(ldapAuthenticator); + when(ldapRealm.getAuthenticator()).thenReturn(ldapAuthenticator); when(ldapRealm.getUsersProvider()).thenReturn(ldapUsersProvider); when(ldapRealm.getGroupsProvider()).thenReturn(null); + when(ldapRealm.isLdapAuthActivated()).thenReturn(true); underTest = new LdapCredentialsAuthentication(settings.asConfig(), userRegistrar, authenticationEvent, ldapRealm); LdapAuthenticator.Context authenticationContext = new LdapAuthenticator.Context(LOGIN, PASSWORD, request); @@ -134,7 +137,6 @@ public class LdapCredentialsAuthenticationTest { assertThat(identity.shouldSyncGroups()).isFalse(); verify(authenticationEvent).loginSuccess(request, LOGIN, Source.realm(BASIC, LDAP_SECURITY_REALM_NAME)); - verify(ldapRealm).init(); } @Test @@ -148,7 +150,6 @@ public class LdapCredentialsAuthenticationTest { assertThat(provider.getDisplay()).isNull(); assertThat(provider.isEnabled()).isTrue(); verify(authenticationEvent).loginSuccess(request, LOGIN, Source.realm(BASIC, LDAP_SECURITY_REALM_NAME)); - verify(ldapRealm).init(); } @Test @@ -265,12 +266,11 @@ public class LdapCredentialsAuthenticationTest { @Test public void return_empty_user_when_ldap_not_activated() { reset(ldapRealm); - settings.clear(); + when(ldapRealm.isLdapAuthActivated()).thenReturn(false); underTest = new LdapCredentialsAuthentication(settings.asConfig(), userRegistrar, authenticationEvent, ldapRealm); assertThat(underTest.authenticate(new Credentials(LOGIN, PASSWORD), request, BASIC)).isEmpty(); verifyNoInteractions(authenticationEvent); - verifyNoInteractions(ldapRealm); } private void executeAuthenticate(@Nullable LdapUserDetails userDetails) { |