-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rbac-manager] Support workload identity #330
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ manifests: generate | |
install-tools: | ||
go install github.com/onsi/ginkgo/[email protected] | ||
go install sigs.k8s.io/controller-tools/cmd/[email protected] | ||
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest | ||
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.17 | ||
|
||
install-tools-homebrew: | ||
brew install kubernetes-cli kustomize kind | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ import ( | |
"os" | ||
"strings" | ||
|
||
"cloud.google.com/go/compute/metadata" | ||
"github.com/alecthomas/kingpin" | ||
"golang.org/x/oauth2/google" | ||
directoryv1 "google.golang.org/api/admin/directory/v1" | ||
"google.golang.org/api/impersonate" | ||
"google.golang.org/api/option" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
|
@@ -110,6 +112,30 @@ func createGoogleDirectory(ctx context.Context, subject string) (*directoryv1.Se | |
return nil, err | ||
} | ||
|
||
// If the found credential doesn't contain JSON, try to fallback to workload identity | ||
if len(creds.JSON) == 0 { | ||
// Get the email address associated with the service account. The account may be empty | ||
// or the string "default" to use the instance's main account. | ||
principal, err := metadata.Email("default") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Access to the directory API must be signed with a Subject to enable domain selection. | ||
config := impersonate.CredentialsConfig{ | ||
TargetPrincipal: principal, | ||
Scopes: scopes, | ||
Subject: subject, | ||
} | ||
|
||
ts, err := impersonate.CredentialsTokenSource(ctx, config, option.WithCredentials(creds)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary for the account to impersonate itself, instead of using the TokenSource already returned by |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return directoryv1.NewService(ctx, option.WithTokenSource(ts)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the option when using a supplied json credentials (old behavior) could also use the TokenSource from If so, likely the code could be simplified a little, instead of needing to call |
||
} | ||
|
||
conf, err := google.JWTConfigFromJSON(creds.JSON, strings.Join(scopes, " ")) | ||
if err != nil { | ||
return nil, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shamelessly stolen from #335 to get my CI tests passing.. 😬