|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux 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 auth_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + |
| 24 | + "github.com/fluxcd/pkg/auth" |
| 25 | +) |
| 26 | + |
| 27 | +func TestInconsistentObjectLevelConfiguration(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + featureGateEnabled bool |
| 31 | + defaultServiceAccount string |
| 32 | + defaultKubeConfigServiceAccount string |
| 33 | + defaultDecryptionServiceAccount string |
| 34 | + expectInconsistent bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "feature gate enabled, no default service accounts", |
| 38 | + featureGateEnabled: true, |
| 39 | + expectInconsistent: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "feature gate enabled, default service account set", |
| 43 | + featureGateEnabled: true, |
| 44 | + defaultServiceAccount: "test-sa", |
| 45 | + expectInconsistent: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "feature gate enabled, default kubeconfig service account set", |
| 49 | + featureGateEnabled: true, |
| 50 | + defaultKubeConfigServiceAccount: "test-kubeconfig-sa", |
| 51 | + expectInconsistent: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "feature gate enabled, default decryption service account set", |
| 55 | + featureGateEnabled: true, |
| 56 | + defaultDecryptionServiceAccount: "test-decryption-sa", |
| 57 | + expectInconsistent: false, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "feature gate disabled, no default service accounts", |
| 61 | + featureGateEnabled: false, |
| 62 | + expectInconsistent: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "feature gate disabled, default service account set", |
| 66 | + featureGateEnabled: false, |
| 67 | + defaultServiceAccount: "test-sa", |
| 68 | + expectInconsistent: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "feature gate disabled, default kubeconfig service account set", |
| 72 | + featureGateEnabled: false, |
| 73 | + defaultKubeConfigServiceAccount: "test-kubeconfig-sa", |
| 74 | + expectInconsistent: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "feature gate disabled, default decryption service account set", |
| 78 | + featureGateEnabled: false, |
| 79 | + defaultDecryptionServiceAccount: "test-decryption-sa", |
| 80 | + expectInconsistent: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "feature gate disabled, all default service accounts set", |
| 84 | + featureGateEnabled: false, |
| 85 | + defaultServiceAccount: "test-sa", |
| 86 | + defaultKubeConfigServiceAccount: "test-kubeconfig-sa", |
| 87 | + defaultDecryptionServiceAccount: "test-decryption-sa", |
| 88 | + expectInconsistent: true, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tt := range tests { |
| 93 | + t.Run(tt.name, func(t *testing.T) { |
| 94 | + g := NewWithT(t) |
| 95 | + |
| 96 | + if tt.featureGateEnabled { |
| 97 | + auth.EnableObjectLevelWorkloadIdentity() |
| 98 | + } |
| 99 | + |
| 100 | + auth.SetDefaultServiceAccount(tt.defaultServiceAccount) |
| 101 | + auth.SetDefaultKubeConfigServiceAccount(tt.defaultKubeConfigServiceAccount) |
| 102 | + auth.SetDefaultDecryptionServiceAccount(tt.defaultDecryptionServiceAccount) |
| 103 | + |
| 104 | + t.Cleanup(func() { |
| 105 | + auth.SetDefaultServiceAccount("") |
| 106 | + auth.SetDefaultKubeConfigServiceAccount("") |
| 107 | + auth.SetDefaultDecryptionServiceAccount("") |
| 108 | + auth.DisableObjectLevelWorkloadIdentity() |
| 109 | + }) |
| 110 | + |
| 111 | + result := auth.InconsistentObjectLevelConfiguration() |
| 112 | + g.Expect(result).To(Equal(tt.expectInconsistent)) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments