You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by mm...@apache.org on 2020/07/10 15:48:22 UTC

[syncope] branch master updated: NOJIRA: Allow push ops to WA to reload services (#202)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 159a03b  NOJIRA: Allow push ops to WA to reload services (#202)
159a03b is described below

commit 159a03b4669f150f5a79ee6f102b6c41c14a946d
Author: Misagh Moayyed <mm...@gmail.com>
AuthorDate: Fri Jul 10 20:18:10 2020 +0430

    NOJIRA: Allow push ops to WA to reload services (#202)
    
    * allow push ops to WA to reload/refresh services
    
    * switch to get
    
    * rename methods
---
 .../syncope/common/lib/types/AMEntitlement.java    |  2 ++
 .../common/rest/api/service/ClientAppService.java  |  7 ++++
 .../apache/syncope/core/logic/ClientAppLogic.java  | 41 ++++++++++++++++++++++
 .../rest/cxf/service/ClientAppServiceImpl.java     |  5 +++
 .../starter/services/SyncopeWAServiceRegistry.java |  4 +--
 5 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/common/am/lib/src/main/java/org/apache/syncope/common/lib/types/AMEntitlement.java b/common/am/lib/src/main/java/org/apache/syncope/common/lib/types/AMEntitlement.java
index 20b08e1..2885db4 100644
--- a/common/am/lib/src/main/java/org/apache/syncope/common/lib/types/AMEntitlement.java
+++ b/common/am/lib/src/main/java/org/apache/syncope/common/lib/types/AMEntitlement.java
@@ -44,6 +44,8 @@ public final class AMEntitlement {
 
     public static final String CLIENTAPP_DELETE = "CLIENTAPP_DELETE";
 
+    public static final String CLIENTAPP_PUSH = "CLIENTAPP_PUSH";
+
     public static final String AUTH_MODULE_LIST = "AUTH_MODULE_LIST";
 
     public static final String AUTH_MODULE_CREATE = "AUTH_MODULE_CREATE";
diff --git a/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/ClientAppService.java b/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/ClientAppService.java
index c2e2398..87e9103 100644
--- a/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/ClientAppService.java
+++ b/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/ClientAppService.java
@@ -131,4 +131,11 @@ public interface ClientAppService extends JAXRSService {
     @Path("{type}/{key}")
     @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML })
     void delete(@NotNull @PathParam("type") ClientAppType type, @NotNull @PathParam("key") String key);
+
+    @ApiResponses(
+        @ApiResponse(responseCode = "204", description = "Operation was successful"))
+    @POST
+    @Path("push")
+    @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML })
+    void pushToWA();
 }
diff --git a/core/am/logic/src/main/java/org/apache/syncope/core/logic/ClientAppLogic.java b/core/am/logic/src/main/java/org/apache/syncope/core/logic/ClientAppLogic.java
index 6e27383..0c9b0cb 100644
--- a/core/am/logic/src/main/java/org/apache/syncope/core/logic/ClientAppLogic.java
+++ b/core/am/logic/src/main/java/org/apache/syncope/core/logic/ClientAppLogic.java
@@ -18,11 +18,21 @@
  */
 package org.apache.syncope.core.logic;
 
+import java.io.IOException;
 import java.lang.reflect.Method;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.cxf.transport.http.auth.DefaultBasicAuthSupplier;
+import org.apache.syncope.common.keymaster.client.api.KeymasterException;
+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.SyncopeClientException;
 import org.apache.syncope.common.lib.to.client.ClientAppTO;
 import org.apache.syncope.common.lib.types.AMEntitlement;
@@ -42,10 +52,17 @@ import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.annotation.Resource;
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.core.HttpHeaders;
+
 @Component
 public class ClientAppLogic extends AbstractTransactionalLogic<ClientAppTO> {
 
     @Autowired
+    private ServiceOps serviceOps;
+
+    @Autowired
     private ClientAppUtilsFactory clientAppUtilsFactory;
 
     @Autowired
@@ -57,6 +74,12 @@ public class ClientAppLogic extends AbstractTransactionalLogic<ClientAppTO> {
     @Autowired
     private OIDCRPDAO oidcrpDAO;
 
+    @Resource(name = "anonymousUser")
+    private String anonymousUser;
+
+    @Resource(name = "anonymousKey")
+    private String anonymousKey;
+
     @PreAuthorize("hasRole('" + AMEntitlement.CLIENTAPP_LIST + "')")
     public <T extends ClientAppTO> List<T> list(final ClientAppType type) {
         Stream<T> stream;
@@ -201,4 +224,22 @@ public class ClientAppLogic extends AbstractTransactionalLogic<ClientAppTO> {
 
         throw new UnresolvedReferenceException();
     }
+
+    @PreAuthorize("hasRole('" + AMEntitlement.CLIENTAPP_PUSH + "')")
+    public void pushToWA() {
+        try {
+            NetworkService wa = serviceOps.get(NetworkService.Type.WA);
+            HttpClient.newBuilder().build().send(
+                HttpRequest.newBuilder(URI.create(
+                    StringUtils.appendIfMissing(wa.getAddress(), "/") + "actuator/registeredServices")).
+                    header(HttpHeaders.AUTHORIZATION,
+                        DefaultBasicAuthSupplier.getBasicAuthHeader(anonymousUser, anonymousKey)).
+                    GET().build(),
+                HttpResponse.BodyHandlers.discarding());
+        } catch (KeymasterException e) {
+            throw new NotFoundException("Could not find any WA instance", e);
+        } catch (IOException | InterruptedException e) {
+            throw new InternalServerErrorException("Errors while communicating with WA instance", e);
+        }
+    }
 }
diff --git a/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/ClientAppServiceImpl.java b/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/ClientAppServiceImpl.java
index e2e461e..4ee2676 100644
--- a/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/ClientAppServiceImpl.java
+++ b/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/ClientAppServiceImpl.java
@@ -63,4 +63,9 @@ public class ClientAppServiceImpl extends AbstractServiceImpl implements ClientA
     public void delete(final ClientAppType type, final String key) {
         logic.delete(type, key);
     }
+
+    @Override
+    public void pushToWA() {
+        logic.pushToWA();
+    }
 }
diff --git a/wa/starter/src/main/java/org/apache/syncope/wa/starter/services/SyncopeWAServiceRegistry.java b/wa/starter/src/main/java/org/apache/syncope/wa/starter/services/SyncopeWAServiceRegistry.java
index 4c32a09..b0c4902 100644
--- a/wa/starter/src/main/java/org/apache/syncope/wa/starter/services/SyncopeWAServiceRegistry.java
+++ b/wa/starter/src/main/java/org/apache/syncope/wa/starter/services/SyncopeWAServiceRegistry.java
@@ -63,7 +63,7 @@ public class SyncopeWAServiceRegistry extends AbstractServiceRegistry {
     public boolean delete(final RegisteredService registeredService) {
         throw new UnsupportedOperationException("Deleting registered services from WA is not supported");
     }
-
+    
     @Override
     public Collection<RegisteredService> load() {
         SyncopeClient syncopeClient = waRestClient.getSyncopeClient();
@@ -73,7 +73,7 @@ public class SyncopeWAServiceRegistry extends AbstractServiceRegistry {
         } else {
             LOG.info("Loading application definitions");
             return waRestClient.getSyncopeClient().getService(WAClientAppService.class).list().stream().
-                    map(clientApp -> registeredServiceMapper.toRegisteredService(clientApp)).
+                    map(registeredServiceMapper::toRegisteredService).
                     collect(Collectors.toList());
         }
     }