/
test.externalclient.ts
221 lines (195 loc) Β· 8.21 KB
/
test.externalclient.ts
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
// Copyright 2021 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 assert from 'assert';
import {describe, it} from 'mocha';
import {AwsClient} from '../src/auth/awsclient';
import {IdentityPoolClient} from '../src/auth/identitypoolclient';
import {ExternalAccountClient} from '../src/auth/externalclient';
import {getAudience, getTokenUrl} from './externalclienthelper';
const serviceAccountKeys = {
type: 'service_account',
project_id: 'PROJECT_ID',
private_key_id: 'PRIVATE_KEY_ID',
private_key:
'-----BEGIN PRIVATE KEY-----\n' + 'REDACTED\n-----END PRIVATE KEY-----\n',
client_email: '$PROJECT_ID@appspot.gserviceaccount.com',
client_id: 'CLIENT_ID',
auth_uri: 'https://accounts.google.com/o/oauth2/auth',
token_uri: 'https://accounts.google.com/o/oauth2/token',
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
client_x509_cert_url:
'https://www.googleapis.com/robot/v1/metadata/x509/' +
'PROEJCT_ID%40appspot.gserviceaccount.com',
};
const fileSourcedOptions = {
type: 'external_account',
audience: getAudience(),
subject_token_type: 'urn:ietf:params:oauth:token-type:jwt',
token_url: getTokenUrl(),
credential_source: {
file: './test/fixtures/external-subject-token.txt',
},
};
const metadataBaseUrl = 'http://169.254.169.254';
const awsCredentialSource = {
environment_id: 'aws1',
region_url: `${metadataBaseUrl}/latest/meta-data/placement/availability-zone`,
url: `${metadataBaseUrl}/latest/meta-data/iam/security-credentials`,
regional_cred_verification_url:
'https://sts.{region}.amazonaws.com?' +
'Action=GetCallerIdentity&Version=2011-06-15',
};
const awsOptions = {
type: 'external_account',
audience: getAudience(),
subject_token_type: 'urn:ietf:params:aws:token-type:aws4_request',
token_url: getTokenUrl(),
credential_source: awsCredentialSource,
};
describe('ExternalAccountClient', () => {
describe('Constructor', () => {
it('should throw on initialization', () => {
assert.throws(() => {
return new ExternalAccountClient();
}, /ExternalAccountClients should be initialized via/);
});
});
describe('fromJSON()', () => {
const refreshOptions = {
eagerRefreshThresholdMillis: 1000 * 10,
forceRefreshOnFailure: true,
};
const invalidWorkforceIdentityPoolClientAudiences = [
'//iam.googleapis.com/locations/global/workloadIdentityPools/pool/providers/oidc',
'//iam.googleapis.com/locations/global/workforcepools/pool/providers/oidc',
'//iam.googleapis.com/locations/global/workforcePools//providers/oidc',
'//iam.googleapis.com/locations/global/workforcePools/providers/oidc',
'//iam.googleapis.com/locations/global/workloadIdentityPools/workforcePools/pool/providers/oidc',
'//iam.googleapis.com//locations/global/workforcePools/pool/providers/oidc',
'//iam.googleapis.com/project/123/locations/global/workforcePools/pool/providers/oidc',
'//iam.googleapis.com/locations/global/workforcePools/workloadIdentityPools/pool/providers/oidc',
'//iam.googleapis.com/locations/global/workforcePools/pool/providers',
'//iam.googleapis.com/locations/global/workforcePools/pool/providers/',
'//iam.googleapis.com/locations//workforcePools/pool/providers/oidc',
'//iam.googleapis.com/locations/workforcePools/pool/providers/oidc',
];
it('should return IdentityPoolClient on IdentityPoolClientOptions', () => {
const expectedClient = new IdentityPoolClient(fileSourcedOptions);
assert.deepStrictEqual(
ExternalAccountClient.fromJSON(fileSourcedOptions),
expectedClient
);
});
it('should return IdentityPoolClient with expected RefreshOptions', () => {
const expectedClient = new IdentityPoolClient(
fileSourcedOptions,
refreshOptions
);
assert.deepStrictEqual(
ExternalAccountClient.fromJSON(fileSourcedOptions, refreshOptions),
expectedClient
);
});
it('should return AwsClient on AwsClientOptions', () => {
const expectedClient = new AwsClient(awsOptions);
assert.deepStrictEqual(
ExternalAccountClient.fromJSON(awsOptions),
expectedClient
);
});
it('should return AwsClient with expected RefreshOptions', () => {
const expectedClient = new AwsClient(awsOptions, refreshOptions);
assert.deepStrictEqual(
ExternalAccountClient.fromJSON(awsOptions, refreshOptions),
expectedClient
);
});
it('should return an IdentityPoolClient with a workforce config', () => {
const validWorkforceIdentityPoolClientAudiences = [
'//iam.googleapis.com/locations/global/workforcePools/workforcePools/providers/provider',
'//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider',
'//iam.googleapis.com/locations/global/workforcePools/workloadPools/providers/oidc',
];
const workforceFileSourcedOptions = Object.assign(
{},
fileSourcedOptions,
{
workforce_pool_user_project: 'workforce_pool_user_project',
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token',
}
);
for (const validWorkforceIdentityPoolClientAudience of validWorkforceIdentityPoolClientAudiences) {
workforceFileSourcedOptions.audience =
validWorkforceIdentityPoolClientAudience;
const expectedClient = new IdentityPoolClient(
workforceFileSourcedOptions
);
assert.deepStrictEqual(
ExternalAccountClient.fromJSON(workforceFileSourcedOptions),
expectedClient
);
}
});
invalidWorkforceIdentityPoolClientAudiences.forEach(
invalidWorkforceIdentityPoolClientAudience => {
const workforceIdentityPoolClientInvalidOptions = Object.assign(
{},
fileSourcedOptions,
{
workforce_pool_user_project: 'workforce_pool_user_project',
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token',
}
);
it(`should throw an error when an invalid workforce audience ${invalidWorkforceIdentityPoolClientAudience} is provided with a workforce user project`, () => {
workforceIdentityPoolClientInvalidOptions.audience =
invalidWorkforceIdentityPoolClientAudience;
assert.throws(() => {
return ExternalAccountClient.fromJSON(
workforceIdentityPoolClientInvalidOptions
);
});
});
}
);
it('should return null when given non-ExternalAccountClientOptions', () => {
assert(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ExternalAccountClient.fromJSON(serviceAccountKeys as any) === null
);
});
it('should throw when given invalid ExternalAccountClient', () => {
const invalidOptions = Object.assign({}, fileSourcedOptions);
delete (invalidOptions as Partial<typeof fileSourcedOptions>)
.credential_source;
assert.throws(() => {
return ExternalAccountClient.fromJSON(invalidOptions);
});
});
it('should throw when given invalid IdentityPoolClient', () => {
const invalidOptions = Object.assign({}, fileSourcedOptions);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(invalidOptions as any).credential_source = {};
assert.throws(() => {
return ExternalAccountClient.fromJSON(invalidOptions);
});
});
it('should throw when given invalid AwsClientOptions', () => {
const invalidOptions = Object.assign({}, awsOptions);
invalidOptions.credential_source.environment_id = 'invalid';
assert.throws(() => {
return ExternalAccountClient.fromJSON(invalidOptions);
});
});
});
});