|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package controller |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 32 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 33 | + "sigs.k8s.io/gateway-api/apis/v1beta1" |
| 34 | + |
| 35 | + "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 36 | + "github.com/apache/apisix-ingress-controller/internal/provider" |
| 37 | +) |
| 38 | + |
| 39 | +const ( |
| 40 | + consumerNS = "team-a" |
| 41 | + secretNS = "team-b" |
| 42 | +) |
| 43 | + |
| 44 | +func buildConsumerReconciler(t *testing.T, objs ...runtime.Object) *ConsumerReconciler { |
| 45 | + t.Helper() |
| 46 | + |
| 47 | + scheme := runtime.NewScheme() |
| 48 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 49 | + require.NoError(t, v1alpha1.AddToScheme(scheme)) |
| 50 | + require.NoError(t, gatewayv1.Install(scheme)) |
| 51 | + require.NoError(t, v1beta1.Install(scheme)) |
| 52 | + |
| 53 | + cli := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build() |
| 54 | + return &ConsumerReconciler{Client: cli, Log: logr.Discard()} |
| 55 | +} |
| 56 | + |
| 57 | +func crossNamespaceConsumer() *v1alpha1.Consumer { |
| 58 | + target := secretNS |
| 59 | + return &v1alpha1.Consumer{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{Name: "attacker", Namespace: consumerNS}, |
| 61 | + Spec: v1alpha1.ConsumerSpec{ |
| 62 | + Credentials: []v1alpha1.Credential{{ |
| 63 | + Name: "cred", |
| 64 | + Type: "key-auth", |
| 65 | + SecretRef: &v1alpha1.SecretReference{Name: "victim-secret", Namespace: &target}, |
| 66 | + }}, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func victimSecret() *corev1.Secret { |
| 72 | + return &corev1.Secret{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{Name: "victim-secret", Namespace: secretNS}, |
| 74 | + Data: map[string][]byte{"key": []byte("victim-key")}, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func secretGrant() *v1beta1.ReferenceGrant { |
| 79 | + return &v1beta1.ReferenceGrant{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{Name: "allow-consumer", Namespace: secretNS}, |
| 81 | + Spec: v1beta1.ReferenceGrantSpec{ |
| 82 | + From: []v1beta1.ReferenceGrantFrom{{ |
| 83 | + Group: v1beta1.Group(v1alpha1.GroupVersion.Group), |
| 84 | + Kind: "Consumer", |
| 85 | + Namespace: consumerNS, |
| 86 | + }}, |
| 87 | + To: []v1beta1.ReferenceGrantTo{{ |
| 88 | + Group: "", |
| 89 | + Kind: "Secret", |
| 90 | + }}, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Without a ReferenceGrant the cross-namespace secret must not be bound. |
| 96 | +func TestProcessSpec_CrossNamespaceSecretRef_DeniedWithoutGrant(t *testing.T) { |
| 97 | + SetEnableReferenceGrant(true) |
| 98 | + defer SetEnableReferenceGrant(false) |
| 99 | + |
| 100 | + r := buildConsumerReconciler(t, victimSecret()) |
| 101 | + consumer := crossNamespaceConsumer() |
| 102 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 103 | + |
| 104 | + err := r.processSpec(context.Background(), tctx, consumer) |
| 105 | + require.Error(t, err) |
| 106 | + require.Empty(t, tctx.Secrets, "foreign secret must not be loaded without a ReferenceGrant") |
| 107 | +} |
| 108 | + |
| 109 | +// A matching ReferenceGrant permits the cross-namespace secret. |
| 110 | +func TestProcessSpec_CrossNamespaceSecretRef_AllowedWithGrant(t *testing.T) { |
| 111 | + SetEnableReferenceGrant(true) |
| 112 | + defer SetEnableReferenceGrant(false) |
| 113 | + |
| 114 | + r := buildConsumerReconciler(t, victimSecret(), secretGrant()) |
| 115 | + consumer := crossNamespaceConsumer() |
| 116 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 117 | + |
| 118 | + err := r.processSpec(context.Background(), tctx, consumer) |
| 119 | + require.NoError(t, err) |
| 120 | + require.Contains(t, tctx.Secrets, types.NamespacedName{Namespace: secretNS, Name: "victim-secret"}) |
| 121 | +} |
| 122 | + |
| 123 | +// Same-namespace SecretRef needs no grant. |
| 124 | +func TestProcessSpec_SameNamespaceSecretRef_Allowed(t *testing.T) { |
| 125 | + SetEnableReferenceGrant(true) |
| 126 | + defer SetEnableReferenceGrant(false) |
| 127 | + |
| 128 | + secret := &corev1.Secret{ |
| 129 | + ObjectMeta: metav1.ObjectMeta{Name: "local-secret", Namespace: consumerNS}, |
| 130 | + Data: map[string][]byte{"key": []byte("local-key")}, |
| 131 | + } |
| 132 | + r := buildConsumerReconciler(t, secret) |
| 133 | + consumer := &v1alpha1.Consumer{ |
| 134 | + ObjectMeta: metav1.ObjectMeta{Name: "local", Namespace: consumerNS}, |
| 135 | + Spec: v1alpha1.ConsumerSpec{ |
| 136 | + Credentials: []v1alpha1.Credential{{ |
| 137 | + Name: "cred", |
| 138 | + Type: "key-auth", |
| 139 | + SecretRef: &v1alpha1.SecretReference{Name: "local-secret"}, |
| 140 | + }}, |
| 141 | + }, |
| 142 | + } |
| 143 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 144 | + |
| 145 | + err := r.processSpec(context.Background(), tctx, consumer) |
| 146 | + require.NoError(t, err) |
| 147 | + require.Contains(t, tctx.Secrets, types.NamespacedName{Namespace: consumerNS, Name: "local-secret"}) |
| 148 | +} |
0 commit comments