-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Allow overriding real_ip_recursive #8366
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
Open
tomaspinho
wants to merge
7
commits into
kubernetes:main
Choose a base branch
from
tomaspinho:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcab7c1
adds the enable-real-ip-recursive allowing control over real_ip_recur…
tomaspinho 8a268e4
documents new enable-real-ip-recursive configmap setting
tomaspinho ae7fee9
add e2e test for enable-real-ip-recursive
tomaspinho 93f0afc
capitalizes IP in EnableRealIPRecursive to match upstream changes
tomaspinho fbb2c3b
appease the linter by promoting re-used header name to const and remo…
tomaspinho 5b21caf
nit: fix copyright year
tomaspinho 6d613e7
fix: make EnableRealIPRecursive default to false
tomaspinho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| Copyright 2017 The Kubernetes Authors. | ||
tomaspinho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package settings | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/onsi/ginkgo" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "k8s.io/ingress-nginx/test/e2e/framework" | ||
| ) | ||
|
|
||
| const forwardedForHost = "forwarded-for-header" | ||
|
|
||
| var _ = framework.DescribeSetting("enable-real-ip-recursive", func() { | ||
| f := framework.NewDefaultFramework("enable-real-ip-recursive") | ||
|
|
||
| setting := "enable-real-ip-recursive" | ||
|
|
||
| ginkgo.BeforeEach(func() { | ||
| f.NewEchoDeployment() | ||
|
|
||
| f.SetNginxConfigMapData(map[string]string{ | ||
| "log-format-escape-json": "true", | ||
| "log-format-upstream": "clientip=\"$remote_addr\"", | ||
| "use-forwarded-headers": "true", | ||
| setting: "false", | ||
| }) | ||
| }) | ||
|
|
||
| ginkgo.It("should use the first IP in X-Forwarded-For header when setting is true", func() { | ||
| host := forwardedForHost | ||
|
|
||
| f.UpdateNginxConfigMapData(setting, "true") | ||
|
|
||
| ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil) | ||
| f.EnsureIngress(ing) | ||
|
|
||
| f.WaitForNginxConfiguration(func(conf string) bool { | ||
| return strings.Contains(conf, "real_ip_recursive on;") | ||
| }) | ||
|
|
||
| ginkgo.By("ensuring single values are parsed correctly") | ||
| body := f.HTTPTestClient(). | ||
| GET("/"). | ||
| WithHeader("Host", host). | ||
| WithHeader("X-Forwarded-For", "127.0.0.1, 1.2.3.4"). | ||
| Expect(). | ||
| Status(http.StatusOK). | ||
| Body(). | ||
| Raw() | ||
|
|
||
| assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-for=127.0.0.1") | ||
|
|
||
| logs, err := f.NginxLogs() | ||
| assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs") | ||
| assert.Contains(ginkgo.GinkgoT(), logs, "clientip=\"127.0.0.1\"") | ||
| }) | ||
|
|
||
| ginkgo.It("should use the last IP in X-Forwarded-For header when setting is false", func() { | ||
| host := forwardedForHost | ||
|
|
||
| f.UpdateNginxConfigMapData(setting, "false") | ||
|
|
||
| f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil)) | ||
|
|
||
| f.WaitForNginxConfiguration(func(conf string) bool { | ||
| return strings.Contains(conf, "real_ip_recursive off;") | ||
| }) | ||
|
|
||
| body := f.HTTPTestClient(). | ||
| GET("/"). | ||
| WithHeader("Host", host). | ||
| WithHeader("X-Forwarded-For", "127.0.0.1, 1.2.3.4"). | ||
| Expect(). | ||
| Status(http.StatusOK). | ||
| Body(). | ||
| Raw() | ||
|
|
||
| assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-for=1.2.3.4") | ||
|
|
||
| logs, err := f.NginxLogs() | ||
| assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs") | ||
| assert.Contains(ginkgo.GinkgoT(), logs, "clientip=\"1.2.3.4\"") | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be disabled by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a "problem" with this approach of a flag defaulted to true: if you set it as false on your configmap, it may or may not represent as empty (depending on the marshaller, eg using omitempty on json flag) so because it is empty it will be defaulted to...true :)
To confirm this behavior, can you add some e2e test with both EnableRealIPRecursive = true or false on the configmap, and check if the template is rendered correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rikatz ,
I've set the default to
false. My initial reasoning for it to betruewas that I think the more secure option should be enabled by default if it doesn't otherwise impact any functionality. Because the setting itself is dependent onEnableRealIpbeing set totrue, the more secure configuration would be the default whenEnableRealIpwould be used. This is a taste thing, and not a deal breaker for getting this merged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nginx default is off, so let's set this to false.