Skip to content

Commit c680b5d

Browse files
aeitzmanlsiracdanielbankheadgcf-owl-bot[bot]
authored
feat: adds suppliers for custom subject token and AWS credentials (#1795)
* feat: refactor AWS and identity pool clients to use suppliers (#1776) * feat: refactor aws and identity pool credentials to use suppliers * Apply suggestions from code review Co-authored-by: Leo <[email protected]> * Apply suggestions from code review Co-authored-by: Daniel Bankhead <[email protected]> * updating suppliers to use options objects * updating docs * moved transporter to context object and deprecated consts * fix imports --------- Co-authored-by: Leo <[email protected]> Co-authored-by: Daniel Bankhead <[email protected]> * feat: adds support for creating AWS and Identity Pool credentials with custom suppliers. (#1783) * feat: adds support for users to build credentials with custom suppliers Also adds default values to make it easier to instantiate credentials in code. * Apply suggestions from code review Co-authored-by: Leo <[email protected]> * responding to review comments --------- Co-authored-by: Leo <[email protected]> * docs: adding documentation for programmatic auth feature (#1790) * docs: adding documentation for programmatic auth feature * fix typos * Apply suggestions from code review Co-authored-by: Leo <[email protected]> Co-authored-by: Daniel Bankhead <[email protected]> * add audience placeholder --------- Co-authored-by: Leo <[email protected]> Co-authored-by: Daniel Bankhead <[email protected]> * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix lint --------- Co-authored-by: Leo <[email protected]> Co-authored-by: Daniel Bankhead <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent b2eae07 commit c680b5d

13 files changed

+1744
-381
lines changed

.readme-partials.yaml

+126
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,55 @@ body: |-
356356
357357
You can now [start using the Auth library](#using-external-identities) to call Google Cloud resources from AWS.
358358
359+
### Accessing resources from AWS using a custom AWS security credentials supplier.
360+
361+
In order to access Google Cloud resources from Amazon Web Services (AWS), the following requirements are needed:
362+
- A workload identity pool needs to be created.
363+
- AWS needs to be added as an identity provider in the workload identity pool (The Google [organization policy](https://cloud.google.com/iam/docs/manage-workload-identity-pools-providers#restrict) needs to allow federation from AWS).
364+
- Permission to impersonate a service account needs to be granted to the external identity.
365+
366+
Follow the detailed [instructions](https://cloud.google.com/iam/docs/access-resources-aws) on how to configure workload identity federation from AWS.
367+
368+
If you want to use AWS security credentials that cannot be retrieved using methods supported natively by this library,
369+
a custom AwsSecurityCredentialsSupplier implementation may be specified when creating an AWS client. The supplier must
370+
return valid, unexpired AWS security credentials when called by the GCP credential.
371+
372+
Note that the client does not cache the returned AWS security credentials, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
373+
374+
```ts
375+
class AwsSupplier implements AwsSecurityCredentialsSupplier {
376+
async getAwsRegion(context: ExternalAccountSupplierContext): Promise<string> {
377+
// Return the current AWS region, i.e. 'us-east-2'.
378+
}
379+
380+
async getAwsSecurityCredentials(
381+
context: ExternalAccountSupplierContext
382+
): Promise<AwsSecurityCredentials> {
383+
const audience = context.audience;
384+
// Return valid AWS security credentials for the requested audience.
385+
}
386+
}
387+
388+
const clientOptions = {
389+
audience: '//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
390+
subject_token_type: 'urn:ietf:params:aws:token-type:aws4_request', // Set the subject token type.
391+
aws_security_credentials_supplier: new AwsSupplier() // Set the custom supplier.
392+
}
393+
394+
const client = new AwsClient(clientOptions);
395+
```
396+
397+
Where the [audience](https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation#provider-audience) is: `//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
398+
399+
Where the following variables need to be substituted:
400+
401+
* `$PROJECT_NUMBER`: The Google Cloud project number.
402+
* `$WORKLOAD_POOL_ID`: The workload pool ID.
403+
* `$PROVIDER_ID`: The provider ID.
404+
405+
406+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/sdk/gcloud/reference/iam/workload-identity-pools/create-cred-config).
407+
359408
### Access resources from Microsoft Azure
360409
361410
In order to access Google Cloud resources from Microsoft Azure, the following requirements are needed:
@@ -464,6 +513,44 @@ body: |-
464513
- `$URL_TO_GET_OIDC_TOKEN`: The URL of the local server endpoint to call to retrieve the OIDC token.
465514
- `$HEADER_KEY` and `$HEADER_VALUE`: The additional header key/value pairs to pass along the GET request to `$URL_TO_GET_OIDC_TOKEN`, e.g. `Metadata-Flavor=Google`.
466515
516+
### Accessing resources from an OIDC or SAML2.0 identity provider using a custom supplier
517+
518+
If you want to use OIDC or SAML2.0 that cannot be retrieved using methods supported natively by this library,
519+
a custom SubjectTokenSupplier implementation may be specified when creating an identity pool client. The supplier must
520+
return a valid, unexpired subject token when called by the GCP credential.
521+
522+
Note that the client does not cache the returned subject token, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
523+
524+
```ts
525+
class CustomSupplier implements SubjectTokenSupplier {
526+
async getSubjectToken(
527+
context: ExternalAccountSupplierContext
528+
): Promise<string> {
529+
const audience = context.audience;
530+
const subjectTokenType = context.subjectTokenType;
531+
// Return a valid subject token for the requested audience and subject token type.
532+
}
533+
}
534+
535+
const clientOptions = {
536+
audience: '//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
537+
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token', // Set the subject token type.
538+
subject_token_supplier: new CustomSupplier() // Set the custom supplier.
539+
}
540+
541+
const client = new CustomSupplier(clientOptions);
542+
```
543+
544+
Where the [audience](https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation#provider-audience) is: `//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
545+
546+
Where the following variables need to be substituted:
547+
548+
* `$PROJECT_NUMBER`: The Google Cloud project number.
549+
* `$WORKLOAD_POOL_ID`: The workload pool ID.
550+
* `$PROVIDER_ID`: The provider ID.
551+
552+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/sdk/gcloud/reference/iam/workload-identity-pools/create-cred-config).
553+
467554
#### Using External Account Authorized User workforce credentials
468555
469556
[External account authorized user credentials](https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#browser-based-sign-in) allow you to sign in with a web browser to an external identity provider account via the
@@ -842,6 +929,45 @@ body: |-
842929
You can now [use the Auth library](#using-external-identities) to call Google Cloud
843930
resources from an OIDC or SAML provider.
844931
932+
### Accessing resources from an OIDC or SAML2.0 identity provider using a custom supplier
933+
934+
If you want to use OIDC or SAML2.0 that cannot be retrieved using methods supported natively by this library,
935+
a custom SubjectTokenSupplier implementation may be specified when creating an identity pool client. The supplier must
936+
return a valid, unexpired subject token when called by the GCP credential.
937+
938+
Note that the client does not cache the returned subject token, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
939+
940+
```ts
941+
class CustomSupplier implements SubjectTokenSupplier {
942+
async getSubjectToken(
943+
context: ExternalAccountSupplierContext
944+
): Promise<string> {
945+
const audience = context.audience;
946+
const subjectTokenType = context.subjectTokenType;
947+
// Return a valid subject token for the requested audience and subject token type.
948+
}
949+
}
950+
951+
const clientOptions = {
952+
audience: '//iam.googleapis.com/locations/global/workforcePools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
953+
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token', // Set the subject token type.
954+
subject_token_supplier: new CustomSupplier() // Set the custom supplier.
955+
}
956+
957+
const client = new CustomSupplier(clientOptions);
958+
```
959+
960+
Where the audience is: `//iam.googleapis.com/locations/global/workforcePools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
961+
962+
Where the following variables need to be substituted:
963+
964+
* `WORKFORCE_POOL_ID`: The worforce pool ID.
965+
* `$PROVIDER_ID`: The provider ID.
966+
967+
and the workforce pool user project is the project number associated with the [workforce pools user project](https://cloud.google.com/iam/docs/workforce-identity-federation#workforce-pools-user-project).
968+
969+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#use_configuration_files_for_sign-in).
970+
845971
### Using External Identities
846972
847973
External identities (AWS, Azure and OIDC-based providers) can be used with `Application Default Credentials`.

README.md

+126
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,55 @@ The gcloud create-cred-config command will be updated to support this soon.
400400

401401
You can now [start using the Auth library](#using-external-identities) to call Google Cloud resources from AWS.
402402

403+
### Accessing resources from AWS using a custom AWS security credentials supplier.
404+
405+
In order to access Google Cloud resources from Amazon Web Services (AWS), the following requirements are needed:
406+
- A workload identity pool needs to be created.
407+
- AWS needs to be added as an identity provider in the workload identity pool (The Google [organization policy](https://cloud.google.com/iam/docs/manage-workload-identity-pools-providers#restrict) needs to allow federation from AWS).
408+
- Permission to impersonate a service account needs to be granted to the external identity.
409+
410+
Follow the detailed [instructions](https://cloud.google.com/iam/docs/access-resources-aws) on how to configure workload identity federation from AWS.
411+
412+
If you want to use AWS security credentials that cannot be retrieved using methods supported natively by this library,
413+
a custom AwsSecurityCredentialsSupplier implementation may be specified when creating an AWS client. The supplier must
414+
return valid, unexpired AWS security credentials when called by the GCP credential.
415+
416+
Note that the client does not cache the returned AWS security credentials, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
417+
418+
```ts
419+
class AwsSupplier implements AwsSecurityCredentialsSupplier {
420+
async getAwsRegion(context: ExternalAccountSupplierContext): Promise<string> {
421+
// Return the current AWS region, i.e. 'us-east-2'.
422+
}
423+
424+
async getAwsSecurityCredentials(
425+
context: ExternalAccountSupplierContext
426+
): Promise<AwsSecurityCredentials> {
427+
const audience = context.audience;
428+
// Return valid AWS security credentials for the requested audience.
429+
}
430+
}
431+
432+
const clientOptions = {
433+
audience: '//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
434+
subject_token_type: 'urn:ietf:params:aws:token-type:aws4_request', // Set the subject token type.
435+
aws_security_credentials_supplier: new AwsSupplier() // Set the custom supplier.
436+
}
437+
438+
const client = new AwsClient(clientOptions);
439+
```
440+
441+
Where the [audience](https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation#provider-audience) is: `//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
442+
443+
Where the following variables need to be substituted:
444+
445+
* `$PROJECT_NUMBER`: The Google Cloud project number.
446+
* `$WORKLOAD_POOL_ID`: The workload pool ID.
447+
* `$PROVIDER_ID`: The provider ID.
448+
449+
450+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/sdk/gcloud/reference/iam/workload-identity-pools/create-cred-config).
451+
403452
### Access resources from Microsoft Azure
404453

405454
In order to access Google Cloud resources from Microsoft Azure, the following requirements are needed:
@@ -508,6 +557,44 @@ Where the following variables need to be substituted:
508557
- `$URL_TO_GET_OIDC_TOKEN`: The URL of the local server endpoint to call to retrieve the OIDC token.
509558
- `$HEADER_KEY` and `$HEADER_VALUE`: The additional header key/value pairs to pass along the GET request to `$URL_TO_GET_OIDC_TOKEN`, e.g. `Metadata-Flavor=Google`.
510559

560+
### Accessing resources from an OIDC or SAML2.0 identity provider using a custom supplier
561+
562+
If you want to use OIDC or SAML2.0 that cannot be retrieved using methods supported natively by this library,
563+
a custom SubjectTokenSupplier implementation may be specified when creating an identity pool client. The supplier must
564+
return a valid, unexpired subject token when called by the GCP credential.
565+
566+
Note that the client does not cache the returned subject token, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
567+
568+
```ts
569+
class CustomSupplier implements SubjectTokenSupplier {
570+
async getSubjectToken(
571+
context: ExternalAccountSupplierContext
572+
): Promise<string> {
573+
const audience = context.audience;
574+
const subjectTokenType = context.subjectTokenType;
575+
// Return a valid subject token for the requested audience and subject token type.
576+
}
577+
}
578+
579+
const clientOptions = {
580+
audience: '//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
581+
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token', // Set the subject token type.
582+
subject_token_supplier: new CustomSupplier() // Set the custom supplier.
583+
}
584+
585+
const client = new CustomSupplier(clientOptions);
586+
```
587+
588+
Where the [audience](https://cloud.google.com/iam/docs/best-practices-for-using-workload-identity-federation#provider-audience) is: `//iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
589+
590+
Where the following variables need to be substituted:
591+
592+
* `$PROJECT_NUMBER`: The Google Cloud project number.
593+
* `$WORKLOAD_POOL_ID`: The workload pool ID.
594+
* `$PROVIDER_ID`: The provider ID.
595+
596+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/sdk/gcloud/reference/iam/workload-identity-pools/create-cred-config).
597+
511598
#### Using External Account Authorized User workforce credentials
512599

513600
[External account authorized user credentials](https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#browser-based-sign-in) allow you to sign in with a web browser to an external identity provider account via the
@@ -886,6 +973,45 @@ credentials unless they do not meet your specific requirements.
886973
You can now [use the Auth library](#using-external-identities) to call Google Cloud
887974
resources from an OIDC or SAML provider.
888975

976+
### Accessing resources from an OIDC or SAML2.0 identity provider using a custom supplier
977+
978+
If you want to use OIDC or SAML2.0 that cannot be retrieved using methods supported natively by this library,
979+
a custom SubjectTokenSupplier implementation may be specified when creating an identity pool client. The supplier must
980+
return a valid, unexpired subject token when called by the GCP credential.
981+
982+
Note that the client does not cache the returned subject token, so caching logic should be implemented in the supplier to prevent multiple requests for the same resources.
983+
984+
```ts
985+
class CustomSupplier implements SubjectTokenSupplier {
986+
async getSubjectToken(
987+
context: ExternalAccountSupplierContext
988+
): Promise<string> {
989+
const audience = context.audience;
990+
const subjectTokenType = context.subjectTokenType;
991+
// Return a valid subject token for the requested audience and subject token type.
992+
}
993+
}
994+
995+
const clientOptions = {
996+
audience: '//iam.googleapis.com/locations/global/workforcePools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID', // Set the GCP audience.
997+
subject_token_type: 'urn:ietf:params:oauth:token-type:id_token', // Set the subject token type.
998+
subject_token_supplier: new CustomSupplier() // Set the custom supplier.
999+
}
1000+
1001+
const client = new CustomSupplier(clientOptions);
1002+
```
1003+
1004+
Where the audience is: `//iam.googleapis.com/locations/global/workforcePools/$WORKLOAD_POOL_ID/providers/$PROVIDER_ID`
1005+
1006+
Where the following variables need to be substituted:
1007+
1008+
* `WORKFORCE_POOL_ID`: The worforce pool ID.
1009+
* `$PROVIDER_ID`: The provider ID.
1010+
1011+
and the workforce pool user project is the project number associated with the [workforce pools user project](https://cloud.google.com/iam/docs/workforce-identity-federation#workforce-pools-user-project).
1012+
1013+
The values for audience, service account impersonation URL, and any other builder field can also be found by generating a [credential configuration file with the gcloud CLI](https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#use_configuration_files_for_sign-in).
1014+
8891015
### Using External Identities
8901016

8911017
External identities (AWS, Azure and OIDC-based providers) can be used with `Application Default Credentials`.

0 commit comments

Comments
 (0)