Skip to content

Commit edcba72

Browse files
authored
Merge pull request #112 from nginxinc/server-tokens
Disable emitting nginx version for Plus controller
2 parents 2c931e6 + 53dd9fd commit edcba72

File tree

7 files changed

+32
-0
lines changed

7 files changed

+32
-0
lines changed

examples/customization/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The table below summarizes some of the options. More options (extensions) are av
2929
| N/A | `set-real-ip-from` | Sets the value of the [set_real_ip_from](http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from) directive. | N/A |
3030
| N/A | `real-ip-header` | Sets the value of the [real_ip_header](http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header) directive. | `X-Real-IP`|
3131
| N/A | `real-ip-recursive` | Enables or disables the [real_ip_recursive](http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_recursive) directive. | `False`|
32+
| `nginx.org/server-tokens` | `server-tokens` | Enables or disables the [server_tokens](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens) directive. Additionally, with the NGINX Plus controller, you can specify a custom string value. The empty string value disables the emission of the “Server” field. | `True`|
3233

3334
## Using ConfigMaps
3435

examples/customization/nginx-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ data:
3131
set-real-ip-from: "192.168.192.168" # No default. Sets the value of the set_real_ip_from directive. See http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
3232
real-ip-header: "proxy_protocol" # default is X-Real-IP. Sets the value of the real_ip_header directive. http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header
3333
real-ip-recursive: "True" # default is "False". Enables or disables the real_ip_recursive directive. See http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_recursive
34+
server-tokens: "False" # default is "True". Enables or disables the server_tokens directive. See http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens

nginx-plus-controller/controller/controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ 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+
// not a boolean value. hence, a custom string
367+
cfg.ServerTokens = cfgm.Data["server-tokens"]
368+
} else {
369+
cfg.ServerTokens = "off"
370+
if serverTokens {
371+
cfg.ServerTokens = "on"
372+
}
373+
}
374+
}
375+
364376
if proxyConnectTimeout, exists := cfgm.Data["proxy-connect-timeout"]; exists {
365377
cfg.ProxyConnectTimeout = proxyConnectTimeout
366378
}

nginx-plus-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 string
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: "on",
3840
ProxyConnectTimeout: "60s",
3941
ProxyReadTimeout: "60s",
4042
ClientMaxBodySize: "1m",

nginx-plus-controller/nginx/configurator.go

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

119119
server := Server{
120120
Name: serverName,
121+
ServerTokens: ingCfg.ServerTokens,
121122
HTTP2: ingCfg.HTTP2,
122123
ProxyProtocol: ingCfg.ProxyProtocol,
123124
HSTS: ingCfg.HSTS,
@@ -173,6 +174,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
173174

174175
server := Server{
175176
Name: serverName,
177+
ServerTokens: ingCfg.ServerTokens,
176178
HTTP2: ingCfg.HTTP2,
177179
ProxyProtocol: ingCfg.ProxyProtocol,
178180
HSTS: ingCfg.HSTS,
@@ -208,6 +210,17 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
208210

209211
func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
210212
ingCfg := *cnf.config
213+
if serverTokens, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/server-tokens", ingEx.Ingress); exists {
214+
if err != nil {
215+
// not a boolean value. hence, a custom string
216+
ingCfg.ServerTokens = ingEx.Ingress.Annotations["nginx.org/server-tokens"]
217+
} else {
218+
ingCfg.ServerTokens = "off"
219+
if serverTokens {
220+
ingCfg.ServerTokens = "on"
221+
}
222+
}
223+
}
211224
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
212225
ingCfg.ProxyConnectTimeout = proxyConnectTimeout
213226
}

nginx-plus-controller/nginx/ingress.tmpl

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

23+
server_tokens "{{$server.ServerTokens}}";
24+
2325
{{if $server.Name}}
2426
server_name {{$server.Name}};
2527
{{end}}

nginx-plus-controller/nginx/nginx.go

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

0 commit comments

Comments
 (0)