This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathexternalclient.ts
More file actions
93 lines (89 loc) · 3.8 KB
/
Copy pathexternalclient.ts
File metadata and controls
93 lines (89 loc) · 3.8 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
// 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 {
BaseExternalAccountClient,
// This is the identifier in the JSON config for the type of credential.
// This string constant indicates that an external account client should be
// instantiated.
// There are 3 types of JSON configs:
// 1. authorized_user => Google end user credential
// 2. service_account => Google service account credential
// 3. external_Account => non-GCP service (eg. AWS, Azure, K8s)
EXTERNAL_ACCOUNT_TYPE,
} from './baseexternalclient';
import {
IdentityPoolClient,
IdentityPoolClientOptions,
} from './identitypoolclient';
import {AwsClient, AwsClientOptions} from './awsclient';
import {
PluggableAuthClient,
PluggableAuthClientOptions,
} from './pluggable-auth-client';
export type ExternalAccountClientOptions =
| IdentityPoolClientOptions
| AwsClientOptions
| PluggableAuthClientOptions;
/**
* Dummy class with no constructor. Developers are expected to use fromJSON.
*/
export class ExternalAccountClient {
constructor() {
throw new Error(
'ExternalAccountClients should be initialized via: ' +
'ExternalAccountClient.fromJSON(), ' +
'directly via explicit constructors, eg. ' +
'new AwsClient(options), new IdentityPoolClient(options), new' +
'PluggableAuthClientOptions, or via ' +
'new GoogleAuth(options).getClient()',
);
}
/**
* This static method will instantiate the
* corresponding type of external account credential depending on the
* underlying credential source.
*
* **IMPORTANT**: This method does not validate the credential configuration.
* A security risk occurs when a credential configuration configured with
* malicious URLs is used. When the credential configuration is accepted from
* an untrusted source, you should validate it before using it with this
* method. For more details, see
* https://cloud.google.com/docs/authentication/external/externally-sourced-credentials.
*
* @param options The external account options object typically loaded
* from the external account JSON credential file.
* @return A BaseExternalAccountClient instance or null if the options
* provided do not correspond to an external account credential.
*/
static fromJSON(
options: ExternalAccountClientOptions,
): BaseExternalAccountClient | null {
console.warn(
'The `fromJSON` method does not validate the credential configuration. A security risk occurs when a credential configuration configured with malicious URLs is used. When the credential configuration is accepted from an untrusted source, you should validate it before using it with this method. For more details, see https://cloud.google.com/docs/authentication/external/externally-sourced-credentials.',
);
if (options && options.type === EXTERNAL_ACCOUNT_TYPE) {
if ((options as AwsClientOptions).credential_source?.environment_id) {
return new AwsClient(options as AwsClientOptions);
} else if (
(options as PluggableAuthClientOptions).credential_source?.executable
) {
return new PluggableAuthClient(options as PluggableAuthClientOptions);
} else {
return new IdentityPoolClient(options as IdentityPoolClientOptions);
}
} else {
return null;
}
}
}