Skip to content

Commit 2eed4d4

Browse files
committed
ENH: Adds srcHttpsPort
1 parent ccbb343 commit 2eed4d4

7 files changed

Lines changed: 101 additions & 11 deletions

File tree

docs/usage.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The following query parameters can be used to send a *reconfigure* request to *D
3636
|setReqHeader |Additional headers that will be set to the request before forwarding it to the service. If a specified header exists, it will be replaced with the new one. Multiple headers should be separated with comma (`,`). Change the environment variable `SEPARATOR` if comma is to be used for other purposes. Please consult [Set a header to the request](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#set-a-header-in-the-request) for more info.<br>**Example:** `X-Forwarded-Port %[dst_port],X-Forwarded-Ssl on if { ssl_fc }`|
3737
|setResHeader |Additional headers that will be set to the response before forwarding it to the client. If a specified header exists, it will be replaced with the new one. Multiple headers should be separated with comma (`,`). Change the environment variable `SEPARATOR` if comma is to be used for other purposes. Please consult [Set a header to the response](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#set-a-header-in-the-response) for more info.<br>**Example:** `X-Via %[env(HOSTNAME)],Server haproxy`|
3838
|srcPort |The source (entry) port of a service. The parameter can be prefixed with an index thus allowing definition of multiple destinations for a single service (e.g. `srcPort.1`, `srcPort.2`, and so on). The parameter is mandatory when specifying multiple destinations of a single service. If this parameter is used with `http` mode, the port needs to be specified with the environment variable `BIND_PORTS` (see [Environment Variables](http://proxy.dockerflow.com/config/#environment-variables) for more info) and the port needs to be published on service level.<br>**Example:** `80`|
39+
|srcHttpsPort |The source (entry) port of a https service. The parameter can be prefixed with an index thus allowing definition of multiple destinations for a single service (e.g. `srcHttpsPort.1`, `srcHttpsPort.2`, and so on). The parameter is mandatory when specifying multiple destinations of a single service. The ports needs to be specified with the environment variable `BIND_PORTS` (see [Environment Variables](http://proxy.dockerflow.com/config/#environment-variables) for more info) and the port needs to be published on service level.<br>**Example:** `4443`|
3940
|timeoutServer |The server timeout in seconds.<br>**Default:** `20`<br>**Example:** `60`|
4041
|timeoutTunnel |The tunnel timeout in seconds.<br>**Default:** `3600`<br>**Example:** `3600`|
4142
|userDef |User defined value. This value is not used with current template. It is designed as a way to provide additional data that can be used with **custom templates**. The parameter must be prefixed with an index thus allowing definition of multiple destinations for a single service (e.g. `userDef.1`, `userDef.2`, and so on).|
@@ -152,6 +153,7 @@ The map between the HTTP query parameters and environment variables is as follow
152153
|setReqHeader |SET_REQ_HEADER |
153154
|setResHeader |SET_RES_HEADER |
154155
|srcPort |SRC_PORT |
156+
|srcHttpsPort |SRC_HTTPS_PORT |
155157
|sslVerifyNone |SSL_VERIFY_NONE |
156158
|templateBePath |TEMPLATE_BE_PATH |
157159
|templateFePath |TEMPLATE_FE_PATH |

proxy/ha_proxy_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,9 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsServicePathExclude(
795795
acl url_my-service-11111_0 path_beg /path-1
796796
acl url_exclude_my-service-11111_0 path_beg /path-2 path_beg /path-3
797797
acl srcPort_my-service-180_0 dst_port 80
798-
acl https_my-service-1_0 dst_port 443
798+
acl srcHttpsPort_my-service-1443_0 dst_port 443
799799
use_backend my-service-1-be1111_0 if url_my-service-11111_0 !url_exclude_my-service-11111_0 srcPort_my-service-180_0
800-
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`,
800+
use_backend https-my-service-1-be1111_0 if url_my-service-11111_0 !url_exclude_my-service-11111_0 srcHttpsPort_my-service-1443_0%s`,
801801
tmpl,
802802
s.ServicesContent,
803803
)
@@ -1837,9 +1837,9 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
18371837
`%s
18381838
acl url_my-service1111_0 path_beg /path
18391839
acl srcPort_my-service80_0 dst_port 80
1840-
acl https_my-service_0 dst_port 443
1840+
acl srcHttpsPort_my-service443_0 dst_port 443
18411841
use_backend my-service-be1111_0 if url_my-service1111_0 srcPort_my-service80_0
1842-
use_backend https-my-service-be1111_0 if url_my-service1111_0 https_my-service_0%s`,
1842+
use_backend https-my-service-be1111_0 if url_my-service1111_0 srcHttpsPort_my-service443_0%s`,
18431843
tmpl,
18441844
s.ServicesContent,
18451845
)
@@ -1871,9 +1871,9 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
18711871
`%s
18721872
acl url_my-service1111_0 path_beg /path
18731873
acl srcPort_my-service8080_0 dst_port 8080
1874-
acl https_my-service_0 dst_port 443
1874+
acl srcHttpsPort_my-service443_0 dst_port 443
18751875
use_backend my-service-be1111_0 if url_my-service1111_0 srcPort_my-service8080_0
1876-
use_backend https-my-service-be1111_0 if url_my-service1111_0 https_my-service_0%s`,
1876+
use_backend https-my-service-be1111_0 if url_my-service1111_0 srcHttpsPort_my-service443_0%s`,
18771877
tmpl,
18781878
s.ServicesContent,
18791879
)

proxy/template.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func getFrontTemplate(s Service) string {
2727
{{- end}}
2828
{{- if $sd.IncludeSrcPortACL }}
2929
{{$sd.SrcPortAcl}}
30+
{{- end }}
31+
{{- if $sd.IncludeSrcHttpsPortACL }}
32+
{{$sd.SrcHttpsPortAcl}}
3033
{{- end }}
3134
{{- $length := len .UserAgent.Value}}{{if gt $length 0}}
3235
acl user_agent_{{$.AclName}}_{{.UserAgent.AclName}}_{{.Index}} hdr_sub(User-Agent) -i{{range .UserAgent.Value}} {{.}}{{end}}
@@ -40,9 +43,6 @@ func getFrontTemplate(s Service) string {
4043
acl hdr_{{$.AclName}}{{$sd.Port}}_{{incIndex}} hdr({{$key}}) {{$value}}
4144
{{- end}}
4245
{{- end}}
43-
{{- if gt $sd.HttpsPort 0 }}
44-
acl https_{{$.ServiceName}}_{{.Index}} dst_port 443
45-
{{- end}}
4646
{{- range $rd := $sd.RedirectFromDomain}}
4747
http-request redirect code 301 prefix http://{{index $sd.ServiceDomain 0}} if { hdr_beg(host) -i {{$rd}} }
4848
{{- end}}
@@ -61,7 +61,7 @@ func getFrontTemplate(s Service) string {
6161
{{- if eq .ReqMode "http"}}{{- if ne .Port ""}}
6262
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}}
6363
{{- if gt $sd.HttpsPort 0 }}
64-
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}}
64+
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}}{{.SrcHttpsPortAclName}}
6565
{{- end}}
6666
{{- $length := len .UserAgent.Value}}{{if gt $length 0}} user_agent_{{$.AclName}}_{{.UserAgent.AclName}}_{{.Index}}{{end}}
6767
{{- if $.IsDefaultBackend}}
@@ -401,6 +401,21 @@ func FormatServiceForTemplates(sr *Service) {
401401
}
402402
sr.ServiceDest[i].SrcPort = srcPort
403403

404+
// Handle https port
405+
srcHttpsPort := sd.SrcHttpsPort
406+
if sd.HttpsPort > 0 && srcHttpsPort == 0 {
407+
srcHttpsPort = 443
408+
}
409+
if srcHttpsPort > 0 {
410+
sr.ServiceDest[i].SrcHttpsPortAclName = fmt.Sprintf(" srcHttpsPort_%s%d_%d", sr.AclName, srcHttpsPort, sd.Index)
411+
sr.ServiceDest[i].SrcHttpsPortAcl = fmt.Sprintf("acl srcHttpsPort_%s%d_%d dst_port %d", sr.AclName, srcHttpsPort, sd.Index, srcHttpsPort)
412+
}
413+
414+
if srcHttpsPort > 0 && sd.HttpsPort > 0 {
415+
sr.ServiceDest[i].IncludeSrcHttpsPortACL = true
416+
}
417+
sr.ServiceDest[i].SrcHttpsPort = srcHttpsPort
418+
404419
}
405420
}
406421

proxy/template_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,47 @@ func (s *TemplateTestSuite) Test_FormatData_SrcPort_DefinesSrcPortAclNameAndSrcP
7171

7272
s.Equal(" srcPort_my-service-14480_0", sd.SrcPortAclName)
7373
s.Equal("acl srcPort_my-service-14480_0 dst_port 4480", sd.SrcPortAcl)
74+
s.True(sd.IncludeSrcPortACL)
75+
}
76+
77+
func (s *TemplateTestSuite) Test_FormatData_SrcHttpsPort_DefinesSrcHttpsPortAclNameAndSrcPortAcl() {
78+
79+
service := Service{
80+
ServiceName: "my-service-1",
81+
ServiceDest: []ServiceDest{
82+
{SrcPort: 4480, Port: "1111",
83+
SrcHttpsPort: 4443,
84+
HttpsPort: 4443,
85+
ServicePath: []string{"/path-1"}}}}
86+
87+
FormatServiceForTemplates(&service)
88+
89+
s.Require().Len(service.ServiceDest, 1)
90+
sd := service.ServiceDest[0]
91+
92+
s.Equal(" srcHttpsPort_my-service-14443_0", sd.SrcHttpsPortAclName)
93+
s.Equal("acl srcHttpsPort_my-service-14443_0 dst_port 4443", sd.SrcHttpsPortAcl)
94+
s.True(sd.IncludeSrcHttpsPortACL)
95+
96+
}
97+
func (s *TemplateTestSuite) Test_FormatData_DefinesSrcPortAclNameAndSrcPortAcl_DefaultsToPort80_443() {
98+
99+
service := Service{
100+
ServiceName: "my-service-1",
101+
ServiceDest: []ServiceDest{
102+
{Port: "1111",
103+
HttpsPort: 443,
104+
ServicePath: []string{"/path-1"}}}}
105+
106+
FormatServiceForTemplates(&service)
107+
108+
s.Require().Len(service.ServiceDest, 1)
109+
sd := service.ServiceDest[0]
74110

111+
s.Equal(" srcPort_my-service-180_0", sd.SrcPortAclName)
112+
s.Equal("acl srcPort_my-service-180_0 dst_port 80", sd.SrcPortAcl)
113+
s.Equal(" srcHttpsPort_my-service-1443_0", sd.SrcHttpsPortAclName)
114+
s.Equal("acl srcHttpsPort_my-service-1443_0 dst_port 443", sd.SrcHttpsPortAcl)
115+
s.True(sd.IncludeSrcPortACL)
116+
s.True(sd.IncludeSrcHttpsPortACL)
75117
}

proxy/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ type ServiceDest struct {
6565
// The source (entry) port of a service.
6666
// Useful only when specifying multiple destinations of a single service.
6767
SrcPort int
68+
// The source (entry) port of a https service.
69+
// Useful only when specifying multiple destinations of a single service.
70+
SrcHttpsPort int
6871
// Internal use only. Do not modify.
6972
SrcPortAcl string
7073
// Internal use only. Do not modify.
7174
SrcPortAclName string
75+
// Internal use only. Do not modify.
76+
SrcHttpsPortAcl string
77+
// Internal use only. Do not modify.
78+
SrcHttpsPortAclName string
7279
// If set to true, server certificates are not verified. This flag should be set for SSL enabled backend services.
7380
SslVerifyNone bool
7481
// The server timeout in seconds
@@ -91,6 +98,8 @@ type ServiceDest struct {
9198
ReqPathSearchReplaceFormatted []string
9299
// Internal use only
93100
IncludeSrcPortACL bool
101+
// Internal use only
102+
IncludeSrcHttpsPortACL bool
94103
}
95104

96105
// 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.
@@ -407,6 +416,7 @@ func getServiceDest(sr *Service, provider ServiceParameterProvider, index int) S
407416
reqMode = "http"
408417
}
409418
srcPort, _ := strconv.Atoi(getFromString(provider, "srcPort", suffix))
419+
srcHttpsPort, _ := strconv.Atoi(getFromString(provider, "srcHttpsPort", suffix))
410420
httpsPort, _ := strconv.Atoi(getFromString(provider, "httpsPort", suffix))
411421
headerString := getFromString(provider, "serviceHeader", suffix)
412422
header := map[string]string{}
@@ -460,6 +470,7 @@ func getServiceDest(sr *Service, provider ServiceParameterProvider, index int) S
460470
ServicePath: getSliceFromString(provider, "servicePath", suffix),
461471
ServicePathExclude: getSliceFromString(provider, "servicePathExclude", suffix),
462472
SrcPort: srcPort,
473+
SrcHttpsPort: srcHttpsPort,
463474
SslVerifyNone: getBoolParam(provider, "sslVerifyNone", suffix),
464475
TimeoutClient: getFromString(provider, "timeoutClient", suffix),
465476
TimeoutServer: getFromString(provider, "timeoutServer", suffix),

server/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
245245
path := getSliceFromString(os.Getenv(prefix + "_SERVICE_PATH"))
246246
port := os.Getenv(prefix + "_PORT")
247247
srcPort, _ := strconv.Atoi(os.Getenv(prefix + "_SRC_PORT"))
248+
srcHttpsPort, _ := strconv.Atoi(os.Getenv(prefix + "_SRC_HTTPS_PORT"))
248249
reqMode := os.Getenv(prefix + "_REQ_MODE")
249250
domain := getSliceFromString(os.Getenv(prefix + "_SERVICE_DOMAIN"))
250251
// TODO: Remove.
@@ -296,6 +297,7 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
296297
ServicePath: path,
297298
ServicePathExclude: servicePathExclude,
298299
SrcPort: srcPort,
300+
SrcHttpsPort: srcHttpsPort,
299301
SslVerifyNone: sslVerifyNone,
300302
TimeoutServer: timeoutServer,
301303
TimeoutTunnel: timeoutTunnel,
@@ -322,6 +324,7 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
322324
reqMode = "http"
323325
}
324326
srcPort, _ := strconv.Atoi(os.Getenv(fmt.Sprintf("%s_SRC_PORT_%d", prefix, i)))
327+
srcHttpsPort, _ := strconv.Atoi(os.Getenv(fmt.Sprintf("%s_SRC_HTTPS_PORT_%d", prefix, i)))
325328
allowedMethods := getSliceFromString(os.Getenv(fmt.Sprintf("%s_ALLOWED_METHODS_%d", prefix, i)))
326329
deniedMethods := getSliceFromString(os.Getenv(fmt.Sprintf("%s_DENIED_METHODS_%d", prefix, i)))
327330
redirectFromDomain := getSliceFromString(os.Getenv(fmt.Sprintf("%s_REDIRECT_FROM_DOMAIN_%d", prefix, i)))
@@ -350,6 +353,7 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
350353
ReqPathSearchReplaceFormatted: reqPathSearchReplaceFormatted,
351354
ServiceDomain: domain,
352355
SrcPort: srcPort,
356+
SrcHttpsPort: srcHttpsPort,
353357
ServicePath: path,
354358
ServicePathExclude: servicePathExclude,
355359
TimeoutServer: timeoutServer,

0 commit comments

Comments
 (0)