DTMDeployHookServlet.java
6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.ConfigurationPolicy
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.cm.ConfigurationAdmin
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dtm.impl.servlets;
import com.adobe.cq.dtm.impl.constants.DTMServerType;
import com.adobe.cq.dtm.impl.util.DTMConfigurationUtil;
import java.io.IOException;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, policy=ConfigurationPolicy.REQUIRE, label="Adobe DTM Deploy Hook Configuration", description="The central DTM deploy hook configuration component.")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"etc/dtm/hook"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"bundle-download"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"POST"}, propertyPrivate=1), @Property(name="sling.auth.requirements", value={"-/etc/dtm-hook"}, propertyPrivate=1)})
public class DTMDeployHookServlet
extends SlingAllMethodsServlet {
private final Logger LOG = LoggerFactory.getLogger(DTMDeployHookServlet.class);
public static final String DOWNLOAD_SELECTOR = "bundle-download";
private String[] stagingIpWhitelist;
private String[] productionIpWhitelist;
@Reference
private EventAdmin eventAdmin;
@Reference
private ConfigurationAdmin configurationAdmin;
@Property(cardinality=Integer.MAX_VALUE, label="Staging DTM IP White List", description="List of DTM staging IPs which are allowed to trigger the DTM download workflow trough the DTM deploy hook feature.")
public static final String DTM_STAGING_IP_WHITELIST = "dtm.staging.ip.whitelist";
@Property(cardinality=Integer.MAX_VALUE, label="Production DTM IP White List", description="List of DTM production IPs which are allowed to trigger the DTM download workflow trough the DTM deploy hook feature.")
public static final String DTM_PRODUCTION_IP_WHITELIST = "dtm.production.ip.whitelist";
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String requestIPAddress;
Resource currentResource = request.getResource();
String dtmConfigName = currentResource.getParent().getName();
DTMServerType dtmServerType = DTMServerType.valueOf(currentResource.getName().toUpperCase());
if (this.isRequestFromAuthorizedHost(dtmServerType, requestIPAddress = request.getRemoteAddr())) {
DTMConfigurationUtil.sendTriggerWorkflowOSGIEvent(this.eventAdmin, "/etc/cloudservices/dynamictagmanagement/" + dtmConfigName + "/" + "jcr:content", dtmServerType);
} else {
this.LOG.error("Request made by unauthorized host: (" + requestIPAddress + ")");
}
}
private boolean isRequestFromAuthorizedHost(DTMServerType dtmServerType, String requestIPAddress) throws ServletException, IOException {
String[] ipWhiteList;
String[] arrstring = ipWhiteList = DTMServerType.STAGING.equals((Object)dtmServerType) ? this.stagingIpWhitelist : this.productionIpWhitelist;
if (ipWhiteList != null) {
for (String ip : ipWhiteList) {
if (!requestIPAddress.equals(ip)) continue;
return true;
}
} else {
this.LOG.error("Unable to find the DTM {} servers IP white list property on the OSGI config.", (Object)dtmServerType);
throw new ServletException("Unable to check if the host is authorized.");
}
return false;
}
@Activate
@Modified
private void activate(Map<String, Object> props) {
if (props != null) {
this.stagingIpWhitelist = PropertiesUtil.toStringArray((Object)props.get("dtm.staging.ip.whitelist"));
this.productionIpWhitelist = PropertiesUtil.toStringArray((Object)props.get("dtm.production.ip.whitelist"));
}
}
protected void bindEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
protected void unbindEventAdmin(EventAdmin eventAdmin) {
if (this.eventAdmin == eventAdmin) {
this.eventAdmin = null;
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
}