Skip to content

Added configmap settings to support perfect forward secrecy #85

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

Merged
merged 2 commits into from
Dec 1, 2016
Merged
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
24 changes: 24 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,30 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
}
}

// SSL block
if sslProtocols, exists := cfgm.Data["ssl-protocols"]; exists {
cfg.MainServerSSLProtocols = sslProtocols
}
if sslPreferServerCiphers, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "ssl-prefer-server-ciphers", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
cfg.MainServerSSLPreferServerCiphers = sslPreferServerCiphers
}
}
if sslCiphers, exists := cfgm.Data["ssl-ciphers"]; exists {
cfg.MainServerSSLCiphers = strings.Trim(sslCiphers, "\n")
}
if sslDHParamFile, exists := cfgm.Data["ssl-dhparam-file"]; exists {
sslDHParamFile = strings.Trim(sslDHParamFile, "\n")
fileName, err := lbc.cnf.AddOrUpdateDHParam(sslDHParamFile)
if err != nil {
glog.Errorf("Configmap %s/%s: Could not update dhparams: %v", cfgm.GetNamespace(), cfgm.GetName(), err)
} else {
cfg.MainServerSSLDHParam = fileName
}
}

if logFormat, exists := cfgm.Data["log-format"]; exists {
cfg.MainLogFormat = logFormat
}
Expand Down
5 changes: 5 additions & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Config struct {
HSTS bool
HSTSMaxAge int64
HSTSIncludeSubdomains bool
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
MainServerSSLProtocols string
MainServerSSLPreferServerCiphers bool
MainServerSSLCiphers string
MainServerSSLDHParam string
}

// NewDefaultConfig creates a Config with default values
Expand Down
8 changes: 8 additions & 0 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func NewConfigurator(nginx *NginxController, config *Config) *Configurator {
return &cnf
}

func (cnf *Configurator) AddOrUpdateDHParam(content string) (string, error) {
return cnf.nginx.AddOrUpdateDHParam(content)
}

// AddOrUpdateIngress adds or updates NGINX configuration for an Ingress resource
func (cnf *Configurator) AddOrUpdateIngress(name string, ingEx *IngressEx) {
cnf.lock.Lock()
Expand Down Expand Up @@ -387,6 +391,10 @@ func (cnf *Configurator) UpdateConfig(config *Config) {
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
LogFormat: config.MainLogFormat,
SSLProtocols: config.MainServerSSLProtocols,
SSLCiphers: config.MainServerSSLCiphers,
SSLDHParam: config.MainServerSSLDHParam,
SSLPreferServerCiphers: config.MainServerSSLPreferServerCiphers,
}

cnf.nginx.UpdateMainConfigFile(mainCfg)
Expand Down
4 changes: 4 additions & 0 deletions nginx-controller/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ http {
default upgrade;
'' close;
}
{{if .SSLProtocols}}ssl_protocols {{.SSLProtocols}};{{end}}
{{if .SSLCiphers}}ssl_ciphers "{{.SSLCiphers}}";{{end}}
{{if .SSLPreferServerCiphers}}ssl_prefer_server_ciphers on;{{end}}
{{if .SSLDHParam}}ssl_dhparam {{.SSLDHParam}};{{end}}

include /etc/nginx/conf.d/*.conf;
}
33 changes: 29 additions & 4 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/golang/glog"
)

const dhparamFilename = "dhparam.pem"

// NginxController Updates NGINX configuration, starts and reloads NGINX
type NginxController struct {
nginxConfdPath string
Expand Down Expand Up @@ -70,6 +72,11 @@ type NginxMainConfig struct {
ServerNamesHashBucketSize string
ServerNamesHashMaxSize string
LogFormat string
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
SSLProtocols string
SSLPreferServerCiphers bool
SSLCiphers string
SSLDHParam string
}

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

if !local {
ngxc.createCertsDir()
createDir(ngxc.nginxCertsPath)
}

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

// AddOrUpdateDHParam creates the servers dhparam.pem file
func (nginx *NginxController) AddOrUpdateDHParam(dhparam string) (string, error) {
fileName := nginx.nginxCertsPath + "/" + dhparamFilename
if !nginx.local {
pem, err := os.Create(fileName)
if err != nil {
return fileName, fmt.Errorf("Couldn't create file %v: %v", fileName, err)
}
defer pem.Close()

_, err = pem.WriteString(dhparam)
if err != nil {
return fileName, fmt.Errorf("Couldn't write to pem file %v: %v", fileName, err)
}
}
return fileName, nil
}

// AddOrUpdateCertAndKey creates a .pem file wth the cert and the key with the
// specified name
func (nginx *NginxController) AddOrUpdateCertAndKey(name string, cert string, key string) string {
Expand Down Expand Up @@ -211,9 +236,9 @@ func (nginx *NginxController) Start() {
}
}

func (nginx *NginxController) createCertsDir() {
if err := os.Mkdir(nginx.nginxCertsPath, os.ModeDir); err != nil {
glog.Fatalf("Couldn't create directory %v: %v", nginx.nginxCertsPath, err)
func createDir(path string) {
if err := os.Mkdir(path, os.ModeDir); err != nil {
glog.Fatalf("Couldn't create directory %v: %v", path, err)
}
}

Expand Down