|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + netv1 "k8s.io/api/networking/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | + "k8s.io/kubernetes/test/e2e/framework" |
| 14 | + "k8s.io/kubernetes/test/e2e/framework/ingress" |
| 15 | + admissionapi "k8s.io/pod-security-admission/api" |
| 16 | + "net/http" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +var _ = Describe("Ingress tests for OPA filters", func() { |
| 21 | + f := framework.NewDefaultFramework("skipper-ingress-with-opa") |
| 22 | + f.NamespacePodSecurityEnforceLevel = admissionapi.LevelBaseline |
| 23 | + |
| 24 | + var ( |
| 25 | + cs kubernetes.Interface |
| 26 | + jig *ingress.TestJig |
| 27 | + ingressCreate *netv1.Ingress |
| 28 | + ns string |
| 29 | + hostName string |
| 30 | + port int |
| 31 | + serviceName = "aaas-infrastructure-tests" |
| 32 | + opaPolicyName = "aaas-infrastructure-tests" |
| 33 | + ) |
| 34 | + |
| 35 | + BeforeEach(func() { |
| 36 | + jig = ingress.NewIngressTestJig(f.ClientSet) |
| 37 | + cs = f.ClientSet |
| 38 | + ns = f.Namespace.Name |
| 39 | + hostName = fmt.Sprintf("%s-%d.%s", serviceName, time.Now().UTC().Unix(), E2EHostedZone()) |
| 40 | + port = 8080 |
| 41 | + |
| 42 | + ingressCreate = createIngressWithInfo(serviceName, hostName, ns, port, cs, jig) |
| 43 | + }) |
| 44 | + |
| 45 | + It("opaAuthorizeRequest filter should pass through authorized requests [Ingress] [Opa]", func() { |
| 46 | + // Authorization done with the rule |
| 47 | + // https://github.bus.zalan.do/corporate-iam/aaas-infrastructure-tests-policies/blob/main/bundle/policy/ingress/rules.rego |
| 48 | + |
| 49 | + authorizationEnforcedPath := "/auth" |
| 50 | + path := "/" |
| 51 | + ingressRoute := fmt.Sprintf(`authorize: PathRegexp("%s") -> modPath("%s", "%s") -> opaAuthorizeRequest("%s") -> inlineContent("Got response") -> <shunt>;`, authorizationEnforcedPath, authorizationEnforcedPath, path, opaPolicyName) |
| 52 | + ingressUpdate := updateIngressAndWait(serviceName, hostName, path, ingressRoute, port, ingressCreate, cs) |
| 53 | + |
| 54 | + By(fmt.Sprintf("Calling ingress %s/%s we wait to get a 403 with opaAuthorizeRequest %s policy", ingressUpdate.Namespace, ingressUpdate.Name, opaPolicyName)) |
| 55 | + rt, quit := createHTTPRoundTripper() |
| 56 | + defer func() { |
| 57 | + quit <- struct{}{} |
| 58 | + }() |
| 59 | + |
| 60 | + url := "https://" + hostName + authorizationEnforcedPath |
| 61 | + req, err := http.NewRequest("GET", url, nil) |
| 62 | + resp, err := getAndWaitResponse(rt, req, 10*time.Second, http.StatusForbidden) |
| 63 | + framework.ExpectNoError(err) |
| 64 | + Expect(resp.StatusCode).To(Equal(http.StatusForbidden)) |
| 65 | + |
| 66 | + By(fmt.Sprintf("Calling ingress %s/%s we wait to get a 200 with opaAuthorizeRequest %s policy", ingressUpdate.Namespace, ingressUpdate.Name, opaPolicyName)) |
| 67 | + req.Header.Set("Authorization", "Basic valid_token") // Authorized request |
| 68 | + resp, err = getAndWaitResponse(rt, req, 10*time.Second, http.StatusOK) |
| 69 | + framework.ExpectNoError(err) |
| 70 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 71 | + s, err := getBody(resp) |
| 72 | + framework.ExpectNoError(err) |
| 73 | + Expect(s).To(Equal("Got response")) |
| 74 | + }) |
| 75 | + |
| 76 | + It("opaServeResponse filter should return body [Ingress] [Opa]", func() { |
| 77 | + // Authorization done with the rule |
| 78 | + // https://github.bus.zalan.do/corporate-iam/aaas-infrastructure-tests-policies/blob/main/bundle/policy/ingress/permissions.rego |
| 79 | + |
| 80 | + serveResponsePath := "/permissions" |
| 81 | + expectedPermissionsContent := "\"permissions\":{\"permission1\":{},\"permission2\":{}}" |
| 82 | + path := serveResponsePath |
| 83 | + ingressRoute := fmt.Sprintf(`authorize: PathRegexp("%s") -> opaServeResponse("%s") -> <shunt>;`, serveResponsePath, opaPolicyName) |
| 84 | + ingressUpdate := updateIngressAndWait(serviceName, hostName, path, ingressRoute, port, ingressCreate, cs) |
| 85 | + |
| 86 | + By(fmt.Sprintf("Calling ingress %s/%s we wait to get a 403 with opaServeResponse %s policy", ingressUpdate.Namespace, ingressUpdate.Name, opaPolicyName)) |
| 87 | + rt, quit := createHTTPRoundTripper() |
| 88 | + defer func() { |
| 89 | + quit <- struct{}{} |
| 90 | + }() |
| 91 | + |
| 92 | + url := "https://" + hostName + serveResponsePath |
| 93 | + req, err := http.NewRequest("GET", url, nil) |
| 94 | + resp, err := getAndWaitResponse(rt, req, 10*time.Second, http.StatusForbidden) |
| 95 | + framework.ExpectNoError(err) |
| 96 | + Expect(resp.StatusCode).To(Equal(http.StatusForbidden)) |
| 97 | + |
| 98 | + By(fmt.Sprintf("Calling ingress %s/%s we wait to get a 200 with opaServeResponse %s policy", ingressUpdate.Namespace, ingressUpdate.Name, opaPolicyName)) |
| 99 | + req.Header.Set("Authorization", "Basic permissions_token") // Authorized request |
| 100 | + resp, err = getAndWaitResponse(rt, req, 10*time.Second, http.StatusOK) |
| 101 | + framework.ExpectNoError(err) |
| 102 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 103 | + s, err := getBody(resp) |
| 104 | + framework.ExpectNoError(err) |
| 105 | + Expect(s).To(ContainSubstring(expectedPermissionsContent)) |
| 106 | + }) |
| 107 | + |
| 108 | +}) |
| 109 | + |
| 110 | +func createIngressWithInfo(serviceName, hostName, ns string, port int, cs kubernetes.Interface, jig *ingress.TestJig) *netv1.Ingress { |
| 111 | + labels := map[string]string{"app": serviceName} |
| 112 | + waitTime := 10 * time.Minute |
| 113 | + |
| 114 | + // Create initial ingress |
| 115 | + ing := createIngress(serviceName, hostName, ns, "/", netv1.PathTypeImplementationSpecific, labels, nil, port) |
| 116 | + var err error |
| 117 | + ingressCreate, err := cs.NetworkingV1().Ingresses(ns).Create(context.TODO(), ing, metav1.CreateOptions{}) |
| 118 | + framework.ExpectNoError(err) |
| 119 | + |
| 120 | + // Wait for ingress address |
| 121 | + addr, err := jig.WaitForIngressAddress(context.TODO(), cs, ns, ingressCreate.Name, waitTime) |
| 122 | + framework.ExpectNoError(err) |
| 123 | + |
| 124 | + // Ensure ingress exists |
| 125 | + _, err = cs.NetworkingV1().Ingresses(ns).Get(context.TODO(), ing.Name, metav1.GetOptions{ResourceVersion: "0"}) |
| 126 | + framework.ExpectNoError(err) |
| 127 | + |
| 128 | + By("Waiting for LB to create endpoint and skipper route") |
| 129 | + err = waitForResponse(addr, "https", waitTime, isNotFound, true) |
| 130 | + framework.ExpectNoError(err) |
| 131 | + |
| 132 | + return ingressCreate |
| 133 | +} |
| 134 | + |
| 135 | +func updateIngressAndWait(serviceName, hostName, path, ingressRoute string, port int, ingressCreate *netv1.Ingress, cs kubernetes.Interface) *netv1.Ingress { |
| 136 | + updatedIng := updateIngress(ingressCreate.ObjectMeta.Name, |
| 137 | + ingressCreate.ObjectMeta.Namespace, |
| 138 | + hostName, |
| 139 | + serviceName, |
| 140 | + path, |
| 141 | + netv1.PathTypeImplementationSpecific, |
| 142 | + ingressCreate.ObjectMeta.Labels, |
| 143 | + map[string]string{ |
| 144 | + "zalando.org/skipper-routes": ingressRoute, |
| 145 | + }, |
| 146 | + port, |
| 147 | + ) |
| 148 | + ingressUpdate, err := cs.NetworkingV1().Ingresses(ingressCreate.ObjectMeta.Namespace).Update(context.TODO(), updatedIng, metav1.UpdateOptions{}) |
| 149 | + framework.ExpectNoError(err) |
| 150 | + time.Sleep(2 * time.Minute) // wait for routing change propagation |
| 151 | + |
| 152 | + return ingressUpdate |
| 153 | +} |
0 commit comments