-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from groldan/fix/default_preauth_roles_config
Fix config of default admin role names for http headers preauthentication
- Loading branch information
Showing
5 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../geoserver/acl/autoconfigure/security/PreAuthenticationSecurityAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* (c) 2023 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.acl.autoconfigure.security; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.geoserver.acl.autoconfigure.security.SecurityConfigProperties.PreauthHeaders; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider; | ||
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter; | ||
|
||
class PreAuthenticationSecurityAutoConfigurationTest { | ||
|
||
private ApplicationContextRunner runner = | ||
new ApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of( | ||
AuthenticationManagerAutoConfiguration.class, | ||
PreAuthenticationSecurityAutoConfiguration.class)); | ||
|
||
@BeforeEach | ||
void setUp() throws Exception {} | ||
|
||
@Test | ||
void testConditionalOnPreAuthenticationEnabledIsDisabledByDefault() { | ||
// contribute a mocked up AuthenticationProvider for AuthenticationManagerAutoConfiguration | ||
// not to fail due to at least one AuthenticationProvider existing | ||
AuthenticationProvider mockProvider = mock(AuthenticationProvider.class); | ||
runner.withBean(AuthenticationProvider.class, () -> mockProvider) | ||
.run( | ||
context -> | ||
assertThat(context) | ||
.hasNotFailed() | ||
.doesNotHaveBean(SecurityConfigProperties.class) | ||
.doesNotHaveBean(RequestHeaderAuthenticationFilter.class) | ||
.doesNotHaveBean( | ||
PreAuthenticatedAuthenticationProvider.class)); | ||
} | ||
|
||
@Test | ||
void testConditionalOnPreAuthenticationEnabled() { | ||
runner.withPropertyValues("geoserver.acl.security.headers.enabled=true") | ||
.run( | ||
context -> | ||
assertThat(context) | ||
.hasNotFailed() | ||
.hasSingleBean(SecurityConfigProperties.class) | ||
.hasSingleBean(RequestHeaderAuthenticationFilter.class) | ||
.hasSingleBean( | ||
PreAuthenticatedAuthenticationProvider.class)); | ||
} | ||
|
||
@Test | ||
void testPreAuthenticationConfig() { | ||
runner.withPropertyValues( | ||
"geoserver.acl.security.headers.enabled: true", | ||
"geoserver.acl.security.headers.user-header: sec-username", | ||
"geoserver.acl.security.headers.roles-header: sec-roles", | ||
"geoserver.acl.security.headers.admin-roles: ROLE_ADMINISTRATOR,ADMIN") | ||
.run( | ||
context -> { | ||
assertThat(context).hasNotFailed(); | ||
PreauthHeaders config = | ||
context.getBean(SecurityConfigProperties.class).getHeaders(); | ||
assertThat(config.getUserHeader()).isEqualTo("sec-username"); | ||
assertThat(config.getRolesHeader()).isEqualTo("sec-roles"); | ||
assertThat(config.getAdminRoles()) | ||
.containsExactly("ROLE_ADMINISTRATOR", "ADMIN"); | ||
}); | ||
} | ||
} |