From 4ecff52bdd1e4e9fee11a45627e229d962b715b1 Mon Sep 17 00:00:00 2001 From: Vamsi Kalapala Date: Tue, 30 Nov 2021 20:52:33 -0800 Subject: [PATCH 1/2] fix: [CNS] fixing a regex to extract hostip in getncversion req --- cns/restserver/api.go | 2 +- cns/restserver/api_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index c5f820c95c..85c57a3eab 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -1082,7 +1082,7 @@ func getAuthTokenFromCreateNetworkContainerURL( return strings.Split(strings.Split(createNetworkContainerURL, "authenticationToken/")[1], "/")[0] } -var rgx = regexp.MustCompile("^http[s]?://(.*?)/joinedVirtualNetworks.*?$") +var rgx = regexp.MustCompile("^http[s]?://(.*?)/machine/plugins/.*?$") func extractHostFromJoinNetworkURL(url string) string { submatches := rgx.FindStringSubmatch(url) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 3c61b1fe53..1315c80ed5 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -562,13 +562,21 @@ func publishNCViaCNS(t *testing.T, } func TestExtractHost(t *testing.T) { - joinURL := "http://127.0.0.1:9001/joinedVirtualNetworks/c9b8e695-2de1-11eb-bf54-000d3af666c8/api-version/1" + joinURL := "http://127.0.0.1:9001/machine/plugins/?comp=nmagent&type=NetworkManagement/joinedVirtualNetworks/c9b8e695-2de1-11eb-bf54-000d3af666c8/api-version/1" host := extractHostFromJoinNetworkURL(joinURL) expected := "127.0.0.1:9001" if host != expected { t.Fatalf("expected host %q, got %q", expected, host) } + + joinURL = "http://168.63.129.16/machine/plugins/?comp=nmagent&type=NetworkManagement/joinedVirtualNetworks/4941a21f-1a8d-4d0f-8256-cc6e73a8cd22/api-version/1" + + host = extractHostFromJoinNetworkURL(joinURL) + expected = "168.63.129.16" + if host != expected { + t.Fatalf("expected host %q, got %q", expected, host) + } } func TestUnpublishNCViaCNS(t *testing.T) { From 5fadf98441e0baa10924f9c8f44f65de843a3c80 Mon Sep 17 00:00:00 2001 From: Vamsi Kalapala Date: Wed, 1 Dec 2021 13:46:13 -0800 Subject: [PATCH 2/2] taking advice and adding net/url package to extract hosT --- cns/restserver/api.go | 12 +++++------- cns/restserver/api_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cns/restserver/api.go b/cns/restserver/api.go index 85c57a3eab..ecbc3efa7e 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -9,7 +9,7 @@ import ( "io" "net" "net/http" - "regexp" + "net/url" "runtime" "strings" @@ -1082,14 +1082,12 @@ func getAuthTokenFromCreateNetworkContainerURL( return strings.Split(strings.Split(createNetworkContainerURL, "authenticationToken/")[1], "/")[0] } -var rgx = regexp.MustCompile("^http[s]?://(.*?)/machine/plugins/.*?$") - -func extractHostFromJoinNetworkURL(url string) string { - submatches := rgx.FindStringSubmatch(url) - if len(submatches) != 2 { +func extractHostFromJoinNetworkURL(urlToParse string) string { + parsedURL, err := url.Parse(urlToParse) + if err != nil { return "" } - return submatches[1] + return parsedURL.Host } // Publish Network Container by calling nmagent diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 1315c80ed5..34fc663f89 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -577,6 +577,14 @@ func TestExtractHost(t *testing.T) { if host != expected { t.Fatalf("expected host %q, got %q", expected, host) } + + joinURL = "http://168.63.129.16/joinedVirtualNetworks/4941a21f-1a8d-4d0f-8256-cc6e73a8cd22/api-version/1" + + host = extractHostFromJoinNetworkURL(joinURL) + expected = "168.63.129.16" + if host != expected { + t.Fatalf("expected host %q, got %q", expected, host) + } } func TestUnpublishNCViaCNS(t *testing.T) {