Skip to content
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

feat(auth): add universe domain support to credentials/impersonate #10953

Merged
merged 16 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ func (c *Credentials) UniverseDomain(ctx context.Context) (string, error) {
return v, err
}

// UniverseDomainPropertyProvider returns a [CredentialsPropertyProvider] for
// the default service domain for a given Cloud universe.
//
// To immediately access the universe domain, use [Credentials.UniverseDomain].
func (c *Credentials) UniverseDomainPropertyProvider() CredentialsPropertyProvider {
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
return c.universeDomain
}

// CredentialsPropertyProvider provides an implementation to fetch a property
// value for [Credentials].
type CredentialsPropertyProvider interface {
Expand Down
20 changes: 1 addition & 19 deletions auth/credentials/impersonate/impersonate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ func NewCredentials(opts *CredentialsOptions) (*auth.Credentials, error) {
// If a subject is specified a domain-wide delegation auth-flow is initiated
// to impersonate as the provided subject (user).
if opts.Subject != "" {
gdu, err := isUniverseDomainGDU(universeDomainProvider)
if err != nil {
return nil, err
}
if !gdu {
return nil, errUniverseNotSupportedDomainWideDelegation
}
tp, err := user(opts, client, lifetime, isStaticToken)
tp, err := user(opts, client, lifetime, isStaticToken, universeDomainProvider)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,17 +149,6 @@ func resolveUniverseDomainProvider(opts *CredentialsOptions, creds *auth.Credent
return internal.StaticCredentialsProperty(internal.DefaultUniverseDomain)
}

// isUniverseDomainGDU returns true if the universe domain is the default Google
// universe or if it is empty.
func isUniverseDomainGDU(universeDomainProvider auth.CredentialsPropertyProvider) (bool, error) {
universeDomain, err := universeDomainProvider.GetProperty(context.Background())
if err != nil {
return false, err
}
gdu := universeDomain == internal.DefaultUniverseDomain || universeDomain == ""
return gdu, nil
}

// CredentialsOptions for generating an impersonated credential token.
type CredentialsOptions struct {
// TargetPrincipal is the email address of the service account to
Expand Down
44 changes: 34 additions & 10 deletions auth/credentials/impersonate/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (

// user provides an auth flow for domain-wide delegation, setting
// CredentialsConfig.Subject to be the impersonated user.
func user(opts *CredentialsOptions, client *http.Client, lifetime time.Duration, isStaticToken bool) (auth.TokenProvider, error) {
func user(opts *CredentialsOptions, client *http.Client, lifetime time.Duration, isStaticToken bool, universeDomainProvider auth.CredentialsPropertyProvider) (auth.TokenProvider, error) {
u := userTokenProvider{
client: client,
targetPrincipal: opts.TargetPrincipal,
subject: opts.Subject,
lifetime: lifetime,
client: client,
targetPrincipal: opts.TargetPrincipal,
subject: opts.Subject,
lifetime: lifetime,
universeDomainProvider: universeDomainProvider,
}
u.delegates = make([]string, len(opts.Delegates))
for i, v := range opts.Delegates {
Expand Down Expand Up @@ -84,21 +85,44 @@ type exchangeTokenResponse struct {
type userTokenProvider struct {
client *http.Client

targetPrincipal string
subject string
scopes []string
lifetime time.Duration
delegates []string
targetPrincipal string
subject string
scopes []string
lifetime time.Duration
delegates []string
universeDomainProvider auth.CredentialsPropertyProvider
}

func (u userTokenProvider) Token(ctx context.Context) (*auth.Token, error) {
// If a subject is specified a domain-wide delegation auth-flow is initiated
// to impersonate as the provided subject (user).
if u.subject != "" {
gdu, err := isUniverseDomainGDU(u.universeDomainProvider)
if err != nil {
return nil, err
}
if !gdu {
return nil, errUniverseNotSupportedDomainWideDelegation
}
}
signedJWT, err := u.signJWT(ctx)
if err != nil {
return nil, err
}
return u.exchangeToken(ctx, signedJWT)
}

// isUniverseDomainGDU returns true if the universe domain is the default Google
// universe or if it is empty.
func isUniverseDomainGDU(universeDomainProvider auth.CredentialsPropertyProvider) (bool, error) {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
universeDomain, err := universeDomainProvider.GetProperty(context.Background())
if err != nil {
return false, err
}
gdu := universeDomain == internal.DefaultUniverseDomain || universeDomain == ""
return gdu, nil
}

func (u userTokenProvider) signJWT(ctx context.Context) (string, error) {
now := time.Now()
exp := now.Add(u.lifetime)
Expand Down
7 changes: 5 additions & 2 deletions auth/credentials/impersonate/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestNewCredentials_user(t *testing.T) {
lifetime time.Duration
subject string
wantErr bool
wantTokenErr bool
universeDomain string
}{
{
Expand All @@ -60,14 +61,13 @@ func TestNewCredentials_user(t *testing.T) {
targetPrincipal: "[email protected]",
scopes: []string{"scope"},
subject: "[email protected]",
wantErr: false,
},
{
name: "universeDomain",
targetPrincipal: "[email protected]",
scopes: []string{"scope"},
subject: "[email protected]",
wantErr: true,
wantTokenErr: true,
// Non-GDU Universe Domain should result in error if
// CredentialsConfig.Subject is present for domain-wide delegation.
universeDomain: "example.com",
Expand Down Expand Up @@ -152,6 +152,9 @@ func TestNewCredentials_user(t *testing.T) {
t.Fatal(err)
}
tok, err := ts.Token(ctx)
if tt.wantTokenErr && err != nil {
return
}
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 1 addition & 6 deletions auth/httptransport/httptransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package httptransport

import (
"context"
"crypto/tls"
"errors"
"fmt"
Expand Down Expand Up @@ -171,14 +170,10 @@ func AddAuthorizationMiddleware(client *http.Client, creds *auth.Credentials) er
base = http.DefaultTransport
}
}
clientUniverseDomain, err := creds.UniverseDomain(context.Background())
if err != nil {
return err
}
client.Transport = &authTransport{
creds: creds,
base: base,
clientUniverseDomain: clientUniverseDomain,
clientUniverseDomain: creds.UniverseDomainPropertyProvider(),
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
Expand Down
26 changes: 18 additions & 8 deletions auth/httptransport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func newTransport(base http.RoundTripper, opts *Options) (http.RoundTripper, err
trans = &authTransport{
base: trans,
creds: creds,
clientUniverseDomain: opts.UniverseDomain,
clientUniverseDomain: internal.StaticCredentialsProperty(opts.UniverseDomain),
}
}
return trans, nil
Expand Down Expand Up @@ -187,7 +187,7 @@ func addOCTransport(trans http.RoundTripper, opts *Options) http.RoundTripper {
type authTransport struct {
creds *auth.Credentials
base http.RoundTripper
clientUniverseDomain string
clientUniverseDomain auth.CredentialsPropertyProvider
}

// getClientUniverseDomain returns the default service domain for a given Cloud
Expand All @@ -199,14 +199,20 @@ type authTransport struct {
//
// This is the universe domain configured for the client, which will be compared
// to the universe domain that is separately configured for the credentials.
func (t *authTransport) getClientUniverseDomain() string {
if t.clientUniverseDomain != "" {
return t.clientUniverseDomain
func (t *authTransport) getClientUniverseDomain(ctx context.Context) (string, error) {
if t.clientUniverseDomain != nil {
clientUD, err := t.clientUniverseDomain.GetProperty(ctx)
if err != nil {
return "", err
}
if clientUD != "" {
return clientUD, nil
}
}
if envUD := os.Getenv(internal.UniverseDomainEnvVar); envUD != "" {
return envUD
return envUD, nil
}
return internal.DefaultUniverseDomain
return internal.DefaultUniverseDomain, nil
}

// RoundTrip authorizes and authenticates the request with an
Expand All @@ -231,7 +237,11 @@ func (t *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
if err := transport.ValidateUniverseDomain(t.getClientUniverseDomain(), credentialsUniverseDomain); err != nil {
clientUniverseDomain, err := t.getClientUniverseDomain(req.Context())
if err != nil {
return nil, err
}
if err := transport.ValidateUniverseDomain(clientUniverseDomain, credentialsUniverseDomain); err != nil {
return nil, err
}
}
Expand Down
5 changes: 3 additions & 2 deletions auth/httptransport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package httptransport

import (
"context"
"testing"

"cloud.google.com/go/auth/internal"
Expand Down Expand Up @@ -57,8 +58,8 @@ func TestAuthTransport_GetClientUniverseDomain(t *testing.T) {
if tt.envUniverseDomain != "" {
t.Setenv(internal.UniverseDomainEnvVar, tt.envUniverseDomain)
}
at := &authTransport{clientUniverseDomain: tt.clientUniverseDomain}
got := at.getClientUniverseDomain()
at := &authTransport{clientUniverseDomain: internal.StaticCredentialsProperty(tt.clientUniverseDomain)}
got, _ := at.getClientUniverseDomain(context.Background())
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
Expand Down
Loading