Skip to content

Commit 714edeb

Browse files
committed
option to disable server tokens
1 parent dc30bc8 commit 714edeb

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

nginx-controller/controller/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
361361
if cfgmExists {
362362
cfgm := obj.(*api.ConfigMap)
363363

364+
if serverTokens, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "server-tokens", cfgm); exists {
365+
if err != nil {
366+
glog.Error(err)
367+
} else {
368+
cfg.ServerTokens = serverTokens
369+
}
370+
}
371+
364372
if proxyConnectTimeout, exists := cfgm.Data["proxy-connect-timeout"]; exists {
365373
cfg.ProxyConnectTimeout = proxyConnectTimeout
366374
}

nginx-controller/nginx/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nginx
22

33
// Config holds NGINX configuration parameters
44
type Config struct {
5+
ServerTokens bool
56
ProxyConnectTimeout string
67
ProxyReadTimeout string
78
ClientMaxBodySize string
@@ -35,6 +36,7 @@ type Config struct {
3536
// NewDefaultConfig creates a Config with default values
3637
func NewDefaultConfig() *Config {
3738
return &Config{
39+
ServerTokens: true,
3840
ProxyConnectTimeout: "60s",
3941
ProxyReadTimeout: "60s",
4042
ClientMaxBodySize: "1m",

nginx-controller/nginx/configurator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
109109

110110
server := Server{
111111
Name: serverName,
112+
ServerTokens: ingCfg.ServerTokens,
112113
HTTP2: ingCfg.HTTP2,
113114
ProxyProtocol: ingCfg.ProxyProtocol,
114115
HSTS: ingCfg.HSTS,
@@ -159,6 +160,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
159160
if len(ingEx.Ingress.Spec.Rules) == 0 && ingEx.Ingress.Spec.Backend != nil {
160161
server := Server{
161162
Name: emptyHost,
163+
ServerTokens: ingCfg.ServerTokens,
162164
HTTP2: ingCfg.HTTP2,
163165
ProxyProtocol: ingCfg.ProxyProtocol,
164166
HSTS: ingCfg.HSTS,
@@ -193,6 +195,13 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
193195

194196
func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
195197
ingCfg := *cnf.config
198+
if serverTokens, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/server-tokens", ingEx.Ingress); exists {
199+
if err != nil {
200+
glog.Error(err)
201+
} else {
202+
ingCfg.ServerTokens = serverTokens
203+
}
204+
}
196205
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
197206
ingCfg.ProxyConnectTimeout = proxyConnectTimeout
198207
}

nginx-controller/nginx/ingress.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ server {
1717
{{if $server.RealIPHeader}}real_ip_header {{$server.RealIPHeader}};{{end}}
1818
{{if $server.RealIPRecursive}}real_ip_recursive on;{{end}}
1919

20+
{{if not $server.ServerTokens}}server_tokens off;{{end}}
21+
2022
{{if $server.Name}}
2123
server_name {{$server.Name}};
2224
{{end}}

nginx-controller/nginx/nginx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type UpstreamServer struct {
4141
// Server describes an NGINX server
4242
type Server struct {
4343
Name string
44+
ServerTokens bool
4445
Locations []Location
4546
SSL bool
4647
SSLCertificate string

0 commit comments

Comments
 (0)