From 24981a6c68e83fe93f3b8fce66cbb11268a4b2ae Mon Sep 17 00:00:00 2001 From: John Cowen Date: Thu, 25 Feb 2021 09:34:47 +0000 Subject: [PATCH] ui: Remove any trailing fullstop/period DNS characters from Gateways UI API (#9752) Previous to this commit, the API response would include Gateway Addresses in the form `domain.name.:8080`, which due to the addition of the port is probably not the expected response. This commit rightTrims any `.` characters from the end of the domain before formatting the address to include the port resulting in `domain.name:8080` --- .changelog/9752.txt | 3 +++ agent/structs/config_entry_gateways.go | 5 ++++- agent/structs/config_entry_gateways_test.go | 6 ++++-- agent/ui_endpoint_test.go | 12 +++++++----- 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 .changelog/9752.txt diff --git a/.changelog/9752.txt b/.changelog/9752.txt new file mode 100644 index 0000000000..e6d3bff9a4 --- /dev/null +++ b/.changelog/9752.txt @@ -0,0 +1,3 @@ +```release-note:bug +api: Remove trailing periods from the gateway internal HTTP API endpoint +``` diff --git a/agent/structs/config_entry_gateways.go b/agent/structs/config_entry_gateways.go index 61b9930823..b87e29436e 100644 --- a/agent/structs/config_entry_gateways.go +++ b/agent/structs/config_entry_gateways.go @@ -453,8 +453,11 @@ func (g *GatewayService) Addresses(defaultHosts []string) []string { } var addresses []string + // loop through the hosts and format that into domain.name:port format, + // ensuring we trim any trailing DNS . characters from the domain name as we + // go for _, h := range hosts { - addresses = append(addresses, fmt.Sprintf("%s:%d", h, g.Port)) + addresses = append(addresses, fmt.Sprintf("%s:%d", strings.TrimRight(h, "."), g.Port)) } return addresses } diff --git a/agent/structs/config_entry_gateways_test.go b/agent/structs/config_entry_gateways_test.go index 3a71dba6d2..acec2d2266 100644 --- a/agent/structs/config_entry_gateways_test.go +++ b/agent/structs/config_entry_gateways_test.go @@ -663,23 +663,25 @@ func TestGatewayService_Addresses(t *testing.T) { argument: []string{ "service.ingress.dc.domain", "service.ingress.dc.alt.domain", + "service.ingress.dc.alt.domain.", }, expected: []string{ "service.ingress.dc.domain:8080", "service.ingress.dc.alt.domain:8080", + "service.ingress.dc.alt.domain:8080", }, }, { name: "user-defined hosts", input: GatewayService{ Port: 8080, - Hosts: []string{"*.test.example.com", "other.example.com"}, + Hosts: []string{"*.test.example.com", "other.example.com", "other.example.com."}, }, argument: []string{ "service.ingress.dc.domain", "service.ingress.alt.domain", }, - expected: []string{"*.test.example.com:8080", "other.example.com:8080"}, + expected: []string{"*.test.example.com:8080", "other.example.com:8080", "other.example.com:8080"}, }, } diff --git a/agent/ui_endpoint_test.go b/agent/ui_endpoint_test.go index e1aff4281a..3cb002e502 100644 --- a/agent/ui_endpoint_test.go +++ b/agent/ui_endpoint_test.go @@ -656,11 +656,13 @@ func TestUIGatewayServiceNodes_Ingress(t *testing.T) { assert.Nil(t, err) assertIndex(t, resp) - // Construct expected addresses so that differences between OSS/Ent are handled by code - webDNS := serviceIngressDNSName("web", "dc1", "consul.", structs.DefaultEnterpriseMeta()) - webDNSAlt := serviceIngressDNSName("web", "dc1", "alt.consul.", structs.DefaultEnterpriseMeta()) - dbDNS := serviceIngressDNSName("db", "dc1", "consul.", structs.DefaultEnterpriseMeta()) - dbDNSAlt := serviceIngressDNSName("db", "dc1", "alt.consul.", structs.DefaultEnterpriseMeta()) + // Construct expected addresses so that differences between OSS/Ent are + // handled by code. We specifically don't include the trailing DNS . here as + // we are constructing what we are expecting, not the actual value + webDNS := serviceIngressDNSName("web", "dc1", "consul", structs.DefaultEnterpriseMeta()) + webDNSAlt := serviceIngressDNSName("web", "dc1", "alt.consul", structs.DefaultEnterpriseMeta()) + dbDNS := serviceIngressDNSName("db", "dc1", "consul", structs.DefaultEnterpriseMeta()) + dbDNSAlt := serviceIngressDNSName("db", "dc1", "alt.consul", structs.DefaultEnterpriseMeta()) dump := obj.([]*ServiceSummary) expect := []*ServiceSummary{