1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic;
20
21 import java.lang.reflect.Method;
22 import java.net.URI;
23 import java.net.http.HttpClient;
24 import java.net.http.HttpRequest;
25 import java.net.http.HttpResponse;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.HttpHeaders;
29 import org.apache.commons.lang3.ArrayUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.cxf.transport.http.auth.DefaultBasicAuthSupplier;
32 import org.apache.syncope.common.keymaster.client.api.KeymasterException;
33 import org.apache.syncope.common.keymaster.client.api.ServiceOps;
34 import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
35 import org.apache.syncope.common.lib.to.SRARouteTO;
36 import org.apache.syncope.common.lib.types.AMEntitlement;
37 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
38 import org.apache.syncope.core.persistence.api.dao.SRARouteDAO;
39 import org.apache.syncope.core.persistence.api.entity.EntityFactory;
40 import org.apache.syncope.core.persistence.api.entity.SRARoute;
41 import org.apache.syncope.core.provisioning.api.data.SRARouteDataBinder;
42 import org.apache.syncope.core.spring.security.SecurityProperties;
43 import org.springframework.security.access.prepost.PreAuthorize;
44
45 public class SRARouteLogic extends AbstractTransactionalLogic<SRARouteTO> {
46
47 protected final SRARouteDAO routeDAO;
48
49 protected final SRARouteDataBinder binder;
50
51 protected final EntityFactory entityFactory;
52
53 protected final ServiceOps serviceOps;
54
55 protected final SecurityProperties securityProperties;
56
57 public SRARouteLogic(
58 final SRARouteDAO routeDAO,
59 final SRARouteDataBinder binder,
60 final EntityFactory entityFactory,
61 final ServiceOps serviceOps,
62 final SecurityProperties securityProperties) {
63
64 this.routeDAO = routeDAO;
65 this.binder = binder;
66 this.entityFactory = entityFactory;
67 this.serviceOps = serviceOps;
68 this.securityProperties = securityProperties;
69 }
70
71 @PreAuthorize("isAuthenticated()")
72 public List<SRARouteTO> list() {
73 return routeDAO.findAll().stream().map(binder::getSRARouteTO).collect(Collectors.toList());
74 }
75
76 @PreAuthorize("hasRole('" + AMEntitlement.SRA_ROUTE_CREATE + "')")
77 public SRARouteTO create(final SRARouteTO routeTO) {
78 SRARoute route = entityFactory.newEntity(SRARoute.class);
79 binder.getSRARoute(route, routeTO);
80
81 return binder.getSRARouteTO(routeDAO.save(route));
82 }
83
84 @PreAuthorize("isAuthenticated()")
85 public SRARouteTO read(final String key) {
86 SRARoute route = routeDAO.find(key);
87 if (route == null) {
88 throw new NotFoundException("SRARoute " + key);
89 }
90 return binder.getSRARouteTO(route);
91 }
92
93 @PreAuthorize("hasRole('" + AMEntitlement.SRA_ROUTE_UPDATE + "')")
94 public SRARouteTO update(final SRARouteTO routeTO) {
95 SRARoute route = routeDAO.find(routeTO.getKey());
96 if (route == null) {
97 throw new NotFoundException("SRARoute " + routeTO.getKey());
98 }
99
100 binder.getSRARoute(route, routeTO);
101
102 return binder.getSRARouteTO(routeDAO.save(route));
103 }
104
105 @PreAuthorize("hasRole('" + AMEntitlement.SRA_ROUTE_DELETE + "')")
106 public SRARouteTO delete(final String key) {
107 SRARoute route = routeDAO.find(key);
108 if (route == null) {
109 throw new NotFoundException("SRARoute " + key);
110 }
111
112 SRARouteTO deleted = binder.getSRARouteTO(route);
113 routeDAO.delete(route);
114 return deleted;
115 }
116
117 @PreAuthorize("hasRole('" + AMEntitlement.SRA_ROUTE_PUSH + "')")
118 public void pushToSRA() {
119 HttpClient client = HttpClient.newHttpClient();
120 try {
121 serviceOps.list(NetworkService.Type.SRA).forEach(sra -> client.sendAsync(
122 HttpRequest.newBuilder(URI.create(
123 StringUtils.appendIfMissing(sra.getAddress(), "/") + "actuator/gateway/refresh")).
124 header(HttpHeaders.AUTHORIZATION, DefaultBasicAuthSupplier.getBasicAuthHeader(
125 securityProperties.getAnonymousUser(), securityProperties.getAnonymousKey())).
126 POST(HttpRequest.BodyPublishers.noBody()).build(),
127 HttpResponse.BodyHandlers.discarding()).
128 thenAcceptAsync(response -> LOG.info(
129 "Pushed to SRA instance {} with HTTP status: {}", sra.getAddress(), response.statusCode())));
130 } catch (KeymasterException e) {
131 throw new NotFoundException("Could not find any WA instance", e);
132 }
133 }
134
135 @Override
136 protected SRARouteTO resolveReference(final Method method, final Object... args)
137 throws UnresolvedReferenceException {
138
139 String key = null;
140
141 if (ArrayUtils.isNotEmpty(args) && ("create".equals(method.getName())
142 || "update".equals(method.getName())
143 || "delete".equals(method.getName()))) {
144 for (int i = 0; key == null && i < args.length; i++) {
145 if (args[i] instanceof String) {
146 key = (String) args[i];
147 } else if (args[i] instanceof SRARouteTO) {
148 key = ((SRARouteTO) args[i]).getKey();
149 }
150 }
151 }
152
153 if (key != null) {
154 try {
155 return binder.getSRARouteTO(routeDAO.find(key));
156 } catch (Throwable ignore) {
157 LOG.debug("Unresolved reference", ignore);
158 throw new UnresolvedReferenceException(ignore);
159 }
160 }
161
162 throw new UnresolvedReferenceException();
163 }
164 }