Skip to content

Commit

Permalink
making ldap config optional
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanehcine committed Oct 9, 2023
1 parent 36dc704 commit 68733ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import org.georchestra.ds.users.AccountDao;
import org.georchestra.ds.users.UserRule;
import org.georchestra.gateway.model.GatewayConfigProperties;
import org.georchestra.gateway.security.oauth2.LdapEnabledCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
Expand Down Expand Up @@ -114,6 +117,8 @@ private Stream<ServerHttpSecurityCustomizer> sortedCustomizers(List<ServerHttpSe
return new ResolveGeorchestraUserGlobalFilter(resolver);
}

@ConditionalOnExpression("${georchestra.gateway.headerAuthentication:false}")
@Conditional(LdapEnabledCondition.class)
public @Bean ResolveHttpHeadersGeorchestraUserFilter resolveHttpHeadersGeorchestraUserFilter() {
return new ResolveHttpHeadersGeorchestraUserFilter();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.georchestra.gateway.security.oauth2;

import com.jayway.jsonpath.JsonPath;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.LinkedHashMap;

public class LdapEnabledCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Resource resource = new FileSystemResource(
System.getProperty("georchestra.datadir", "/etc/georchestra") + "/gateway/security.yaml");
YamlMapFactoryBean yaml = new YamlMapFactoryBean();
yaml.setResources(resource);
LinkedHashMap<String, LinkedHashMap<String, Object>> ldap = JsonPath.read(yaml.getObject().get("georchestra"),
"$.gateway.security.ldap");
return ldap.values().stream().anyMatch(el -> el.containsKey("enabled") && ((Boolean) el.get("enabled")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.ldap.core.LdapTemplate;
Expand Down Expand Up @@ -110,6 +111,7 @@ private ServerLogoutSuccessHandler oidcLogoutSuccessHandler(

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public LdapContextSource singleContextSource(LdapConfigProperties config) {
ExtendedLdapConfig ldapConfig = config.extendedEnabled().get(0);
LdapContextSource singleContextSource = new LdapContextSource();
Expand All @@ -122,6 +124,7 @@ public LdapContextSource singleContextSource(LdapConfigProperties config) {

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public PoolingContextSource contextSource(LdapConfigProperties config, LdapContextSource singleContextSource) {
ExtendedLdapConfig ldapConfig = config.extendedEnabled().get(0);
PoolingContextSource contextSource = new PoolingContextSource();
Expand All @@ -138,13 +141,15 @@ public PoolingContextSource contextSource(LdapConfigProperties config, LdapConte

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public LdapTemplate ldapTemplate(PoolingContextSource contextSource) throws Exception {
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
return ldapTemplate;
}

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public RoleDao roleDao(LdapTemplate ldapTemplate, LdapConfigProperties config) {
RoleDaoImpl impl = new RoleDaoImpl();
impl.setLdapTemplate(ldapTemplate);
Expand All @@ -154,6 +159,7 @@ public RoleDao roleDao(LdapTemplate ldapTemplate, LdapConfigProperties config) {

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public AccountDao accountDao(LdapTemplate ldapTemplate, LdapConfigProperties config) throws Exception {
ExtendedLdapConfig ldapConfig = config.extendedEnabled().get(0);
String baseDn = ldapConfig.getBaseDn();
Expand Down Expand Up @@ -188,6 +194,7 @@ public AccountDao accountDao(LdapTemplate ldapTemplate, LdapConfigProperties con

@Bean
@ConditionalOnExpression("${georchestra.gateway.security.createNonExistingUsersInLDAP:true}")
@Conditional(LdapEnabledCondition.class)
public RoleProtected roleProtected() {
RoleProtected roleProtected = new RoleProtected();
roleProtected.setListOfprotectedRoles(
Expand Down

0 comments on commit 68733ec

Please sign in to comment.