You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/12/15 02:19:58 UTC

[GitHub] elsloo closed pull request #3129: Fix TO CRConfig tm.url to be FQDN, not URI

elsloo closed pull request #3129: Fix TO CRConfig tm.url to be FQDN, not URI
URL: https://github.com/apache/trafficcontrol/pull/3129
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/traffic_ops/testing/api/v14/crconfig_test.go b/traffic_ops/testing/api/v14/crconfig_test.go
index 08fb1301b..81569f5d8 100644
--- a/traffic_ops/testing/api/v14/crconfig_test.go
+++ b/traffic_ops/testing/api/v14/crconfig_test.go
@@ -61,7 +61,7 @@ func UpdateTestCRConfigSnapshot(t *testing.T) {
 	cdn := testData.CDNs[0].Name
 
 	tmURLParamName := "tm.url"
-	tmURLExpected := "https://crconfig.tm.url.test.invalid"
+	tmURLExpected := "crconfig.tm.url.test.invalid"
 	_, _, err := TOSession.CreateParameter(tc.Parameter{
 		ConfigFile: "global",
 		Name:       tmURLParamName,
diff --git a/traffic_ops/traffic_ops_golang/crconfig/crconfig.go b/traffic_ops/traffic_ops_golang/crconfig/crconfig.go
index 407c296e6..55d3d217b 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/crconfig.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/crconfig.go
@@ -22,6 +22,8 @@ package crconfig
 import (
 	"database/sql"
 	"errors"
+	"net/url"
+	"strings"
 
 	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
@@ -52,14 +54,14 @@ func Make(tx *sql.Tx, cdn, user, toHost, reqPath, toVersion string, useClientReq
 	}
 
 	if !useClientReqHost {
-		paramHost, ok, err := getGlobalParam(tx, "tm.url")
+		paramTMURL, ok, err := getGlobalParam(tx, "tm.url")
 		if err != nil {
 			return nil, errors.New("getting global 'tm.url' parameter: " + err.Error())
 		}
 		if !ok {
 			log.Warnln("Making CRConfig: no global tm.url parameter found! Using request host header instead!")
 		}
-		toHost = paramHost
+		toHost = getTMURLHost(paramTMURL)
 	}
 
 	if emulateOldPath {
@@ -69,3 +71,26 @@ func Make(tx *sql.Tx, cdn, user, toHost, reqPath, toVersion string, useClientReq
 	crc.Stats = makeStats(cdn, user, toHost, reqPath, toVersion)
 	return &crc, nil
 }
+
+// getTMURLHost returns the FQDN from a tm.url global parameter, which should be either an FQDN or a Hostname.
+// If tmURL is a valid URL, the FQDN is returned.
+// if tmURL is not a valid URL, it is returned with any leading 'http://' or 'http://' removed, and everything after the next '/' removed.
+func getTMURLHost(tmURL string) string {
+	if !strings.HasPrefix(tmURL, "http://") && !strings.HasPrefix(tmURL, "https://") {
+		tmURL = "http://" + tmURL // if it doesn't begin with "http://", add it so it's a valid URL to parse
+	}
+	uri, err := url.Parse(tmURL)
+	if err == nil {
+		return uri.Host
+	}
+
+	// if it isn't a valid URL, do the best we can: strip the protocol and path
+	tmURL = strings.TrimPrefix(tmURL, "https://")
+	tmURL = strings.TrimPrefix(tmURL, "http://")
+	pathStart := strings.Index(tmURL, "/")
+	if pathStart == -1 {
+		return tmURL
+	}
+	tmURL = tmURL[:pathStart]
+	return tmURL
+}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/crconfig_test.go b/traffic_ops/traffic_ops_golang/crconfig/crconfig_test.go
new file mode 100644
index 000000000..3c4670b7c
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/crconfig/crconfig_test.go
@@ -0,0 +1,77 @@
+package crconfig
+
+/*
+ * 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.
+ */
+
+import (
+	"testing"
+)
+
+func TestGetTMURLHost(t *testing.T) {
+	inputOutputs := [][]string{
+		{"a", "a"},
+		{"example.com", "example.com"},
+		{"example.com:42", "example.com:42"},
+		{"example.com:80", "example.com:80"},
+		{"example.com:443", "example.com:443"},
+		{"http://example.com:42", "example.com:42"},
+		{"https://example.com:42", "example.com:42"},
+		{"http://example.com:80", "example.com:80"},
+		{"https://example.com:80", "example.com:80"},
+		{"https://example.com:443", "example.com:443"},
+		{"http://example.com:443", "example.com:443"},
+		{"example.com:42/", "example.com:42"},
+		{"http://example.com:42/", "example.com:42"},
+		{"https://example.com:42/", "example.com:42"},
+		{"http://example.com:80/", "example.com:80"},
+		{"https://example.com:80/", "example.com:80"},
+		{"https://example.com:443/", "example.com:443"},
+		{"http://example.com:443/", "example.com:443"},
+		{"example.com:42/a/b", "example.com:42"},
+		{"http://example.com:42/a/b", "example.com:42"},
+		{"https://example.com:42/a/b", "example.com:42"},
+		{"http://example.com:80/a/b", "example.com:80"},
+		{"https://example.com:80/a/b", "example.com:80"},
+		{"https://example.com:443/a/b", "example.com:443"},
+		{"http://example.com:443/a/b", "example.com:443"},
+		{"example.com:42/a/b?c=42/d", "example.com:42"},
+		{"http://example.com:42/a/b?c=42/d", "example.com:42"},
+		{"https://example.com:42/a/b?c=42/d", "example.com:42"},
+		{"http://example.com:80/a/b?c=42/d", "example.com:80"},
+		{"https://example.com:80/a/b?c=42/d", "example.com:80"},
+		{"https://example.com:443/a/b?c=42/d", "example.com:443"},
+		{"http://example.com:443/a/b?c=42/d", "example.com:443"},
+		{"http://foo.example.com", "foo.example.com"},
+		{"https://foo.example.com", "foo.example.com"},
+		{"https://foo.example.com/bar", "foo.example.com"},
+		{"http://foo.example.com/bar", "foo.example.com"},
+		{"foo.example.com/bar", "foo.example.com"},
+		{"http:::foo.com$%^&*()__+/abc/def?42", "http:::foo.com$%^&*()__+"},
+		{"http:::foo.com$%^&*()__+/", "http:::foo.com$%^&*()__+"},
+		{"http:::foo.com$%^&*()__+", "http:::foo.com$%^&*()__+"},
+		{"asdf1345978fasf", "asdf1345978fasf"},
+	}
+	for _, inputOutput := range inputOutputs {
+		input := inputOutput[0]
+		expected := inputOutput[1]
+		if actual := getTMURLHost(input); expected != actual {
+			t.Errorf("getTMHostURL expected: '%+v', actual: '%+v'", expected, actual)
+		}
+	}
+}


 

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


With regards,
Apache Git Services