1 | import { Gaxios, GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
|
2 | import { Credentials } from './credentials';
|
3 | import { AuthClient, AuthClientOptions } from './authclient';
|
4 | import { BodyResponseCallback, Transporter } from '../transporters';
|
5 | import { GetAccessTokenResponse, Headers } from './oauth2client';
|
6 | import { SnakeToCamelObject } from '../util';
|
7 | /**
|
8 | * Offset to take into account network delays and server clock skews.
|
9 | */
|
10 | export declare const EXPIRATION_TIME_OFFSET: number;
|
11 | /**
|
12 | * The credentials JSON file type for external account clients.
|
13 | * There are 3 types of JSON configs:
|
14 | * 1. authorized_user => Google end user credential
|
15 | * 2. service_account => Google service account credential
|
16 | * 3. external_Account => non-GCP service (eg. AWS, Azure, K8s)
|
17 | */
|
18 | export declare const EXTERNAL_ACCOUNT_TYPE = "external_account";
|
19 | /**
|
20 | * Cloud resource manager URL used to retrieve project information.
|
21 | *
|
22 | * @deprecated use {@link BaseExternalAccountClient.cloudResourceManagerURL} instead
|
23 | **/
|
24 | export declare const CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/";
|
25 | /**
|
26 | * For backwards compatibility.
|
27 | */
|
28 | export { DEFAULT_UNIVERSE } from './authclient';
|
29 | /**
|
30 | * Shared options used to build {@link ExternalAccountClient} and
|
31 | * {@link ExternalAccountAuthorizedUserClient}.
|
32 | */
|
33 | export interface SharedExternalAccountClientOptions extends AuthClientOptions {
|
34 | /**
|
35 | * The Security Token Service audience, which is usually the fully specified
|
36 | * resource name of the workload or workforce pool provider.
|
37 | */
|
38 | audience: string;
|
39 | /**
|
40 | * The Security Token Service token URL used to exchange the third party token
|
41 | * for a GCP access token. If not provided, will default to
|
42 | * 'https://sts.googleapis.com/v1/token'
|
43 | */
|
44 | token_url?: string;
|
45 | }
|
46 | /**
|
47 | * Interface containing context about the requested external identity. This is
|
48 | * passed on all requests from external account clients to external identity suppliers.
|
49 | */
|
50 | export interface ExternalAccountSupplierContext {
|
51 | /**
|
52 | * The requested external account audience. For example:
|
53 | * * "//iam.googleapis.com/locations/global/workforcePools/$WORKFORCE_POOL_ID/providers/$PROVIDER_ID"
|
54 | * * "//iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID"
|
55 | */
|
56 | audience: string;
|
57 | /**
|
58 | * The requested subject token type. Expected values include:
|
59 | * * "urn:ietf:params:oauth:token-type:jwt"
|
60 | * * "urn:ietf:params:aws:token-type:aws4_request"
|
61 | * * "urn:ietf:params:oauth:token-type:saml2"
|
62 | * * "urn:ietf:params:oauth:token-type:id_token"
|
63 | */
|
64 | subjectTokenType: string;
|
65 | /** The {@link Gaxios} or {@link Transporter} instance from
|
66 | * the calling external account to use for requests.
|
67 | */
|
68 | transporter: Transporter | Gaxios;
|
69 | }
|
70 | /**
|
71 | * Base external account credentials json interface.
|
72 | */
|
73 | export interface BaseExternalAccountClientOptions extends SharedExternalAccountClientOptions {
|
74 | /**
|
75 | * Credential type, should always be 'external_account'.
|
76 | */
|
77 | type?: string;
|
78 | /**
|
79 | * The Security Token Service subject token type based on the OAuth 2.0
|
80 | * token exchange spec. Expected values include:
|
81 | * * 'urn:ietf:params:oauth:token-type:jwt'
|
82 | * * 'urn:ietf:params:aws:token-type:aws4_request'
|
83 | * * 'urn:ietf:params:oauth:token-type:saml2'
|
84 | * * 'urn:ietf:params:oauth:token-type:id_token'
|
85 | */
|
86 | subject_token_type: string;
|
87 | /**
|
88 | * The URL for the service account impersonation request. This URL is required
|
89 | * for some APIs. If this URL is not available, the access token from the
|
90 | * Security Token Service is used directly.
|
91 | */
|
92 | service_account_impersonation_url?: string;
|
93 | /**
|
94 | * Object containing additional options for service account impersonation.
|
95 | */
|
96 | service_account_impersonation?: {
|
97 | /**
|
98 | * The desired lifetime of the impersonated service account access token.
|
99 | * If not provided, the default lifetime will be 3600 seconds.
|
100 | */
|
101 | token_lifetime_seconds?: number;
|
102 | };
|
103 | /**
|
104 | * The endpoint used to retrieve account related information.
|
105 | */
|
106 | token_info_url?: string;
|
107 | /**
|
108 | * Client ID of the service account from the console.
|
109 | */
|
110 | client_id?: string;
|
111 | /**
|
112 | * Client secret of the service account from the console.
|
113 | */
|
114 | client_secret?: string;
|
115 | /**
|
116 | * The workforce pool user project. Required when using a workforce identity
|
117 | * pool.
|
118 | */
|
119 | workforce_pool_user_project?: string;
|
120 | /**
|
121 | * The scopes to request during the authorization grant.
|
122 | */
|
123 | scopes?: string[];
|
124 | /**
|
125 | * @example
|
126 | * https://cloudresourcemanager.googleapis.com/v1/projects/
|
127 | **/
|
128 | cloud_resource_manager_url?: string | URL;
|
129 | }
|
130 | /**
|
131 | * Interface defining the successful response for iamcredentials
|
132 | * generateAccessToken API.
|
133 | * https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateAccessToken
|
134 | */
|
135 | export interface IamGenerateAccessTokenResponse {
|
136 | accessToken: string;
|
137 | /**
|
138 | * ISO format used for expiration time.
|
139 | *
|
140 | * @example
|
141 | * '2014-10-02T15:01:23.045123456Z'
|
142 | */
|
143 | expireTime: string;
|
144 | }
|
145 | /**
|
146 | * Interface defining the project information response returned by the cloud
|
147 | * resource manager.
|
148 | * https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project
|
149 | */
|
150 | export interface ProjectInfo {
|
151 | projectNumber: string;
|
152 | projectId: string;
|
153 | lifecycleState: string;
|
154 | name: string;
|
155 | createTime?: string;
|
156 | parent: {
|
157 | [key: string]: any;
|
158 | };
|
159 | }
|
160 | /**
|
161 | * Internal interface for tracking the access token expiration time.
|
162 | */
|
163 | interface CredentialsWithResponse extends Credentials {
|
164 | res?: GaxiosResponse | null;
|
165 | }
|
166 | /**
|
167 | * Base external account client. This is used to instantiate AuthClients for
|
168 | * exchanging external account credentials for GCP access token and authorizing
|
169 | * requests to GCP APIs.
|
170 | * The base class implements common logic for exchanging various type of
|
171 | * external credentials for GCP access token. The logic of determining and
|
172 | * retrieving the external credential based on the environment and
|
173 | * credential_source will be left for the subclasses.
|
174 | */
|
175 | export declare abstract class BaseExternalAccountClient extends AuthClient {
|
176 | #private;
|
177 | /**
|
178 | * OAuth scopes for the GCP access token to use. When not provided,
|
179 | * the default https://www.googleapis.com/auth/cloud-platform is
|
180 | * used.
|
181 | */
|
182 | scopes?: string | string[];
|
183 | private cachedAccessToken;
|
184 | protected readonly audience: string;
|
185 | protected readonly subjectTokenType: string;
|
186 | private readonly serviceAccountImpersonationUrl?;
|
187 | private readonly serviceAccountImpersonationLifetime?;
|
188 | private readonly stsCredential;
|
189 | private readonly clientAuth?;
|
190 | private readonly workforcePoolUserProject?;
|
191 | projectNumber: string | null;
|
192 | private readonly configLifetimeRequested;
|
193 | protected credentialSourceType?: string;
|
194 | /**
|
195 | * @example
|
196 | * ```ts
|
197 | * new URL('https://cloudresourcemanager.googleapis.com/v1/projects/');
|
198 | * ```
|
199 | */
|
200 | protected cloudResourceManagerURL: URL | string;
|
201 | protected supplierContext: ExternalAccountSupplierContext;
|
202 | /**
|
203 | * Instantiate a BaseExternalAccountClient instance using the provided JSON
|
204 | * object loaded from an external account credentials file.
|
205 | * @param options The external account options object typically loaded
|
206 | * from the external account JSON credential file. The camelCased options
|
207 | * are aliases for the snake_cased options.
|
208 | * @param additionalOptions **DEPRECATED, all options are available in the
|
209 | * `options` parameter.** Optional additional behavior customization options.
|
210 | * These currently customize expiration threshold time and whether to retry
|
211 | * on 401/403 API request errors.
|
212 | */
|
213 | constructor(options: BaseExternalAccountClientOptions | SnakeToCamelObject<BaseExternalAccountClientOptions>, additionalOptions?: AuthClientOptions);
|
214 | /** The service account email to be impersonated, if available. */
|
215 | getServiceAccountEmail(): string | null;
|
216 | /**
|
217 | * Provides a mechanism to inject GCP access tokens directly.
|
218 | * When the provided credential expires, a new credential, using the
|
219 | * external account options, is retrieved.
|
220 | * @param credentials The Credentials object to set on the current client.
|
221 | */
|
222 | setCredentials(credentials: Credentials): void;
|
223 | /**
|
224 | * Triggered when a external subject token is needed to be exchanged for a GCP
|
225 | * access token via GCP STS endpoint.
|
226 | * This abstract method needs to be implemented by subclasses depending on
|
227 | * the type of external credential used.
|
228 | * @return A promise that resolves with the external subject token.
|
229 | */
|
230 | abstract retrieveSubjectToken(): Promise<string>;
|
231 | /**
|
232 | * @return A promise that resolves with the current GCP access token
|
233 | * response. If the current credential is expired, a new one is retrieved.
|
234 | */
|
235 | getAccessToken(): Promise<GetAccessTokenResponse>;
|
236 | /**
|
237 | * The main authentication interface. It takes an optional url which when
|
238 | * present is the endpoint being accessed, and returns a Promise which
|
239 | * resolves with authorization header fields.
|
240 | *
|
241 | * The result has the form:
|
242 | * { Authorization: 'Bearer <access_token_value>' }
|
243 | */
|
244 | getRequestHeaders(): Promise<Headers>;
|
245 | /**
|
246 | * Provides a request implementation with OAuth 2.0 flow. In cases of
|
247 | * HTTP 401 and 403 responses, it automatically asks for a new access token
|
248 | * and replays the unsuccessful request.
|
249 | * @param opts Request options.
|
250 | * @param callback callback.
|
251 | * @return A promise that resolves with the HTTP response when no callback is
|
252 | * provided.
|
253 | */
|
254 | request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
|
255 | request<T>(opts: GaxiosOptions, callback: BodyResponseCallback<T>): void;
|
256 | /**
|
257 | * @return A promise that resolves with the project ID corresponding to the
|
258 | * current workload identity pool or current workforce pool if
|
259 | * determinable. For workforce pool credential, it returns the project ID
|
260 | * corresponding to the workforcePoolUserProject.
|
261 | * This is introduced to match the current pattern of using the Auth
|
262 | * library:
|
263 | * const projectId = await auth.getProjectId();
|
264 | * const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
|
265 | * const res = await client.request({ url });
|
266 | * The resource may not have permission
|
267 | * (resourcemanager.projects.get) to call this API or the required
|
268 | * scopes may not be selected:
|
269 | * https://cloud.google.com/resource-manager/reference/rest/v1/projects/get#authorization-scopes
|
270 | */
|
271 | getProjectId(): Promise<string | null>;
|
272 | /**
|
273 | * Authenticates the provided HTTP request, processes it and resolves with the
|
274 | * returned response.
|
275 | * @param opts The HTTP request options.
|
276 | * @param reAuthRetried Whether the current attempt is a retry after a failed attempt due to an auth failure.
|
277 | * @return A promise that resolves with the successful response.
|
278 | */
|
279 | protected requestAsync<T>(opts: GaxiosOptions, reAuthRetried?: boolean): Promise<GaxiosResponse<T>>;
|
280 | /**
|
281 | * Forces token refresh, even if unexpired tokens are currently cached.
|
282 | * External credentials are exchanged for GCP access tokens via the token
|
283 | * exchange endpoint and other settings provided in the client options
|
284 | * object.
|
285 | * If the service_account_impersonation_url is provided, an additional
|
286 | * step to exchange the external account GCP access token for a service
|
287 | * account impersonated token is performed.
|
288 | * @return A promise that resolves with the fresh GCP access tokens.
|
289 | */
|
290 | protected refreshAccessTokenAsync(): Promise<CredentialsWithResponse>;
|
291 | /**
|
292 | * Returns the workload identity pool project number if it is determinable
|
293 | * from the audience resource name.
|
294 | * @param audience The STS audience used to determine the project number.
|
295 | * @return The project number associated with the workload identity pool, if
|
296 | * this can be determined from the STS audience field. Otherwise, null is
|
297 | * returned.
|
298 | */
|
299 | private getProjectNumber;
|
300 | /**
|
301 | * Exchanges an external account GCP access token for a service
|
302 | * account impersonated access token using iamcredentials
|
303 | * GenerateAccessToken API.
|
304 | * @param token The access token to exchange for a service account access
|
305 | * token.
|
306 | * @return A promise that resolves with the service account impersonated
|
307 | * credentials response.
|
308 | */
|
309 | private getImpersonatedAccessToken;
|
310 | /**
|
311 | * Returns whether the provided credentials are expired or not.
|
312 | * If there is no expiry time, assumes the token is not expired or expiring.
|
313 | * @param accessToken The credentials to check for expiration.
|
314 | * @return Whether the credentials are expired or not.
|
315 | */
|
316 | private isExpired;
|
317 | /**
|
318 | * @return The list of scopes for the requested GCP access token.
|
319 | */
|
320 | private getScopesArray;
|
321 | private getMetricsHeaderValue;
|
322 | }
|