private static SecurityRealm selectRealm(SecurityRealm[] realms, String realmName) { for (SecurityRealm realm : realms) { if (StringUtils.equals(realmName, realm.getName())) { return realm; } } return null; }
@CheckForNull private String getExternalUserAuthentication() { SecurityRealm realm = securityRealmFactory.getRealm(); return realm == null ? null : realm.getName(); }
@CheckForNull private String getExternalUserAuthentication() { SecurityRealm realm = securityRealmFactory.getRealm(); return realm == null ? null : realm.getName(); }
private Source realmEventSource(AuthenticationEvent.Method method) { return Source.realm(method, realm.getName()); }
@Before public void setUp() throws Exception { when(realm.getName()).thenReturn(REALM_NAME); }
@Override public void start() { if (realm != null) { Logger logger = Loggers.get("org.sonar.INFO"); try { logger.info("Security realm: " + realm.getName()); realm.init(); logger.info("Security realm started"); } catch (RuntimeException e) { if (ignoreStartupFailure) { logger.error("IGNORED - Security realm fails to start: " + e.getMessage()); } else { throw new SonarException("Security realm fails to start: " + e.getMessage(), e); } } } }
@Test public void get_realm() { SecurityRealm realm = mock(SecurityRealm.class); when(realm.getName()).thenReturn("LDAP"); when(securityRealmFactory.getRealm()).thenReturn(realm); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External User Authentication", "LDAP"); }
@Test public void get_realm() { SecurityRealm realm = mock(SecurityRealm.class); when(realm.getName()).thenReturn("LDAP"); when(securityRealmFactory.getRealm()).thenReturn(realm); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External User Authentication", "LDAP"); }
@Test public void should_fail() { SecurityRealm realm = spy(new AlwaysFailsRealm()); settings.setProperty("sonar.security.realm", realm.getName()); try { new SecurityRealmFactory(settings.asConfig(), new SecurityRealm[] {realm}).start(); fail(); } catch (SonarException e) { assertThat(e.getCause()).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Security realm fails to start"); } }
/** * Typical usage. */ @Test public void should_select_realm_and_start() { SecurityRealm realm = spy(new FakeRealm()); settings.setProperty("sonar.security.realm", realm.getName()); SecurityRealmFactory factory = new SecurityRealmFactory(settings.asConfig(), new SecurityRealm[] {realm}); factory.start(); assertThat(factory.getRealm()).isSameAs(realm); assertThat(factory.hasExternalAuthentication()).isTrue(); verify(realm).init(); factory.stop(); }
@Test public void ignore_startup_failure() { SecurityRealm realm = spy(new AlwaysFailsRealm()); settings.setProperty("sonar.security.realm", realm.getName()); settings.setProperty(CoreProperties.CORE_AUTHENTICATOR_IGNORE_STARTUP_FAILURE, true); new SecurityRealmFactory(settings.asConfig(), new SecurityRealm[] {realm}).start(); verify(realm).init(); }
@Test public void should_take_precedence_over_authenticator() { SecurityRealm realm = new FakeRealm(); settings.setProperty("sonar.security.realm", realm.getName()); LoginPasswordAuthenticator authenticator = new FakeAuthenticator(); settings.setProperty(CoreProperties.CORE_AUTHENTICATOR_CLASS, FakeAuthenticator.class.getName()); SecurityRealmFactory factory = new SecurityRealmFactory(settings.asConfig(), new SecurityRealm[] {realm}, new LoginPasswordAuthenticator[] {authenticator}); assertThat(factory.getRealm()).isSameAs(realm); }
private static SecurityRealm selectRealm(SecurityRealm[] realms, String realmName) { for (SecurityRealm realm : realms) { if (StringUtils.equals(realmName, realm.getName())) { return realm; } } return null; }
@CheckForNull private String getExternalUserAuthentication() { SecurityRealm realm = securityRealmFactory.getRealm(); return realm == null ? null : realm.getName(); }
@CheckForNull private String getExternalUserAuthentication() { SecurityRealm realm = securityRealmFactory.getRealm(); return realm == null ? null : realm.getName(); }
private Source realmEventSource(AuthenticationEvent.Method method) { return Source.realm(method, realm.getName()); }
@Override public void start() { if (realm != null) { Logger logger = Loggers.get("org.sonar.INFO"); try { logger.info("Security realm: " + realm.getName()); realm.init(); logger.info("Security realm started"); } catch (RuntimeException e) { if (ignoreStartupFailure) { logger.error("IGNORED - Security realm fails to start: " + e.getMessage()); } else { throw new SonarException("Security realm fails to start: " + e.getMessage(), e); } } } }