Skip to content

Commit 7cb91ef

Browse files
authored
Support none keyword in log-format escape (#8692)
* Support none keyword in log-format escape ## What this PR does / why we need it: ingress-nginx does not support disabling escaping of special characters in the nginx log. This PR exposes the setting to support that functionality. ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation only ## Which issue/s this PR fixes <!-- (optional, in `fixes #<issue number>` format, will close that issue when PR gets merged): fixes # --> ## How Has This Been Tested? Followed the [getting-started](https://github.com/kubernetes/ingress-nginx/blob/96b6228a6b65a85e421b8a348a149e99181664d1/docs/developer-guide/getting-started.md) guide. Used ppa:longsleep/golang-backports on WSL Ubuntu to establish a golang-1.18 environment with latest docker and recommended kind. Built the dev-env successfully; had issues with make test, but they are entirely unrelated to anything I touched. Ultimate test was ``` FOCUS=log-format make kind-e2e-test ... Ginkgo ran 1 suite in 6m29.7437865s Test Suite Passed ``` ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] My change requires a change to the documentation. - [x] I have updated the documentation accordingly. - [x] I've read the [CONTRIBUTION](https://github.com/kubernetes/ingress-nginx/blob/main/CONTRIBUTING.md) guide - [x] I have added tests to cover my changes. - [x] All new and existing tests passed. I did not update docs/e2e-tests.md. * gofmt -s ./internal/ingress/controller/config/config.go
1 parent 0b04270 commit 7cb91ef

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

docs/user-guide/nginx-configuration/configmap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The following table shows a configuration option's name, type, and the default v
6464
|[keep-alive](#keep-alive)|int|75|
6565
|[keep-alive-requests](#keep-alive-requests)|int|100|
6666
|[large-client-header-buffers](#large-client-header-buffers)|string|"4 8k"|
67+
|[log-format-escape-none](#log-format-escape-none)|bool|"false"|
6768
|[log-format-escape-json](#log-format-escape-json)|bool|"false"|
6869
|[log-format-upstream](#log-format-upstream)|string|`$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id`|
6970
|[log-format-stream](#log-format-stream)|string|`[$remote_addr] [$time_local] $protocol $status $bytes_sent $bytes_received $session_time`|
@@ -468,6 +469,10 @@ Sets the maximum number and size of buffers used for reading large client reques
468469
_References:_
469470
[https://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers](https://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers)
470471

472+
## log-format-escape-none
473+
474+
Sets if the escape parameter is disabled entirely for character escaping in variables ("true") or controlled by log-format-escape-json ("false") Sets the nginx [log format](https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format).
475+
471476
## log-format-escape-json
472477

473478
Sets if the escape parameter allows JSON ("true") or default characters escaping in variables ("false") Sets the nginx [log format](https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format).

internal/ingress/controller/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ type Configuration struct {
263263
// Default: 4 8k
264264
LargeClientHeaderBuffers string `json:"large-client-header-buffers"`
265265

266+
// Disable all escaping
267+
// http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
268+
LogFormatEscapeNone bool `json:"log-format-escape-none,omitempty"`
269+
266270
// Enable json escaping
267271
// http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
268272
LogFormatEscapeJSON bool `json:"log-format-escape-json,omitempty"`

rootfs/etc/nginx/template/nginx.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ http {
361361
# $ingress_name
362362
# $service_name
363363
# $service_port
364-
log_format upstreaminfo {{ if $cfg.LogFormatEscapeJSON }}escape=json {{ end }}'{{ $cfg.LogFormatUpstream }}';
364+
log_format upstreaminfo {{ if $cfg.LogFormatEscapeNone }}escape=none {{ else if $cfg.LogFormatEscapeJSON }}escape=json {{ end }}'{{ $cfg.LogFormatUpstream }}';
365365

366366
{{/* map urls that should not appear in access.log */}}
367367
{{/* http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log */}}

test/e2e/settings/log-format.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ var _ = framework.DescribeSetting("log-format-*", func() {
3535
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil))
3636
})
3737

38-
ginkgo.Context("Check log-format-escape-json", func() {
38+
ginkgo.Context("Check log-format-escape-json and log-format-escape-none", func() {
3939

40-
ginkgo.It("should disable the log-format-escape-json by default", func() {
40+
ginkgo.It("should not configure log-format escape by default", func() {
4141
f.WaitForNginxConfiguration(
4242
func(cfg string) bool {
43-
return !strings.Contains(cfg, "log_format upstreaminfo escape=json")
43+
return !strings.Contains(cfg, "log_format upstreaminfo escape")
4444
})
4545
})
4646

@@ -59,9 +59,25 @@ var _ = framework.DescribeSetting("log-format-*", func() {
5959
return !strings.Contains(cfg, "log_format upstreaminfo escape=json")
6060
})
6161
})
62+
63+
ginkgo.It("should enable the log-format-escape-none", func() {
64+
f.UpdateNginxConfigMapData("log-format-escape-none", "true")
65+
f.WaitForNginxConfiguration(
66+
func(cfg string) bool {
67+
return strings.Contains(cfg, "log_format upstreaminfo escape=none")
68+
})
69+
})
70+
71+
ginkgo.It("should disable the log-format-escape-none", func() {
72+
f.UpdateNginxConfigMapData("log-format-escape-none", "false")
73+
f.WaitForNginxConfiguration(
74+
func(cfg string) bool {
75+
return !strings.Contains(cfg, "log_format upstreaminfo escape=none")
76+
})
77+
})
6278
})
6379

64-
ginkgo.Context("Check log-format-upstream with log-format-escape-json", func() {
80+
ginkgo.Context("Check log-format-upstream with log-format-escape-json and log-format-escape-none", func() {
6581

6682
ginkgo.It("log-format-escape-json enabled", func() {
6783
f.SetNginxConfigMapData(map[string]string{
@@ -86,27 +102,50 @@ var _ = framework.DescribeSetting("log-format-*", func() {
86102
assert.Contains(ginkgo.GinkgoT(), logs, `{"my_header1":"Here is \"header1\" with json escape", "my_header2":""}`)
87103
})
88104

89-
ginkgo.It("log-format-escape-json disabled", func() {
105+
ginkgo.It("log-format default escape", func() {
90106
f.SetNginxConfigMapData(map[string]string{
91107
"log-format-escape-json": "false",
92108
"log-format-upstream": "\"{\"my_header3\":\"$http_header3\", \"my_header4\":\"$http_header4\"}\"",
93109
})
94110

95111
f.WaitForNginxConfiguration(
96112
func(cfg string) bool {
97-
return !strings.Contains(cfg, "log_format upstreaminfo escape=json")
113+
return !strings.Contains(cfg, "log_format upstreaminfo escape")
114+
})
115+
116+
f.HTTPTestClient().
117+
GET("/").
118+
WithHeader("Host", host).
119+
WithHeader("header3", `Here is "header3" with default escape`).
120+
Expect().
121+
Status(http.StatusOK)
122+
123+
logs, err := f.NginxLogs()
124+
assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs")
125+
assert.Contains(ginkgo.GinkgoT(), logs, `{"my_header3":"Here is \x22header3\x22 with default escape", "my_header4":"-"}`)
126+
})
127+
128+
ginkgo.It("log-format-escape-none enabled", func() {
129+
f.SetNginxConfigMapData(map[string]string{
130+
"log-format-escape-none": "true",
131+
"log-format-upstream": "\"{\"my_header5\":\"$http_header5\", \"my_header6\":\"$http_header6\"}\"",
132+
})
133+
134+
f.WaitForNginxConfiguration(
135+
func(cfg string) bool {
136+
return strings.Contains(cfg, "log_format upstreaminfo escape=none")
98137
})
99138

100139
f.HTTPTestClient().
101140
GET("/").
102141
WithHeader("Host", host).
103-
WithHeader("header3", `Here is "header3" with json escape`).
142+
WithHeader("header5", `Here is "header5" with none escape`).
104143
Expect().
105144
Status(http.StatusOK)
106145

107146
logs, err := f.NginxLogs()
108147
assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs")
109-
assert.Contains(ginkgo.GinkgoT(), logs, `{"my_header3":"Here is \x22header3\x22 with json escape", "my_header4":"-"}`)
148+
assert.Contains(ginkgo.GinkgoT(), logs, `{"my_header5":"Here is "header5" with none escape", "my_header6":""}`)
110149
})
111150
})
112151
})

0 commit comments

Comments
 (0)