|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.auth.samples.customcredentials.aws; |
| 18 | + |
| 19 | +// [START auth_custom_credential_supplier_aws] |
| 20 | +import com.google.auth.oauth2.AwsCredentials; |
| 21 | +import com.google.auth.oauth2.AwsSecurityCredentials; |
| 22 | +import com.google.auth.oauth2.AwsSecurityCredentialsSupplier; |
| 23 | +import com.google.auth.oauth2.ExternalAccountSupplierContext; |
| 24 | +import com.google.auth.oauth2.GoogleCredentials; |
| 25 | +import com.google.cloud.storage.Bucket; |
| 26 | +import com.google.cloud.storage.Storage; |
| 27 | +import com.google.cloud.storage.StorageOptions; |
| 28 | +import com.google.gson.Gson; |
| 29 | +import com.google.gson.reflect.TypeToken; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.Reader; |
| 32 | +import java.lang.reflect.Type; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import java.util.Map; |
| 36 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 37 | +import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; |
| 38 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
| 39 | +import software.amazon.awssdk.regions.Region; |
| 40 | +import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; |
| 41 | + |
| 42 | +// [END auth_custom_credential_supplier_aws] |
| 43 | + |
| 44 | +/** |
| 45 | + * This sample demonstrates how to use a custom AWS security credentials supplier to authenticate to |
| 46 | + * Google Cloud Storage using AWS Workload Identity Federation. |
| 47 | + */ |
| 48 | +public class CustomCredentialSupplierAwsWorkload { |
| 49 | + |
| 50 | + public static void main(String[] args) throws IOException { |
| 51 | + |
| 52 | + // Reads the custom-credentials-aws-secrets.json if running locally. |
| 53 | + loadConfigFromFile(); |
| 54 | + |
| 55 | + // The audience for the workload identity federation. |
| 56 | + // Format: //iam.googleapis.com/projects/<project-number>/locations/global/ |
| 57 | + // workloadIdentityPools/<pool-id>/providers/<provider-id> |
| 58 | + String gcpWorkloadAudience = getConfiguration("GCP_WORKLOAD_AUDIENCE"); |
| 59 | + |
| 60 | + // The bucket to fetch data from. |
| 61 | + String gcsBucketName = getConfiguration("GCS_BUCKET_NAME"); |
| 62 | + |
| 63 | + // (Optional) The service account impersonation URL. |
| 64 | + String saImpersonationUrl = getConfiguration("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL"); |
| 65 | + |
| 66 | + if (gcpWorkloadAudience == null || gcsBucketName == null) { |
| 67 | + System.err.println( |
| 68 | + "Required configuration missing. Please provide it in a " |
| 69 | + + "custom-credentials-aws-secrets.json file or as environment variables: " |
| 70 | + + "GCP_WORKLOAD_AUDIENCE, GCS_BUCKET_NAME"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + System.out.println("Retrieving metadata for bucket: " + gcsBucketName + "..."); |
| 76 | + Bucket bucket = |
| 77 | + authenticateWithAwsCredentials(gcpWorkloadAudience, saImpersonationUrl, gcsBucketName); |
| 78 | + |
| 79 | + System.out.println(" --- SUCCESS! ---"); |
| 80 | + System.out.println("Bucket details:"); |
| 81 | + System.out.printf(" Name: %s%n", bucket.getName()); |
| 82 | + System.out.printf(" Location: %s%n", bucket.getLocation()); |
| 83 | + System.out.printf(" Storage Class: %s%n", bucket.getStorageClass()); |
| 84 | + System.out.printf(" Metageneration: %s%n", bucket.getMetageneration()); |
| 85 | + } catch (Exception e) { |
| 86 | + System.err.println("Authentication or Request failed: " + e.getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Helper method to retrieve configuration. It checks Environment variables first, then System |
| 92 | + * properties (populated by loadConfigFromFile). |
| 93 | + */ |
| 94 | + static String getConfiguration(String key) { |
| 95 | + String value = System.getenv(key); |
| 96 | + if (value == null) { |
| 97 | + value = System.getProperty(key); |
| 98 | + } |
| 99 | + return value; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * If a local secrets file is present, load it into the System Properties. This is a |
| 104 | + * "just-in-time" configuration for local development. These variables are only set for the |
| 105 | + * current process. |
| 106 | + */ |
| 107 | + static void loadConfigFromFile() { |
| 108 | + // By default, this expects the file to be in the project root. |
| 109 | + String secretsFilePath = "custom-credentials-aws-secrets.json"; |
| 110 | + if (!Files.exists(Paths.get(secretsFilePath))) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + try (Reader reader = Files.newBufferedReader(Paths.get(secretsFilePath))) { |
| 115 | + // Use Gson to parse the JSON file into a Map |
| 116 | + Gson gson = new Gson(); |
| 117 | + Type type = new TypeToken<Map<String, String>>() {}.getType(); |
| 118 | + Map<String, String> secrets = gson.fromJson(reader, type); |
| 119 | + |
| 120 | + if (secrets == null) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // AWS SDK for Java looks for System Properties with specific names (camelCase) |
| 125 | + // if environment variables are missing. |
| 126 | + if (secrets.containsKey("aws_access_key_id")) { |
| 127 | + System.setProperty("aws.accessKeyId", secrets.get("aws_access_key_id")); |
| 128 | + } |
| 129 | + if (secrets.containsKey("aws_secret_access_key")) { |
| 130 | + System.setProperty("aws.secretAccessKey", secrets.get("aws_secret_access_key")); |
| 131 | + } |
| 132 | + if (secrets.containsKey("aws_region")) { |
| 133 | + System.setProperty("aws.region", secrets.get("aws_region")); |
| 134 | + } |
| 135 | + |
| 136 | + // Set custom GCP variables as System Properties so getConfiguration() can find them. |
| 137 | + if (secrets.containsKey("gcp_workload_audience")) { |
| 138 | + System.setProperty("GCP_WORKLOAD_AUDIENCE", secrets.get("gcp_workload_audience")); |
| 139 | + } |
| 140 | + if (secrets.containsKey("gcs_bucket_name")) { |
| 141 | + System.setProperty("GCS_BUCKET_NAME", secrets.get("gcs_bucket_name")); |
| 142 | + } |
| 143 | + if (secrets.containsKey("gcp_service_account_impersonation_url")) { |
| 144 | + System.setProperty( |
| 145 | + "GCP_SERVICE_ACCOUNT_IMPERSONATION_URL", |
| 146 | + secrets.get("gcp_service_account_impersonation_url")); |
| 147 | + } |
| 148 | + |
| 149 | + } catch (IOException e) { |
| 150 | + System.err.println("Error reading secrets file: " + e.getMessage()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Authenticates using a custom AWS credential supplier and retrieves bucket metadata. |
| 156 | + * |
| 157 | + * @param gcpWorkloadAudience The WIF provider audience. |
| 158 | + * @param saImpersonationUrl Optional service account impersonation URL. |
| 159 | + * @param gcsBucketName The GCS bucket name. |
| 160 | + * @return The Bucket object containing metadata. |
| 161 | + * @throws IOException If authentication fails. |
| 162 | + */ |
| 163 | + // [START auth_custom_credential_supplier_aws] |
| 164 | + public static Bucket authenticateWithAwsCredentials( |
| 165 | + String gcpWorkloadAudience, String saImpersonationUrl, String gcsBucketName) |
| 166 | + throws IOException { |
| 167 | + |
| 168 | + CustomAwsSupplier customSupplier = new CustomAwsSupplier(); |
| 169 | + |
| 170 | + AwsCredentials.Builder credentialsBuilder = |
| 171 | + AwsCredentials.newBuilder() |
| 172 | + .setAudience(gcpWorkloadAudience) |
| 173 | + // This token type indicates that the subject token is an AWS Signature Version 4 signed |
| 174 | + // request. This is required for AWS Workload Identity Federation. |
| 175 | + .setSubjectTokenType("urn:ietf:params:aws:token-type:aws4_request") |
| 176 | + .setAwsSecurityCredentialsSupplier(customSupplier); |
| 177 | + |
| 178 | + if (saImpersonationUrl != null) { |
| 179 | + credentialsBuilder.setServiceAccountImpersonationUrl(saImpersonationUrl); |
| 180 | + } |
| 181 | + |
| 182 | + GoogleCredentials credentials = credentialsBuilder.build(); |
| 183 | + |
| 184 | + Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService(); |
| 185 | + |
| 186 | + return storage.get(gcsBucketName); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Custom AWS Security Credentials Supplier. |
| 191 | + * |
| 192 | + * <p>This implementation resolves AWS credentials and regions using the default provider chains |
| 193 | + * from the AWS SDK (v2). This supports environment variables, ~/.aws/credentials, and EC2/EKS |
| 194 | + * metadata. |
| 195 | + */ |
| 196 | + private static class CustomAwsSupplier implements AwsSecurityCredentialsSupplier { |
| 197 | + private final AwsCredentialsProvider awsCredentialsProvider; |
| 198 | + private String region; |
| 199 | + |
| 200 | + public CustomAwsSupplier() { |
| 201 | + // The AWS SDK handles caching internally. |
| 202 | + this.awsCredentialsProvider = DefaultCredentialsProvider.create(); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public String getRegion(ExternalAccountSupplierContext context) { |
| 207 | + if (this.region == null) { |
| 208 | + Region awsRegion = new DefaultAwsRegionProviderChain().getRegion(); |
| 209 | + if (awsRegion == null) { |
| 210 | + throw new IllegalStateException( |
| 211 | + "Unable to resolve AWS region. Ensure AWS_REGION is set or configured."); |
| 212 | + } |
| 213 | + this.region = awsRegion.id(); |
| 214 | + } |
| 215 | + return this.region; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public AwsSecurityCredentials getCredentials(ExternalAccountSupplierContext context) { |
| 220 | + software.amazon.awssdk.auth.credentials.AwsCredentials credentials = |
| 221 | + this.awsCredentialsProvider.resolveCredentials(); |
| 222 | + |
| 223 | + if (credentials == null) { |
| 224 | + throw new IllegalStateException("Unable to resolve AWS credentials."); |
| 225 | + } |
| 226 | + |
| 227 | + String sessionToken = null; |
| 228 | + if (credentials instanceof AwsSessionCredentials) { |
| 229 | + sessionToken = ((AwsSessionCredentials) credentials).sessionToken(); |
| 230 | + } |
| 231 | + |
| 232 | + return new AwsSecurityCredentials( |
| 233 | + credentials.accessKeyId(), credentials.secretAccessKey(), sessionToken); |
| 234 | + } |
| 235 | + } |
| 236 | + // [END auth_custom_credential_supplier_aws] |
| 237 | +} |
0 commit comments