You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to use external credentials (like AWS or Okta) that require custom retrieval logic not supported natively by the library, you can provide a custom supplier implementation.
66
-
67
-
### Custom AWS Credential Supplier
68
-
69
-
This sample demonstrates how to use the AWS SDK for Node.js as a custom `AwsSecurityCredentialsSupplier` to bridge AWS credentials—from sources like EKS IRSA, ECS, or local profiles—to Google Cloud Workload Identity.
To run this in an EKS cluster using IAM Roles for Service Accounts (IRSA):
97
-
98
-
1.**Configure IRSA:** Associate an AWS IAM Role with your Kubernetes Service Account.
99
-
2.**Configure GCP:** Allow the AWS IAM Role ARN to impersonate your Workload Identity Pool.
100
-
3.**Deploy:** When deploying your Node.js application, ensure the Pod uses the annotated Service Account. The AWS SDK in the sample will automatically detect the credentials injected by the EKS OIDC webhook.
101
-
102
-
---
103
-
104
-
### Custom Okta Credential Supplier
105
-
106
-
This sample demonstrates how to use a custom `SubjectTokenSupplier` to fetch an OIDC token from **Okta** using the Client Credentials flow and exchange it for Google Cloud credentials via Workload Identity Federation.
107
-
108
-
#### 1. Okta Configuration
109
-
110
-
Ensure you have an Okta Machine-to-Machine (M2M) application set up with "Client Credentials" grant type enabled. You will need the Domain, Client ID, and Client Secret.
# Running the Custom AWS Credential Supplier Sample (Node.js)
2
+
3
+
This sample demonstrates how to use a custom AWS security credential supplier to authenticate with Google Cloud using AWS as an external identity provider. It uses the **AWS SDK for JavaScript (v3)** to fetch credentials from sources like Amazon Elastic Kubernetes Service (EKS) with IAM Roles for Service Accounts (IRSA), Elastic Container Service (ECS), or Fargate.
4
+
5
+
## Prerequisites
6
+
7
+
* An AWS account.
8
+
* A Google Cloud project with the IAM API enabled.
9
+
* A GCS bucket.
10
+
***Node.js 16** or later installed.
11
+
***npm** installed.
12
+
13
+
If you want to use AWS security credentials that cannot be retrieved using methods supported natively by the Google Auth library, a custom `AwsSecurityCredentialsSupplier` implementation may be specified. The supplier must return valid, unexpired AWS security credentials when called by the Google Cloud Auth library.
14
+
15
+
## Running Locally
16
+
17
+
For local development, you can provide credentials and configuration in a JSON file.
18
+
19
+
### Install Dependencies
20
+
21
+
Ensure you have Node.js installed, then install the required libraries:
22
+
23
+
```bash
24
+
npm install
25
+
```
26
+
27
+
### Configure Credentials for Local Development
28
+
29
+
1. Copy the example secrets file to a new file named `custom-credentials-aws-secrets.json` in the project root:
2. Open `custom-credentials-aws-secrets.json` and fill in the required values for your AWS and Google Cloud configuration. Do not check your `custom-credentials-aws-secrets.json` file into version control.
34
+
35
+
36
+
### Run the Application
37
+
38
+
Execute the script using node:
39
+
40
+
```bash
41
+
node customCredentialSupplierAws.js
42
+
```
43
+
44
+
When run locally, the application will detect the `custom-credentials-aws-secrets.json` file and use it to configure the necessary environment variables for the AWS SDK.
45
+
46
+
## Running in a Containerized Environment (EKS)
47
+
48
+
This section provides a brief overview of how to run the sample in an Amazon EKS cluster.
49
+
50
+
### EKS Cluster Setup
51
+
52
+
First, you need an EKS cluster. You can create one using `eksctl` or the AWS Management Console. For detailed instructions, refer to the [Amazon EKS documentation](https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html).
53
+
54
+
### Configure IAM Roles for Service Accounts (IRSA)
55
+
56
+
IRSA enables you to associate an IAM role with a Kubernetes service account. This provides a secure way for your pods to access AWS services without hardcoding long-lived credentials.
57
+
58
+
Run the following command to create the IAM role and bind it to a Kubernetes Service Account:
>**Note**: The `--attach-policy-arn` flag is used here to demonstrate attaching permissions. Update this with the specific AWS policy ARN your application requires.
72
+
73
+
For a deep dive into how this works without using `eksctl`, refer to the [IAM Roles for Service Accounts](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) documentation.
74
+
75
+
### Configure Google Cloud to Trust the AWS Role
76
+
77
+
To allow your AWS role to authenticate as a Google Cloud service account, you need to configure Workload Identity Federation. This process involves these key steps:
78
+
79
+
1. **Create a Workload Identity Pool and an AWS Provider:** The pool holds the configuration, and the provider is set up to trust your AWS account.
80
+
81
+
2. **Create or selecta Google Cloud Service Account:** This service account will be impersonated by your AWS role.
82
+
83
+
3. **Bind the AWS Role to the Google Cloud Service Account:** Create an IAM policy binding that gives your AWS role the `Workload Identity User` (`roles/iam.workloadIdentityUser`) role on the Google Cloud service account.
84
+
85
+
For more detailed information, see the documentation on [Configuring Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds).
86
+
87
+
### Containerize and Package the Application
88
+
89
+
Create a `Dockerfile`for the Node.js application and push the image to a container registry (for example Amazon ECR) that your EKS cluster can access.
90
+
91
+
**Note:** The provided [`Dockerfile`](Dockerfile) is an example and may need modification for your specific needs.
92
+
93
+
Build and push the image:
94
+
```bash
95
+
docker build -t your-container-image:latest .
96
+
docker push your-container-image:latest
97
+
```
98
+
99
+
### Deploy to EKS
100
+
101
+
Create a Kubernetes deployment manifest to deploy your application to the EKS cluster. See the [`pod.yaml`](pod.yaml) file for an example.
102
+
103
+
**Note:** The provided [`pod.yaml`](pod.yaml) is an example and may need to be modified for your specific needs.
104
+
105
+
Deploy the pod:
106
+
107
+
```bash
108
+
kubectl apply -f pod.yaml
109
+
```
110
+
111
+
### Clean Up
112
+
113
+
To clean up the resources, delete the EKS cluster and any other AWS and Google Cloud resources you created.
114
+
115
+
```bash
116
+
eksctl delete cluster --name your-cluster-name
117
+
```
118
+
119
+
## Testing
120
+
121
+
This sample is not continuously tested. It is provided forinstructional purposes and may require modifications to workin your environment.
0 commit comments