Skip to content

Commit

Permalink
support added for ingress.kubernetes.io/force-ssl-redirect
Browse files Browse the repository at this point in the history
Signed-off-by: பாலாஜி <rbalajis25@gmail.com>
  • Loading branch information
poonai committed Jan 13, 2018
1 parent 343fa39 commit 95012e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
13 changes: 13 additions & 0 deletions internal/contour/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ func (lc *ListenerCache) httpsAddress() string {
return DEFAULT_HTTPS_LISTENER_ADDRESS
}

func (lc *listenerCache) HostHasTls(host string) bool {
for _, listener := range lc.Values() {
for _, filterChain := range listener.FilterChains {
for _, sniDomain := range filterChain.GetFilterChainMatch().GetSniDomains() {
if sniDomain == host {
return true
}
}
}
}
return false
}

// httpsPort returns the port for the HTTPS (TLS) listener
// or DEFAULT_HTTPS_LISTENER_PORT if not configured.
func (lc *ListenerCache) httpsPort() uint32 {
Expand Down
34 changes: 32 additions & 2 deletions internal/contour/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (t *Translator) addIngress(i *v1beta1.Ingress) {
host = "*"
}
t.vhosts[host] = appendIfMissing(t.vhosts[host], i)
t.recomputevhost(host, t.vhosts[host])
t.recomputevhost(host, t.listenerCache.HostHasTls(host), t.vhosts[host])
}
}

Expand All @@ -309,7 +309,7 @@ func (t *Translator) removeIngress(i *v1beta1.Ingress) {
}
for _, rule := range i.Spec.Rules {
t.vhosts[rule.Host] = removeIfPresent(t.vhosts[rule.Host], i)
t.recomputevhost(rule.Host, t.vhosts[rule.Host])
t.recomputevhost(rule.Host, t.listenerCache.HostHasTls(rule.Host), t.vhosts[rule.Host])
}
}

Expand All @@ -318,6 +318,8 @@ func (t *Translator) recomputeListeners() {
}

func (t *Translator) addSecret(s *v1.Secret) {
defer t.VirtualHostCache.Notify()

_, cert := s.Data[v1.TLSCertKey]
_, key := s.Data[v1.TLSPrivateKeyKey]
if !cert || !key {
Expand All @@ -333,11 +335,39 @@ func (t *Translator) addSecret(s *v1.Secret) {
t.secrets[metadata{name: s.Name, namespace: s.Namespace}] = s

t.recomputeTLSListener(t.ingresses, t.secrets)

if t.ingresses[metadata{name: s.Name, namespace: s.Namespace}] != nil {
i := t.ingresses[metadata{name: s.Name, namespace: s.Namespace}]
for _, rule := range i.Spec.Rules {
host := rule.Host
if host == "" {
// If the host is unspecified, the Ingress routes all traffic based on the specified IngressRuleValue.
host = "*"
}
t.vhosts[host] = appendIfMissing(t.vhosts[host], i)
t.recomputevhost(host, t.listenerCache.HostHasTls(host), t.vhosts[host])
}
}
}

func (t *Translator) removeSecret(s *v1.Secret) {
defer t.VirtualHostCache.Notify()

delete(t.secrets, metadata{name: s.Name, namespace: s.Namespace})
t.recomputeTLSListener(t.ingresses, t.secrets)
if t.ingresses[metadata{name: s.Name, namespace: s.Namespace}] != nil {
i := t.ingresses[metadata{name: s.Name, namespace: s.Namespace}]
for _, rule := range i.Spec.Rules {
host := rule.Host
if host == "" {
// If the host is unspecified, the Ingress routes all traffic based on the specified IngressRuleValue.
host = "*"
}
t.vhosts[host] = appendIfMissing(t.vhosts[host], i)
t.Infof("%s is %t", host, t.listenerCache.HostHasTls(host))
t.recomputevhost(host, t.listenerCache.HostHasTls(host), t.vhosts[host])
}
}
}

// writeSecret writes the contents of the secret to a fixed location on
Expand Down
14 changes: 13 additions & 1 deletion internal/contour/virtualhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type VirtualHostCache struct {
// recomputevhost recomputes the *v2.VirutalHost record from the list of ingresses
// supplied and the cache updated. If ingresses is empty then the *v2.VirtualHost
// record will be removed from the cache.
func (v *VirtualHostCache) recomputevhost(vhost string, ingresses []*v1beta1.Ingress) {
func (v *VirtualHostCache) recomputevhost(vhost string, tls bool, ingresses []*v1beta1.Ingress) {
switch len(ingresses) {
case 0:
// there are no ingresses registered with this vhost any more
Expand All @@ -55,6 +55,11 @@ func (v *VirtualHostCache) recomputevhost(vhost string, ingresses []*v1beta1.Ing
// TODO(dfc) plumb a logger in here so we can log this error.
continue
}
if tls {
if sslRedirect(ing) {
vv.RequireTls = v2.VirtualHost_ALL
}
}
for _, p := range rule.IngressRuleValue.HTTP.Paths {
m := pathToRouteMatch(p)
a := clusteraction(ingressBackendToClusterName(ing, &p.Backend))
Expand Down Expand Up @@ -145,3 +150,10 @@ func clusteraction(cluster string) *v2.Route_Route {
},
}
}

func sslRedirect(i *v1beta1.Ingress) bool {
if i.Annotations["kubenetes.io/ingress.ssl-redirect"] == "false" {
return false
}
return true
}

0 comments on commit 95012e0

Please sign in to comment.