You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2019/12/29 06:01:08 UTC

[syncope] 02/03: Upgrading to latest Zookeeper

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git

commit 7c971849d2204448dbab6f06702ae4eac654c019
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Sat Dec 28 08:28:21 2019 +0100

    Upgrading to latest Zookeeper
---
 pom.xml                                            | 16 +---
 sra/pom.xml                                        |  2 +-
 .../apache/syncope/sra/ManagementController.java   | 93 ----------------------
 .../apache/syncope/sra/SyncopeSRAApplication.java  | 20 ++---
 sra/src/main/resources/application.properties      |  7 +-
 .../org/apache/syncope/sra/RouteRefresher.java     |  0
 ...eStartup.java => SyncopeCoreTestingServer.java} | 13 ++-
 .../org/apache/syncope/sra/SyncopeSRATest.java     | 10 ++-
 ...terStartup.java => ZookeeperTestingServer.java} | 38 +++------
 9 files changed, 43 insertions(+), 156 deletions(-)

diff --git a/pom.xml b/pom.xml
index 191f250..0c84250 100644
--- a/pom.xml
+++ b/pom.xml
@@ -492,7 +492,7 @@ under the License.
     <antlr4.version>4.7.2</antlr4.version>
 
     <curator.version>4.2.0</curator.version>
-    <zookeeper.version>3.4.14</zookeeper.version>
+    <zookeeper.version>3.5.6</zookeeper.version>
 
     <testds.port>1389</testds.port>
     <testdb.webport>9082</testdb.webport>
@@ -1723,23 +1723,11 @@ under the License.
         <groupId>org.apache.curator</groupId>
         <artifactId>curator-framework</artifactId>
         <version>${curator.version}</version>
-        <exclusions>
-          <exclusion>
-            <groupId>org.apache.zookeeper</groupId>
-            <artifactId>zookeeper</artifactId>
-          </exclusion>
-        </exclusions>
       </dependency>
       <dependency>
         <groupId>org.apache.curator</groupId>
         <artifactId>curator-x-discovery</artifactId>
         <version>${curator.version}</version>
-        <exclusions>
-          <exclusion>
-            <groupId>org.apache.zookeeper</groupId>
-            <artifactId>zookeeper</artifactId>
-          </exclusion>
-        </exclusions>
       </dependency>
       <dependency>
         <groupId>org.apache.zookeeper</groupId>
@@ -1761,7 +1749,7 @@ under the License.
       <dependency>
         <groupId>org.apache.curator</groupId>
         <artifactId>curator-test</artifactId>
-        <version>2.13.0</version>
+        <version>${curator.version}</version>
         <scope>test</scope>
       </dependency>
       <dependency>
diff --git a/sra/pom.xml b/sra/pom.xml
index db03aae..1139819 100644
--- a/sra/pom.xml
+++ b/sra/pom.xml
@@ -262,7 +262,7 @@ under the License.
             <configuration>
               <excludes>
                 <exclude>**/org/apache/syncope/sra/**Test.java</exclude>
-                <exclude>**/org/apache/syncope/sra/**Keymaster*.java</exclude>
+                <exclude>**/org/apache/syncope/sra/**Zookeeper*.java</exclude>
               </excludes>
             </configuration>
           </plugin>
diff --git a/sra/src/main/java/org/apache/syncope/sra/ManagementController.java b/sra/src/main/java/org/apache/syncope/sra/ManagementController.java
deleted file mode 100644
index 7d15556..0000000
--- a/sra/src/main/java/org/apache/syncope/sra/ManagementController.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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.sra;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cloud.gateway.route.Route;
-import org.springframework.cloud.gateway.route.RouteDefinition;
-import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
-import org.springframework.cloud.gateway.route.RouteLocator;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-import reactor.core.publisher.Mono;
-
-@RestController
-@RequestMapping(path = "/management")
-public class ManagementController {
-
-    @Autowired
-    private RouteRefresher routeRefresher;
-
-    @Autowired
-    private RouteDefinitionLocator routeDefinitionLocator;
-
-    @Autowired
-    private RouteLocator routeLocator;
-
-    @PostMapping("/routes/refresh")
-    public Mono<Void> refresh() {
-        routeRefresher.refresh();
-        return Mono.empty();
-    }
-
-    @GetMapping("/routes")
-    public Mono<List<Map<String, Object>>> routes() {
-        Mono<Map<String, RouteDefinition>> routeDefs =
-                routeDefinitionLocator.getRouteDefinitions().collectMap(RouteDefinition::getId);
-        Mono<List<Route>> routes = routeLocator.getRoutes().collectList();
-        return Mono.zip(routeDefs, routes).map(tuple -> {
-            Map<String, RouteDefinition> defs = tuple.getT1();
-            List<Route> routeList = tuple.getT2();
-            List<Map<String, Object>> allRoutes = new ArrayList<>();
-
-            routeList.forEach(route -> {
-                Map<String, Object> r = new HashMap<>();
-                r.put("route_id", route.getId());
-                r.put("order", route.getOrder());
-
-                if (defs.containsKey(route.getId())) {
-                    r.put("route_definition", defs.get(route.getId()));
-                } else {
-                    Map<String, Object> obj = new HashMap<>();
-
-                    obj.put("predicate", route.getPredicate().toString());
-
-                    if (!route.getFilters().isEmpty()) {
-                        obj.put("filters",
-                                route.getFilters().stream().map(Object::toString).collect(Collectors.toList()));
-                    }
-
-                    if (!obj.isEmpty()) {
-                        r.put("route_object", obj);
-                    }
-                }
-                allRoutes.add(r);
-            });
-
-            return allRoutes;
-        });
-    }
-}
diff --git a/sra/src/main/java/org/apache/syncope/sra/SyncopeSRAApplication.java b/sra/src/main/java/org/apache/syncope/sra/SyncopeSRAApplication.java
index e93191d..5a2e05e 100644
--- a/sra/src/main/java/org/apache/syncope/sra/SyncopeSRAApplication.java
+++ b/sra/src/main/java/org/apache/syncope/sra/SyncopeSRAApplication.java
@@ -18,9 +18,11 @@
  */
 package org.apache.syncope.sra;
 
+import java.util.Objects;
 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
+import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.gateway.route.Route;
 import org.springframework.cloud.gateway.route.RouteLocator;
@@ -35,12 +37,10 @@ import org.springframework.security.core.userdetails.MapReactiveUserDetailsServi
 import org.springframework.security.core.userdetails.User;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.web.server.SecurityWebFilterChain;
-import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
-import org.springframework.web.util.pattern.PathPatternParser;
+import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher;
+import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
 import reactor.core.publisher.Flux;
 
-import java.util.Objects;
-
 @PropertySource("classpath:sra.properties")
 @PropertySource(value = "file:${conf.directory}/sra.properties", ignoreResourceNotFound = true)
 @EnableWebFluxSecurity
@@ -67,11 +67,13 @@ public class SyncopeSRAApplication implements EnvironmentAware {
     }
 
     @Bean
-    public SecurityWebFilterChain springSecurityFilterChain(final ServerHttpSecurity http) {
-        http.csrf().disable().securityMatcher(
-                new PathPatternParserServerWebExchangeMatcher(new PathPatternParser().parse("/management/**"))).
-                authorizeExchange().anyExchange().hasRole(IdRepoEntitlement.ANONYMOUS).and().httpBasic();
-        return http.build();
+    public SecurityWebFilterChain actuatorSecurityFilterChain(final ServerHttpSecurity http) {
+        ServerWebExchangeMatcher actuatorMatcher = EndpointRequest.toAnyEndpoint();
+        return http.securityMatcher(actuatorMatcher).
+                authorizeExchange().anyExchange().authenticated().
+                and().httpBasic().
+                and().csrf().requireCsrfProtectionMatcher(new NegatedServerWebExchangeMatcher(actuatorMatcher)).
+                and().build();
     }
 
     @Bean
diff --git a/sra/src/main/resources/application.properties b/sra/src/main/resources/application.properties
index b50f4f9..c77fcaa 100644
--- a/sra/src/main/resources/application.properties
+++ b/sra/src/main/resources/application.properties
@@ -21,8 +21,11 @@ spring.main.banner-mode=log
 server.port=8080
 
 spring.cloud.gateway.metrics.enabled=true
+ 
+management.security.enabled=true
+management.endpoint.gateway.enabled=true
+management.endpoints.web.exposure.include=health,loggers,metrics,prometheus,gateway
+management.endpoint.health.show-details=always
 management.endpoint.metrics.enabled=true
-management.endpoint.prometheus.enabled=true
-management.metrics.export.prometheus.enabled=true
 
 service.discovery.address=http://localhost:8080/
diff --git a/sra/src/main/java/org/apache/syncope/sra/RouteRefresher.java b/sra/src/test/java/org/apache/syncope/sra/RouteRefresher.java
similarity index 100%
rename from sra/src/main/java/org/apache/syncope/sra/RouteRefresher.java
rename to sra/src/test/java/org/apache/syncope/sra/RouteRefresher.java
diff --git a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestCoreStartup.java b/sra/src/test/java/org/apache/syncope/sra/SyncopeCoreTestingServer.java
similarity index 92%
rename from sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestCoreStartup.java
rename to sra/src/test/java/org/apache/syncope/sra/SyncopeCoreTestingServer.java
index 71c44ea..0d740e4 100644
--- a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestCoreStartup.java
+++ b/sra/src/test/java/org/apache/syncope/sra/SyncopeCoreTestingServer.java
@@ -29,18 +29,17 @@ import javax.ws.rs.NotFoundException;
 import javax.ws.rs.core.Response;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.syncope.common.keymaster.client.api.ServiceOps;
 import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
 import org.apache.syncope.common.lib.to.GatewayRouteTO;
 import org.apache.syncope.common.rest.api.service.GatewayRouteService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.ContextRefreshedEvent;
-import org.springframework.core.Ordered;
 import org.springframework.stereotype.Component;
 
 @Component
-public class SyncopeSRATestCoreStartup extends SyncopeSRAStartStop
-        implements ApplicationListener<ContextRefreshedEvent>, Ordered {
+public class SyncopeCoreTestingServer implements ApplicationListener<ContextRefreshedEvent> {
 
     public static final String ADDRESS = "http://localhost:9080/syncope/rest";
 
@@ -49,10 +48,8 @@ public class SyncopeSRATestCoreStartup extends SyncopeSRAStartStop
     @Autowired
     private RouteRefresher routeRefresher;
 
-    @Override
-    public int getOrder() {
-        return Ordered.LOWEST_PRECEDENCE;
-    }
+    @Autowired
+    private ServiceOps serviceOps;
 
     @Override
     public void onApplicationEvent(final ContextRefreshedEvent event) {
@@ -69,7 +66,7 @@ public class SyncopeSRATestCoreStartup extends SyncopeSRAStartStop
         // 2. register Core in Keymaster
         NetworkService core = new NetworkService();
         core.setType(NetworkService.Type.CORE);
-        core.setAddress(SyncopeSRATestCoreStartup.ADDRESS);
+        core.setAddress(ADDRESS);
         serviceOps.register(core);
     }
 
diff --git a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATest.java b/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATest.java
index 7225d68..e5caef8 100644
--- a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATest.java
+++ b/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATest.java
@@ -46,10 +46,12 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.web.reactive.server.WebTestClient;
 import org.springframework.web.reactive.function.BodyInserters;
 
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@ContextConfiguration(initializers = ZookeeperTestingServer.class)
 @AutoConfigureWireMock(port = 0)
 public class SyncopeSRATest {
 
@@ -66,7 +68,7 @@ public class SyncopeSRATest {
 
     @BeforeEach
     public void clearRoutes() {
-        SyncopeSRATestCoreStartup.ROUTES.clear();
+        SyncopeCoreTestingServer.ROUTES.clear();
     }
 
     @Test
@@ -94,7 +96,7 @@ public class SyncopeSRATest {
         routeTO.getFilters().add(new GatewayRouteFilter.Builder().
                 factory(FilterFactory.ADD_RESPONSE_HEADER).args("Hello,World").build());
 
-        SyncopeSRATestCoreStartup.ROUTES.put(routeTO.getKey(), routeTO);
+        SyncopeCoreTestingServer.ROUTES.put(routeTO.getKey(), routeTO);
 
         routeRefresher.refresh();
 
@@ -147,7 +149,7 @@ public class SyncopeSRATest {
         routeTO.getFilters().add(new GatewayRouteFilter.Builder().
                 factory(FilterFactory.HYSTRIX).args("fallbackcmd,forward:/fallback").build());
 
-        SyncopeSRATestCoreStartup.ROUTES.put(routeTO.getKey(), routeTO);
+        SyncopeCoreTestingServer.ROUTES.put(routeTO.getKey(), routeTO);
 
         routeRefresher.refresh();
 
@@ -179,7 +181,7 @@ public class SyncopeSRATest {
                 factory(FilterFactory.CUSTOM).
                 args(BodyPropertyAddingGatewayFilterFactory.class.getName() + ";customized=true").build());
 
-        SyncopeSRATestCoreStartup.ROUTES.put(routeTO.getKey(), routeTO);
+        SyncopeCoreTestingServer.ROUTES.put(routeTO.getKey(), routeTO);
 
         routeRefresher.refresh();
 
diff --git a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestKeymasterStartup.java b/sra/src/test/java/org/apache/syncope/sra/ZookeeperTestingServer.java
similarity index 68%
rename from sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestKeymasterStartup.java
rename to sra/src/test/java/org/apache/syncope/sra/ZookeeperTestingServer.java
index c5649f6..89fc3bc 100644
--- a/sra/src/test/java/org/apache/syncope/sra/SyncopeSRATestKeymasterStartup.java
+++ b/sra/src/test/java/org/apache/syncope/sra/ZookeeperTestingServer.java
@@ -18,6 +18,8 @@
  */
 package org.apache.syncope.sra;
 
+import static org.junit.jupiter.api.Assertions.fail;
+
 import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
@@ -27,24 +29,15 @@ import javax.security.auth.login.AppConfigurationEntry;
 import javax.security.auth.login.Configuration;
 import org.apache.curator.test.InstanceSpec;
 import org.apache.curator.test.TestingServer;
-import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.event.ContextRefreshedEvent;
-import org.springframework.core.Ordered;
-import org.springframework.stereotype.Component;
-
-@Component
-public class SyncopeSRATestKeymasterStartup extends SyncopeSRAStartStop
-        implements ApplicationListener<ContextRefreshedEvent>, Ordered {
+import org.apache.zookeeper.server.auth.DigestLoginModule;
+import org.apache.zookeeper.server.auth.SASLAuthenticationProvider;
+import org.springframework.context.ApplicationContextInitializer;
+import org.springframework.context.ConfigurableApplicationContext;
 
-    @Override
-    public int getOrder() {
-        return Ordered.HIGHEST_PRECEDENCE;
-    }
+public class ZookeeperTestingServer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
 
     @Override
-    public void onApplicationEvent(final ContextRefreshedEvent event) {
-        // 1. start Zookeeper for Keymaster
+    public void initialize(final ConfigurableApplicationContext ctx) {
         AtomicReference<String> username = new AtomicReference<>();
         AtomicReference<String> password = new AtomicReference<>();
         try (InputStream propStream = getClass().getResourceAsStream("/keymaster.properties")) {
@@ -61,7 +54,7 @@ public class SyncopeSRATestKeymasterStartup extends SyncopeSRAStartStop
 
             private final AppConfigurationEntry[] entries = {
                 new AppConfigurationEntry(
-                "org.apache.zookeeper.server.auth.DigestLoginModule",
+                DigestLoginModule.class.getName(),
                 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
                 Map.of(
                 "user_" + username.get(), password.get()
@@ -75,18 +68,13 @@ public class SyncopeSRATestKeymasterStartup extends SyncopeSRAStartStop
         });
 
         Map<String, Object> customProperties = new HashMap<>();
-        customProperties.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
+        customProperties.put("authProvider.1", SASLAuthenticationProvider.class.getName());
         InstanceSpec spec = new InstanceSpec(null, 2181, -1, -1, true, 1, -1, -1, customProperties);
+
         try {
-            new TestingServer(spec, true).start();
+            new TestingServer(spec, true);
         } catch (Exception e) {
-            throw new IllegalStateException("Could not start Zookeeper", e);
+            fail(e);
         }
-
-        // 2. register Core in Keymaster
-        NetworkService core = new NetworkService();
-        core.setType(NetworkService.Type.CORE);
-        core.setAddress(SyncopeSRATestCoreStartup.ADDRESS);
-        serviceOps.register(core);
     }
 }