Skip to content

Commit ca950d1

Browse files
authored
Merge pull request #1011 from cappyzawa/feat/object-level-config-validation
auth: add object-level configuration validation
2 parents 74ade96 + 524b599 commit ca950d1

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

auth/controller_options.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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
18+
19+
import (
20+
"fmt"
21+
)
22+
23+
// ErrInconsistentObjectLevelConfiguration is used when the controller has
24+
// an inconsistent object-level workload identity configuration.
25+
var ErrInconsistentObjectLevelConfiguration = fmt.Errorf(
26+
"cannot set default service accounts when the feature gate %s is not enabled",
27+
FeatureGateObjectLevelWorkloadIdentity)
28+
29+
// InconsistentObjectLevelConfiguration checks if the controller's object-level
30+
// workload identity configuration is inconsistent.
31+
func InconsistentObjectLevelConfiguration() bool {
32+
return !IsObjectLevelWorkloadIdentityEnabled() &&
33+
(GetDefaultServiceAccount() != "" ||
34+
GetDefaultKubeConfigServiceAccount() != "" ||
35+
GetDefaultDecryptionServiceAccount() != "")
36+
}

auth/controller_options_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

tests/integration/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/elazarl/goproxy v1.7.2
1919
github.com/fluxcd/cli-utils v0.36.0-flux.14
2020
github.com/fluxcd/pkg/apis/meta v1.18.0
21-
github.com/fluxcd/pkg/auth v0.26.0
21+
github.com/fluxcd/pkg/auth v0.27.0
2222
github.com/fluxcd/pkg/cache v0.10.0
2323
github.com/fluxcd/pkg/git v0.35.0
2424
github.com/fluxcd/pkg/git/gogit v0.39.0

0 commit comments

Comments
 (0)