Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Apr 24, 2017
2 parents 4c6ab96 + 6bb4ff0 commit 5f6fcdf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
12 changes: 12 additions & 0 deletions actions/reconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ func (m *Reconfigure) getHeaders(sr *proxy.Service) string {
header,
)
}
for _, header := range sr.DelReqHeader {
tmpl += fmt.Sprintf(`
http-request del-header %s`,
header,
)
}
for _, header := range sr.DelResHeader {
tmpl += fmt.Sprintf(`
http-response del-header %s`,
header,
)
}
return tmpl
}

Expand Down
64 changes: 64 additions & 0 deletions actions/reconfigure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,70 @@ backend %s-be%s
s.Equal(expectedData, actualData)
}

func (s ReconfigureTestSuite) Test_Execute_DelReqHeader_WhenDelReqHeaderIsSet() {
s.reconfigure.Mode = "swarm"
s.reconfigure.DelReqHeader = []string{"header-1", "header-2"}
var actualFilename, actualData string
expectedFilename := fmt.Sprintf("%s/%s-be.cfg", s.TemplatesPath, s.ServiceName)
expectedData := fmt.Sprintf(
`
backend %s-be%s
mode http
http-request del-header header-1
http-request del-header header-2
server %s %s:%s`,
s.ServiceName,
s.reconfigure.ServiceDest[0].Port,
s.ServiceName,
s.ServiceName,
s.reconfigure.ServiceDest[0].Port,
)
writeBeTemplateOrig := writeBeTemplate
defer func() { writeBeTemplate = writeBeTemplateOrig }()
writeBeTemplate = func(filename string, data []byte, perm os.FileMode) error {
actualFilename = filename
actualData = string(data)
return nil
}

s.reconfigure.Execute(true)

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
}

func (s ReconfigureTestSuite) Test_Execute_DelResHeader_WhenDelResHeaderIsSet() {
s.reconfigure.Mode = "swarm"
s.reconfigure.DelResHeader = []string{"header-1", "header-2"}
var actualFilename, actualData string
expectedFilename := fmt.Sprintf("%s/%s-be.cfg", s.TemplatesPath, s.ServiceName)
expectedData := fmt.Sprintf(
`
backend %s-be%s
mode http
http-response del-header header-1
http-response del-header header-2
server %s %s:%s`,
s.ServiceName,
s.reconfigure.ServiceDest[0].Port,
s.ServiceName,
s.ServiceName,
s.reconfigure.ServiceDest[0].Port,
)
writeBeTemplateOrig := writeBeTemplate
defer func() { writeBeTemplate = writeBeTemplateOrig }()
writeBeTemplate = func(filename string, data []byte, perm os.FileMode) error {
actualFilename = filename
actualData = string(data)
return nil
}

s.reconfigure.Execute(true)

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
}

func (s ReconfigureTestSuite) Test_Execute_DoesNotInvokeRegistrarableCreateConfigs_WhenModeIsService() {
mockObj := getRegistrarableMock("")
registryInstanceOrig := registryInstance
Expand Down
10 changes: 6 additions & 4 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ The following query parameters can be used to send a *reconfigure* request to *D
|Query |Description |Required|Default|
|---------------|------------------------------------------------------------------------------------------|--------|-------|
|aclName |ACLs are ordered alphabetically by their names. If not specified, serviceName is used instead.<br>Example: `05-go-demo-acl`|No | |
|addReqHeader |Additional headers that will be added to the request before forwarding it to the service. Multiple headers should be separated with comma (`,`). Please consult [Add a header to the request](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#add-a-header-to-the-request) for more info.<br>Example: `X-Forwarded-Port %[dst_port],X-Forwarded-Ssl on if { ssl_fc }`|No| |
|addResHeader |Additional headers that will be added to the response before forwarding it to the client. Multiple headers should be separated with comma (`,`). Please consult [Add a header to the response](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#rewriting-http-responses) for more info.<br>Example: `X-Via %[env(HOSTNAME)],Server haproxy`|No| |
|addReqHeader |Additional headers that will be added to the request before forwarding it to the service. Multiple headers should be separated with comma (`,`). Please consult [Add a header to the request](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#add-a-header-to-the-request) for more info.<br>Example: `X-Forwarded-Port %[dst_port],X-Forwarded-Ssl on if { ssl_fc }`|No| |
|addResHeader |Additional headers that will be added to the response before forwarding it to the client. Multiple headers should be separated with comma (`,`). Please consult [Add a header to the response](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#rewriting-http-responses) for more info.<br>Example: `X-Via %[env(HOSTNAME)],Server haproxy`|No| |
|connectionMode |HAProxy supports 5 connection modes.<br><br>`http-keep-alive`: all requests and responses are processed.<br>`http-tunnel`: only the first request and response are processed, everything else is forwarded with no analysis.<br>`httpclose`: tunnel with "Connection: close" added in both directions.<br>`http-server-close`: the server-facing connection is closed after the response.<br>`forceclose`: the connection is actively closed after end of response.<br><br>In general, it is preferred to use `http-server-close` with application servers, and some static servers might benefit from `http-keep-alive`.<br>Connection mode is restricted to HTTP mode only. If specified, connection mode will be applied to the backend section.<br>Example: http-keep-alive|No| |
|delReqHeader |Additional headers that will be deleted in the request before forwarding it to the service. Multiple headers should be separated with comma (`,`). Please consult [Delete a header in the request](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-request) for more info.<br>Example: `X-Forwarded-For,Cookie`|No| |
|delResHeader |Additional headers that will be deleted in the response before forwarding it to the client. Multiple headers should be separated with comma (`,`). Please consult [Delete a header in the response](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-response) for more info.<br>Example: `X-Varnish,X-Cache`|No| |
|httpsPort |The internal HTTPS port of a service that should be reconfigured. The port is used only in the `swarm` mode. If not specified, the `port` parameter will be used instead.<br>Example: `443`|No| |
|port |The internal port of a service that should be reconfigured. The port is used only in the `swarm` mode. The parameter can be prefixed with an index thus allowing definition of multiple destinations for a single service (e.g. `port.1`, `port.2`, and so on).<br>Example: `8080`|Only in `swarm` mode.| |
|reqMode |The request mode. The proxy should be able to work with any mode supported by HAProxy. However, actively supported and tested modes are `http`, `tcp`, and `sni`. The `sni` mode implies TCP with an SNI-based routing.<br>Example: `tcp`|No|http|
Expand All @@ -26,8 +28,8 @@ The following query parameters can be used to send a *reconfigure* request to *D
|serviceDomain |The domain of the service. If set, the proxy will allow access only to requests coming to that domain. Multiple domains should be separated with comma (`,`).**This parameter cannot be used with TCP.**<br>Example: ecme.com|No| |
|serviceDomainMatchAll|Whether to include subdomains and FDQN domains in the match. If set to false, and, for example, `serviceDomain` is set to `acme.com`, `something.acme.com` would not be considered a match unless this parameter is set to `true`. If this option is used, it is recommended to put any subdomains higher in the list using `aclName`.<br>Example: `true`|No|false|
|serviceName |The name of the service. It must match the name of the Swarm service.<br>Example: `go-demo`|Yes| |
|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 (`,`). 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 }`|No| |
|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 (`,`). 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`|No| |
|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 (`,`). 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 }`|No| |
|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 (`,`). 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`|No| |
|srcPort |The source (entry) port of a service. Useful only when specifying multiple destinations of a single 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).<br>Example: `80`|No| |
|timeoutServer |The server timeout in seconds.<br>Example: `60` |No |20 |
|timeoutTunnel |The tunnel timeout in seconds.<br>Example: `3600` |No |3600 |
Expand Down
10 changes: 10 additions & 0 deletions proxy/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type Service struct {
// The path to the Consul Template representing a snippet of the frontend configuration.
// If specified, proxy template will be loaded from the specified file.
ConsulTemplateFePath string `split_words:"true"`
// Additional headers that will be deleted in the request before forwarding it to the service. Please consult https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-request for more info.
DelReqHeader []string `split_words:"true"`
// Additional headers that will be deleted in the response before forwarding it to the client. Please consult https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-response for more info.
DelResHeader []string `split_words:"true"`
// Whether to distribute a request to all the instances of the proxy.
// Used only in the swarm mode.
Distribute bool `split_words:"true"`
Expand Down Expand Up @@ -285,12 +289,18 @@ func GetServiceFromProvider(provider ServiceParameterProvider) *Service {
} else if len(provider.GetString("setHeader")) > 0 { // TODO: Deprecated since Apr. 2017.
sr.SetReqHeader = strings.Split(provider.GetString("setHeader"), ",")
}
if len(provider.GetString("delReqHeader")) > 0 {
sr.DelReqHeader = strings.Split(provider.GetString("delReqHeader"), ",")
}
if len(provider.GetString("addResHeader")) > 0 {
sr.AddResHeader = strings.Split(provider.GetString("addResHeader"), ",")
}
if len(provider.GetString("setResHeader")) > 0 {
sr.SetResHeader = strings.Split(provider.GetString("setResHeader"), ",")
}
if len(provider.GetString("delResHeader")) > 0 {
sr.DelResHeader = strings.Split(provider.GetString("delResHeader"), ",")
}
globalUsersString := GetSecretOrEnvVar("USERS", "")
globalUsersEncrypted := strings.EqualFold(GetSecretOrEnvVar("USERS_PASS_ENCRYPTED", ""), "true")
sr.Users = mergeUsers(
Expand Down
4 changes: 4 additions & 0 deletions proxy/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func (s *TypesTestSuite) Test_GetServiceFromMap_ReturnsProxyService() {
AddResHeader: []string{"add-header-1", "add-header-2"},
ConsulTemplateFePath: "consulTemplateFePath",
ConsulTemplateBePath: "consulTemplateBePath",
DelReqHeader: []string{"del-header-1", "del-header-2"},
DelResHeader: []string{"del-header-1", "del-header-2"},
Distribute: true,
HttpsOnly: true,
HttpsPort: 1234,
Expand Down Expand Up @@ -230,6 +232,8 @@ func (s *TypesTestSuite) Test_GetServiceFromMap_ReturnsProxyService() {
"addResHeader": strings.Join(expected.AddResHeader, ","),
"setReqHeader": strings.Join(expected.SetReqHeader, ","),
"setResHeader": strings.Join(expected.SetResHeader, ","),
"delReqHeader": strings.Join(expected.DelReqHeader, ","),
"delResHeader": strings.Join(expected.DelResHeader, ","),
"port": expected.ServiceDest[0].Port,
"servicePath": strings.Join(expected.ServiceDest[0].ServicePath, ","),
}
Expand Down
12 changes: 11 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ func (s *ServerTestSuite) Test_GetServiceFromUrl_ReturnsProxyService() {
ConnectionMode: "my-connection-mode",
ConsulTemplateFePath: "consulTemplateFePath",
ConsulTemplateBePath: "consulTemplateBePath",
DelReqHeader: []string{"add-header-1", "add-header-2"},
DelResHeader: []string{"add-header-1", "add-header-2"},
Distribute: true,
HttpsOnly: true,
HttpsPort: 1234,
Expand Down Expand Up @@ -634,7 +636,7 @@ func (s *ServerTestSuite) Test_GetServiceFromUrl_ReturnsProxyService() {
{Username: "user2", Password: "pass2", PassEncrypted: true}},
}
addr := fmt.Sprintf(
"%s?serviceName=%s&users=%s&usersPassEncrypted=%t&aclName=%s&serviceColor=%s&serviceCert=%s&outboundHostname=%s&consulTemplateFePath=%s&consulTemplateBePath=%s&pathType=%s&reqPathSearch=%s&reqPathReplace=%s&templateFePath=%s&templateBePath=%s&timeoutServer=%s&timeoutTunnel=%s&reqMode=%s&httpsOnly=%t&xForwardedProto=%t&redirectWhenHttpProto=%t&httpsPort=%d&serviceDomain=%s&skipCheck=%t&distribute=%t&sslVerifyNone=%t&serviceDomainMatchAll=%t&addReqHeader=%s&addResHeader=%s&setReqHeader=%s&setResHeader=%s&servicePath=/&port=1234&connectionMode=%s",
"%s?serviceName=%s&users=%s&usersPassEncrypted=%t&aclName=%s&serviceColor=%s&serviceCert=%s&outboundHostname=%s&consulTemplateFePath=%s&consulTemplateBePath=%s&pathType=%s&reqPathSearch=%s&reqPathReplace=%s&templateFePath=%s&templateBePath=%s&timeoutServer=%s&timeoutTunnel=%s&reqMode=%s&httpsOnly=%t&xForwardedProto=%t&redirectWhenHttpProto=%t&httpsPort=%d&serviceDomain=%s&skipCheck=%t&distribute=%t&sslVerifyNone=%t&serviceDomainMatchAll=%t&addReqHeader=%s&addResHeader=%s&setReqHeader=%s&setResHeader=%s&delReqHeader=%s&delResHeader=%s&servicePath=/&port=1234&connectionMode=%s",
s.BaseUrl,
expected.ServiceName,
"user1:pass1,user2:pass2",
Expand Down Expand Up @@ -666,6 +668,8 @@ func (s *ServerTestSuite) Test_GetServiceFromUrl_ReturnsProxyService() {
strings.Join(expected.AddResHeader, ","),
strings.Join(expected.SetReqHeader, ","),
strings.Join(expected.SetResHeader, ","),
strings.Join(expected.DelReqHeader, ","),
strings.Join(expected.DelResHeader, ","),
expected.ConnectionMode,
)
req, _ := http.NewRequest("GET", addr, nil)
Expand Down Expand Up @@ -719,6 +723,8 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
ConnectionMode: "my-connection-mode",
ConsulTemplateBePath: "my-ConsulTemplateBePath",
ConsulTemplateFePath: "my-ConsulTemplateFePath",
DelReqHeader: []string{"del-header-1", "del-header-2"},
DelResHeader: []string{"del-header-1", "del-header-2"},
Distribute: true,
HttpsOnly: true,
HttpsPort: 1234,
Expand Down Expand Up @@ -751,6 +757,8 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
os.Setenv("DFP_SERVICE_CONNECTION_MODE", service.ConnectionMode)
os.Setenv("DFP_SERVICE_CONSUL_TEMPLATE_FE_PATH", service.ConsulTemplateFePath)
os.Setenv("DFP_SERVICE_CONSUL_TEMPLATE_BE_PATH", service.ConsulTemplateBePath)
os.Setenv("DFP_SERVICE_DEL_REQ_HEADER", strings.Join(service.DelReqHeader, ","))
os.Setenv("DFP_SERVICE_DEL_RES_HEADER", strings.Join(service.DelResHeader, ","))
os.Setenv("DFP_SERVICE_DISTRIBUTE", strconv.FormatBool(service.Distribute))
os.Setenv("DFP_SERVICE_HTTPS_ONLY", strconv.FormatBool(service.HttpsOnly))
os.Setenv("DFP_SERVICE_HTTPS_PORT", strconv.Itoa(service.HttpsPort))
Expand Down Expand Up @@ -784,6 +792,8 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
os.Unsetenv("DFP_SERVICE_CONNECTION_MODE")
os.Unsetenv("DFP_SERVICE_CONSUL_TEMPLATE_BE_PATH")
os.Unsetenv("DFP_SERVICE_CONSUL_TEMPLATE_FE_PATH")
os.Unsetenv("DFP_SERVICE_DEL_REQ_HEADER")
os.Unsetenv("DFP_SERVICE_DEL_RES_HEADER")
os.Unsetenv("DFP_SERVICE_DISTRIBUTE")
os.Unsetenv("DFP_SERVICE_HTTPS_ONLY")
os.Unsetenv("DFP_SERVICE_HTTPS_PORT")
Expand Down

0 comments on commit 5f6fcdf

Please sign in to comment.