|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. 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 | +package v1 |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 28 | + |
| 29 | + apisixv2 "github.com/apache/apisix-ingress-controller/api/v2" |
| 30 | +) |
| 31 | + |
| 32 | +func buildApisixConsumerValidator(t *testing.T, objects ...runtime.Object) *ApisixConsumerCustomValidator { |
| 33 | + t.Helper() |
| 34 | + |
| 35 | + scheme := runtime.NewScheme() |
| 36 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 37 | + require.NoError(t, apisixv2.AddToScheme(scheme)) |
| 38 | + |
| 39 | + builder := fake.NewClientBuilder().WithScheme(scheme) |
| 40 | + if len(objects) > 0 { |
| 41 | + builder = builder.WithRuntimeObjects(objects...) |
| 42 | + } |
| 43 | + |
| 44 | + return NewApisixConsumerCustomValidator(builder.Build()) |
| 45 | +} |
| 46 | + |
| 47 | +func TestApisixConsumerValidator_MissingBasicAuthSecret(t *testing.T) { |
| 48 | + consumer := &apisixv2.ApisixConsumer{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: "demo", |
| 51 | + Namespace: "default", |
| 52 | + }, |
| 53 | + Spec: apisixv2.ApisixConsumerSpec{ |
| 54 | + AuthParameter: apisixv2.ApisixConsumerAuthParameter{ |
| 55 | + BasicAuth: &apisixv2.ApisixConsumerBasicAuth{ |
| 56 | + SecretRef: &corev1.LocalObjectReference{Name: "basic-auth"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + validator := buildApisixConsumerValidator(t) |
| 63 | + |
| 64 | + warnings, err := validator.ValidateCreate(context.Background(), consumer) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Equal(t, 1, len(warnings)) |
| 67 | + require.Equal(t, "Referenced Secret 'default/basic-auth' not found", warnings[0]) |
| 68 | +} |
| 69 | + |
| 70 | +func TestApisixConsumerValidator_MultipleSecretWarnings(t *testing.T) { |
| 71 | + consumer := &apisixv2.ApisixConsumer{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: "demo", |
| 74 | + Namespace: "default", |
| 75 | + }, |
| 76 | + Spec: apisixv2.ApisixConsumerSpec{ |
| 77 | + AuthParameter: apisixv2.ApisixConsumerAuthParameter{ |
| 78 | + BasicAuth: &apisixv2.ApisixConsumerBasicAuth{ |
| 79 | + SecretRef: &corev1.LocalObjectReference{Name: "basic-auth"}, |
| 80 | + }, |
| 81 | + JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ |
| 82 | + SecretRef: &corev1.LocalObjectReference{Name: "jwt-auth"}, |
| 83 | + }, |
| 84 | + HMACAuth: &apisixv2.ApisixConsumerHMACAuth{ |
| 85 | + SecretRef: &corev1.LocalObjectReference{Name: "hmac-auth"}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + basicAuthSecret := &corev1.Secret{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: "basic-auth", |
| 94 | + Namespace: "default", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + validator := buildApisixConsumerValidator(t, basicAuthSecret) |
| 99 | + |
| 100 | + warnings, err := validator.ValidateCreate(context.Background(), consumer) |
| 101 | + require.NoError(t, err) |
| 102 | + require.Len(t, warnings, 2) |
| 103 | + require.ElementsMatch(t, []string{ |
| 104 | + "Referenced Secret 'default/jwt-auth' not found", |
| 105 | + "Referenced Secret 'default/hmac-auth' not found", |
| 106 | + }, warnings) |
| 107 | +} |
| 108 | + |
| 109 | +func TestApisixConsumerValidator_NoWarningsWhenSecretsExist(t *testing.T) { |
| 110 | + consumer := &apisixv2.ApisixConsumer{ |
| 111 | + ObjectMeta: metav1.ObjectMeta{ |
| 112 | + Name: "demo", |
| 113 | + Namespace: "default", |
| 114 | + }, |
| 115 | + Spec: apisixv2.ApisixConsumerSpec{ |
| 116 | + AuthParameter: apisixv2.ApisixConsumerAuthParameter{ |
| 117 | + KeyAuth: &apisixv2.ApisixConsumerKeyAuth{ |
| 118 | + SecretRef: &corev1.LocalObjectReference{Name: "key-auth"}, |
| 119 | + }, |
| 120 | + WolfRBAC: &apisixv2.ApisixConsumerWolfRBAC{ |
| 121 | + SecretRef: &corev1.LocalObjectReference{Name: "wolf-rbac"}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + secrets := []runtime.Object{ |
| 128 | + &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "key-auth", Namespace: "default"}}, |
| 129 | + &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "wolf-rbac", Namespace: "default"}}, |
| 130 | + } |
| 131 | + |
| 132 | + validator := buildApisixConsumerValidator(t, secrets...) |
| 133 | + |
| 134 | + warnings, err := validator.ValidateCreate(context.Background(), consumer) |
| 135 | + require.NoError(t, err) |
| 136 | + require.Empty(t, warnings) |
| 137 | +} |
0 commit comments