Skip to content

Commit c2a0355

Browse files
authored
Merge pull request #1161 from cappyzawa/feat/default-service-account-flag
[RFC-0010] Add default-service-account for lockdown
2 parents 5bd63a9 + 10a6172 commit c2a0355

6 files changed

Lines changed: 29 additions & 19 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/fluxcd/notification-controller/api v1.6.0
2020
github.com/fluxcd/pkg/apis/event v0.18.0
2121
github.com/fluxcd/pkg/apis/meta v1.18.0
22-
github.com/fluxcd/pkg/auth v0.23.0
22+
github.com/fluxcd/pkg/auth v0.27.0
2323
github.com/fluxcd/pkg/cache v0.10.0
2424
github.com/fluxcd/pkg/git v0.34.0
2525
github.com/fluxcd/pkg/masktoken v0.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ github.com/fluxcd/pkg/apis/kustomize v1.11.0 h1:0IzDgxZkc4v+5SDNCvgZhfwfkdkQLPXC
138138
github.com/fluxcd/pkg/apis/kustomize v1.11.0/go.mod h1:j302mJGDww8cn9qvMsRQ0LJ1HPAPs/IlX7CSsoJV7BI=
139139
github.com/fluxcd/pkg/apis/meta v1.18.0 h1:ACHrMIjlcioE9GKS7NGk62KX4NshqNewr8sBwMcXABs=
140140
github.com/fluxcd/pkg/apis/meta v1.18.0/go.mod h1:97l3hTwBpJbXBY+wetNbqrUsvES8B1jGioKcBUxmqd8=
141-
github.com/fluxcd/pkg/auth v0.23.0 h1:Xt89QO1Hzh7X0JFwCeONyxMlgOX/zOPx0eyIyFoKyF0=
142-
github.com/fluxcd/pkg/auth v0.23.0/go.mod h1:YEAHpBFuW5oLlH9ekuJaQdnJ2Q3A7Ny8kha3WY7QMnY=
141+
github.com/fluxcd/pkg/auth v0.27.0 h1:DFsizUxt9ZDAc+z7+o7jcbtfaxRH55MRD/wdU4CXNCQ=
142+
github.com/fluxcd/pkg/auth v0.27.0/go.mod h1:YEAHpBFuW5oLlH9ekuJaQdnJ2Q3A7Ny8kha3WY7QMnY=
143143
github.com/fluxcd/pkg/cache v0.10.0 h1:M+OGDM4da1cnz7q+sZSBtkBJHpiJsLnKVmR9OdMWxEY=
144144
github.com/fluxcd/pkg/cache v0.10.0/go.mod h1:pPXRzQUDQagsCniuOolqVhnAkbNgYOg8d2cTliPs7ME=
145145
github.com/fluxcd/pkg/git v0.34.0 h1:qTViWkfpEDnjzySyKRKliqUeGj/DznqlkmPhaDNIsFY=

internal/notifier/azure_helpers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"net/url"
2323

24-
"k8s.io/apimachinery/pkg/types"
2524
"sigs.k8s.io/controller-runtime/pkg/client"
2625

2726
"github.com/fluxcd/pkg/auth"
@@ -33,7 +32,12 @@ import (
3332

3433
// newManagedIdentityToken is used to attempt credential-free authentication.
3534
func newManagedIdentityToken(ctx context.Context, proxy, serviceAccountName, providerName, providerNamespace, scope string, tokenClient client.Client, tokenCache *cache.TokenCache) (string, error) {
36-
opts := []auth.Option{auth.WithScopes(scope)}
35+
opts := []auth.Option{
36+
auth.WithScopes(scope),
37+
auth.WithClient(tokenClient),
38+
auth.WithServiceAccountNamespace(providerNamespace),
39+
}
40+
3741
if proxy != "" {
3842
proxyURL, err := url.Parse(proxy)
3943
if err != nil {
@@ -43,11 +47,7 @@ func newManagedIdentityToken(ctx context.Context, proxy, serviceAccountName, pro
4347
}
4448

4549
if serviceAccountName != "" {
46-
serviceAccount := types.NamespacedName{
47-
Name: serviceAccountName,
48-
Namespace: providerNamespace,
49-
}
50-
opts = append(opts, auth.WithServiceAccount(serviceAccount, tokenClient))
50+
opts = append(opts, auth.WithServiceAccountName(serviceAccountName))
5151
}
5252

5353
if tokenCache != nil {

internal/notifier/google_helpers.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"net/url"
2323

2424
"google.golang.org/api/option"
25-
"sigs.k8s.io/controller-runtime/pkg/client"
2625

2726
"github.com/fluxcd/pkg/auth"
2827
"github.com/fluxcd/pkg/auth/gcp"
@@ -39,8 +38,10 @@ func buildGCPClientOptions(ctx context.Context, opts notifierOptions) ([]option.
3938
if opts.Token != "" {
4039
clientOpts = append(clientOpts, option.WithCredentialsJSON([]byte(opts.Token)))
4140
} else {
42-
var authOpts []auth.Option
43-
authOpts = append(authOpts, auth.WithClient(opts.TokenClient))
41+
authOpts := []auth.Option{
42+
auth.WithClient(opts.TokenClient),
43+
auth.WithServiceAccountNamespace(opts.ProviderNamespace),
44+
}
4445

4546
if opts.TokenCache != nil {
4647
involvedObject := cache.InvolvedObject{
@@ -53,11 +54,7 @@ func buildGCPClientOptions(ctx context.Context, opts notifierOptions) ([]option.
5354
}
5455

5556
if opts.ServiceAccountName != "" {
56-
serviceAccountKey := client.ObjectKey{
57-
Name: opts.ServiceAccountName,
58-
Namespace: opts.ProviderNamespace,
59-
}
60-
authOpts = append(authOpts, auth.WithServiceAccount(serviceAccountKey, opts.TokenClient))
57+
authOpts = append(authOpts, auth.WithServiceAccountName(opts.ServiceAccountName))
6158
}
6259

6360
if opts.ProxyURL != "" {

internal/server/event_handlers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ func TestGetNotificationParams(t *testing.T) {
525525
}
526526

527527
if tt.enableObjLevelWI {
528-
t.Setenv(auth.EnvVarEnableObjectLevelWorkloadIdentity, "true")
528+
auth.EnableObjectLevelWorkloadIdentity()
529+
t.Cleanup(auth.DisableObjectLevelWorkloadIdentity)
529530
}
530531

531532
// Create fake objects and event server.

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func main() {
9696
exportHTTPPathMetrics bool
9797
tokenCacheOptions pkgcache.TokenFlags
9898
watchOptions runtimeCtrl.WatchOptions
99+
defaultServiceAccount string
99100
)
100101

101102
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
@@ -118,6 +119,8 @@ func main() {
118119
rateLimiterOptions.BindFlags(flag.CommandLine)
119120
featureGates.BindFlags(flag.CommandLine)
120121
tokenCacheOptions.BindFlags(flag.CommandLine, tokenCacheDefaultMaxSize)
122+
flag.StringVar(&defaultServiceAccount, auth.ControllerFlagDefaultServiceAccount,
123+
"", "Default service account to use for workload identity when not specified in resources.")
121124

122125
flag.Parse()
123126

@@ -136,6 +139,15 @@ func main() {
136139
auth.EnableObjectLevelWorkloadIdentity()
137140
}
138141

142+
if defaultServiceAccount != "" {
143+
auth.SetDefaultServiceAccount(defaultServiceAccount)
144+
}
145+
146+
if auth.InconsistentObjectLevelConfiguration() {
147+
setupLog.Error(auth.ErrInconsistentObjectLevelConfiguration, "invalid configuration")
148+
os.Exit(1)
149+
}
150+
139151
watchNamespace := ""
140152
if !watchOptions.AllNamespaces {
141153
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")

0 commit comments

Comments
 (0)