Skip to content

Commit ccbb343

Browse files
committed
RFC: Moves srcPortAcl formatting to template
1 parent 5b8ee57 commit ccbb343

4 files changed

Lines changed: 42 additions & 22 deletions

File tree

proxy/ha_proxy_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,14 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEnd() {
747747
tmpl := s.TemplateContent
748748
expectedData := fmt.Sprintf(
749749
`%s
750-
acl url_my-service-11111_0 path_beg /path-1 path_beg /path-2 port1111Acl
751-
acl url_my-service-12222_1 path_beg /path-3 port2222Acl
750+
acl url_my-service-11111_0 path_beg /path-1 path_beg /path-2
751+
acl port1111Acl
752+
acl url_my-service-12222_1 path_beg /path-3
753+
acl port2222Acl
752754
use_backend my-service-1-be1111_0 if url_my-service-11111_0 my-src-port
753755
use_backend my-service-1-be2222_1 if url_my-service-12222_1
754-
acl url_my-service-23333_0 path_beg /path-4 port3333Acl
756+
acl url_my-service-23333_0 path_beg /path-4
757+
acl port3333Acl
755758
use_backend my-service-2-be3333_0 if url_my-service-23333_0%s`,
756759
tmpl,
757760
s.ServicesContent,
@@ -765,15 +768,15 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEnd() {
765768
ServiceName: "my-service-1",
766769
PathType: "path_beg",
767770
ServiceDest: []ServiceDest{
768-
{Port: "1111", ServicePath: []string{"/path-1", "/path-2"}, SrcPortAcl: " port1111Acl", SrcPortAclName: " my-src-port", Index: 0},
769-
{Port: "2222", ServicePath: []string{"/path-3"}, SrcPortAcl: " port2222Acl", Index: 1},
771+
{Port: "1111", ServicePath: []string{"/path-1", "/path-2"}, SrcPortAcl: "acl port1111Acl", SrcPortAclName: " my-src-port", IncludeSrcPortACL: true, Index: 0},
772+
{Port: "2222", ServicePath: []string{"/path-3"}, SrcPortAcl: "acl port2222Acl", Index: 1, IncludeSrcPortACL: true},
770773
},
771774
}
772775
service2 := Service{
773776
ServiceName: "my-service-2",
774777
PathType: "path_beg",
775778
ServiceDest: []ServiceDest{
776-
{Port: "3333", ServicePath: []string{"/path-4"}, SrcPortAcl: " port3333Acl", Index: 0},
779+
{Port: "3333", ServicePath: []string{"/path-4"}, SrcPortAcl: "acl port3333Acl", Index: 0, IncludeSrcPortACL: true},
777780
},
778781
}
779782
p.AddService(service1)
@@ -791,9 +794,9 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsServicePathExclude(
791794
`%s
792795
acl url_my-service-11111_0 path_beg /path-1
793796
acl url_exclude_my-service-11111_0 path_beg /path-2 path_beg /path-3
794-
acl http_my-service-1_0 dst_port 80
797+
acl srcPort_my-service-180_0 dst_port 80
795798
acl https_my-service-1_0 dst_port 443
796-
use_backend my-service-1-be1111_0 if url_my-service-11111_0 !url_exclude_my-service-11111_0 http_my-service-1_0
799+
use_backend my-service-1-be1111_0 if url_my-service-11111_0 !url_exclude_my-service-11111_0 srcPort_my-service-180_0
797800
use_backend https-my-service-1-be1111_0 if url_my-service-11111_0 !url_exclude_my-service-11111_0 https_my-service-1_0%s`,
798801
tmpl,
799802
s.ServicesContent,
@@ -812,6 +815,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsServicePathExclude(
812815
}
813816
p.AddService(service)
814817

818+
FormatServiceForTemplates(&service)
815819
p.CreateConfigFromTemplates()
816820

817821
s.Equal(expectedData, actualData)
@@ -1832,9 +1836,9 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
18321836
expectedData := fmt.Sprintf(
18331837
`%s
18341838
acl url_my-service1111_0 path_beg /path
1835-
acl http_my-service_0 dst_port 80
1839+
acl srcPort_my-service80_0 dst_port 80
18361840
acl https_my-service_0 dst_port 443
1837-
use_backend my-service-be1111_0 if url_my-service1111_0 http_my-service_0
1841+
use_backend my-service-be1111_0 if url_my-service1111_0 srcPort_my-service80_0
18381842
use_backend https-my-service-be1111_0 if url_my-service1111_0 https_my-service_0%s`,
18391843
tmpl,
18401844
s.ServicesContent,
@@ -1854,6 +1858,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
18541858
}
18551859
p.AddService(service1)
18561860

1861+
FormatServiceForTemplates(&service1)
18571862
p.CreateConfigFromTemplates()
18581863

18591864
s.Equal(expectedData, actualData)
@@ -1866,9 +1871,8 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
18661871
`%s
18671872
acl url_my-service1111_0 path_beg /path
18681873
acl srcPort_my-service8080_0 dst_port 8080
1869-
acl http_my-service_0 dst_port 8080
18701874
acl https_my-service_0 dst_port 443
1871-
use_backend my-service-be1111_0 if url_my-service1111_0 srcPort_my-service8080_0 http_my-service_0
1875+
use_backend my-service-be1111_0 if url_my-service1111_0 srcPort_my-service8080_0
18721876
use_backend https-my-service-be1111_0 if url_my-service1111_0 https_my-service_0%s`,
18731877
tmpl,
18741878
s.ServicesContent,

proxy/template.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ func getFrontTemplate(s Service) string {
2020
{{- end}}
2121
{{- end}}
2222
{{- if ne .Port ""}}
23-
acl url_{{$.AclName}}{{.Port}}_{{.Index}}{{range .ServicePath}} {{if eq $.PathType ""}}path_beg{{end}}{{if ne $.PathType ""}}{{$.PathType}}{{end}} {{.}}{{end}}{{.SrcPortAcl}}
23+
acl url_{{$.AclName}}{{.Port}}_{{.Index}}{{range .ServicePath}} {{if eq $.PathType ""}}path_beg{{end}}{{if ne $.PathType ""}}{{$.PathType}}{{end}} {{.}}{{end}}
2424
{{- end}}
2525
{{- if .ServicePathExclude}}
26-
acl url_exclude_{{$.AclName}}{{.Port}}_{{.Index}}{{range .ServicePathExclude}} {{if eq $.PathType ""}}path_beg{{end}}{{if ne $.PathType ""}}{{$.PathType}}{{end}} {{.}}{{end}}{{.SrcPortAcl}}
26+
acl url_exclude_{{$.AclName}}{{.Port}}_{{.Index}}{{range .ServicePathExclude}} {{if eq $.PathType ""}}path_beg{{end}}{{if ne $.PathType ""}}{{$.PathType}}{{end}} {{.}}{{end}}
2727
{{- end}}
28+
{{- if $sd.IncludeSrcPortACL }}
29+
{{$sd.SrcPortAcl}}
30+
{{- end }}
2831
{{- $length := len .UserAgent.Value}}{{if gt $length 0}}
2932
acl user_agent_{{$.AclName}}_{{.UserAgent.AclName}}_{{.Index}} hdr_sub(User-Agent) -i{{range .UserAgent.Value}} {{.}}{{end}}
3033
{{- end}}
@@ -38,7 +41,6 @@ func getFrontTemplate(s Service) string {
3841
{{- end}}
3942
{{- end}}
4043
{{- if gt $sd.HttpsPort 0 }}
41-
acl http_{{$.ServiceName}}_{{.Index}} dst_port {{ if gt .SrcPort 0 }}{{.SrcPort}}{{ else }}80{{ end }}
4244
acl https_{{$.ServiceName}}_{{.Index}} dst_port 443
4345
{{- end}}
4446
{{- range $rd := $sd.RedirectFromDomain}}
@@ -58,7 +60,7 @@ func getFrontTemplate(s Service) string {
5860
{{- range $sd := .ServiceDest}}
5961
{{- if eq .ReqMode "http"}}{{- if ne .Port ""}}
6062
use_backend {{$.AclName}}-be{{.Port}}_{{.Index}} if url_{{$.AclName}}{{.Port}}_{{.Index}}{{if .ServicePathExclude}} !url_exclude_{{$.AclName}}{{.Port}}_{{.Index}}{{end}}{{if .ServiceDomain}} domain_{{$.AclName}}{{.Port}}_{{.Index}}{{end}}{{if .ServiceHeader}}{{resetIndex}}{{range $key, $value := .ServiceHeader}} hdr_{{$.AclName}}{{$sd.Port}}_{{incIndex}}{{end}}{{end}}{{.SrcPortAclName}}
61-
{{- if gt $sd.HttpsPort 0 }} http_{{$.ServiceName}}_{{.Index}}
63+
{{- if gt $sd.HttpsPort 0 }}
6264
use_backend https-{{$.AclName}}-be{{.Port}}_{{.Index}} if url_{{$.AclName}}{{.Port}}_{{.Index}}{{if .ServicePathExclude}} !url_exclude_{{$.AclName}}{{.Port}}_{{.Index}}{{end}}{{if .ServiceDomain}} domain_{{$.AclName}}{{.Port}}_{{.Index}}{{end}} https_{{$.ServiceName}}_{{.Index}}
6365
{{- end}}
6466
{{- $length := len .UserAgent.Value}}{{if gt $length 0}} user_agent_{{$.AclName}}_{{.UserAgent.AclName}}_{{.Index}}{{end}}
@@ -135,7 +137,10 @@ frontend service_{{$sd1.SrcPort}}
135137
tcp-request content accept if { req_ssl_hello_type 1 }`, si)
136138
}
137139
tmplString += fmt.Sprintf(`{{$sd := index $.ServiceDest %d}}
138-
acl sni_{{.AclName}}{{$sd.Port}}-%d{{range $sd.ServicePath}} {{$.PathType}} {{.}}{{end}}{{$sd.SrcPortAcl}}
140+
acl sni_{{.AclName}}{{$sd.Port}}-%d{{range $sd.ServicePath}} {{$.PathType}} {{.}}{{end}}
141+
{{- if ne $sd.SrcPortAcl "" }}
142+
{{$sd.SrcPortAcl}}
143+
{{- end }}
139144
use_backend {{$.ServiceName}}-be{{$sd.Port}}_{{$sd.Index}} if sni_{{$.AclName}}{{$sd.Port}}-%d{{$.AclCondition}}{{$sd.SrcPortAclName}}`, si, si+1, si+1)
140145
return templateToString(tmplString, s)
141146
}
@@ -382,11 +387,20 @@ func FormatServiceForTemplates(sr *Service) {
382387
sr.ServiceDest[i].ReqMode = "http"
383388
}
384389

385-
if sd.SrcPort > 0 {
386-
sr.ServiceDest[i].SrcPortAclName = fmt.Sprintf(" srcPort_%s%d_%d", sr.AclName, sd.SrcPort, sd.Index)
387-
sr.ServiceDest[i].SrcPortAcl = fmt.Sprintf("\n acl srcPort_%s%d_%d dst_port %d",
388-
sr.AclName, sd.SrcPort, sd.Index, sd.SrcPort)
390+
srcPort := sd.SrcPort
391+
if sd.HttpsPort > 0 && srcPort == 0 {
392+
srcPort = 80
389393
}
394+
if srcPort > 0 {
395+
sr.ServiceDest[i].SrcPortAclName = fmt.Sprintf(" srcPort_%s%d_%d", sr.AclName, srcPort, sd.Index)
396+
sr.ServiceDest[i].SrcPortAcl = fmt.Sprintf("acl srcPort_%s%d_%d dst_port %d",
397+
sr.AclName, srcPort, sd.Index, srcPort)
398+
}
399+
if srcPort > 0 && (len(sd.Port) > 0 || len(sd.ServicePathExclude) > 0) {
400+
sr.ServiceDest[i].IncludeSrcPortACL = true
401+
}
402+
sr.ServiceDest[i].SrcPort = srcPort
403+
390404
}
391405
}
392406

proxy/template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ func (s *TemplateTestSuite) Test_FormatData_SrcPort_DefinesSrcPortAclNameAndSrcP
7070
sd := service.ServiceDest[0]
7171

7272
s.Equal(" srcPort_my-service-14480_0", sd.SrcPortAclName)
73-
s.Equal("\n acl srcPort_my-service-14480_0 dst_port 4480", sd.SrcPortAcl)
73+
s.Equal("acl srcPort_my-service-14480_0 dst_port 4480", sd.SrcPortAcl)
7474

7575
}

proxy/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type ServiceDest struct {
8989
Index int
9090
// Internal use only
9191
ReqPathSearchReplaceFormatted []string
92+
// Internal use only
93+
IncludeSrcPortACL bool
9294
}
9395

9496
// UserAgent holds data used to generate proxy configuration. It is extracted as a separate struct since each user agent needs an ACL identifier. If specified, only requests with the same agent will be forwarded to the backend.

0 commit comments

Comments
 (0)