Skip to content

Commit

Permalink
Placing /.well-known paths to the top (fixes #164)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Mar 6, 2017
1 parent 9042e59 commit f4d1f9a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ The following environment variables can be used to configure the *Docker Flow Pr
|-------------------|----------------------------------------------------------|--------|-------|-------|
|BIND_PORTS |Ports to bind in addition to `80` and `443`. Multiple values can be separated with comma|No| |8085, 8086|
|CERTS |This parameter is **deprecated** as of February 2017. All the certificates from the `/cets/` directory are now loaded automatically| | | |
|CONNECTION_MODE |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*.|No|http-server-close|http-keep-alive|
|CONSUL_ADDRESS |The address of a Consul instance used for storing proxy information and discovering running nodes. Multiple addresses can be separated with comma (e.g. 192.168.0.10:8500,192.168.0.11:8500).|Only in the *default* mode| |192.168.0.10:8500|
|CONNECTION_MODE |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`.|No|http-server-close|http-keep-alive|
|CONSUL_ADDRESS |The address of a Consul instance used for storing proxy information and discovering running nodes. Multiple addresses can be separated with comma (e.g. 192.168.0.10:8500,192.168.0.11:8500).|Only in the `default` mode| |192.168.0.10:8500|
|DEFAULT_PORTS |The default ports used by the proxy. Multiple values can be separated with comma (`,`). If a port should be for SSL connections, append it with `:ssl.|No|80,443:ssl| |
|EXTRA_FRONTEND |Value will be added to the default `frontend` configuration.|No | | |
|EXTRA_GLOBAL |Value will be added to the default `global` configuration.|No | | |
Expand Down
55 changes: 55 additions & 0 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,61 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_PutsServicesWithRootPat
s.Equal(expectedData, actualData)
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_PutsServicesWellKnownPathToTheBeginning() {
var actualData string
tmpl := s.TemplateContent
expectedData := fmt.Sprintf(
`%s
acl url_02-another-well-known-service1111 path_beg /.well-known/and/somrthing/else
use_backend 02-another-well-known-service-be1111 if url_02-another-well-known-service1111
acl url_02-well-known-service1111 path_beg /.well-known
use_backend 02-well-known-service-be1111 if url_02-well-known-service1111
acl url_01-first-service1111 path_beg /path
use_backend 01-first-service-be1111 if url_01-first-service1111
acl url_03-third-service1111 path_beg /path
use_backend 03-third-service-be1111 if url_03-third-service1111%s`,
tmpl,
s.ServicesContent,
)
writeFile = func(filename string, data []byte, perm os.FileMode) error {
actualData = string(data)
return nil
}
p := NewHaProxy(s.TemplatesPath, s.ConfigsPath)
// Will be listed third
data.Services["01-first-service"] = Service{
ServiceName: "01-first-service",
ServiceDest: []ServiceDest{
{Port: "1111", ServicePath: []string{"/path"}},
},
}
// Will be listed second bacause of the well-known path and service name
data.Services["02-well-known-service"] = Service{
ServiceName: "02-well-known-service",
ServiceDest: []ServiceDest{
{Port: "1111", ServicePath: []string{"/.well-known"}},
},
}
// Will be listed first because of the well-known path and service name
data.Services["02-another-well-known-service"] = Service{
ServiceName: "02-another-well-known-service",
ServiceDest: []ServiceDest{
{Port: "1111", ServicePath: []string{"/.well-known/and/somrthing/else"}},
},
}
// Will be listed last
data.Services["03-third-service"] = Service{
ServiceName: "03-third-service",
ServiceDest: []ServiceDest{
{Port: "1111", ServicePath: []string{"/path"}},
},
}

p.CreateConfigFromTemplates()

s.Equal(expectedData, actualData)
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndTcp() {
var actualData string
tmpl := s.TemplateContent
Expand Down
25 changes: 22 additions & 3 deletions proxy/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ func (slice Services) Len() int {
func (slice Services) Less(i, j int) bool {
firstHasRoot := hasRoot(slice[i])
secondHasRoot := hasRoot(slice[j])
if (firstHasRoot && secondHasRoot) || (!firstHasRoot && !secondHasRoot) {
return slice[i].AclName < slice[j].AclName
} else if firstHasRoot {
firstHasWelKnown := hasWellKnown(slice[i])

This comment has been minimized.

Copy link
@mhabegger

mhabegger Mar 6, 2017

Contributor

typo ? WelKnown ?

This comment has been minimized.

Copy link
@vfarcic

vfarcic Mar 6, 2017

Author Owner

Yep. It's a typo of the variable name. Just submitted a new commit with the fix. Since this does not affect how the service behaves, this commit will not be built until the next release.

secondHasWelKnown := hasWellKnown(slice[j])
if firstHasRoot && !secondHasRoot {
return false
} else if !firstHasRoot && secondHasRoot {
return true
} else if firstHasWelKnown && !secondHasWelKnown {
return true
} else if !firstHasWelKnown && secondHasWelKnown {
return false
} else {
return slice[i].AclName < slice[j].AclName
}
return true
}
Expand All @@ -130,6 +138,17 @@ func hasRoot(service Service) bool {
return false
}

func hasWellKnown(service Service) bool {
for _, sd := range service.ServiceDest {
for _, path := range sd.ServicePath {
if strings.HasPrefix(strings.ToLower(path), "/.well-known") {
return true
}
}
}
return false
}

func (slice Services) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
Expand Down

0 comments on commit f4d1f9a

Please sign in to comment.