@@ -13,28 +13,18 @@ import (
13
13
"testing"
14
14
)
15
15
16
- var testImpersonateConfig = Config {
17
- Audience : "32555940559.apps.googleusercontent.com" ,
18
- SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
19
- TokenInfoURL : "http://localhost:8080/v1/tokeninfo" ,
20
- ClientSecret : "notsosecret" ,
21
- ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
22
- CredentialSource : testBaseCredSource ,
23
- Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
24
- }
25
-
26
16
var (
27
17
baseImpersonateCredsReqBody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&subject_token=street123&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
28
18
baseImpersonateCredsRespBody = `{"accessToken":"Second.Access.Token","expireTime":"2020-12-28T15:01:23Z"}`
29
19
)
30
20
31
- func TestImpersonation ( t * testing.T ) {
32
- impersonateServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
33
- if got , want := r .URL .String (), "/" ; got != want {
21
+ func createImpersonationServer ( urlWanted , authWanted , bodyWanted , response string , t * testing.T ) * httptest. Server {
22
+ return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
23
+ if got , want := r .URL .String (), urlWanted ; got != want {
34
24
t .Errorf ("URL.String(): got %v but want %v" , got , want )
35
25
}
36
26
headerAuth := r .Header .Get ("Authorization" )
37
- if got , want := headerAuth , "Bearer Sample.Access.Token" ; got != want {
27
+ if got , want := headerAuth , authWanted ; got != want {
38
28
t .Errorf ("got %v but want %v" , got , want )
39
29
}
40
30
headerContentType := r .Header .Get ("Content-Type" )
@@ -45,14 +35,16 @@ func TestImpersonation(t *testing.T) {
45
35
if err != nil {
46
36
t .Fatalf ("Failed reading request body: %v." , err )
47
37
}
48
- if got , want := string (body ), "{ \" lifetime \" : \" 3600s \" , \" scope \" :[ \" https://www.googleapis.com/auth/devstorage.full_control \" ]}" ; got != want {
38
+ if got , want := string (body ), bodyWanted ; got != want {
49
39
t .Errorf ("Unexpected impersonation payload: got %v but want %v" , got , want )
50
40
}
51
41
w .Header ().Set ("Content-Type" , "application/json" )
52
- w .Write ([]byte (baseImpersonateCredsRespBody ))
42
+ w .Write ([]byte (response ))
53
43
}))
54
- testImpersonateConfig .ServiceAccountImpersonationURL = impersonateServer .URL
55
- targetServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
44
+ }
45
+
46
+ func createTargetServer (t * testing.T ) * httptest.Server {
47
+ return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
56
48
if got , want := r .URL .String (), "/" ; got != want {
57
49
t .Errorf ("URL.String(): got %v but want %v" , got , want )
58
50
}
@@ -74,27 +66,74 @@ func TestImpersonation(t *testing.T) {
74
66
w .Header ().Set ("Content-Type" , "application/json" )
75
67
w .Write ([]byte (baseCredsResponseBody ))
76
68
}))
77
- defer targetServer . Close ()
69
+ }
78
70
79
- testImpersonateConfig .TokenURL = targetServer .URL
80
- allURLs := regexp .MustCompile (".+" )
81
- ourTS , err := testImpersonateConfig .tokenSource (context .Background (), []* regexp.Regexp {allURLs }, []* regexp.Regexp {allURLs }, "http" )
82
- if err != nil {
83
- t .Fatalf ("Failed to create TokenSource: %v" , err )
84
- }
71
+ var impersonationTests = []struct {
72
+ name string
73
+ config Config
74
+ expectedImpersonationBody string
75
+ }{
76
+ {
77
+ name : "Base Impersonation" ,
78
+ config : Config {
79
+ Audience : "32555940559.apps.googleusercontent.com" ,
80
+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
81
+ TokenInfoURL : "http://localhost:8080/v1/tokeninfo" ,
82
+ ClientSecret : "notsosecret" ,
83
+ ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
84
+ CredentialSource : testBaseCredSource ,
85
+ Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
86
+ },
87
+ expectedImpersonationBody : "{\" lifetime\" :\" 3600s\" ,\" scope\" :[\" https://www.googleapis.com/auth/devstorage.full_control\" ]}" ,
88
+ },
89
+ {
90
+ name : "With TokenLifetime Set" ,
91
+ config : Config {
92
+ Audience : "32555940559.apps.googleusercontent.com" ,
93
+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
94
+ TokenInfoURL : "http://localhost:8080/v1/tokeninfo" ,
95
+ ClientSecret : "notsosecret" ,
96
+ ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
97
+ CredentialSource : testBaseCredSource ,
98
+ Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
99
+ ServiceAccountImpersonationLifetimeSeconds : 10000 ,
100
+ },
101
+ expectedImpersonationBody : "{\" lifetime\" :\" 10000s\" ,\" scope\" :[\" https://www.googleapis.com/auth/devstorage.full_control\" ]}" ,
102
+ },
103
+ }
85
104
86
- oldNow := now
87
- defer func () { now = oldNow }()
88
- now = testNow
105
+ func TestImpersonation (t * testing.T ) {
106
+ for _ , tt := range impersonationTests {
107
+ t .Run (tt .name , func (t * testing.T ) {
108
+ testImpersonateConfig := tt .config
109
+ impersonateServer := createImpersonationServer ("/" , "Bearer Sample.Access.Token" , tt .expectedImpersonationBody , baseImpersonateCredsRespBody , t )
110
+ defer impersonateServer .Close ()
111
+ testImpersonateConfig .ServiceAccountImpersonationURL = impersonateServer .URL
89
112
90
- tok , err := ourTS .Token ()
91
- if err != nil {
92
- t .Fatalf ("Unexpected error: %e" , err )
93
- }
94
- if got , want := tok .AccessToken , "Second.Access.Token" ; got != want {
95
- t .Errorf ("Unexpected access token: got %v, but wanted %v" , got , want )
96
- }
97
- if got , want := tok .TokenType , "Bearer" ; got != want {
98
- t .Errorf ("Unexpected TokenType: got %v, but wanted %v" , got , want )
113
+ targetServer := createTargetServer (t )
114
+ defer targetServer .Close ()
115
+ testImpersonateConfig .TokenURL = targetServer .URL
116
+
117
+ allURLs := regexp .MustCompile (".+" )
118
+ ourTS , err := testImpersonateConfig .tokenSource (context .Background (), []* regexp.Regexp {allURLs }, []* regexp.Regexp {allURLs }, "http" )
119
+ if err != nil {
120
+ t .Fatalf ("Failed to create TokenSource: %v" , err )
121
+ }
122
+
123
+ oldNow := now
124
+ defer func () { now = oldNow }()
125
+ now = testNow
126
+
127
+ tok , err := ourTS .Token ()
128
+ if err != nil {
129
+ t .Fatalf ("Unexpected error: %e" , err )
130
+ }
131
+ if got , want := tok .AccessToken , "Second.Access.Token" ; got != want {
132
+ t .Errorf ("Unexpected access token: got %v, but wanted %v" , got , want )
133
+ }
134
+ if got , want := tok .TokenType , "Bearer" ; got != want {
135
+ t .Errorf ("Unexpected TokenType: got %v, but wanted %v" , got , want )
136
+ }
137
+ })
99
138
}
100
139
}
0 commit comments