-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(impersonate): add universe domain support (#2296)
- Loading branch information
Showing
10 changed files
with
450 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,33 +20,48 @@ import ( | |
func TestTokenSource_serviceAccount(t *testing.T) { | ||
ctx := context.Background() | ||
tests := []struct { | ||
name string | ||
targetPrincipal string | ||
scopes []string | ||
lifetime time.Duration | ||
wantErr bool | ||
name string | ||
config CredentialsConfig | ||
opts option.ClientOption | ||
wantErr error | ||
}{ | ||
{ | ||
name: "missing targetPrincipal", | ||
wantErr: true, | ||
wantErr: errMissingTargetPrincipal, | ||
}, | ||
{ | ||
name: "missing scopes", | ||
targetPrincipal: "[email protected]", | ||
wantErr: true, | ||
name: "missing scopes", | ||
config: CredentialsConfig{ | ||
TargetPrincipal: "[email protected]", | ||
}, | ||
wantErr: errMissingScopes, | ||
}, | ||
{ | ||
name: "lifetime over max", | ||
targetPrincipal: "[email protected]", | ||
scopes: []string{"scope"}, | ||
lifetime: 13 * time.Hour, | ||
wantErr: true, | ||
name: "lifetime over max", | ||
config: CredentialsConfig{ | ||
TargetPrincipal: "[email protected]", | ||
Scopes: []string{"scope"}, | ||
Lifetime: 13 * time.Hour, | ||
}, | ||
wantErr: errLifetimeOverMax, | ||
}, | ||
{ | ||
name: "works", | ||
targetPrincipal: "[email protected]", | ||
scopes: []string{"scope"}, | ||
wantErr: false, | ||
name: "works", | ||
config: CredentialsConfig{ | ||
TargetPrincipal: "[email protected]", | ||
Scopes: []string{"scope"}, | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "universe domain", | ||
config: CredentialsConfig{ | ||
TargetPrincipal: "[email protected]", | ||
Scopes: []string{"scope"}, | ||
Subject: "[email protected]", | ||
}, | ||
opts: option.WithUniverseDomain("example.com"), | ||
wantErr: errUniverseNotSupportedDomainWideDelegation, | ||
}, | ||
} | ||
|
||
|
@@ -74,23 +89,26 @@ func TestTokenSource_serviceAccount(t *testing.T) { | |
return nil | ||
}), | ||
} | ||
ts, err := CredentialsTokenSource(ctx, CredentialsConfig{ | ||
TargetPrincipal: tt.targetPrincipal, | ||
Scopes: tt.scopes, | ||
Lifetime: tt.lifetime, | ||
}, option.WithHTTPClient(client)) | ||
if tt.wantErr && err != nil { | ||
return | ||
opts := []option.ClientOption{ | ||
option.WithHTTPClient(client), | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
if tt.opts != nil { | ||
opts = append(opts, tt.opts) | ||
} | ||
tok, err := ts.Token() | ||
ts, err := CredentialsTokenSource(ctx, tt.config, opts...) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if tok.AccessToken != saTok { | ||
t.Fatalf("got %q, want %q", tok.AccessToken, saTok) | ||
if err != tt.wantErr { | ||
t.Fatalf("%s: err: %v", tt.name, err) | ||
} | ||
} else { | ||
tok, err := ts.Token() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if tok.AccessToken != saTok { | ||
t.Fatalf("got %q, want %q", tok.AccessToken, saTok) | ||
} | ||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ func TestTokenSource_user(t *testing.T) { | |
lifetime time.Duration | ||
subject string | ||
wantErr bool | ||
universeDomain string | ||
}{ | ||
{ | ||
name: "missing targetPrincipal", | ||
|
@@ -50,6 +51,16 @@ func TestTokenSource_user(t *testing.T) { | |
subject: "[email protected]", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "universeDomain", | ||
targetPrincipal: "[email protected]", | ||
scopes: []string{"scope"}, | ||
subject: "[email protected]", | ||
wantErr: true, | ||
// Non-GDU Universe Domain should result in error if | ||
// CredentialsConfig.Subject is present for domain-wide delegation. | ||
universeDomain: "example.com", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
@@ -92,12 +103,15 @@ func TestTokenSource_user(t *testing.T) { | |
return nil | ||
}), | ||
} | ||
ts, err := CredentialsTokenSource(ctx, CredentialsConfig{ | ||
TargetPrincipal: tt.targetPrincipal, | ||
Scopes: tt.scopes, | ||
Lifetime: tt.lifetime, | ||
Subject: tt.subject, | ||
}, option.WithHTTPClient(client)) | ||
ts, err := CredentialsTokenSource(ctx, | ||
CredentialsConfig{ | ||
TargetPrincipal: tt.targetPrincipal, | ||
Scopes: tt.scopes, | ||
Lifetime: tt.lifetime, | ||
Subject: tt.subject, | ||
}, | ||
option.WithHTTPClient(client), | ||
option.WithUniverseDomain(tt.universeDomain)) | ||
if tt.wantErr && err != nil { | ||
return | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.