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.
73
-
74
-
### Authenticate with Okta (Custom Supplier)
75
-
76
-
This sample demonstrates how to use a custom `IdentityPoolSubjectTokenSupplier` to fetch an OIDC token from Okta using the Client Credentials flow and exchange it for Google Cloud credentials.
This sample demonstrates how to use the **AWS SDK for Java (v2)** as a custom `AwsSecurityCredentialsSupplier` to bridge AWS credentials (from environment, `~/.aws/credentials`, or EKS/ECS metadata) to Google Cloud Workload Identity.
Copy file name to clipboardExpand all lines: auth/src/main/java/com/google/cloud/auth/samples/customcredentials/aws/CustomCredentialSupplierAwsWorkload.java
# Running the Custom AWS Credential Supplier Sample (Java)
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 Java (v2)** 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
+
***Java 11** or later installed.
11
+
***Maven** 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
+
### Build the Project
20
+
21
+
Ensure you have Java and Maven installed, then build the project to download dependencies and create an executable JAR:
22
+
23
+
```bash
24
+
mvn clean package
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
+
**Note:** This file is only used forlocal development and is not needed when runningin a containerized environment like EKS with IRSA.
36
+
37
+
### Run the Application
38
+
39
+
Execute the JAR file generated in the `target` directory:
*Note: Adjust the JAR filename version if you modified it in your `pom.xml`.*
46
+
47
+
When run locally, the application will detect the `custom-credentials-aws-secrets.json` file and use it to configure the necessary system properties for the AWS SDK.
48
+
49
+
## Running in a Containerized Environment (EKS)
50
+
51
+
This section provides a brief overview of how to run the sample in an Amazon EKS cluster.
52
+
53
+
### EKS Cluster Setup
54
+
55
+
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).
56
+
57
+
### Configure IAM Roles for Service Accounts (IRSA)
58
+
59
+
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.
60
+
61
+
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.
75
+
76
+
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.
77
+
78
+
### Configure Google Cloud to Trust the AWS Role
79
+
80
+
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:
81
+
82
+
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.
83
+
84
+
2. **Create or selecta Google Cloud Service Account:** This service account will be impersonated by your AWS role.
85
+
86
+
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.
87
+
88
+
For more detailed information, see the documentation on [Configuring Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds).
89
+
90
+
### Containerize and Package the Application
91
+
92
+
Create a `Dockerfile`for the Java application and push the image to a container registry (for example Amazon ECR) that your EKS cluster can access.
93
+
94
+
**Note:** The provided [`Dockerfile`](Dockerfile) uses a multi-stage build to compile the Java code. It is an example that may need modification for your specific needs.
95
+
96
+
Build and push the image:
97
+
```bash
98
+
docker build -t your-container-image:latest .
99
+
docker push your-container-image:latest
100
+
```
101
+
102
+
### Deploy to EKS
103
+
104
+
Create a Kubernetes deployment manifest to deploy your application to the EKS cluster. See the [`pod.yaml`](pod.yaml) file for an example.
105
+
106
+
**Note:** The provided [`pod.yaml`](pod.yaml) is an example and may need to be modified for your specific needs.
107
+
108
+
Deploy the pod:
109
+
110
+
```bash
111
+
kubectl apply -f pod.yaml
112
+
```
113
+
114
+
### Clean Up
115
+
116
+
To clean up the resources, delete the EKS cluster and any other AWS and Google Cloud resources you created.
117
+
118
+
```bash
119
+
eksctl delete cluster --name your-cluster-name
120
+
```
121
+
122
+
## Testing
123
+
124
+
This sample is not continuously tested. It is provided forinstructional purposes and may require modifications to workin your environment.
0 commit comments