#216: sets passHostHeader to true by default (#351)

This commit is contained in:
Marcus Stong 2016-05-10 07:43:24 -04:00 committed by Emile Vauge
parent 4a4ba2791d
commit be209ed30c
11 changed files with 142 additions and 33 deletions

View file

@ -237,6 +237,7 @@ func TestConsulCatalogBuildConfig(t *testing.T) {
expectedFrontends: map[string]*types.Frontend{
"frontend-test": {
Backend: "backend-test",
PassHostHeader: true,
Routes: map[string]types.Route{
"route-host-test": {
Rule: "Host:test.localhost",

View file

@ -279,7 +279,7 @@ func (provider *Docker) getPassHostHeader(container dockertypes.ContainerJSON) s
if passHostHeader, err := getLabel(container, "traefik.frontend.passHostHeader"); err == nil {
return passHostHeader
}
return "false"
return "true"
}
func (provider *Docker) getEntryPoints(container dockertypes.ContainerJSON) []string {

View file

@ -401,7 +401,6 @@ func TestDockerGetProtocol(t *testing.T) {
func TestDockerGetPassHostHeader(t *testing.T) {
provider := &Docker{}
containers := []struct {
container docker.ContainerJSON
expected string
@ -413,7 +412,7 @@ func TestDockerGetPassHostHeader(t *testing.T) {
},
Config: &container.Config{},
},
expected: "false",
expected: "true",
},
{
container: docker.ContainerJSON{
@ -422,11 +421,11 @@ func TestDockerGetPassHostHeader(t *testing.T) {
},
Config: &container.Config{
Labels: map[string]string{
"traefik.frontend.passHostHeader": "true",
"traefik.frontend.passHostHeader": "false",
},
},
},
expected: "true",
expected: "false",
},
}
@ -745,6 +744,7 @@ func TestDockerLoadDockerConfig(t *testing.T) {
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test-docker-localhost": {
Backend: "backend-test",
PassHostHeader: true,
EntryPoints: []string{},
Routes: map[string]types.Route{
"route-frontend-Host-test-docker-localhost": {
@ -817,6 +817,7 @@ func TestDockerLoadDockerConfig(t *testing.T) {
expectedFrontends: map[string]*types.Frontend{
"frontend-Host-test1-docker-localhost": {
Backend: "backend-foobar",
PassHostHeader: true,
EntryPoints: []string{"http", "https"},
Routes: map[string]types.Route{
"route-frontend-Host-test1-docker-localhost": {
@ -826,6 +827,7 @@ func TestDockerLoadDockerConfig(t *testing.T) {
},
"frontend-Host-test2-docker-localhost": {
Backend: "backend-foobar",
PassHostHeader: true,
EntryPoints: []string{},
Routes: map[string]types.Route{
"route-frontend-Host-test2-docker-localhost": {

View file

@ -23,6 +23,7 @@ const (
type Kubernetes struct {
BaseProvider `mapstructure:",squash"`
Endpoint string
disablePassHostHeaders bool
Namespaces []string
}
@ -142,6 +143,7 @@ func (provider *Kubernetes) loadIngresses(k8sClient k8s.Client) (*types.Configur
map[string]*types.Backend{},
map[string]*types.Frontend{},
}
PassHostHeader := provider.getPassHostHeader()
for _, i := range ingresses {
for _, r := range i.Spec.Rules {
for _, pa := range r.HTTP.Paths {
@ -153,6 +155,7 @@ func (provider *Kubernetes) loadIngresses(k8sClient k8s.Client) (*types.Configur
if _, exists := templateObjects.Frontends[r.Host+pa.Path]; !exists {
templateObjects.Frontends[r.Host+pa.Path] = &types.Frontend{
Backend: r.Host + pa.Path,
PassHostHeader: PassHostHeader,
Routes: make(map[string]types.Route),
}
}
@ -199,6 +202,13 @@ func (provider *Kubernetes) loadIngresses(k8sClient k8s.Client) (*types.Configur
return &templateObjects, nil
}
func (provider *Kubernetes) getPassHostHeader() bool {
if provider.disablePassHostHeaders {
return false
}
return true
}
func (provider *Kubernetes) loadConfig(templateObjects types.Configuration) *types.Configuration {
var FuncMap = template.FuncMap{}
configuration, err := provider.getConfiguration("templates/kubernetes.tmpl", FuncMap, templateObjects)

View file

@ -140,6 +140,7 @@ func TestLoadIngresses(t *testing.T) {
Frontends: map[string]*types.Frontend{
"foo/bar": {
Backend: "foo/bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"/bar": {
Rule: "PathPrefixStrip:/bar",
@ -151,6 +152,7 @@ func TestLoadIngresses(t *testing.T) {
},
"bar": {
Backend: "bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"bar": {
Rule: "Host:bar",
@ -167,6 +169,93 @@ func TestLoadIngresses(t *testing.T) {
}
}
func TestGetPassHostHeader(t *testing.T) {
ingresses := []k8s.Ingress{{
Spec: k8s.IngressSpec{
Rules: []k8s.IngressRule{
{
Host: "foo",
IngressRuleValue: k8s.IngressRuleValue{
HTTP: &k8s.HTTPIngressRuleValue{
Paths: []k8s.HTTPIngressPath{
{
Path: "/bar",
Backend: k8s.IngressBackend{
ServiceName: "service1",
ServicePort: k8s.FromInt(801),
},
},
},
},
},
},
},
},
}}
services := []k8s.Service{
{
ObjectMeta: k8s.ObjectMeta{
Name: "service1",
UID: "1",
},
Spec: k8s.ServiceSpec{
ClusterIP: "10.0.0.1",
Ports: []k8s.ServicePort{
{
Name: "http",
Port: 801,
},
},
},
},
}
watchChan := make(chan interface{})
client := clientMock{
ingresses: ingresses,
services: services,
watchChan: watchChan,
}
provider := Kubernetes{disablePassHostHeaders: true}
actual, err := provider.loadIngresses(client)
if err != nil {
t.Fatalf("error %+v", err)
}
expected := &types.Configuration{
Backends: map[string]*types.Backend{
"foo/bar": {
Servers: map[string]types.Server{
"1": {
URL: "http://10.0.0.1:801",
Weight: 1,
},
},
CircuitBreaker: nil,
LoadBalancer: nil,
},
},
Frontends: map[string]*types.Frontend{
"foo/bar": {
Backend: "foo/bar",
Routes: map[string]types.Route{
"/bar": {
Rule: "PathPrefixStrip:/bar",
},
"foo": {
Rule: "Host:foo",
},
},
},
},
}
actualJSON, _ := json.Marshal(actual)
expectedJSON, _ := json.Marshal(expected)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
}
}
func TestLoadNamespacedIngresses(t *testing.T) {
ingresses := []k8s.Ingress{
{
@ -331,6 +420,7 @@ func TestLoadNamespacedIngresses(t *testing.T) {
Frontends: map[string]*types.Frontend{
"foo/bar": {
Backend: "foo/bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"/bar": {
Rule: "PathPrefixStrip:/bar",
@ -342,6 +432,7 @@ func TestLoadNamespacedIngresses(t *testing.T) {
},
"bar": {
Backend: "bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"bar": {
Rule: "Host:bar",
@ -557,6 +648,7 @@ func TestLoadMultipleNamespacedIngresses(t *testing.T) {
Frontends: map[string]*types.Frontend{
"foo/bar": {
Backend: "foo/bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"/bar": {
Rule: "PathPrefixStrip:/bar",
@ -568,6 +660,7 @@ func TestLoadMultipleNamespacedIngresses(t *testing.T) {
},
"bar": {
Backend: "bar",
PassHostHeader: true,
Routes: map[string]types.Route{
"bar": {
Rule: "Host:bar",
@ -576,6 +669,7 @@ func TestLoadMultipleNamespacedIngresses(t *testing.T) {
},
"awesome/quix": {
Backend: "awesome/quix",
PassHostHeader: true,
Routes: map[string]types.Route{
"/quix": {
Rule: "PathPrefixStrip:/quix",

View file

@ -430,7 +430,7 @@ func TestKVLoadConfig(t *testing.T) {
Frontends: map[string]*types.Frontend{
"frontend.with.dot": {
Backend: "backend.with.dot.too",
PassHostHeader: false,
PassHostHeader: true,
EntryPoints: []string{},
Routes: map[string]types.Route{
"route.with.dot": {

View file

@ -316,7 +316,7 @@ func (provider *Marathon) getPassHostHeader(application marathon.Application) st
if passHostHeader, err := provider.getLabel(application, "traefik.frontend.passHostHeader"); err == nil {
return passHostHeader
}
return "false"
return "true"
}
func (provider *Marathon) getEntryPoints(application marathon.Application) []string {

View file

@ -83,6 +83,7 @@ func TestMarathonLoadConfig(t *testing.T) {
expectedFrontends: map[string]*types.Frontend{
`frontend-test`: {
Backend: "backend-test",
PassHostHeader: true,
EntryPoints: []string{},
Routes: map[string]types.Route{
`route-host-test`: {
@ -780,15 +781,15 @@ func TestMarathonGetPassHostHeader(t *testing.T) {
}{
{
application: marathon.Application{},
expected: "false",
expected: "true",
},
{
application: marathon.Application{
Labels: map[string]string{
"traefik.frontend.passHostHeader": "true",
"traefik.frontend.passHostHeader": "false",
},
},
expected: "true",
expected: "false",
},
}

View file

@ -29,7 +29,7 @@
{{range .Services}}
[frontends.frontend-{{.ServiceName}}]
backend = "backend-{{.ServiceName}}"
passHostHeader = {{getAttribute "frontend.passHostHeader" .Attributes "false"}}
passHostHeader = {{getAttribute "frontend.passHostHeader" .Attributes "true"}}
{{$entryPoints := getAttribute "frontend.entrypoints" .Attributes ""}}
{{with $entryPoints}}
entrypoints = [{{range getEntryPoints $entryPoints}}

View file

@ -9,6 +9,7 @@
[frontends]{{range $frontendName, $frontend := .Frontends}}
[frontends."{{$frontendName}}"]
backend = "{{$frontend.Backend}}"
passHostHeader = {{$frontend.PassHostHeader}}
{{range $routeName, $route := $frontend.Routes}}
[frontends."{{$frontendName}}".routes."{{$routeName}}"]
rule = "{{$route.Rule}}"

View file

@ -39,7 +39,7 @@
{{$entryPoints := SplitGet . "/entrypoints"}}
[frontends."{{$frontend}}"]
backend = "{{Get "" . "/backend"}}"
passHostHeader = {{Get "false" . "/passHostHeader"}}
passHostHeader = {{Get "true" . "/passHostHeader"}}
entryPoints = [{{range $entryPoints}}
"{{.}}",
{{end}}]