From 89758007405c6489b8a06294d3eb81d9b7649f12 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Thu, 18 Jan 2018 16:37:22 -0200 Subject: [PATCH] Add support to hide headers from upstream servers (#1928) --- docs/user-guide/configmap.md | 8 +++++++- internal/ingress/controller/config/config.go | 5 +++++ internal/ingress/controller/template/configmap.go | 8 ++++++++ rootfs/etc/nginx/template/nginx.tmpl | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/user-guide/configmap.md b/docs/user-guide/configmap.md index 9113570966..c90f7315b9 100644 --- a/docs/user-guide/configmap.md +++ b/docs/user-guide/configmap.md @@ -21,6 +21,7 @@ The following table shows a configuration option's name, type, and the default v |:---|:---|:------| |[add‑headers](#add-headers)|string|""| |[allow‑backend‑server‑header](#allow-backend-server-header)|bool|false| +|[hide‑headers‑](#hide-headers)|string array|empty| |[access‑log‑path](#access-log-path)|string|"/var/log/nginx/access.log"| |[error‑log‑path](#error-log-path)|string|"/var/log/nginx/error.log"| |[enable‑dynamic‑tls‑records](#enable-dynamic-tls-records)|bool|true| @@ -126,7 +127,12 @@ Sets custom headers from named configmap before sending traffic to the client. S ## allow-backend-server-header -AllowBackendServerHeader enables the return of the header Server from the backend instead of the generic nginx string. By default this is disabled. +Enables the return of the header Server from the backend instead of the generic nginx string. By default this is disabled. + +## hide-headers + +Sets additional header that will not be passed from the upstream server to the client response. +Default: empty _References:_ - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index 116aca7604..e35ce77e7d 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -462,6 +462,11 @@ type Configuration struct { // Default: false // Reason for the default: https://trac.nginx.org/nginx/ticket/1300 ReusePort bool `json:"reuse-port"` + + // HideHeaders sets additional header that will not be passed from the upstream + // server to the client response + // Default: empty + HideHeaders []string `json:"hide-headers"` } // NewDefault returns the default nginx configuration diff --git a/internal/ingress/controller/template/configmap.go b/internal/ingress/controller/template/configmap.go index 5a65364e1a..68a88a3b18 100644 --- a/internal/ingress/controller/template/configmap.go +++ b/internal/ingress/controller/template/configmap.go @@ -38,6 +38,7 @@ const ( bindAddress = "bind-address" httpRedirectCode = "http-redirect-code" proxyStreamResponses = "proxy-stream-responses" + hideHeaders = "hide-headers" ) var ( @@ -56,6 +57,8 @@ func ReadConfig(src map[string]string) config.Configuration { skipUrls := make([]string, 0) whitelist := make([]string, 0) proxylist := make([]string, 0) + hideHeaderslist := make([]string, 0) + bindAddressIpv4List := make([]string, 0) bindAddressIpv6List := make([]string, 0) redirectCode := 308 @@ -71,6 +74,10 @@ func ReadConfig(src map[string]string) config.Configuration { } } } + if val, ok := conf[hideHeaders]; ok { + delete(conf, hideHeaders) + hideHeaderslist = strings.Split(val, ",") + } if val, ok := conf[skipAccessLogUrls]; ok { delete(conf, skipAccessLogUrls) skipUrls = strings.Split(val, ",") @@ -133,6 +140,7 @@ func ReadConfig(src map[string]string) config.Configuration { to.ProxyRealIPCIDR = proxylist to.BindAddressIpv4 = bindAddressIpv4List to.BindAddressIpv6 = bindAddressIpv6List + to.HideHeaders = hideHeaderslist to.HTTPRedirectCode = redirectCode to.ProxyStreamResponses = streamResponses diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index d33dfee152..c9f1323dc2 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -290,6 +290,9 @@ http { proxy_pass_header Server; {{ end }} + {{ range $header := $cfg.HideHeaders }}proxy_hide_header {{ $header }}; + {{ end }} + {{ if not (empty $cfg.HTTPSnippet) }} # Custom code snippet configured in the configuration configmap {{ $cfg.HTTPSnippet }}