Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve host header even with extra trusted root certificates #1526

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/annotations/ingress_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
// Null means Host specified in the request to Application Gateway is used to connect to the backend.
BackendHostNameKey = ApplicationGatewayPrefix + "/backend-hostname"

// OverrideBackendHostNameKey defines the key to enable/disable host name overriding.
OverrideBackendHostNameKey = ApplicationGatewayPrefix + "/override-backend-host"

// HealthProbeHostKey defines the key for Host which should be used as a target for health probe.
HealthProbeHostKey = ApplicationGatewayPrefix + "/health-probe-hostname"

Expand Down Expand Up @@ -158,11 +161,16 @@ func BackendPathPrefix(ing *networking.Ingress) (string, error) {
return parseString(ing, BackendPathPrefixKey)
}

// BackendHostName override hostname
// BackendHostName specific domain name to override the backend hostname with
func BackendHostName(ing *networking.Ingress) (string, error) {
return parseString(ing, BackendHostNameKey)
}

// OverrideBackendHostName whether to override the backend hostname or not
func OverrideBackendHostName(ing *networking.Ingress) (string, error) {
return parseBool(ing, OverrideBackendHostNameKey)
}

// HealthProbeHostName probe hostname override
func HealthProbeHostName(ing *networking.Ingress) (string, error) {
return parseString(ing, HealthProbeHostKey)
Expand Down
15 changes: 15 additions & 0 deletions pkg/annotations/ingress_annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var _ = Describe("Test ingress annotation functions", func() {
"appgw.ingress.kubernetes.io/connection-draining-timeout": "3456",
"appgw.ingress.kubernetes.io/backend-path-prefix": "prefix-here",
"appgw.ingress.kubernetes.io/backend-hostname": "www.backend.com",
"appgw.ingress.kubernetes.io/overrride-backend-hostname": "true",
"appgw.ingress.kubernetes.io/hostname-extension": "www.bye.com, www.b*.com",
"appgw.ingress.kubernetes.io/appgw-ssl-certificate": "appgw-cert",
"appgw.ingress.kubernetes.io/appgw-ssl-profile": "legacy-tls",
Expand Down Expand Up @@ -340,6 +341,20 @@ var _ = Describe("Test ingress annotation functions", func() {
})
})

Context("test OverrideBackendHostName", func() {
It("returns error when ingress has no annotations", func() {
ing := &networking.Ingress{}
actual, err := OverrideBackendHostName(ing)
Expect(err).To(HaveOccurred())
Expect(actual).To(Equal(""))
})
It("returns true with correct annotation", func() {
actual, err := OverrideBackendHostName(ing)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(true))
})
})

Context("test IsSslRedirect", func() {
It("returns error when ingress has no annotations", func() {
ing := &networking.Ingress{}
Expand Down
29 changes: 21 additions & 8 deletions pkg/appgw/backendhttpsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,29 @@ func (c *appGwConfigBuilder) generateHTTPSettings(backendID backendIdentifier, p
c.recorder.Event(backendID.Ingress, v1.EventTypeWarning, events.ReasonInvalidAnnotation, err.Error())
}

// To use an HTTP setting with a trusted root certificate, we must either override with a specific domain name or choose "Pick host name from backend target".
if httpSettings.TrustedRootCertificates != nil {
if httpSettings.Protocol == n.ApplicationGatewayProtocolHTTPS && len(*httpSettings.TrustedRootCertificates) > 0 {
if httpSettings.HostName != nil && len(*httpSettings.HostName) > 0 {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(false)
} else {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(true)
if overrideBackendHostName, err := annotations.OverrideBackendHostName(backendID.Ingress); err == nil {
if httpSettings.HostName != nil && !overrideBackendHostName {
// TODO Warn about pointless setting of hostname
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the best way to implement this warning would be. Help would be appreciated.

} else if httpSettings.HostName != nil && overrideBackendHostName {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(false)
} else if httpSettings.HostName == nil && overrideBackendHostName {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(true)
}
} else if err != nil && !controllererrors.IsErrorCode(err, controllererrors.ErrorMissingAnnotation) {
c.recorder.Event(backendID.Ingress, v1.EventTypeWarning, events.ReasonInvalidAnnotation, err.Error())
} else if err != nil && controllererrors.IsErrorCode(err, controllererrors.ErrorMissingAnnotation) {
// When overrideBackendHostName is unset, fallback to original behaviour:

// To use an HTTP setting with a trusted root certificate, we must either override with a specific domain name or choose "Pick host name from backend target".
if httpSettings.TrustedRootCertificates != nil {
if httpSettings.Protocol == n.ApplicationGatewayProtocolHTTPS && len(*httpSettings.TrustedRootCertificates) > 0 {
if httpSettings.HostName != nil && len(*httpSettings.HostName) > 0 {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(false)
} else {
httpSettings.PickHostNameFromBackendAddress = to.BoolPtr(true)
}
}
}
}

return httpSettings
}