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 authconfig hosts when targeting gateway #310

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/authpolicy_authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *AuthPolicyReconciler) desiredAuthConfig(ctx context.Context, ap *api.Au
// that do not have an authpolicy of its own, so we can generate wasm rules for those cases
gw := common.GatewayWrapper{Gateway: obj}
gwHostnames := gw.Hostnames()
if len(hosts) == 0 {
if len(gwHostnames) == 0 {
gwHostnames = []gatewayapiv1.Hostname{"*"}
}
hosts = common.HostnamesToStrings(gwHostnames)
Expand Down
77 changes: 69 additions & 8 deletions controllers/authpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package controllers
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -43,11 +44,7 @@ var _ = Describe("AuthPolicy controller", func() {
err := k8sClient.Create(context.Background(), gateway)
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType)
}, 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testGatewayIsReady(gateway), 15*time.Second, 5*time.Second).Should(BeTrue())

ApplyKuadrantCR(testNamespace)
})
Expand Down Expand Up @@ -116,7 +113,7 @@ var _ = Describe("AuthPolicy controller", func() {
Eventually(func() bool {
err := k8sClient.Get(context.Background(), authConfigKey, authConfig)
logf.Log.V(1).Info("Fetching Authorino's AuthConfig", "key", authConfigKey.String(), "error", err)
return err == nil || authConfig.Status.Ready()
return err == nil && authConfig.Status.Ready()
}, 2*time.Minute, 5*time.Second).Should(BeTrue())
logf.Log.V(1).Info("authConfig.Spec", "hosts", authConfig.Spec.Hosts, "conditions", authConfig.Spec.Conditions)
Expect(authConfig.Spec.Hosts).To(Equal([]string{"*"}))
Expand All @@ -132,6 +129,62 @@ var _ = Describe("AuthPolicy controller", func() {
Expect(authConfig.Spec.Conditions[0].Any[0].Any[0].All[1].Value).To(Equal("/toy.*"))
})

It("Attaches policy to a Gateway with hostname in listeners", func() {
gatewayName := fmt.Sprintf("%s-with-hostnames", testGatewayName)
gateway := testBuildBasicGateway(gatewayName, testNamespace)
Expect(gateway.Spec.Listeners).To(HaveLen(1))
// Set hostname
gateway.Spec.Listeners[0].Hostname = &[]gatewayapiv1.Hostname{"*.example.com"}[0]
err := k8sClient.Create(context.Background(), gateway)
Expect(err).ToNot(HaveOccurred())

Eventually(testGatewayIsReady(gateway), 15*time.Second, 5*time.Second).Should(BeTrue())

routeName := fmt.Sprintf("%s-with-hostnames", testHTTPRouteName)
route := testBuildBasicHttpRoute(routeName, gatewayName, testNamespace, []string{"*.api.example.com"})
err = k8sClient.Create(context.Background(), route)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
existingRoute := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(route), existingRoute)
return err == nil && common.IsHTTPRouteAccepted(existingRoute)
}, 15*time.Second, 5*time.Second).Should(BeTrue())

policy := &api.AuthPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "gw-auth",
Namespace: testNamespace,
},
Spec: api.AuthPolicySpec{
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Kind: "Gateway",
Name: gatewayapiv1.ObjectName(gatewayName),
Namespace: ptr.To(gatewayapiv1.Namespace(testNamespace)),
},
AuthScheme: testBasicAuthScheme(),
},
}

err = k8sClient.Create(context.Background(), policy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

// check policy status
Eventually(testPolicyIsReady(policy), 60*time.Second, 5*time.Second).Should(BeTrue())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhmm, maybe we can't rely on the time frame provided

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check is correct. It was rightfully saying something is wrong.

I missed adding a route. So a policy targeting a gateway without routes do not have any effect and the policy is not ready.


// check authorino authconfig hosts
authConfigKey := types.NamespacedName{Name: authConfigName(client.ObjectKeyFromObject(policy)), Namespace: testNamespace}
authConfig := &authorinoapi.AuthConfig{}
Eventually(func() bool {
err := k8sClient.Get(context.Background(), authConfigKey, authConfig)
logf.Log.V(1).Info("Fetching Authorino's AuthConfig", "key", authConfigKey.String(), "error", err)
return err == nil && authConfig.Status.Ready()
}, 2*time.Minute, 5*time.Second).Should(BeTrue())

Expect(authConfig.Spec.Hosts).To(ConsistOf("*.example.com"))
})

It("Attaches policy to the HTTPRoute", func() {
policy := &api.AuthPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -275,7 +328,7 @@ var _ = Describe("AuthPolicy controller", func() {
Eventually(func() bool {
err := k8sClient.Get(context.Background(), authConfigKey, authConfig)
logf.Log.V(1).Info("Fetching Authorino's AuthConfig", "key", authConfigKey.String(), "error", err)
return err == nil || authConfig.Status.Ready()
return err == nil && authConfig.Status.Ready()
}, 2*time.Minute, 5*time.Second).Should(BeTrue())
logf.Log.V(1).Info("authConfig.Spec", "hosts", authConfig.Spec.Hosts, "conditions", authConfig.Spec.Conditions)
Expect(authConfig.Spec.Hosts).To(Equal([]string{"*"}))
Expand Down Expand Up @@ -844,7 +897,7 @@ var _ = Describe("AuthPolicy controller", func() {
Eventually(func() bool {
err := k8sClient.Get(context.Background(), authConfigKey, authConfig)
logf.Log.V(1).Info("Fetching Authorino's AuthConfig", "key", authConfigKey.String(), "error", err)
return err == nil || authConfig.Status.Ready()
return err == nil && authConfig.Status.Ready()
}, 2*time.Minute, 5*time.Second).Should(BeTrue())
logf.Log.V(1).Info("authConfig.Spec", "hosts", authConfig.Spec.Hosts, "conditions", authConfig.Spec.Conditions)
Expect(authConfig.Spec.Hosts).To(Equal([]string{"*.toystore.com", "*.admin.toystore.com"}))
Expand Down Expand Up @@ -1246,6 +1299,14 @@ func testBasicAuthScheme() api.AuthSchemeSpec {
}
}

func testGatewayIsReady(gateway *gatewayapiv1.Gateway) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType)
}
}

func testPolicyIsReady(policy *api.AuthPolicy) func() bool {
return func() bool {
existingPolicy := &api.AuthPolicy{}
Expand Down
Loading