Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

Commit 1ec21d8

Browse files
committed
Merge pull request #850 from simonswine/feature-ssl-redirect
ingress: adds configurable SSL redirect nginx controller
2 parents 4348ee4 + 6fdd444 commit 1ec21d8

File tree

17 files changed

+415
-311
lines changed

17 files changed

+415
-311
lines changed

hack/verify-flags/exceptions.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ingress/controllers/nginx/README.md:Enables which HTTP codes should be passed fo
22
ingress/controllers/nginx/README.md:Setting at least one code this also enables [proxy_intercept_errors](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors) (required to process error_page)
33
ingress/controllers/nginx/nginx.tmpl: require("error_page")
44
ingress/controllers/nginx/nginx.tmpl: error_page {{ $errCode }} = @custom_{{ $errCode }};{{ end }}
5-
ingress/controllers/nginx/nginx/main.go: // enables which HTTP codes should be passed for processing with the error_page directive
5+
ingress/controllers/nginx/nginx/config/config.go: // enables which HTTP codes should be passed for processing with the error_page directive
66
mungegithub/mungers/submit-queue.go: sq.e2e = &fake_e2e.FakeE2ETester{
77
mungegithub/mungers/submit-queue.go: fake_e2e "k8s.io/contrib/mungegithub/mungers/e2e/fake"
88
mungegithub/mungers/submit-queue_test.go: fake_e2e "k8s.io/contrib/mungegithub/mungers/e2e/fake"

ingress/controllers/nginx/Changelog.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Changelog
22

3+
### next
4+
5+
- [X] [#1063](https://github.com/kubernetes/contrib/pull/1063) watches referenced tls secrets
6+
- [X] [#850](https://github.com/kubernetes/contrib/pull/850) adds configurable SSL redirect nginx controller
7+
38
### 0.7
49

510
- [X] [#898](https://github.com/kubernetes/contrib/pull/898) reorder locations. Location / must be the last one to avoid errors routing to subroutes
@@ -16,10 +21,3 @@ Changelog
1621
- [X] [#1102](https://github.com/kubernetes/contrib/pull/1102) geolocation of traffic in stats
1722
- [X] [#884](https://github.com/kubernetes/contrib/issues/884) support services running ssl
1823
- [X] [#930](https://github.com/kubernetes/contrib/issues/930) detect changes in configuration configmaps
19-
20-
21-
TODO
22-
23-
- [ ] [#1063](https://github.com/kubernetes/contrib/pull/1063) watches referenced tls secrets
24-
- [ ] [#850](https://github.com/kubernetes/contrib/pull/850) adds configurable SSL redirect nginx controller
25-

ingress/controllers/nginx/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ To disable this behavior use `hsts=false` in the NGINX ConfigMap.
131131

132132
NGINX provides the configuration option [ssl_buffer_size](http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size) to allow the optimization of the TLS record size. This improves the [Time To First Byte](https://www.igvita.com/2013/12/16/optimizing-nginx-tls-time-to-first-byte/) (TTTFB). The default value in the Ingress controller is `4k` (nginx default is `16k`);
133133

134+
### Server-side HTTPS enforcement through redirect
135+
136+
By default the controller redirects (301) to HTTPS if TLS is enabled for that ingress . If you want to disable that behaviour globally, you can use `ssl-redirect: "false"` in the NGINX ConfigMap.
137+
138+
To configure this feature for specfic ingress resources, you can use the `ingress.kubernetes.io/ssl-redirect: "false"` annotation in theparticular resource.
134139

135140
## Proxy Protocol
136141

ingress/controllers/nginx/controller.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242

4343
"k8s.io/contrib/ingress/controllers/nginx/nginx"
4444
"k8s.io/contrib/ingress/controllers/nginx/nginx/auth"
45+
"k8s.io/contrib/ingress/controllers/nginx/nginx/config"
4546
"k8s.io/contrib/ingress/controllers/nginx/nginx/healthcheck"
4647
"k8s.io/contrib/ingress/controllers/nginx/nginx/ratelimit"
4748
"k8s.io/contrib/ingress/controllers/nginx/nginx/rewrite"
@@ -647,7 +648,7 @@ func (lbc *loadBalancerController) getDefaultUpstream() *nginx.Upstream {
647648
return upstream
648649
}
649650

650-
func (lbc *loadBalancerController) getUpstreamServers(ngxCfg nginx.Configuration, data []interface{}) ([]*nginx.Upstream, []*nginx.Server) {
651+
func (lbc *loadBalancerController) getUpstreamServers(ngxCfg config.Configuration, data []interface{}) ([]*nginx.Upstream, []*nginx.Server) {
651652
upstreams := lbc.createUpstreams(ngxCfg, data)
652653
upstreams[defUpstreamName] = lbc.getDefaultUpstream()
653654

@@ -691,6 +692,11 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg nginx.Configuration
691692
glog.V(3).Infof("error reading secure upstream in Ingress %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
692693
}
693694

695+
locRew, err := rewrite.ParseAnnotations(ngxCfg, ing)
696+
if err != nil {
697+
glog.V(3).Infof("error parsing rewrite annotations for Ingress rule %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
698+
}
699+
694700
host := rule.Host
695701
if host == "" {
696702
host = defServerName
@@ -720,13 +726,8 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg nginx.Configuration
720726
loc.Upstream = *ups
721727
loc.Auth = *nginxAuth
722728
loc.RateLimit = *rl
723-
loc.SecureUpstream = secUpstream
724-
725-
locRew, err := rewrite.ParseAnnotations(ing)
726-
if err != nil {
727-
glog.V(3).Infof("error parsing rewrite annotations for Ingress rule %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
728-
}
729729
loc.Redirect = *locRew
730+
loc.SecureUpstream = secUpstream
730731

731732
addLoc = false
732733
continue
@@ -741,10 +742,6 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg nginx.Configuration
741742
}
742743

743744
if addLoc {
744-
locRew, err := rewrite.ParseAnnotations(ing)
745-
if err != nil {
746-
glog.V(3).Infof("error parsing rewrite annotations for Ingress rule %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
747-
}
748745

749746
server.Locations = append(server.Locations, &nginx.Location{
750747
Path: nginxPath,
@@ -785,7 +782,7 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg nginx.Configuration
785782

786783
// createUpstreams creates the NGINX upstreams for each service referenced in
787784
// Ingress rules. The servers inside the upstream are endpoints.
788-
func (lbc *loadBalancerController) createUpstreams(ngxCfg nginx.Configuration, data []interface{}) map[string]*nginx.Upstream {
785+
func (lbc *loadBalancerController) createUpstreams(ngxCfg config.Configuration, data []interface{}) map[string]*nginx.Upstream {
789786
upstreams := make(map[string]*nginx.Upstream)
790787

791788
for _, ingIf := range data {

ingress/controllers/nginx/nginx.tmpl

+6-4
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ http {
172172
{{- end }}
173173

174174
{{ if (and $server.SSL $cfg.hsts) -}}
175-
if ($scheme = http) {
176-
return 301 https://$host$request_uri;
177-
}
178-
179175
more_set_headers "Strict-Transport-Security: max-age={{ $cfg.hstsMaxAge }}{{ if $cfg.hstsIncludeSubdomains }}; includeSubDomains{{ end }}; preload";
180176
{{- end }}
181177

@@ -184,6 +180,12 @@ http {
184180
{{- range $location := $server.Locations }}
185181
{{ $path := buildLocation $location }}
186182
location {{ $path }} {
183+
{{ if (and $server.SSL $location.Redirect.SSLRedirect) -}}
184+
# enforce ssl on server side
185+
if ($scheme = http) {
186+
return 301 https://$host$request_uri;
187+
}
188+
{{- end }}
187189
{{/* if the location contains a rate limit annotation, create one */}}
188190
{{ $limits := buildRateLimit $location }}
189191
{{- range $limit := $limits }}

ingress/controllers/nginx/nginx/command.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/golang/glog"
2626

2727
"k8s.io/kubernetes/pkg/healthz"
28+
29+
"k8s.io/contrib/ingress/controllers/nginx/nginx/config"
2830
)
2931

3032
// Start starts a nginx (master process) and waits. If the process ends
@@ -54,7 +56,7 @@ func (ngx *Manager) Start() {
5456
// shut down, stop accepting new connections and continue to service current requests
5557
// until all such requests are serviced. After that, the old worker processes exit.
5658
// http://nginx.org/en/docs/beginners_guide.html#control
57-
func (ngx *Manager) CheckAndReload(cfg Configuration, ingressCfg IngressConfig) {
59+
func (ngx *Manager) CheckAndReload(cfg config.Configuration, ingressCfg IngressConfig) {
5860
ngx.reloadRateLimiter.Accept()
5961

6062
ngx.reloadLock.Lock()

0 commit comments

Comments
 (0)