|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -package com.google.cloud.auth.samples; |
| 17 | +package com.google.cloud.auth.samples.customcredentials.aws; |
18 | 18 |
|
19 | 19 | // [START auth_custom_credential_supplier_aws] |
20 | 20 | import com.google.auth.oauth2.AwsCredentials; |
|
25 | 25 | import com.google.cloud.storage.Bucket; |
26 | 26 | import com.google.cloud.storage.Storage; |
27 | 27 | import com.google.cloud.storage.StorageOptions; |
| 28 | +import com.google.gson.Gson; |
| 29 | +import com.google.gson.reflect.TypeToken; |
28 | 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; |
29 | 36 | import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
30 | 37 | import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; |
31 | 38 | import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
|
41 | 48 | public class CustomCredentialSupplierAwsWorkload { |
42 | 49 |
|
43 | 50 | public static void main(String[] args) throws IOException { |
| 51 | + |
| 52 | + // Reads the custom-credentials-aws-secrets.json if running locally. |
| 53 | + loadConfigFromFile(); |
| 54 | + |
44 | 55 | // The audience for the workload identity federation. |
45 | 56 | // Format: //iam.googleapis.com/projects/<project-number>/locations/global/ |
46 | 57 | // workloadIdentityPools/<pool-id>/providers/<provider-id> |
47 | | - String gcpWorkloadAudience = System.getenv("GCP_WORKLOAD_AUDIENCE"); |
| 58 | + String gcpWorkloadAudience = getConfiguration("GCP_WORKLOAD_AUDIENCE"); |
48 | 59 |
|
49 | 60 | // The bucket to fetch data from. |
50 | | - String gcsBucketName = System.getenv("GCS_BUCKET_NAME"); |
| 61 | + String gcsBucketName = getConfiguration("GCS_BUCKET_NAME"); |
51 | 62 |
|
52 | 63 | // (Optional) The service account impersonation URL. |
53 | | - String saImpersonationUrl = System.getenv("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL"); |
| 64 | + String saImpersonationUrl = getConfiguration("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL"); |
54 | 65 |
|
55 | 66 | if (gcpWorkloadAudience == null || gcsBucketName == null) { |
56 | 67 | System.err.println( |
57 | | - "Error: GCP_WORKLOAD_AUDIENCE and GCS_BUCKET_NAME environment variables are required."); |
| 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 | + String secretsFile = "custom-credentials-aws-secrets.json"; |
| 109 | + if (!Files.exists(Paths.get(secretsFile))) { |
58 | 110 | return; |
59 | 111 | } |
60 | 112 |
|
61 | | - System.out.println("Getting metadata for bucket: " + gcsBucketName + "..."); |
62 | | - Bucket bucket = |
63 | | - authenticateWithAwsCredentials(gcpWorkloadAudience, saImpersonationUrl, gcsBucketName); |
| 113 | + try (Reader reader = Files.newBufferedReader(Paths.get(secretsFile))) { |
| 114 | + // Use Gson to parse the JSON file into a Map |
| 115 | + Gson gson = new Gson(); |
| 116 | + Type type = new TypeToken<Map<String, String>>() {}.getType(); |
| 117 | + Map<String, String> secrets = gson.fromJson(reader, type); |
| 118 | + |
| 119 | + if (secrets == null) { |
| 120 | + return; |
| 121 | + } |
64 | 122 |
|
65 | | - System.out.println(" --- SUCCESS! ---"); |
66 | | - System.out.printf("Bucket Name: %s%n", bucket.getName()); |
67 | | - System.out.printf("Bucket Location: %s%n", bucket.getLocation()); |
| 123 | + // AWS SDK for Java looks for System Properties with specific names (camelCase) |
| 124 | + // if environment variables are missing. |
| 125 | + if (secrets.containsKey("aws_access_key_id")) { |
| 126 | + System.setProperty("aws.accessKeyId", secrets.get("aws_access_key_id")); |
| 127 | + } |
| 128 | + if (secrets.containsKey("aws_secret_access_key")) { |
| 129 | + System.setProperty("aws.secretAccessKey", secrets.get("aws_secret_access_key")); |
| 130 | + } |
| 131 | + if (secrets.containsKey("aws_region")) { |
| 132 | + System.setProperty("aws.region", secrets.get("aws_region")); |
| 133 | + } |
| 134 | + |
| 135 | + // Set custom GCP variables as System Properties so getConfiguration() can find them. |
| 136 | + if (secrets.containsKey("gcp_workload_audience")) { |
| 137 | + System.setProperty("GCP_WORKLOAD_AUDIENCE", secrets.get("gcp_workload_audience")); |
| 138 | + } |
| 139 | + if (secrets.containsKey("gcs_bucket_name")) { |
| 140 | + System.setProperty("GCS_BUCKET_NAME", secrets.get("gcs_bucket_name")); |
| 141 | + } |
| 142 | + if (secrets.containsKey("gcp_service_account_impersonation_url")) { |
| 143 | + System.setProperty( |
| 144 | + "GCP_SERVICE_ACCOUNT_IMPERSONATION_URL", |
| 145 | + secrets.get("gcp_service_account_impersonation_url")); |
| 146 | + } |
| 147 | + |
| 148 | + } catch (IOException e) { |
| 149 | + System.err.println("Error reading secrets file: " + e.getMessage()); |
| 150 | + } |
68 | 151 | } |
69 | 152 |
|
70 | 153 | /** |
@@ -117,7 +200,7 @@ private static class CustomAwsSupplier implements AwsSecurityCredentialsSupplier |
117 | 200 | private String region; |
118 | 201 |
|
119 | 202 | public CustomAwsSupplier() { |
120 | | - // The AWS SDK handles memoization and refreshing internally. |
| 203 | + // The AWS SDK handles caching internally. |
121 | 204 | this.awsCredentialsProvider = DefaultCredentialsProvider.create(); |
122 | 205 | } |
123 | 206 |
|
|
0 commit comments