-
Notifications
You must be signed in to change notification settings - Fork 8
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 #15 from groldan/server/auth
API security: define authentication methods and secure the APIs
- Loading branch information
Showing
30 changed files
with
967 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ FROM eclipse-temurin:17-jre | |
LABEL maintainer="GeoServer PSC <[email protected]>" | ||
|
||
WORKDIR /opt/app/bin | ||
ENV JAVA_TOOL_OPTS="\ | ||
ENV DEF_JAVA_TOOL_OPTS="\ | ||
--add-exports=java.desktop/sun.awt.image=ALL-UNNAMED \ | ||
--add-opens=java.base/java.lang=ALL-UNNAMED \ | ||
--add-opens=java.base/java.util=ALL-UNNAMED \ | ||
|
@@ -22,6 +22,9 @@ ENV JAVA_TOOL_OPTS="\ | |
--add-opens=java.naming/com.sun.jndi.ldap=ALL-UNNAMED \ | ||
-Djava.awt.headless=true" | ||
ENV JAVA_OPTS="-XX:MaxRAMPercentage=80 -XshowSettings:system" | ||
|
||
ENV JAVA_TOOL_OPTS="$DEF_JAVA_TOOL_OPTS $JAVA_OPTS" | ||
|
||
EXPOSE 8080 | ||
|
||
COPY --from=builder dependencies/ ./ | ||
|
@@ -36,4 +39,8 @@ HEALTHCHECK \ | |
--retries=5 \ | ||
CMD curl -f -s -o /dev/null localhost:8080/actuator/health || exit 1 | ||
|
||
CMD exec java $JAVA_OPTS $JAVA_TOOL_OPTS org.springframework.boot.loader.JarLauncher | ||
ARG APP_ARGS="" | ||
|
||
ENTRYPOINT [ "/bin/bash", "-c", "exec java $JAVA_TOOL_OPTS org.springframework.boot.loader.JarLauncher \"${@}\"", "--" ] | ||
|
||
CMD ["${APP_ARGS}"] |
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
68 changes: 68 additions & 0 deletions
68
...in/java/org/geoserver/acl/autoconfigure/security/AclServiceSecurityAutoConfiguration.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,68 @@ | ||
/* (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 lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter; | ||
|
||
@AutoConfiguration | ||
@EnableWebSecurity | ||
@EnableConfigurationProperties(SecurityConfigProperties.class) | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
@Slf4j(topic = "org.geoserver.acl.autoconfigure.security") | ||
public class AclServiceSecurityAutoConfiguration { | ||
|
||
private @Autowired(required = false) RequestHeaderAuthenticationFilter preAuthFilter; | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain( | ||
HttpSecurity http, | ||
AuthenticationManager authenticationManager, | ||
SecurityConfigProperties config) | ||
throws Exception { | ||
|
||
http.csrf().disable(); | ||
|
||
if (!config.enabled()) { | ||
log.warn("No security authentication method is defined!"); | ||
return http.build(); | ||
} | ||
|
||
http.authenticationManager(authenticationManager); | ||
|
||
if (null == preAuthFilter) { | ||
log.info("Pre-authentication headers disabled"); | ||
} else { | ||
log.info( | ||
"Pre-authentication headers enabled for {}/{}. Admin roles: {}", | ||
config.getHeaders().getUserHeader(), | ||
config.getHeaders().getRolesHeader(), | ||
config.getHeaders().getAdminRoles()); | ||
http.addFilterAfter(preAuthFilter, RequestHeaderAuthenticationFilter.class); | ||
} | ||
|
||
http.authorizeRequests() | ||
.antMatchers("/", "/api/api-docs/**", "/api/swagger-ui.html", "/api/swagger-ui/**") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
.and(); | ||
|
||
if (config.getInternal().isEnabled()) { | ||
http.httpBasic(); | ||
} | ||
return http.build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...java/org/geoserver/acl/autoconfigure/security/AuthenticationManagerAutoConfiguration.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,23 @@ | ||
/* (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 org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.ProviderManager; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class AuthenticationManagerAutoConfiguration { | ||
|
||
@Bean | ||
AuthenticationManager authenticationManager(List<AuthenticationProvider> providers) | ||
throws Exception { | ||
return new ProviderManager(providers); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../org/geoserver/acl/autoconfigure/security/ConditionalOnInternalAuthenticationEnabled.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,26 @@ | ||
/* (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 org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ConditionalOnProperty( | ||
prefix = ConditionalOnInternalAuthenticationEnabled.PREFIX, | ||
name = "enabled", | ||
havingValue = "true", | ||
matchIfMissing = false) | ||
public @interface ConditionalOnInternalAuthenticationEnabled { | ||
|
||
public static final String PREFIX = SecurityConfigProperties.PREFIX + ".internal"; | ||
} |
26 changes: 26 additions & 0 deletions
26
.../java/org/geoserver/acl/autoconfigure/security/ConditionalOnPreAuthenticationEnabled.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,26 @@ | ||
/* (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 org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ConditionalOnProperty( | ||
prefix = ConditionalOnPreAuthenticationEnabled.PREFIX, | ||
name = "enabled", | ||
havingValue = "true", | ||
matchIfMissing = false) | ||
public @interface ConditionalOnPreAuthenticationEnabled { | ||
|
||
public static final String PREFIX = SecurityConfigProperties.PREFIX + ".headers"; | ||
} |
Oops, something went wrong.