Skip to content

Commit e3d54e2

Browse files
committed
2 parents dc80ae9 + 3411e78 commit e3d54e2

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

docs/usage.md

+2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ The map between the HTTP query parameters and environment variables is as follow
119119
|compressionAlgo |COMPRESSION_ALGO |
120120
|compressionType |COMPRESSION_TYPE |
121121
|deniedMethods |DENIED_METHODS |
122+
|denyHttp |DENY_HTTP |
122123
|distribute |DISTRIBUTE |
123124
|httpsOnly |HTTPS_ONLY |
124125
|httpsPort |HTTPS_PORT |
126+
|ignoreAuthorization |IGNORE_AUTHORIZATION |
125127
|isDefaultBackend |IS_DEFAULT_BACKEND |
126128
|outboundHostname |OUTBOUND_HOSTNAME |
127129
|pathType |PATH_TYPE |

server/server.go

+45-12
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,11 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
242242
return proxy.Service{}, fmt.Errorf("%s_SERVICE_NAME is not set", prefix)
243243
}
244244
sd := []proxy.ServiceDest{}
245-
path := []string{}
246-
if len(os.Getenv(prefix+"_SERVICE_PATH")) > 0 {
247-
path = strings.Split(os.Getenv(prefix+"_SERVICE_PATH"), ",")
248-
}
245+
path := getSliceFromString(os.Getenv(prefix+"_SERVICE_PATH"))
249246
port := os.Getenv(prefix + "_PORT")
250247
srcPort, _ := strconv.Atoi(os.Getenv(prefix + "_SRC_PORT"))
251248
reqMode := os.Getenv(prefix + "_REQ_MODE")
252-
domain := []string{}
253-
if len(os.Getenv(prefix+"_SERVICE_DOMAIN")) > 0 {
254-
domain = strings.Split(os.Getenv(prefix+"_SERVICE_DOMAIN"), ",")
255-
}
249+
domain := getSliceFromString(os.Getenv(prefix+"_SERVICE_DOMAIN"))
256250
// TODO: Remove.
257251
// It is a temporary workaround to maintain compatibility with the deprecated serviceDomainMatchAll parameter (since July 2017).
258252
if len(s.ServiceDomainAlgo) == 0 && strings.EqualFold(os.Getenv(prefix+"_SERVICE_DOMAIN_MATCH_ALL"), "true") {
@@ -269,27 +263,42 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
269263
if len(reqPathSearchReplace) > 0 {
270264
reqPathSearchReplaceFormatted = strings.Split(reqPathSearchReplace, ":")
271265
}
266+
allowedMethods := getSliceFromString(os.Getenv(prefix + "_ALLOWED_METHODS"))
267+
deniedMethods := getSliceFromString(os.Getenv(prefix + "_DENIED_METHODS"))
268+
redirectFromDomain := getSliceFromString(os.Getenv(prefix + "_REDIRECT_FROM_DOMAIN"))
269+
servicePathExclude := getSliceFromString(os.Getenv(prefix + "_SERVICE_PATH_EXCLUDE"))
270+
verifyClientSsl, _ := strconv.ParseBool(os.Getenv(prefix + "_SSL_VERIFY_NONE"))
271+
denyHttp, _ := strconv.ParseBool(os.Getenv(prefix + "_DENY_HTTP"))
272+
ignoreAuthorization, _ := strconv.ParseBool(os.Getenv(prefix + "_IGNORE_AUTHORIZATION"))
272273

273274
if len(path) > 0 || len(port) > 0 {
274275
sd = append(
275276
sd,
276277
proxy.ServiceDest{
278+
AllowedMethods: allowedMethods,
279+
DeniedMethods: deniedMethods,
280+
DenyHttp: denyHttp,
277281
HttpsOnly: httpsOnly,
278282
HttpsRedirectCode: httpsRedirectCode,
283+
IgnoreAuthorization: ignoreAuthorization,
279284
OutboundHostname: globalOutboundHostname,
280285
Port: port,
286+
RedirectFromDomain: redirectFromDomain,
281287
ReqMode: reqMode,
282288
ReqPathSearchReplace: reqPathSearchReplace,
283289
ReqPathSearchReplaceFormatted: reqPathSearchReplaceFormatted,
284290
ServiceDomain: domain,
285291
ServicePath: path,
292+
ServicePathExclude: servicePathExclude,
286293
SrcPort: srcPort,
294+
VerifyClientSsl: verifyClientSsl,
287295
},
288296
)
289297
}
290298
for i := 1; i <= 10; i++ {
299+
domain := getSliceFromString(os.Getenv(fmt.Sprintf("%s_SERVICE_DOMAIN_%d", prefix, i)))
291300
port := os.Getenv(fmt.Sprintf("%s_PORT_%d", prefix, i))
292-
path := os.Getenv(fmt.Sprintf("%s_SERVICE_PATH_%d", prefix, i))
301+
path := getSliceFromString(os.Getenv(fmt.Sprintf("%s_SERVICE_PATH_%d", prefix, i)))
293302
reqMode := os.Getenv(fmt.Sprintf("%s_REQ_MODE_%d", prefix, i))
294303
reqPathSearchReplace := os.Getenv(fmt.Sprintf("%s_REQ_PATH_SEARCH_REPLACE_%d", prefix, i))
295304
reqPathSearchReplaceFormatted := []string{}
@@ -302,6 +311,13 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
302311
reqMode = "http"
303312
}
304313
srcPort, _ := strconv.Atoi(os.Getenv(fmt.Sprintf("%s_SRC_PORT_%d", prefix, i)))
314+
allowedMethods := getSliceFromString(os.Getenv(fmt.Sprintf("%s_ALLOWED_METHODS_%d", prefix, i)))
315+
deniedMethods := getSliceFromString(os.Getenv(fmt.Sprintf("%s_DENIED_METHODS_%d", prefix, i)))
316+
redirectFromDomain := getSliceFromString(os.Getenv(fmt.Sprintf("%s_REDIRECT_FROM_DOMAIN_%d", prefix, i)))
317+
servicePathExclude := getSliceFromString(os.Getenv(fmt.Sprintf("%s_SERVICE_PATH_EXCLUDE_%d", prefix, i)))
318+
verifyClientSsl, _ := strconv.ParseBool(os.Getenv(fmt.Sprintf("%s_SSL_VERIFY_NONE_%d", prefix, i)))
319+
denyHttp, _ := strconv.ParseBool(os.Getenv(fmt.Sprintf("%s_DENY_HTTP_%d", prefix, i)))
320+
ignoreAuthorization, _ := strconv.ParseBool(os.Getenv(fmt.Sprintf("%s_IGNORE_AUTHORIZATION_%d", prefix, i)))
305321
if len(path) > 0 && len(port) > 0 {
306322
outboundHostname := os.Getenv(fmt.Sprintf("%s_OUTBOUND_HOSTNAME_%d", prefix, i))
307323
if len(outboundHostname) == 0 {
@@ -310,15 +326,23 @@ func (m *serve) getServiceFromEnvVars(prefix string) (proxy.Service, error) {
310326
sd = append(
311327
sd,
312328
proxy.ServiceDest{
329+
AllowedMethods: allowedMethods,
330+
DeniedMethods: deniedMethods,
331+
DenyHttp: denyHttp,
313332
HttpsOnly: httpsOnly,
314333
HttpsRedirectCode: httpsRedirectCode,
334+
IgnoreAuthorization: ignoreAuthorization,
315335
OutboundHostname: outboundHostname,
316336
Port: port,
337+
RedirectFromDomain: redirectFromDomain,
317338
ReqPathSearchReplace: reqPathSearchReplace,
318339
ReqPathSearchReplaceFormatted: reqPathSearchReplaceFormatted,
319-
SrcPort: srcPort,
320-
ServicePath: strings.Split(path, ","),
321-
ReqMode: reqMode,
340+
ServiceDomain: domain,
341+
SrcPort: srcPort,
342+
ServicePath: path,
343+
ServicePathExclude: servicePathExclude,
344+
ReqMode: reqMode,
345+
VerifyClientSsl: verifyClientSsl,
322346
},
323347
)
324348
} else {
@@ -350,3 +374,12 @@ func (m *serve) writeInternalServerError(w http.ResponseWriter, resp *Response,
350374
func (m *serve) hasPort(sd []proxy.ServiceDest) bool {
351375
return len(sd) > 0 && len(sd[0].Port) > 0
352376
}
377+
378+
func getSliceFromString(input string) []string {
379+
separator := os.Getenv("SEPARATOR")
380+
value := []string{}
381+
if len(input) > 0 {
382+
value = strings.Split(input, separator)
383+
}
384+
return value
385+
}

server/server_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -696,31 +696,44 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
696696
ServicePath: []string{"my-path-11", "my-path-12"},
697697
SrcPort: 1112,
698698
ReqMode: "my-ReqMode",
699+
AllowedMethods: []string{"GET", "POST"},
700+
DeniedMethods: []string{"OPTION", "TRACE"},
701+
RedirectFromDomain: []string{"proxy.dockerflow.com", "dockerflow.com"},
702+
ServicePathExclude: []string{"some-path", "some-path2"},
703+
VerifyClientSsl: true,
704+
DenyHttp: true,
705+
IgnoreAuthorization: true,
699706
},
700707
},
701708
}
702709
os.Setenv("DFP_SERVICE_ACL_NAME", service.AclName)
703710
os.Setenv("DFP_SERVICE_ADD_REQ_HEADER", strings.Join(service.AddReqHeader, ","))
704711
os.Setenv("DFP_SERVICE_ADD_RES_HEADER", strings.Join(service.AddResHeader, ","))
712+
os.Setenv("DFP_SERVICE_ALLOWED_METHODS", strings.Join(service.ServiceDest[0].AllowedMethods, ","))
705713
os.Setenv("DFP_SERVICE_COMPRESSION_ALGO", service.CompressionAlgo)
706714
os.Setenv("DFP_SERVICE_COMPRESSION_TYPE", service.CompressionType)
707715
os.Setenv("DFP_SERVICE_CONNECTION_MODE", service.ConnectionMode)
708716
os.Setenv("DFP_SERVICE_DEL_REQ_HEADER", strings.Join(service.DelReqHeader, ","))
709717
os.Setenv("DFP_SERVICE_DEL_RES_HEADER", strings.Join(service.DelResHeader, ","))
718+
os.Setenv("DFP_SERVICE_DENIED_METHODS", strings.Join(service.ServiceDest[0].DeniedMethods, ","))
719+
os.Setenv("DFP_SERVICE_DENY_HTTP", strconv.FormatBool(service.ServiceDest[0].DenyHttp))
710720
os.Setenv("DFP_SERVICE_DISTRIBUTE", strconv.FormatBool(service.Distribute))
711721
os.Setenv("DFP_SERVICE_HTTPS_ONLY", strconv.FormatBool(service.ServiceDest[0].HttpsOnly))
712722
os.Setenv("DFP_SERVICE_HTTPS_REDIRECT_CODE", service.ServiceDest[0].HttpsRedirectCode)
713723
os.Setenv("DFP_SERVICE_HTTPS_PORT", strconv.Itoa(service.HttpsPort))
724+
os.Setenv("DFP_SERVICE_IGNORE_AUTHORIZATION", strconv.FormatBool(service.ServiceDest[0].IgnoreAuthorization))
714725
os.Setenv("DFP_SERVICE_IS_DEFAULT_BACKEND", strconv.FormatBool(service.IsDefaultBackend))
715726
os.Setenv("DFP_SERVICE_OUTBOUND_HOSTNAME", service.ServiceDest[0].OutboundHostname)
716727
os.Setenv("DFP_SERVICE_PATH_TYPE", service.PathType)
728+
os.Setenv("DFP_SERVICE_REDIRECT_FROM_DOMAIN", strings.Join(service.ServiceDest[0].RedirectFromDomain, ","))
717729
os.Setenv("DFP_SERVICE_REDIRECT_WHEN_HTTP_PROTO", strconv.FormatBool(service.RedirectWhenHttpProto))
718730
os.Setenv("DFP_SERVICE_REQ_MODE", service.ServiceDest[0].ReqMode)
719731
os.Setenv("DFP_SERVICE_REQ_PATH_SEARCH_REPLACE", service.ServiceDest[0].ReqPathSearchReplace)
720732
os.Setenv("DFP_SERVICE_SERVICE_CERT", service.ServiceCert)
721733
os.Setenv("DFP_SERVICE_SERVICE_DOMAIN", strings.Join(service.ServiceDest[0].ServiceDomain, ","))
722734
os.Setenv("DFP_SERVICE_SERVICE_DOMAIN_ALGO", service.ServiceDomainAlgo)
723735
os.Setenv("DFP_SERVICE_SERVICE_NAME", service.ServiceName)
736+
os.Setenv("DFP_SERVICE_SERVICE_PATH_EXCLUDE", strings.Join(service.ServiceDest[0].ServicePathExclude, ","))
724737
os.Setenv("DFP_SERVICE_SSL_VERIFY_NONE", strconv.FormatBool(service.SslVerifyNone))
725738
os.Setenv("DFP_SERVICE_TEMPLATE_BE_PATH", service.TemplateBePath)
726739
os.Setenv("DFP_SERVICE_TEMPLATE_FE_PATH", service.TemplateFePath)
@@ -731,24 +744,30 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
731744
os.Setenv("DFP_SERVICE_SET_REQ_HEADER", strings.Join(service.SetReqHeader, ","))
732745
os.Setenv("DFP_SERVICE_SET_RES_HEADER", strings.Join(service.SetResHeader, ","))
733746
os.Setenv("DFP_SERVICE_SRC_PORT", strconv.Itoa(service.ServiceDest[0].SrcPort))
747+
os.Setenv("DFP_SERVICE_SSL_VERIFY_NONE", strconv.FormatBool(service.ServiceDest[0].VerifyClientSsl))
734748

735749
defer func() {
736750
os.Unsetenv("DFP_SERVICE_ACL_NAME")
737751
os.Unsetenv("DFP_SERVICE_ADD_REQ_HEADER")
738752
os.Unsetenv("DFP_SERVICE_ADD_RES_HEADER")
753+
os.Unsetenv("DFP_SERVICE_ALLOWED_METHODS")
739754
os.Unsetenv("DFP_SERVICE_COMPRESSION_ALGO")
740755
os.Unsetenv("DFP_SERVICE_COMPRESSION_TYPE")
741756
os.Unsetenv("DFP_SERVICE_CONNECTION_MODE")
742757
os.Unsetenv("DFP_SERVICE_DEL_REQ_HEADER")
743758
os.Unsetenv("DFP_SERVICE_DEL_RES_HEADER")
759+
os.Unsetenv("DFP_SERVICE_DENIED_METHODS")
760+
os.Unsetenv("DFP_SERVICE_DENY_HTTP")
744761
os.Unsetenv("DFP_SERVICE_DISTRIBUTE")
745762
os.Unsetenv("DFP_SERVICE_HTTPS_ONLY")
746763
os.Unsetenv("DFP_SERVICE_HTTPS_PORT")
747764
os.Unsetenv("DFP_SERVICE_HTTPS_REDIRECT_CODE")
765+
os.Unsetenv("DFP_SERVICE_IGNORE_AUTHORIZATION")
748766
os.Unsetenv("DFP_SERVICE_IS_DEFAULT_BACKEND")
749767
os.Unsetenv("DFP_SERVICE_OUTBOUND_HOSTNAME")
750768
os.Unsetenv("DFP_SERVICE_PATH_TYPE")
751769
os.Unsetenv("DFP_SERVICE_PORT")
770+
os.Unsetenv("DFP_SERVICE_REDIRECT_FROM_DOMAIN")
752771
os.Unsetenv("DFP_SERVICE_REDIRECT_WHEN_HTTP_PROTO")
753772
os.Unsetenv("DFP_SERVICE_REQ_MODE")
754773
os.Unsetenv("DFP_SERVICE_REQ_PATH_SEARCH_REPLACE")
@@ -757,10 +776,12 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
757776
os.Unsetenv("DFP_SERVICE_SERVICE_DOMAIN_ALGO")
758777
os.Unsetenv("DFP_SERVICE_SERVICE_NAME")
759778
os.Unsetenv("DFP_SERVICE_SERVICE_PATH")
779+
os.Unsetenv("DFP_SERVICE_SERVICE_PATH_EXCLUDE")
760780
os.Unsetenv("DFP_SERVICE_SET_REQ_HEADER")
761781
os.Unsetenv("DFP_SERVICE_SET_RES_HEADER")
762782
os.Unsetenv("DFP_SERVICE_SRC_PORT")
763783
os.Unsetenv("DFP_SERVICE_SSL_VERIFY_NONE")
784+
os.Unsetenv("DFP_SERVICE_SSL_VERIFY_NONE")
764785
os.Unsetenv("DFP_SERVICE_TEMPLATE_BE_PATH")
765786
os.Unsetenv("DFP_SERVICE_TEMPLATE_FE_PATH")
766787
os.Unsetenv("DFP_SERVICE_TIMEOUT_SERVER")
@@ -784,6 +805,10 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_SetsServiceDomainAlgoToHdr
784805
ReqPathSearchReplaceFormatted: []string{},
785806
ServiceDomain: []string{"my-domain-1.com", "my-domain-2.com"},
786807
ServicePath: []string{"my-path-11", "my-path-12"},
808+
AllowedMethods: []string{},
809+
DeniedMethods: []string{},
810+
RedirectFromDomain: []string{},
811+
ServicePathExclude: []string{},
787812
},
788813
},
789814
ServiceDomainAlgo: "hdr_dom(host)",
@@ -815,16 +840,29 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServicesWithIndexed
815840
ReqPathSearchReplace: "/this,/that",
816841
ReqPathSearchReplaceFormatted: []string{"/this,/that"},
817842
ServicePath: []string{"my-path-11", "my-path-12"},
843+
ServiceDomain: []string{"some-domain.com", "some-domain2.com"},
844+
AllowedMethods: []string{},
845+
DeniedMethods: []string{},
846+
RedirectFromDomain: []string{},
847+
ServicePathExclude: []string{},
818848
SrcPort: 1112,
819849
HttpsOnly: true,
820850
}, {
821851
Port: "2221",
822852
ReqPathSearchReplace: "/something,/else",
823853
ReqPathSearchReplaceFormatted: []string{"/something,/else"},
824854
ServicePath: []string{"my-path-21", "my-path-22"},
855+
ServiceDomain: []string{},
825856
SrcPort: 2222,
826857
HttpsOnly: false,
827858
OutboundHostname: "my-outbound-domain.com",
859+
AllowedMethods: []string{"GET", "POST"},
860+
DeniedMethods: []string{"OPTION", "TRACE"},
861+
RedirectFromDomain: []string{"proxy.dockerflow.com", "dockerflow.com"},
862+
ServicePathExclude: []string{"some-path", "some-path2"},
863+
VerifyClientSsl: true,
864+
DenyHttp: true,
865+
IgnoreAuthorization: true,
828866
},
829867
},
830868
}
@@ -833,13 +871,21 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServicesWithIndexed
833871
os.Setenv("DFP_SERVICE_PORT_1", expected.ServiceDest[0].Port)
834872
os.Setenv("DFP_SERVICE_REQ_PATH_SEARCH_REPLACE_1", expected.ServiceDest[0].ReqPathSearchReplace)
835873
os.Setenv("DFP_SERVICE_SERVICE_PATH_1", strings.Join(expected.ServiceDest[0].ServicePath, ","))
874+
os.Setenv("DFP_SERVICE_SERVICE_DOMAIN_1", strings.Join(expected.ServiceDest[0].ServiceDomain, ","))
836875
os.Setenv("DFP_SERVICE_SRC_PORT_1", strconv.Itoa(expected.ServiceDest[0].SrcPort))
837876
os.Setenv("DFP_SERVICE_HTTPS_ONLY_2", "false")
838877
os.Setenv("DFP_SERVICE_PORT_2", expected.ServiceDest[1].Port)
839878
os.Setenv("DFP_SERVICE_REQ_PATH_SEARCH_REPLACE_2", expected.ServiceDest[1].ReqPathSearchReplace)
840879
os.Setenv("DFP_SERVICE_SERVICE_PATH_2", strings.Join(expected.ServiceDest[1].ServicePath, ","))
841880
os.Setenv("DFP_SERVICE_SRC_PORT_2", strconv.Itoa(expected.ServiceDest[1].SrcPort))
842881
os.Setenv("DFP_SERVICE_OUTBOUND_HOSTNAME_2", expected.ServiceDest[1].OutboundHostname)
882+
os.Setenv("DFP_SERVICE_ALLOWED_METHODS_2", strings.Join(expected.ServiceDest[1].AllowedMethods, ","))
883+
os.Setenv("DFP_SERVICE_DENIED_METHODS_2", strings.Join(expected.ServiceDest[1].DeniedMethods, ","))
884+
os.Setenv("DFP_SERVICE_REDIRECT_FROM_DOMAIN_2", strings.Join(expected.ServiceDest[1].RedirectFromDomain, ","))
885+
os.Setenv("DFP_SERVICE_SERVICE_PATH_EXCLUDE_2", strings.Join(expected.ServiceDest[1].ServicePathExclude, ","))
886+
os.Setenv("DFP_SERVICE_SSL_VERIFY_NONE_2", strconv.FormatBool(expected.ServiceDest[1].VerifyClientSsl))
887+
os.Setenv("DFP_SERVICE_DENY_HTTP_2", strconv.FormatBool(expected.ServiceDest[1].DenyHttp))
888+
os.Setenv("DFP_SERVICE_IGNORE_AUTHORIZATION_2", strconv.FormatBool(expected.ServiceDest[1].IgnoreAuthorization))
843889

844890
defer func() {
845891
os.Unsetenv("DFP_SERVICE_SERVICE_NAME")
@@ -854,6 +900,14 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServicesWithIndexed
854900
os.Unsetenv("DFP_SERVICE_SERVICE_PATH_2")
855901
os.Unsetenv("DFP_SERVICE_SRC_PORT_2")
856902
os.Unsetenv("DFP_SERVICE_OUTBOUND_HOSTNAME_2")
903+
os.Unsetenv("DFP_SERVICE_ALLOWED_METHODS_2")
904+
os.Unsetenv("DFP_SERVICE_DENIED_METHODS_2")
905+
os.Unsetenv("DFP_SERVICE_REDIRECT_FROM_DOMAIN_2")
906+
os.Unsetenv("DFP_SERVICE_SERVICE_PATH_EXCLUDE_2")
907+
os.Unsetenv("DFP_SERVICE_SERVICE_DOMAIN_2")
908+
os.Unsetenv("DFP_SERVICE_VERIFY_CLIENT_SSL_2")
909+
os.Unsetenv("DFP_SERVICE_DENY_HTTP_2")
910+
os.Unsetenv("DFP_SERVICE_IGNORE_AUTHORIZATION_2")
857911
}()
858912
srv := serve{}
859913
actual := srv.GetServicesFromEnvVars()
@@ -882,6 +936,10 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsMultipleServices()
882936
ServicePath: []string{"my-path-11", "my-path-12"},
883937
SrcPort: 1112,
884938
ReqMode: "http",
939+
AllowedMethods: []string{},
940+
DeniedMethods: []string{},
941+
RedirectFromDomain: []string{},
942+
ServicePathExclude: []string{},
885943
},
886944
},
887945
}

0 commit comments

Comments
 (0)