Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype applying georchestra access rules #4

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
package org.georchestra.gateway.config;

import java.util.List;
import java.util.regex.Pattern;

import lombok.Data;

@Data
public class RoleBasedAccessRule {

private List<Pattern> interceptUrl;
private List<String> interceptUrl;
private boolean anonymous;
private List<String> allowedRoles;
private List<String> allowedRoles = List.of();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package org.georchestra.gateway.security;

import java.util.List;

import org.georchestra.gateway.config.GatewayConfigProperties;
import org.georchestra.gateway.config.RoleBasedAccessRule;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -27,6 +31,8 @@
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec;
import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec.Access;
import org.springframework.security.config.web.server.ServerHttpSecurity.OAuth2LoginSpec;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
Expand All @@ -41,12 +47,13 @@
@Configuration(proxyBeanMethods = false)
@EnableWebFluxSecurity
@EnableConfigurationProperties(OAuth2ProxyConfigProperties.class)
@Slf4j
@Slf4j(topic = "org.georchestra.gateway.security")
public class GatewaySecurityAutoconfiguration {

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http,
@Value("${georchestra.gateway.security.ldap.enabled:false}") boolean ldapEnabled) throws Exception {
@Value("${georchestra.gateway.security.ldap.enabled:false}") boolean ldapEnabled,
GatewayConfigProperties config) throws Exception {

// disable csrf and cors or the websocket connection gets a 403 Forbidden.
// Revisit.
Expand All @@ -59,10 +66,12 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http,
http.httpBasic().and().formLogin();
}
// configure path matchers
http.authorizeExchange()//
.pathMatchers("/", "/header/**").permitAll()//
.pathMatchers("/ws/**").permitAll()//
.pathMatchers("/**").authenticated();
applyAccessRules(http, config);

// http.authorizeExchange()//
// .pathMatchers("/", "/header/**").permitAll()//
// .pathMatchers("/ws/**").permitAll()//
// .pathMatchers("/**").authenticated();

return http.build();
}
Expand Down Expand Up @@ -129,4 +138,23 @@ public WebClient oauth2WebClient(OAuth2ProxyConfigProperties proxyConfig) {
return webClient;
}

private ServerHttpSecurity applyAccessRules(ServerHttpSecurity http, GatewayConfigProperties config) {
AuthorizeExchangeSpec authorizeExchange = http.authorizeExchange();

for (RoleBasedAccessRule rule : config.getGlobalAccessRules()) {
List<String> antPatterns = rule.getInterceptUrl();
boolean anonymous = rule.isAnonymous();
List<String> allowedRoles = rule.getAllowedRoles();
Access access = authorizeExchange.pathMatchers(antPatterns.toArray(String[]::new));
if (anonymous) {
log.info("Access rule: {} anonymous");
access.permitAll();
} else {
log.info("Access rule: {} has any role: {}", antPatterns, allowedRoles);
access.hasAnyAuthority(allowedRoles.toArray(String[]::new));
}
}

return http;
}
}