-
Notifications
You must be signed in to change notification settings - Fork 678
Expand file tree
/
Copy pathgdchclient.ts
More file actions
257 lines (233 loc) · 7.84 KB
/
gdchclient.ts
File metadata and controls
257 lines (233 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as https from 'https';
import {GaxiosOptions} from 'gaxios';
import {
GetTokenResponse,
OAuth2Client,
OAuth2ClientOptions,
} from './oauth2client';
import {CredentialRequest, Credentials} from './credentials';
const DEFAULT_LIFETIME_IN_SECONDS = 3600;
export const GDCH_CREDENTIALS_TYPE = 'gdch_credentials';
export interface GdchClientOptions extends OAuth2ClientOptions {
projectId?: string | null;
privateKeyId?: string;
privateKey?: string;
serviceIdentityName?: string;
tokenServerUri?: string;
caCertPath?: string;
apiAudience?: string;
lifetime?: number;
}
export interface GdchCredentialsInput {
type: 'gdch_credentials';
format_version: string;
project: string;
private_key_id: string;
private_key: string;
name: string;
token_uri: string;
ca_cert_path?: string;
}
export class GdchClient extends OAuth2Client {
projectId?: string;
privateKeyId?: string;
privateKey?: string;
serviceIdentityName?: string;
tokenServerUri?: string;
caCertPath?: string;
apiAudience?: string;
lifetime: number;
private gdchOptions: GdchClientOptions;
constructor(options: GdchClientOptions = {}) {
super(options);
this.gdchOptions = options;
this.projectId = options.projectId || undefined;
this.privateKeyId = options.privateKeyId;
this.privateKey = options.privateKey;
this.serviceIdentityName = options.serviceIdentityName;
this.tokenServerUri = options.tokenServerUri;
this.caCertPath = options.caCertPath;
this.apiAudience = options.apiAudience;
this.lifetime = options.lifetime || DEFAULT_LIFETIME_IN_SECONDS;
// Start with an expired refresh token, which will automatically be
// refreshed before the first API call is made.
this.credentials = {refresh_token: 'gdch-placeholder', expiry_date: 1};
}
createWithGdchAudience(apiAudience: string): GdchClient {
if (!apiAudience) {
throw new Error(
'Audience cannot be null or empty for GDCH service account credentials.'
);
}
return new GdchClient({
...this.gdchOptions,
projectId: this.projectId,
privateKeyId: this.privateKeyId,
privateKey: this.privateKey,
serviceIdentityName: this.serviceIdentityName,
tokenServerUri: this.tokenServerUri,
caCertPath: this.caCertPath,
lifetime: this.lifetime,
apiAudience,
});
}
fromJSON(json: GdchCredentialsInput): void {
if (!json) {
throw new Error(
'Must pass in a JSON object containing the GDCH credentials settings.'
);
}
if (json.type !== GDCH_CREDENTIALS_TYPE) {
throw new Error(
`The incoming JSON object does not have the "${GDCH_CREDENTIALS_TYPE}" type`
);
}
if (json.format_version !== '1') {
throw new Error('Only format version 1 is supported.');
}
if (!json.project) {
throw new Error('The incoming JSON object does not contain a project field');
}
if (!json.private_key_id) {
throw new Error(
'The incoming JSON object does not contain a private_key_id field'
);
}
if (!json.private_key) {
throw new Error('The incoming JSON object does not contain a private_key field');
}
if (!json.name) {
throw new Error('The incoming JSON object does not contain a name field');
}
if (!json.token_uri) {
throw new Error('The incoming JSON object does not contain a token_uri field');
}
this.projectId = json.project;
this.privateKeyId = json.private_key_id;
this.privateKey = json.private_key;
this.serviceIdentityName = json.name;
this.tokenServerUri = json.token_uri;
this.caCertPath = json.ca_cert_path;
}
protected async refreshTokenNoCache(): Promise<GetTokenResponse> {
if (!this.apiAudience) {
throw new Error(
'Audience cannot be null or empty for GDCH service account credentials. ' +
'Specify the audience by calling createWithGdchAudience.'
);
}
if (!this.privateKey) {
throw new Error('Private key is not configured for GDCH credentials.');
}
if (!this.privateKeyId) {
throw new Error('Private key ID is not configured for GDCH credentials.');
}
if (!this.projectId) {
throw new Error('Project is not configured for GDCH credentials.');
}
if (!this.serviceIdentityName) {
throw new Error('Service identity name is not configured for GDCH credentials.');
}
if (!this.tokenServerUri) {
throw new Error('Token server URI is not configured for GDCH credentials.');
}
const assertion = this.createAssertion();
const data = {
audience: this.apiAudience,
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
requested_token_type: 'urn:ietf:params:oauth:token-type:access_token',
subject_token: assertion,
subject_token_type: 'urn:k8s:params:oauth:token-type:serviceaccount',
};
const requestOpts: GaxiosOptions = {
url: this.tokenServerUri,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data,
responseType: 'json',
};
if (this.caCertPath) {
try {
const ca = fs.readFileSync(this.caCertPath);
requestOpts.agent = new https.Agent({ ca });
} catch (err) {
if (err instanceof Error) {
err.message = `Error reading certificate file from CA cert path, value '${this.caCertPath}': ${err.message}`;
}
throw err;
}
}
try {
const res = await this.transporter.request<CredentialRequest>(requestOpts);
const tokenResponse = res.data;
const tokens: Credentials = {
access_token: tokenResponse.access_token,
token_type: 'Bearer',
};
if (tokenResponse.expires_in) {
tokens.expiry_date = new Date().getTime() + tokenResponse.expires_in * 1000;
}
this.emit('tokens', tokens);
return {res, tokens};
} catch (e) {
if (e instanceof Error) {
e.message = `Error getting access token for GDCH service account: ${e.message}, iss: ${this.serviceIdentityName}`;
}
throw e;
}
}
private createAssertion(): string {
const header = {
alg: 'ES256',
typ: 'JWT',
kid: this.privateKeyId,
};
const issSub = `system:serviceaccount:${this.projectId}:${this.serviceIdentityName}`;
const currentTime = Math.floor(Date.now() / 1000);
const payload = {
iss: issSub,
sub: issSub,
iat: currentTime,
exp: currentTime + this.lifetime,
aud: this.tokenServerUri,
};
const encodedHeader = this.base64UrlEncode(JSON.stringify(header));
const encodedPayload = this.base64UrlEncode(JSON.stringify(payload));
const signingInput = `${encodedHeader}.${encodedPayload}`;
const signature = crypto.sign(
'sha256',
Buffer.from(signingInput),
{
key: this.privateKey!,
dsaEncoding: 'ieee-p1363',
}
);
const encodedSignature = this.base64UrlEncode(signature);
return `${signingInput}.${encodedSignature}`;
}
private base64UrlEncode(str: string | Buffer): string {
const buffer = typeof str === 'string' ? Buffer.from(str) : str;
return buffer
.toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}
}