Skip to content

Commit eaa30a3

Browse files
authored
Merge pull request #199 from lseelenbinder/master
Add support for Kubernetes ssl-redirect annotation.
2 parents ab6d927 + edb3703 commit eaa30a3

File tree

8 files changed

+36
-8
lines changed

8 files changed

+36
-8
lines changed

examples/customization/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ The table below summarizes some of the options. More options (extensions) are av
1919
| N/A | `server-names-hash-max-size` | Sets the value of the [server_names_hash_max_size](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_names_hash_max_size) directive. | `512` |
2020
| N/A | `http2` | Enables HTTP/2 in servers with SSL enabled. | `False` |
2121
| `nginx.org/redirect-to-https` | `redirect-to-https` | Sets the 301 redirect rule based on the value of the `http_x_forwarded_proto` header on the server block to force incoming traffic to be over HTTPS. Useful when terminating SSL in a load balancer in front of the Ingress controller — see [115](https://github.com/nginxinc/kubernetes-ingress/issues/115) | `False` |
22-
| N/A | `log-format` | Sets the custom [log format](http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format). | See the [template file](../../nginx-controller/nginx/nginx.conf.tmpl). |
22+
| `ingress.kubernetes.io/ssl-redirect` | `ssl-redirect` | Sets an unconditional 301 redirect rule for all incoming HTTP traffic to force incoming traffic over HTTPS. | `True` |
23+
| N/A | `log-format` | Sets the custom [log format](http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format). | See the [template file](../../nginx-controller/nginx/nginx.conf.tmpl). |
2324
| `nginx.org/hsts` | `hsts` | Enables [HTTP Strict Transport Security (HSTS)](https://www.nginx.com/blog/http-strict-transport-security-hsts-and-nginx/): the HSTS header is added to the responses from backends. The `preload` directive is included in the header. | `False` |
2425
| `nginx.org/hsts-max-age` | `hsts-max-age` | Sets the value of the `max-age` directive of the HSTS header. | `2592000` (1 month) |
2526
| `nginx.org/hsts-include-subdomains` | `hsts-include-subdomains` | Adds the `includeSubDomains` directive to the HSTS header. | `False`|

nginx-controller/controller/controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,13 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) {
440440
cfg.RedirectToHTTPS = redirectToHTTPS
441441
}
442442
}
443+
if sslRedirect, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "ssl-redirect", cfgm); exists {
444+
if err != nil {
445+
glog.Error(err)
446+
} else {
447+
cfg.SSLRedirect = sslRedirect
448+
}
449+
}
443450

444451
// HSTS block
445452
if hsts, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "hsts", cfgm); exists {

nginx-controller/nginx/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Config struct {
1010
ClientMaxBodySize string
1111
HTTP2 bool
1212
RedirectToHTTPS bool
13+
SSLRedirect bool
1314
MainHTTPSnippets []string
1415
MainServerNamesHashBucketSize string
1516
MainServerNamesHashMaxSize string
@@ -56,6 +57,7 @@ func NewDefaultConfig() *Config {
5657
ProxyConnectTimeout: "60s",
5758
ProxyReadTimeout: "60s",
5859
ClientMaxBodySize: "1m",
60+
SSLRedirect: true,
5961
MainServerNamesHashMaxSize: "512",
6062
ProxyBuffering: true,
6163
MainWorkerProcesses: "auto",

nginx-controller/nginx/configurator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
122122
ServerTokens: ingCfg.ServerTokens,
123123
HTTP2: ingCfg.HTTP2,
124124
RedirectToHTTPS: ingCfg.RedirectToHTTPS,
125+
SSLRedirect: ingCfg.SSLRedirect,
125126
ProxyProtocol: ingCfg.ProxyProtocol,
126127
HSTS: ingCfg.HSTS,
127128
HSTSMaxAge: ingCfg.HSTSMaxAge,
@@ -259,6 +260,13 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
259260
ingCfg.RedirectToHTTPS = redirectToHTTPS
260261
}
261262
}
263+
if sslRedirect, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "ingress.kubernetes.io/ssl-redirect", ingEx.Ingress); exists {
264+
if err != nil {
265+
glog.Error(err)
266+
} else {
267+
ingCfg.SSLRedirect = sslRedirect
268+
}
269+
}
262270
if proxyBuffering, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/proxy-buffering", ingEx.Ingress); exists {
263271
if err != nil {
264272
glog.Error(err)

nginx-controller/nginx/nginx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Server struct {
6161
StatusZone string
6262
HTTP2 bool
6363
RedirectToHTTPS bool
64+
SSLRedirect bool
6465
ProxyProtocol bool
6566
HSTS bool
6667
HSTSMaxAge int64

nginx-controller/nginx/templates/nginx-plus.ingress.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ server {
3939
proxy_pass_header {{$proxyPassHeader}};{{end}}
4040

4141
{{if $server.SSL}}
42+
{{- if $server.SSLRedirect}}
4243
if ($scheme = http) {
4344
return 301 https://$host:{{index $server.SSLPorts 0}}$request_uri;
4445
}
46+
{{- end}}
4547
{{- if $server.HSTS}}
4648
add_header Strict-Transport-Security "max-age={{$server.HSTSMaxAge}}; {{if $server.HSTSIncludeSubdomains}}includeSubDomains; {{end}}preload" always;{{end}}
4749
{{- end}}

nginx-controller/nginx/templates/nginx.ingress.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ server {
3232
{{range $proxyPassHeader := $server.ProxyPassHeaders}}
3333
proxy_pass_header {{$proxyPassHeader}};{{end}}
3434
{{if $server.SSL}}
35+
{{- if $server.SSLRedirect}}
3536
if ($scheme = http) {
3637
return 301 https://$host:{{index $server.SSLPorts 0}}$request_uri;
3738
}
39+
{{- end}}
3840
{{- if $server.HSTS}}
3941
proxy_hide_header Strict-Transport-Security;
4042
add_header Strict-Transport-Security "max-age={{$server.HSTSMaxAge}}; {{if $server.HSTSIncludeSubdomains}}includeSubDomains; {{end}}preload" always;{{end}}

nginx-controller/nginx/templates/templates_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ var ingCfg = nginx.IngressNginxConfig{
2424

2525
Servers: []nginx.Server{
2626
nginx.Server{
27-
Name: "test.example.com",
28-
ServerTokens: "off",
29-
StatusZone: "test.example.com",
30-
JWTKey: "/etc/nginx/secrets/key.jwk",
31-
JWTRealm: "closed site",
32-
JWTToken: "$cookie_auth_token",
33-
JWTLoginURL: "https://test.example.com/login",
27+
Name: "test.example.com",
28+
ServerTokens: "off",
29+
StatusZone: "test.example.com",
30+
JWTKey: "/etc/nginx/secrets/key.jwk",
31+
JWTRealm: "closed site",
32+
JWTToken: "$cookie_auth_token",
33+
JWTLoginURL: "https://test.example.com/login",
34+
SSL: true,
35+
SSLCertificate: "secret.pem",
36+
SSLCertificateKey: "secret.pem",
37+
SSLPorts: []int{443},
38+
SSLRedirect: true,
3439
Locations: []nginx.Location{
3540
nginx.Location{
3641
Path: "/",

0 commit comments

Comments
 (0)