Skip to content

Commit e5189f6

Browse files
authored
Merge pull request #1872 from cappyzawa/feat/default-service-account-flag
[RFC-0010] Add default-service-account for lockdown
2 parents 24412ed + dc3eba6 commit e5189f6

File tree

8 files changed

+40
-27
lines changed

8 files changed

+40
-27
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/fluxcd/cli-utils v0.36.0-flux.14
2929
github.com/fluxcd/pkg/apis/event v0.18.0
3030
github.com/fluxcd/pkg/apis/meta v1.18.0
31-
github.com/fluxcd/pkg/auth v0.21.0
31+
github.com/fluxcd/pkg/auth v0.27.0
3232
github.com/fluxcd/pkg/cache v0.10.0
3333
github.com/fluxcd/pkg/git v0.35.0
3434
github.com/fluxcd/pkg/git/gogit v0.38.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ github.com/fluxcd/pkg/apis/event v0.18.0 h1:PNbWk9gvX8gMIi6VsJapnuDO+giLEeY+6olL
378378
github.com/fluxcd/pkg/apis/event v0.18.0/go.mod h1:7S/DGboLolfbZ6stO6dcDhG1SfkPWQ9foCULvbiYpiA=
379379
github.com/fluxcd/pkg/apis/meta v1.18.0 h1:ACHrMIjlcioE9GKS7NGk62KX4NshqNewr8sBwMcXABs=
380380
github.com/fluxcd/pkg/apis/meta v1.18.0/go.mod h1:97l3hTwBpJbXBY+wetNbqrUsvES8B1jGioKcBUxmqd8=
381-
github.com/fluxcd/pkg/auth v0.21.0 h1:ckAQqP12wuptXEkMY18SQKWEY09m9e6yI0mEMsDV15M=
382-
github.com/fluxcd/pkg/auth v0.21.0/go.mod h1:MXmpsXT97c874HCw5hnfqFUP7TsG8/Ss1vFrk8JccfM=
381+
github.com/fluxcd/pkg/auth v0.27.0 h1:DFsizUxt9ZDAc+z7+o7jcbtfaxRH55MRD/wdU4CXNCQ=
382+
github.com/fluxcd/pkg/auth v0.27.0/go.mod h1:YEAHpBFuW5oLlH9ekuJaQdnJ2Q3A7Ny8kha3WY7QMnY=
383383
github.com/fluxcd/pkg/cache v0.10.0 h1:M+OGDM4da1cnz7q+sZSBtkBJHpiJsLnKVmR9OdMWxEY=
384384
github.com/fluxcd/pkg/cache v0.10.0/go.mod h1:pPXRzQUDQagsCniuOolqVhnAkbNgYOg8d2cTliPs7ME=
385385
github.com/fluxcd/pkg/git v0.35.0 h1:mAauhsdfxNW4yQdXviVlvcN/uCGGG0+6p5D1+HFZI9w=

internal/controller/bucket_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,13 @@ func (r *BucketReconciler) setupCredentials(ctx context.Context, obj *sourcev1.B
860860
// createBucketProvider creates a provider-specific bucket client using the given credentials and configuration.
861861
// It handles different bucket providers (AWS, GCP, Azure, generic) and returns the appropriate client.
862862
func (r *BucketReconciler) createBucketProvider(ctx context.Context, obj *sourcev1.Bucket, creds *bucketCredentials) (BucketProvider, error) {
863-
var authOpts []auth.Option
863+
authOpts := []auth.Option{
864+
auth.WithClient(r.Client),
865+
auth.WithServiceAccountNamespace(obj.GetNamespace()),
866+
}
864867

865868
if obj.Spec.ServiceAccountName != "" {
866-
serviceAccount := client.ObjectKey{
867-
Name: obj.Spec.ServiceAccountName,
868-
Namespace: obj.GetNamespace(),
869-
}
870-
authOpts = append(authOpts, auth.WithServiceAccount(serviceAccount, r.Client))
869+
authOpts = append(authOpts, auth.WithServiceAccountName(obj.Spec.ServiceAccountName))
871870
}
872871

873872
if r.TokenCache != nil {

internal/controller/bucket_controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838

3939
kstatus "github.com/fluxcd/cli-utils/pkg/kstatus/status"
4040
"github.com/fluxcd/pkg/apis/meta"
41+
"github.com/fluxcd/pkg/auth"
4142
"github.com/fluxcd/pkg/runtime/conditions"
4243
conditionscheck "github.com/fluxcd/pkg/runtime/conditions/check"
4344
"github.com/fluxcd/pkg/runtime/jitter"
@@ -1390,11 +1391,10 @@ func TestBucketReconciler_reconcileSource_gcs(t *testing.T) {
13901391
patchOptions: getPatchOptions(bucketReadyCondition.Owned, "sc"),
13911392
}
13921393

1393-
// Handle ObjectLevelWorkloadIdentity feature gate environment variable
1394-
if tt.disableObjectLevelWorkloadIdentity {
1395-
t.Setenv("ENABLE_OBJECT_LEVEL_WORKLOAD_IDENTITY", "false")
1396-
} else if tt.serviceAccount != nil {
1397-
t.Setenv("ENABLE_OBJECT_LEVEL_WORKLOAD_IDENTITY", "true")
1394+
// Handle ObjectLevelWorkloadIdentity feature gate
1395+
if !tt.disableObjectLevelWorkloadIdentity {
1396+
auth.EnableObjectLevelWorkloadIdentity()
1397+
t.Cleanup(auth.DisableObjectLevelWorkloadIdentity)
13981398
}
13991399

14001400
tmpDir := t.TempDir()

internal/controller/gitrepository_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,10 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
661661
switch provider := obj.GetProvider(); provider {
662662
case sourcev1.GitProviderAzure: // If AWS or GCP are added in the future they can be added here separated by a comma.
663663
getCreds = func() (*authutils.GitCredentials, error) {
664-
var opts []auth.Option
664+
opts := []auth.Option{
665+
auth.WithClient(r.Client),
666+
auth.WithServiceAccountNamespace(obj.GetNamespace()),
667+
}
665668

666669
if obj.Spec.ServiceAccountName != "" {
667670
// Check object-level workload identity feature gate.
@@ -672,11 +675,8 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
672675
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, meta.FeatureGateDisabledReason, "%s", err)
673676
return nil, err
674677
}
675-
serviceAccount := client.ObjectKey{
676-
Name: obj.Spec.ServiceAccountName,
677-
Namespace: obj.GetNamespace(),
678-
}
679-
opts = append(opts, auth.WithServiceAccount(serviceAccount, r.Client))
678+
// Set ServiceAccountName only if explicitly specified
679+
opts = append(opts, auth.WithServiceAccountName(obj.Spec.ServiceAccountName))
680680
}
681681

682682
if r.TokenCache != nil {

internal/controller/ocirepository_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
373373
}
374374

375375
if _, ok := keychain.(soci.Anonymous); obj.Spec.Provider != "" && obj.Spec.Provider != sourcev1.GenericOCIProvider && ok {
376-
var opts []auth.Option
376+
opts := []auth.Option{
377+
auth.WithClient(r.Client),
378+
auth.WithServiceAccountNamespace(obj.GetNamespace()),
379+
}
380+
377381
if obj.Spec.ServiceAccountName != "" {
378382
// Check object-level workload identity feature gate.
379383
if !auth.IsObjectLevelWorkloadIdentityEnabled() {
@@ -382,11 +386,8 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
382386
err := fmt.Errorf(msgFmt, gate)
383387
return sreconcile.ResultEmpty, serror.NewStalling(err, meta.FeatureGateDisabledReason)
384388
}
385-
serviceAccount := client.ObjectKey{
386-
Name: obj.Spec.ServiceAccountName,
387-
Namespace: obj.GetNamespace(),
388-
}
389-
opts = append(opts, auth.WithServiceAccount(serviceAccount, r.Client))
389+
// Set ServiceAccountName only if explicitly specified
390+
opts = append(opts, auth.WithServiceAccountName(obj.Spec.ServiceAccountName))
390391
}
391392
if r.TokenCache != nil {
392393
involvedObject := cache.InvolvedObject{

internal/controller/ocirepository_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,8 @@ func TestOCIRepository_objectLevelWorkloadIdentityFeatureGate(t *testing.T) {
30593059
g.Expect(stalledCondition.Reason).Should(Equal(meta.FeatureGateDisabledReason))
30603060
g.Expect(stalledCondition.Message).Should(Equal("to use spec.serviceAccountName for provider authentication please enable the ObjectLevelWorkloadIdentity feature gate in the controller"))
30613061

3062-
t.Setenv(auth.EnvVarEnableObjectLevelWorkloadIdentity, "true")
3062+
auth.EnableObjectLevelWorkloadIdentity()
3063+
t.Cleanup(auth.DisableObjectLevelWorkloadIdentity)
30633064

30643065
g.Eventually(func() bool {
30653066
if err := testEnv.Get(ctx, key, resultobj); err != nil {

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func main() {
121121
artifactRetentionRecords int
122122
artifactDigestAlgo string
123123
tokenCacheOptions pkgcache.TokenFlags
124+
defaultServiceAccount string
124125
)
125126

126127
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"),
@@ -159,6 +160,8 @@ func main() {
159160
"The maximum number of artifacts to be kept in storage after a garbage collection.")
160161
flag.StringVar(&artifactDigestAlgo, "artifact-digest-algo", intdigest.Canonical.String(),
161162
"The algorithm to use to calculate the digest of artifacts.")
163+
flag.StringVar(&defaultServiceAccount, auth.ControllerFlagDefaultServiceAccount,
164+
"", "Default service account to use for workload identity when not specified in resources.")
162165

163166
clientOptions.BindFlags(flag.CommandLine)
164167
logOptions.BindFlags(flag.CommandLine)
@@ -173,6 +176,10 @@ func main() {
173176

174177
logger.SetLogger(logger.NewLogger(logOptions))
175178

179+
if defaultServiceAccount != "" {
180+
auth.SetDefaultServiceAccount(defaultServiceAccount)
181+
}
182+
176183
if err := featureGates.WithLogger(setupLog).SupportedFeatures(features.FeatureGates()); err != nil {
177184
setupLog.Error(err, "unable to load feature gates")
178185
os.Exit(1)
@@ -186,6 +193,11 @@ func main() {
186193
auth.EnableObjectLevelWorkloadIdentity()
187194
}
188195

196+
if auth.InconsistentObjectLevelConfiguration() {
197+
setupLog.Error(auth.ErrInconsistentObjectLevelConfiguration, "invalid configuration")
198+
os.Exit(1)
199+
}
200+
189201
if err := intervalJitterOptions.SetGlobalJitter(nil); err != nil {
190202
setupLog.Error(err, "unable to set global jitter")
191203
os.Exit(1)

0 commit comments

Comments
 (0)