ajschmidt commented on a change in pull request #3534: TP Delivery Service 
Generate SSL update, new letsencrypt generate and…
URL: https://github.com/apache/trafficcontrol/pull/3534#discussion_r338674648
 
 

 ##########
 File path: 
traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/ds/LetsEncryptDnsChallengeWatcher.java
 ##########
 @@ -0,0 +1,180 @@
+package com.comcast.cdn.traffic_control.traffic_router.core.ds;
+
+/*
+ *
+ * Licensed 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.
+ */
+
+import 
com.comcast.cdn.traffic_control.traffic_router.core.config.ConfigHandler;
+import 
com.comcast.cdn.traffic_control.traffic_router.core.util.AbstractResourceWatcher;
+import com.comcast.cdn.traffic_control.traffic_router.core.util.JsonUtils;
+import 
com.comcast.cdn.traffic_control.traffic_router.core.util.JsonUtilsException;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.log4j.Logger;
+
+import java.io.*;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.List;
+
+public class LetsEncryptDnsChallengeWatcher extends AbstractResourceWatcher {
+    private static final Logger LOGGER = 
Logger.getLogger(LetsEncryptDnsChallengeWatcher.class);
+    public static final String DEFAULT_LE_DNS_CHALLENGE_URL = 
"https://${toHostname}/api/1.4/letsencrypt/dnsrecords/";;
+    private static final String configFile = 
"/opt/traffic_router/db/cr-config.json";
+
+    private ConfigHandler configHandler;
+
+    public void setConfigHandler(final ConfigHandler configHandler) {
+        this.configHandler = configHandler;
+    }
+
+    public LetsEncryptDnsChallengeWatcher() {
+        setDatabaseUrl(DEFAULT_LE_DNS_CHALLENGE_URL);
+    }
+
+    @Override
+    public boolean useData(final String data) {
+        try {
+            final ObjectMapper mapper = new ObjectMapper(new JsonFactory());
+            final HashMap<String, List<LetsEncryptDnsChallenge>> dataMap = 
mapper.readValue(data, new TypeReference<HashMap<String, 
List<LetsEncryptDnsChallenge>>>() { });
+            final List<LetsEncryptDnsChallenge> challengeList = 
dataMap.get("response");
+
+            final JsonNode mostRecentConfig = 
mapper.readTree(readConfigFile());
+            final ObjectNode deliveryServicesNode = (ObjectNode) 
JsonUtils.getJsonNode(mostRecentConfig, ConfigHandler.deliveryServicesKey);
+
+
+            challengeList.forEach(challenge -> {
+                final StringBuilder sb = new StringBuilder();
+                sb.append(challenge.getFqdn());
+                if (!challenge.getFqdn().endsWith(".")) {
+                    sb.append('.');
+                }
+                final String challengeDomain = sb.toString();
+                final String fqdn = challengeDomain.substring(0, 
challengeDomain.length() - 1).replace("_acme-challenge.", "");
+
+                ObjectNode deliveryServiceConfig = null;
+                String dsLabel = "";
+                final StringBuilder nameSb = new StringBuilder();
+                nameSb.append("_acme-challenge");
+                for (final String label : fqdn.split("\\.")) {
+                    deliveryServiceConfig = (ObjectNode) 
deliveryServicesNode.get(label);
+                    if (deliveryServiceConfig != null) {
+                        dsLabel = label;
+                        break;
+                    } else {
+                        nameSb.append('.');
+                        nameSb.append(label);
+                    }
+                }
+
+                final String name = nameSb.toString();
+
+                final ArrayNode staticDnsEntriesNode = 
updateStaticEntries(challenge, name, mapper, deliveryServiceConfig);
+
+                deliveryServiceConfig.set("staticDnsEntries", 
staticDnsEntriesNode);
+                deliveryServicesNode.set(dsLabel, deliveryServiceConfig);
+
+            });
+
+            final ObjectNode statsNode = (ObjectNode) 
mostRecentConfig.get("stats");
+            statsNode.put("date", Instant.now().toEpochMilli() / 1000L);
+
+            final ObjectNode fullConfig = (ObjectNode) mostRecentConfig;
+            fullConfig.set(ConfigHandler.deliveryServicesKey, 
deliveryServicesNode);
+            fullConfig.set("stats", statsNode);
+
+            try {
+                configHandler.processConfig(fullConfig.toString());
 
 Review comment:
   All these Traffic Router changes look good to me. This call to 
'processConfig' currently has a high impact on the Traffic Router service 
because it forces TR to reload its entire configuration and re-sign all the DNS 
zones on the CDN, but the [Delivery Service Only Snapshots 
2785](https://github.com/apache/trafficcontrol/pull/2785) feature which will 
soon be added to Traffic Router will almost completely mitigate this impact. I 
recommend the these two PRs be placed in the same release. 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to