14
14
15
15
import { URLSearchParams } from 'url' ;
16
16
17
- import { HttpClient } from '@actions/http-client ' ;
17
+ import { errorMessage } from '@google-github- actions/actions-utils ' ;
18
18
19
- import { Logger } from './logger ' ;
20
- import { expandEndpoint , userAgent } from './utils ' ;
19
+ import { Client } from './client ' ;
20
+ import { Logger } from '../logger ' ;
21
21
22
22
/**
23
23
* GenerateAccessTokenParameters are the inputs to the generateAccessToken call.
@@ -43,35 +43,27 @@ export interface GenerateIDTokenParameters {
43
43
* IAMCredentialsClientParameters are the inputs to the IAM client.
44
44
*/
45
45
export interface IAMCredentialsClientParameters {
46
+ readonly logger : Logger ;
47
+ readonly universe : string ;
48
+
46
49
readonly authToken : string ;
47
50
}
48
51
49
52
/**
50
53
* IAMCredentialsClient is a thin HTTP client around the Google Cloud IAM
51
54
* Credentials API.
52
55
*/
53
- export class IAMCredentialsClient {
54
- readonly #logger: Logger ;
55
- readonly #httpClient: HttpClient ;
56
+ export class IAMCredentialsClient extends Client {
56
57
readonly #authToken: string ;
57
58
58
- readonly #universe: string = 'googleapis.com' ;
59
- readonly #endpoints = {
60
- iamcredentials : 'https://iamcredentials.{universe}/v1' ,
61
- oauth2 : 'https://oauth2.{universe}' ,
62
- } ;
63
-
64
- constructor ( logger : Logger , opts : IAMCredentialsClientParameters ) {
65
- this . #logger = logger . withNamespace ( this . constructor . name ) ;
66
- this . #httpClient = new HttpClient ( userAgent ) ;
59
+ constructor ( opts : IAMCredentialsClientParameters ) {
60
+ super ( {
61
+ logger : opts . logger ,
62
+ universe : opts . universe ,
63
+ child : `IAMCredentialsClient` ,
64
+ } ) ;
67
65
68
66
this . #authToken = opts . authToken ;
69
-
70
- const endpoints = this . #endpoints;
71
- for ( const key of Object . keys ( this . #endpoints) as Array < keyof typeof endpoints > ) {
72
- this . #endpoints[ key ] = expandEndpoint ( this . #endpoints[ key ] , this . #universe) ;
73
- }
74
- this . #logger. debug ( `Computed endpoints` , this . #endpoints) ;
75
67
}
76
68
77
69
/**
@@ -84,7 +76,9 @@ export class IAMCredentialsClient {
84
76
scopes,
85
77
lifetime,
86
78
} : GenerateAccessTokenParameters ) : Promise < string > {
87
- const pth = `${ this . #endpoints. iamcredentials } /projects/-/serviceAccounts/${ serviceAccount } :generateAccessToken` ;
79
+ const logger = this . _logger . withNamespace ( 'generateAccessToken' ) ;
80
+
81
+ const pth = `${ this . _endpoints . iamcredentials } /projects/-/serviceAccounts/${ serviceAccount } :generateAccessToken` ;
88
82
89
83
const headers = { Authorization : `Bearer ${ this . #authToken} ` } ;
90
84
@@ -100,15 +94,15 @@ export class IAMCredentialsClient {
100
94
body . lifetime = `${ lifetime } s` ;
101
95
}
102
96
103
- this . # logger. withNamespace ( 'generateAccessToken' ) . debug ( {
97
+ logger . debug ( `Built request` , {
104
98
method : `POST` ,
105
99
path : pth ,
106
100
headers : headers ,
107
101
body : body ,
108
102
} ) ;
109
103
110
104
try {
111
- const resp = await this . #httpClient . postJson < { accessToken : string } > ( pth , body , headers ) ;
105
+ const resp = await this . _httpClient . postJson < { accessToken : string } > ( pth , body , headers ) ;
112
106
const statusCode = resp . statusCode || 500 ;
113
107
if ( statusCode < 200 || statusCode > 299 ) {
114
108
throw new Error ( `Failed to call ${ pth } : HTTP ${ statusCode } : ${ resp . result || '[no body]' } ` ) ;
@@ -120,14 +114,17 @@ export class IAMCredentialsClient {
120
114
}
121
115
return result . accessToken ;
122
116
} catch ( err ) {
117
+ const msg = errorMessage ( err ) ;
123
118
throw new Error (
124
- `Failed to generate Google Cloud OAuth 2.0 Access Token for ${ serviceAccount } : ${ err } ` ,
119
+ `Failed to generate Google Cloud OAuth 2.0 Access Token for ${ serviceAccount } : ${ msg } ` ,
125
120
) ;
126
121
}
127
122
}
128
123
129
124
async generateDomainWideDelegationAccessToken ( assertion : string ) : Promise < string > {
130
- const pth = `${ this . #endpoints. oauth2 } /token` ;
125
+ const logger = this . _logger . withNamespace ( 'generateDomainWideDelegationAccessToken' ) ;
126
+
127
+ const pth = `${ this . _endpoints . oauth2 } /token` ;
131
128
132
129
const headers = {
133
130
'Accept' : 'application/json' ,
@@ -138,15 +135,15 @@ export class IAMCredentialsClient {
138
135
body . append ( 'grant_type' , 'urn:ietf:params:oauth:grant-type:jwt-bearer' ) ;
139
136
body . append ( 'assertion' , assertion ) ;
140
137
141
- this . # logger. withNamespace ( 'generateDomainWideDelegationAccessToken' ) . debug ( {
138
+ logger . debug ( `Built request` , {
142
139
method : `POST` ,
143
140
path : pth ,
144
141
headers : headers ,
145
142
body : body ,
146
143
} ) ;
147
144
148
145
try {
149
- const resp = await this . #httpClient . post ( pth , body . toString ( ) , headers ) ;
146
+ const resp = await this . _httpClient . post ( pth , body . toString ( ) , headers ) ;
150
147
const respBody = await resp . readBody ( ) ;
151
148
const statusCode = resp . message . statusCode || 500 ;
152
149
if ( statusCode < 200 || statusCode > 299 ) {
@@ -155,8 +152,9 @@ export class IAMCredentialsClient {
155
152
const parsed = JSON . parse ( respBody ) as { accessToken : string } ;
156
153
return parsed . accessToken ;
157
154
} catch ( err ) {
155
+ const msg = errorMessage ( err ) ;
158
156
throw new Error (
159
- `Failed to generate Google Cloud Domain Wide Delegation OAuth 2.0 Access Token: ${ err } ` ,
157
+ `Failed to generate Google Cloud Domain Wide Delegation OAuth 2.0 Access Token: ${ msg } ` ,
160
158
) ;
161
159
}
162
160
}
@@ -171,7 +169,9 @@ export class IAMCredentialsClient {
171
169
delegates,
172
170
includeEmail,
173
171
} : GenerateIDTokenParameters ) : Promise < string > {
174
- const pth = `${ this . #endpoints. iamcredentials } /projects/-/serviceAccounts/${ serviceAccount } :generateIdToken` ;
172
+ const logger = this . _logger . withNamespace ( 'generateIDToken' ) ;
173
+
174
+ const pth = `${ this . _endpoints . iamcredentials } /projects/-/serviceAccounts/${ serviceAccount } :generateIdToken` ;
175
175
176
176
const headers = { Authorization : `Bearer ${ this . #authToken} ` } ;
177
177
@@ -183,15 +183,15 @@ export class IAMCredentialsClient {
183
183
body . delegates = delegates ;
184
184
}
185
185
186
- this . # logger. withNamespace ( 'generateIDToken' ) . debug ( {
186
+ logger . debug ( `Built request` , {
187
187
method : `POST` ,
188
188
path : pth ,
189
189
headers : headers ,
190
190
body : body ,
191
191
} ) ;
192
192
193
193
try {
194
- const resp = await this . #httpClient . postJson < { token : string } > ( pth , body , headers ) ;
194
+ const resp = await this . _httpClient . postJson < { token : string } > ( pth , body , headers ) ;
195
195
const statusCode = resp . statusCode || 500 ;
196
196
if ( statusCode < 200 || statusCode > 299 ) {
197
197
throw new Error ( `Failed to call ${ pth } : HTTP ${ statusCode } : ${ resp . result || '[no body]' } ` ) ;
@@ -203,19 +203,10 @@ export class IAMCredentialsClient {
203
203
}
204
204
return result . token ;
205
205
} catch ( err ) {
206
+ const msg = errorMessage ( err ) ;
206
207
throw new Error (
207
- `Failed to generate Google Cloud OpenID Connect ID token for ${ serviceAccount } : ${ err } ` ,
208
+ `Failed to generate Google Cloud OpenID Connect ID token for ${ serviceAccount } : ${ msg } ` ,
208
209
) ;
209
210
}
210
211
}
211
212
}
212
-
213
- export { AuthClient } from './client/auth_client' ;
214
- export {
215
- ServiceAccountKeyClientParameters ,
216
- ServiceAccountKeyClient ,
217
- } from './client/credentials_json_client' ;
218
- export {
219
- WorkloadIdentityFederationClientParameters ,
220
- WorkloadIdentityFederationClient ,
221
- } from './client/workload_identity_client' ;
0 commit comments