Skip to content

Commit eadba66

Browse files
author
Nico Schieder
committed
Added configmap settings to support perfect forward secrecy
1 parent 3840597 commit eadba66

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

nginx-controller/controller/controller.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,29 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
364364
}
365365
}
366366

367+
// SSL block
368+
if sslProtocols, exists := cfgm.Data["ssl-protocols"]; exists {
369+
cfg.MainServerSSLProtocols = sslProtocols
370+
}
371+
if sslPreferServerCiphers, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "ssl-prefer-server-ciphers", cfgm); exists {
372+
if err != nil {
373+
glog.Error(err)
374+
} else {
375+
cfg.MainServerSSLPreferServerCiphers = sslPreferServerCiphers
376+
}
377+
}
378+
if sslCiphers, exists := cfgm.Data["ssl-ciphers"]; exists {
379+
cfg.MainServerSSLCiphers = strings.Trim(sslCiphers, "\n")
380+
}
381+
if sslDHParamFile, exists := cfgm.Data["ssl-dhparam-file"]; exists {
382+
sslDHParamFile = strings.Trim(sslDHParamFile, "\n")
383+
fileName, err := lbc.cnf.AddOrUpdateDHParam(sslDHParamFile)
384+
if err != nil {
385+
glog.Errorf("Configmap %s/%s: Could not update dhparams: %v", cfgm.GetNamespace(), cfgm.GetName(), err)
386+
}
387+
cfg.MainServerSSLDHParam = fileName
388+
}
389+
367390
if logFormat, exists := cfgm.Data["log-format"]; exists {
368391
cfg.MainLogFormat = logFormat
369392
}
@@ -467,7 +490,7 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) nginx.
467490
glog.Warningf("Error retrieving secret %v for Ingress %v: %v", secretName, ing.Name, err)
468491
continue
469492
}
470-
ingEx.Secrets[secretName] = secret
493+
ingEx.Secrets[fmt.Sprintf("%s-%s", ing.GetNamespace(), secretName)] = secret
471494
}
472495

473496
ingEx.Endpoints = make(map[string][]string)

nginx-controller/nginx/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type Config struct {
1616
HSTS bool
1717
HSTSMaxAge int64
1818
HSTSIncludeSubdomains bool
19+
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
20+
MainServerSSLProtocols string
21+
MainServerSSLPreferServerCiphers bool
22+
MainServerSSLCiphers string
23+
MainServerSSLDHParam string
1924
}
2025

2126
// NewDefaultConfig creates a Config with default values

nginx-controller/nginx/configurator.go

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func NewConfigurator(nginx *NginxController, config *Config) *Configurator {
2929
return &cnf
3030
}
3131

32+
func (cnf *Configurator) AddOrUpdateDHParam(content string) (string, error) {
33+
return cnf.nginx.AddOrUpdateDHParam(content)
34+
}
35+
3236
// AddOrUpdateIngress adds or updates NGINX configuration for an Ingress resource
3337
func (cnf *Configurator) AddOrUpdateIngress(name string, ingEx *IngressEx) {
3438
cnf.lock.Lock()
@@ -387,6 +391,10 @@ func (cnf *Configurator) UpdateConfig(config *Config) {
387391
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
388392
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
389393
LogFormat: config.MainLogFormat,
394+
SSLProtocols: config.MainServerSSLProtocols,
395+
SSLCiphers: config.MainServerSSLCiphers,
396+
SSLDHParam: config.MainServerSSLDHParam,
397+
SSLPreferServerCiphers: config.MainServerSSLPreferServerCiphers,
390398
}
391399

392400
cnf.nginx.UpdateMainConfigFile(mainCfg)

nginx-controller/nginx/nginx.conf.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ http {
3838
default upgrade;
3939
'' close;
4040
}
41+
{{if .SSLProtocols}}ssl_protocols {{.SSLProtocols}};{{end}}
42+
{{if .SSLCiphers}}ssl_ciphers "{{.SSLCiphers}}";{{end}}
43+
{{if .SSLPreferServerCiphers}}ssl_prefer_server_ciphers on;{{end}}
44+
{{if .SSLDHParam}}ssl_dhparam {{.SSLDHParam}};{{end}}
4145

4246
include /etc/nginx/conf.d/*.conf;
4347
}

nginx-controller/nginx/nginx.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/golang/glog"
1212
)
1313

14+
const dhparamFilename = "dhparam.pem"
15+
1416
// NginxController Updates NGINX configuration, starts and reloads NGINX
1517
type NginxController struct {
1618
nginxConfdPath string
@@ -70,6 +72,11 @@ type NginxMainConfig struct {
7072
ServerNamesHashBucketSize string
7173
ServerNamesHashMaxSize string
7274
LogFormat string
75+
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
76+
SSLProtocols string
77+
SSLPreferServerCiphers bool
78+
SSLCiphers string
79+
SSLDHParam string
7380
}
7481

7582
// NewUpstreamWithDefaultServer creates an upstream with the default server.
@@ -91,7 +98,7 @@ func NewNginxController(nginxConfPath string, local bool) (*NginxController, err
9198
}
9299

93100
if !local {
94-
ngxc.createCertsDir()
101+
createDir(ngxc.nginxCertsPath)
95102
}
96103

97104
cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize}
@@ -121,6 +128,24 @@ func (nginx *NginxController) AddOrUpdateIngress(name string, config IngressNgin
121128
nginx.templateIt(config, filename)
122129
}
123130

131+
// AddOrUpdateDHParam creates the servers dhparam.pem file
132+
func (nginx *NginxController) AddOrUpdateDHParam(dhparam string) (string, error) {
133+
fileName := nginx.nginxCertsPath + "/" + dhparamFilename
134+
if !nginx.local {
135+
pem, err := os.Create(fileName)
136+
if err != nil {
137+
return fileName, fmt.Errorf("Couldn't create file %v: %v", fileName, err)
138+
}
139+
defer pem.Close()
140+
141+
_, err = pem.WriteString(dhparam)
142+
if err != nil {
143+
return fileName, fmt.Errorf("Couldn't write to pem file %v: %v", fileName, err)
144+
}
145+
}
146+
return fileName, nil
147+
}
148+
124149
// AddOrUpdateCertAndKey creates a .pem file wth the cert and the key with the
125150
// specified name
126151
func (nginx *NginxController) AddOrUpdateCertAndKey(name string, cert string, key string) string {
@@ -211,9 +236,9 @@ func (nginx *NginxController) Start() {
211236
}
212237
}
213238

214-
func (nginx *NginxController) createCertsDir() {
215-
if err := os.Mkdir(nginx.nginxCertsPath, os.ModeDir); err != nil {
216-
glog.Fatalf("Couldn't create directory %v: %v", nginx.nginxCertsPath, err)
239+
func createDir(path string) {
240+
if err := os.Mkdir(path, os.ModeDir); err != nil {
241+
glog.Fatalf("Couldn't create directory %v: %v", path, err)
217242
}
218243
}
219244

0 commit comments

Comments
 (0)