Skip to content
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

fix newlines location denied #3130

Merged

Conversation

alanbover
Copy link
Contributor

What this PR does / why we need it:
Escape '\n' simbols when logging 'Location denied' error in nginx configuration. Without it, nginx completelly breaks and it's unable to generate new configurations affecting other possible tenants in the cluster.

Which issue this PR fixes
We create an ingress object with an incorrect ip-whitelisting annotation. For example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.ingress.kubernetes.io/whitelist-source-range: |
       # This comment breaks ingress
       8.8.8.8/32,
       9.9.9.9/32
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee
        backend:
          serviceName: coffee-svc
          servicePort: 80

Ingress will start failing with an error:

E0921 09:40:26.453561       8 controller.go:183] Unexpected failure reloading the backend:-------------------------------------------------------------------------------
Error: exit status 1
2018/09/21 09:40:26 [emerg] 660#660: unknown directive "8.8.8.8/32" in /tmp/nginx-cfg534865713:414
nginx: [emerg] unknown directive "8.8.8.8/32" in /tmp/nginx-cfg534865713:414
nginx: configuration file /tmp/nginx-cfg534865713 test failed-------------------------------------------------------------------------------
W0921 09:40:26.453594       8 queue.go:130] requeuing default/cafe-ingress, err 

Because it's generating an incorrect location on nginx:

 location /tea {

                        set $namespace      "default";
                        set $ingress_name   "cafe-ingress";
                        set $service_name   "tea-svc";
                        set $service_port   "80";
                        set $location_path  "/tea";

                        rewrite_by_lua_block {

                                balancer.rewrite()

                        }

                        log_by_lua_block {

                                balancer.log()

                                monitor.call()
                        }

                        port_in_redirect off;

                        set $proxy_upstream_name "default-tea-svc-80";

                        # Location denied. Reason: the annotation does not contain a valid IP address or network: invalid CIDR address: # This should make it fail
                        8.8.8.8/32
                        return 503;

                }

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 25, 2018
@alanbover
Copy link
Contributor Author

Contributor License Agreement (CLA) should be signed now

@alanbover alanbover force-pushed the fix/newlines_location_denied branch from efe0df0 to 6454608 Compare September 25, 2018 13:36
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Sep 25, 2018
@antoineco
Copy link
Contributor

antoineco commented Sep 25, 2018

@alanbover this sounds like an input issue to me.

  1. The annotation does not support comments, the input must be a comma separated list with nothing else.
  2. Please use the > syntax instead of |, this effectively ignores new lines in YAML.

With that being said, this PR is only about preventing formatting issues, right? If yes, to my knowledge %q does not suppress newlines.

@alanbover
Copy link
Contributor Author

@antoineco ,

Yes, I know the input is incorrect. In fact the PR, as you said, is not about making nginx support comments or new lines, but to prevent it from explote.

This is what you get as result with this PR (tested locally on minikube):

		location /coffee {
			
			set $namespace      "default";
			set $ingress_name   "cafe-ingress2";
			set $service_name   "coffee-svc";
			set $service_port   "80";
			set $location_path  "/coffee";
			
			rewrite_by_lua_block {
				
				balancer.rewrite()
				
			}
			
			log_by_lua_block {
				
				balancer.log()
				
				monitor.call()
			}
			
			if ($scheme = https) {
				more_set_headers                        "Strict-Transport-Security: max-age=15724800; includeSubDomains";
			}
			
			port_in_redirect off;
			
			set $proxy_upstream_name "default-coffee-svc-80";
			
			# enforce ssl on server side
			if ($redirect_to_https) {
				
				return 308 https://$best_http_host$request_uri;
				
			}
			
			# Location denied. Reason: "the annotation does not contain a valid IP address or network: invalid CIDR address: # This comment breaks ingress\n8.8.8.8"
			return 503;
			
		}

As you can see, the \n are now escaped. From https://golang.org/pkg/fmt/:

%q	a single-quoted character literal safely escaped with Go syntax.

@antoineco
Copy link
Contributor

antoineco commented Sep 25, 2018

@alanbover Alright, thanks for the confirmation. And you're right about %q 👍 New lines are converted to a litteral \n.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 25, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alanbover, antoineco

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 25, 2018
@k8s-ci-robot k8s-ci-robot merged commit c4a562d into kubernetes:master Sep 25, 2018
@alanbover
Copy link
Contributor Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants