|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package annotations |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/onsi/ginkgo/v2" |
| 24 | + |
| 25 | + "k8s.io/ingress-nginx/test/e2e/framework" |
| 26 | +) |
| 27 | + |
| 28 | +var _ = framework.DescribeAnnotation("limit-except", func() { |
| 29 | + f := framework.NewDefaultFramework("limitexcept") |
| 30 | + |
| 31 | + ginkgo.BeforeEach(func() { |
| 32 | + f.NewEchoDeployment() |
| 33 | + }) |
| 34 | + |
| 35 | + ginkgo.It("should return 403 when the request is not allowed", func() { |
| 36 | + host := "foo.com" |
| 37 | + |
| 38 | + annotations := map[string]string{ |
| 39 | + "nginx.ingress.kubernetes.io/server-snippet": ` |
| 40 | + location = / { |
| 41 | + return 403; |
| 42 | + }`, |
| 43 | + "nginx.ingress.kubernetes.io/configuration-snippet": ` |
| 44 | + limit_except GET { |
| 45 | + deny all; |
| 46 | + }`, |
| 47 | + } |
| 48 | + |
| 49 | + ing := framework.NewSingleIngress(host, "/foo", host, f.Namespace, framework.EchoService, 80, annotations) |
| 50 | + f.EnsureIngress(ing) |
| 51 | + |
| 52 | + f.WaitForNginxServer(host, |
| 53 | + func(server string) bool { |
| 54 | + return strings.Contains(server, `location = / { return 403; }`) && |
| 55 | + strings.Contains(server, `limit_except GET { deny all; }`) |
| 56 | + }) |
| 57 | + |
| 58 | + ginkgo.By("sending request to foo.com") |
| 59 | + f.HTTPTestClient(). |
| 60 | + POST("/"). |
| 61 | + WithHeader("Host", host). |
| 62 | + Expect(). |
| 63 | + Status(http.StatusForbidden) |
| 64 | + |
| 65 | + f.HTTPTestClient(). |
| 66 | + GET("/"). |
| 67 | + WithHeader("Host", host). |
| 68 | + Expect(). |
| 69 | + Status(http.StatusForbidden) |
| 70 | + |
| 71 | + f.HTTPTestClient(). |
| 72 | + POST("/foo"). |
| 73 | + WithHeader("Host", host). |
| 74 | + Expect(). |
| 75 | + Status(http.StatusForbidden) |
| 76 | + |
| 77 | + f.HTTPTestClient(). |
| 78 | + GET("/foo"). |
| 79 | + WithHeader("Host", host). |
| 80 | + Expect(). |
| 81 | + Status(http.StatusOK) |
| 82 | + }) |
| 83 | +}) |
0 commit comments