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

The nginx.ingress.kubernetes.io/server-snippet annotation functionality is not working properly. #12625

Closed
SY16399 opened this issue Jan 6, 2025 · 5 comments
Labels
needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-priority needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@SY16399
Copy link

SY16399 commented Jan 6, 2025

echo "
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: snippet.example.com
namespace: default
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
set $agentflag 0;
if ($http_user_agent ~* "(Android|IPhone)") {
set $agentflag 1;
}
if ($agentflag = 1) {
return 302 http://www.baidu.com;
}
spec:
rules:
- host: snippet.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: snippet
port:
number: 80
" | kubectl apply -f -

@SY16399 SY16399 added the kind/bug Categorizes issue or PR as related to a bug. label Jan 6, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jan 6, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If Ingress contributors determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-sigs/prow repository.

@longwuyuan
Copy link
Contributor

/remove-kind bug

The information you have provided does not show any problem.
I think you should look at the template of a new bug report and then edit this issue description to answer those questions.

The request you sent, the config of the controller, the config of the ingress, the logs of the controller, the result of the curl request with -i and -v, are some of the example of information that helps a reader get some idea of what problem has to be solved.

@k8s-ci-robot k8s-ci-robot added needs-kind Indicates a PR lacks a `kind/foo` label and requires one. and removed kind/bug Categorizes issue or PR as related to a bug. labels Jan 6, 2025
@MattiDeGrauwe
Copy link

MattiDeGrauwe commented Jan 6, 2025

I might have a similar issue, the following snippet is not allowed since 4.12.0

nginx.ingress.kubernetes.io/server-snippet: |
      add_header 'Access-Control-Allow-Origin' $http_origin always;
      add_header 'Access-Control-Allow-Credentials' 'true' always;
      add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Origin, Content-Type, Accept' always;
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

ingress-nginx is deployed through the chart and validations have been set to false:

controller:
      enableAnnotationValidations: false

resulting in the following pod arguments:

- args:
        - /nginx-ingress-controller
        - --enable-annotation-validation=false
        - --default-backend-service=$(POD_NAMESPACE)/ingress-nginx-defaultbackend
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
        - --election-id=ingress-nginx-leader
        - --controller-class=k8s.io/ingress-nginx
        - --ingress-class=nginx
        - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
        - --validating-webhook=:8443
        - --validating-webhook-certificate=/usr/local/certificates/cert
        - --validating-webhook-key=/usr/local/certificates/key
        - --enable-metrics=true
        - --configmap=infra/nginx-ingress-default-configmap
        - --default-ssl-certificate=infra/tools-cert-tls

Most of our ingresses broke since 4.12 due to this snippet (resulting in the following error):

Error from server (BadRequest): error when replacing "/tmp/kubectl-edit-3494057835.yaml": admission webhook "validate.nginx.ingress.kubernetes.io" denied the request: annotation group ServerSnippet contains risky annotation based on ingress configuration

Should I dig deeper or is this a known issue @longwuyuan ?

@longwuyuan
Copy link
Contributor

Can you play with the risk settings

@Gacko
Copy link
Member

Gacko commented Jan 6, 2025

Snippet annotations have annotation risk level Critical. The highest accepted risk level got lowered to High with v1.12. Please read the release notes for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-priority needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
Development

No branches or pull requests

5 participants