From 65e4999838453bebfe75e62056c52d89447e0c9a Mon Sep 17 00:00:00 2001 From: Bram Verschueren Date: Fri, 13 Sep 2024 10:21:57 +0200 Subject: [PATCH] ensure routes with a dot are parsed correctly --- cmd/haproxy/backends.go | 2 +- cmd/haproxy/backends_test.go | 12 +++++++++++- .../router-default-abc123-a1b1c3/haproxy.config | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/haproxy/backends.go b/cmd/haproxy/backends.go index 887d1ee6..439ff3a8 100644 --- a/cmd/haproxy/backends.go +++ b/cmd/haproxy/backends.go @@ -149,7 +149,7 @@ func newBackendFromLine(raw []string) *backend { } func isBackendBlock(line string) *backend { - backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_]*)$` + backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_\.]*)$` re := regexp.MustCompile(backendRe) matches := re.FindStringSubmatch(line) diff --git a/cmd/haproxy/backends_test.go b/cmd/haproxy/backends_test.go index a5f627c9..a5fe6eca 100644 --- a/cmd/haproxy/backends_test.go +++ b/cmd/haproxy/backends_test.go @@ -35,6 +35,7 @@ func TestParseHAProxyConfig(t *testing.T) { includeOpenShiftNamespaces: false, expected: []*backend{ &backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}, + &backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"}, &backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}}, }, { @@ -45,6 +46,7 @@ func TestParseHAProxyConfig(t *testing.T) { expected: []*backend{ &backend{namespace: "openshift-monitoring", routeName: "thanos-querier", ingressController: "default", service: &service{serviceName: "thanos-querier", port: &port{portNr: 9091, portName: "web"}}, termination: "be_secure"}, &backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}, + &backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"}, &backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}}, }, { @@ -52,7 +54,9 @@ func TestParseHAProxyConfig(t *testing.T) { configFile: "../../testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config", wantedNamespace: "testdata", includeOpenShiftNamespaces: true, - expected: []*backend{&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}}, + expected: []*backend{ + &backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}, + &backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"}}, }, } @@ -134,6 +138,12 @@ func TestIsBackendBlock(t *testing.T) { includeOpenShift: true, expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node", ingressController: "", service: (*service)(nil)}, }, + { + name: "return backend from valid backend block including dot in route", + line: "backend be_edge_http:testdata:hello-node.example.com", + includeOpenShift: true, + expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node.example.com", ingressController: "", service: (*service)(nil)}, + }, { name: "return nil from invalid backend block", line: "nonbackend be_edge_http:testdata:hello-node", diff --git a/testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config b/testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config index f4010549..20494b87 100644 --- a/testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config +++ b/testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config @@ -35,6 +35,22 @@ backend be_http:testdata:rails-postgresql-example cookie 94806193aae7eda5cffd44a87b61d794 insert indirect nocache httponly server pod:rails-postgresql-example-1-vq49n:rails-postgresql-example:web:10.129.2.11:8080 10.129.2.11:8080 cookie bece57f8fd3ee9e776b3f1746960e4d5 weight 1 +backend be_http:testdata:app.example.com + mode http + option redispatch + option forwardfor + balance random + + timeout check 5000ms + http-request add-header X-Forwarded-Host %[req.hdr(host)] + http-request add-header X-Forwarded-Port %[dst_port] + http-request add-header X-Forwarded-Proto http if !{ ssl_fc } + http-request add-header X-Forwarded-Proto https if { ssl_fc } + http-request add-header X-Forwarded-Proto-Version h2 if { ssl_fc_alpn -i h2 } + http-request add-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)] + cookie 822b1a92d3efc1a6b2c276f5e2827b3c insert indirect nocache httponly + server pod:hello-node-8dd54cb99-6wsnt:hello-node::10.129.2.132:8080 10.129.2.132:8080 cookie 9d16ea4a111c1b03043d873e9efd86b1 weight 1 + # Plain http backend or backend with TLS terminated at the edge or a # secure backend with re-encryption. backend be_edge_http:other-testdata:hello-node-secure