This is an automated email from the ASF dual-hosted git repository. mmoayyed pushed a commit to branch SYNCOPE-163-1 in repository https://gitbox.apache.org/repos/asf/syncope.git
commit 5b3cd4fa4b8247cbe25b0348cc166105ca05f0ab Author: Misagh Moayyed <[email protected]> AuthorDate: Wed Mar 25 12:35:35 2020 +0430 working on config mgmt over rest --- wa/bootstrap/pom.xml | 7 +- ...n.java => SyncopeWABootstrapConfiguration.java} | 22 +----- .../bootstrap/SyncopeWAPropertySourceLocator.java | 89 ++++++++++++++++++++++ .../src/main/resources/META-INF/spring.factories | 2 +- wa/starter/pom.xml | 1 - 5 files changed, 98 insertions(+), 23 deletions(-) diff --git a/wa/bootstrap/pom.xml b/wa/bootstrap/pom.xml index ecf7f01..6e80838 100644 --- a/wa/bootstrap/pom.xml +++ b/wa/bootstrap/pom.xml @@ -47,13 +47,16 @@ under the License. <groupId>org.apache.syncope.common.keymaster</groupId> <artifactId>syncope-common-keymaster-client-zookeeper</artifactId> <version>${project.version}</version> - <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.apache.syncope.core</groupId> + <artifactId>syncope-core-provisioning-api</artifactId> + <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.syncope.ext.self-keymaster</groupId> <artifactId>syncope-ext-self-keymaster-client</artifactId> <version>${project.version}</version> - <scope>compile</scope> </dependency> <dependency> <groupId>org.apereo.cas</groupId> diff --git a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/RestfulCloudConfigBootstrapConfiguration.java b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java similarity index 73% rename from wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/RestfulCloudConfigBootstrapConfiguration.java rename to wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java index 2249220..c583f79 100644 --- a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/RestfulCloudConfigBootstrapConfiguration.java +++ b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java @@ -31,18 +31,13 @@ import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.core.env.MapPropertySource; - -import java.util.HashMap; -import java.util.Map; @Configuration(proxyBeanMethods = false) @ImportAutoConfiguration(classes = {ZookeeperKeymasterClientContext.class, SelfKeymasterClientContext.class}) @PropertySource("classpath:wa.properties") @PropertySource(value = "file:${conf.directory}/wa.properties", ignoreResourceNotFound = true) -public class RestfulCloudConfigBootstrapConfiguration { - private static final Logger LOG = LoggerFactory.getLogger(RestfulCloudConfigBootstrapConfiguration.class); +public class SyncopeWABootstrapConfiguration { + private static final Logger LOG = LoggerFactory.getLogger(SyncopeWABootstrapConfiguration.class); @Value("${anonymousUser}") private String anonymousUser; @@ -62,17 +57,6 @@ public class RestfulCloudConfigBootstrapConfiguration { @Autowired @Bean public PropertySourceLocator configPropertySourceLocator(final WARestClient waRestClient) { - return new PropertySourceLocator() { - @Override - public org.springframework.core.env.PropertySource<?> locate(final Environment environment) { - try { - LOG.info("Bootstrapping WA configuration"); - Map<String, Object> payload = new HashMap<>(); - return new MapPropertySource(getClass().getName(), payload); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to fetch settings", e); - } - } - }; + return new SyncopeWAPropertySourceLocator(waRestClient); } } diff --git a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java new file mode 100644 index 0000000..78f7633 --- /dev/null +++ b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.syncope.wa.bootstrap; + +import org.apereo.cas.configuration.CasConfigurationProperties; +import org.apereo.cas.configuration.CasCoreConfigurationUtils; + +import org.apache.syncope.common.lib.authentication.module.StaticAuthModuleConf; +import org.apache.syncope.common.lib.authentication.policy.AuthPolicyConf; +import org.apache.syncope.common.lib.policy.PolicyTO; +import org.apache.syncope.common.lib.to.AuthPolicyTO; +import org.apache.syncope.common.lib.to.ImplementationTO; +import org.apache.syncope.common.lib.types.AMImplementationType; +import org.apache.syncope.common.lib.types.PolicyType; +import org.apache.syncope.common.rest.api.service.ImplementationService; +import org.apache.syncope.common.rest.api.service.PolicyService; +import org.apache.syncope.core.provisioning.api.serialization.POJOHelper; +import org.apache.syncope.wa.WARestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; + +import java.util.List; +import java.util.Map; + +@Order +public class SyncopeWAPropertySourceLocator implements PropertySourceLocator { + private static final Logger LOG = LoggerFactory.getLogger(SyncopeWABootstrapConfiguration.class); + + private final PolicyService policyService; + + private final ImplementationService implementationService; + + public SyncopeWAPropertySourceLocator(final WARestClient waRestClient) { + this.policyService = waRestClient.getSyncopeClient().getService(PolicyService.class); + this.implementationService = waRestClient.getSyncopeClient().getService(ImplementationService.class); + } + + @Override + public PropertySource<?> locate(final Environment environment) { + try { + LOG.info("Bootstrapping WA configuration"); + List<PolicyTO> policies = policyService.list(PolicyType.AUTHENTICATION); + CasConfigurationProperties cas = new CasConfigurationProperties(); + policies. + stream(). + map(AuthPolicyTO.class::cast). + forEach(policy -> translatePolicy(policy, cas)); + Map<String, Object> payload = CasCoreConfigurationUtils.asMap(cas.withHolder()); + return new MapPropertySource(getClass().getName(), payload); + } catch (Exception e) { + throw new IllegalArgumentException("Unable to fetch settings", e); + } + } + + private void translatePolicy(final AuthPolicyTO policy, final CasConfigurationProperties cas) { + ImplementationTO implTO = implementationService. + read(AMImplementationType.AUTH_POLICY_CONFIGURATIONS, policy.getKey()); + AuthPolicyConf policyConf = POJOHelper.deserialize(implTO.getBody(), AuthPolicyConf.class); + if (policyConf instanceof StaticAuthModuleConf) { + StaticAuthModuleConf staticConf = (StaticAuthModuleConf) policyConf; + StringBuilder users = new StringBuilder(); + staticConf.getUsers().forEach((key, value) -> users.append(key).append("::").append(value)); + cas.getAuthn().getAccept().setName(policyConf.getName()); + cas.getAuthn().getAccept().setUsers(users.toString()); + } + } +} diff --git a/wa/bootstrap/src/main/resources/META-INF/spring.factories b/wa/bootstrap/src/main/resources/META-INF/spring.factories index 4cd20b8..0729129 100644 --- a/wa/bootstrap/src/main/resources/META-INF/spring.factories +++ b/wa/bootstrap/src/main/resources/META-INF/spring.factories @@ -16,4 +16,4 @@ # under the License. org.springframework.cloud.bootstrap.BootstrapConfiguration=\ - org.apache.syncope.wa.bootstrap.RestfulCloudConfigBootstrapConfiguration + org.apache.syncope.wa.bootstrap.SyncopeWABootstrapConfiguration diff --git a/wa/starter/pom.xml b/wa/starter/pom.xml index 246a577..2e2c7dc 100644 --- a/wa/starter/pom.xml +++ b/wa/starter/pom.xml @@ -303,7 +303,6 @@ under the License. <groupId>org.apache.syncope.common.keymaster</groupId> <artifactId>syncope-common-keymaster-client-zookeeper</artifactId> <version>${project.version}</version> - <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.curator</groupId>
