Skip to content

Commit 9243b12

Browse files
committed
fix: simplify configuration of secrets rather than a bunch of if statements
also remove some extraneous comments Signed-off-by: Jennifer Davis <sigje@google.com>
1 parent 93c4a2e commit 9243b12

File tree

4 files changed

+18
-64
lines changed

4 files changed

+18
-64
lines changed
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
# Use the official Node.js image.
2-
# https://hub.docker.com/_/node
31
FROM node:20-slim
42

5-
# Create and change to the app directory.
63
WORKDIR /app
74

8-
# Copy application dependency manifests to the container image.
9-
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
105
COPY package*.json ./
116

12-
# Install production dependencies.
137
RUN npm install --omit=dev
148

15-
# Create a non-root user for security
169
RUN useradd -m appuser
1710

18-
# Copy local code to the container image.
1911
COPY --chown=appuser:appuser . .
2012

21-
# Switch to non-root user
2213
USER appuser
2314

24-
# Run the web service on container startup.
2515
CMD [ "node", "customCredentialSupplierAws.js" ]

auth/customcredentials/aws/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,3 @@ eksctl delete cluster --name your-cluster-name
119119
## Testing
120120

121121
This sample is not continuously tested. It is provided for instructional purposes and may require modifications to work in your environment.
122-
```

auth/customcredentials/aws/customCredentialSupplierAws.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ const {Storage} = require('@google-cloud/storage');
3030
*/
3131
class CustomAwsSupplier {
3232
constructor() {
33-
// Will be cached upon first resolution.
3433
this.region = null;
3534

36-
// Initialize the AWS credential provider.
37-
// The AWS SDK handles memoization (caching) and proactive refreshing internally.
3835
this.awsCredentialsProvider = fromNodeProviderChain();
3936
}
4037

@@ -65,7 +62,6 @@ class CustomAwsSupplier {
6562
* Retrieves AWS security credentials using the AWS SDK's default provider chain.
6663
*/
6764
async getAwsSecurityCredentials(_context) {
68-
// Call the initialized provider. It will return cached creds or refresh if needed.
6965
const awsCredentials = await this.awsCredentialsProvider();
7066

7167
if (!awsCredentials.accessKeyId || !awsCredentials.secretAccessKey) {
@@ -75,7 +71,6 @@ class CustomAwsSupplier {
7571
);
7672
}
7773

78-
// Map the AWS SDK format to the google-auth-library format.
7974
return {
8075
accessKeyId: awsCredentials.accessKeyId,
8176
secretAccessKey: awsCredentials.secretAccessKey,
@@ -131,40 +126,25 @@ function loadConfigFromFile() {
131126

132127
try {
133128
const secrets = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
134-
135129
if (!secrets) {
136130
return;
137131
}
138132

139-
// AWS SDK for Node.js looks for environment variables with specific names.
140-
if (secrets.aws_access_key_id) {
141-
process.env.AWS_ACCESS_KEY_ID = secrets.aws_access_key_id;
142-
}
143-
if (secrets.aws_secret_access_key) {
144-
process.env.AWS_SECRET_ACCESS_KEY = secrets.aws_secret_access_key;
145-
}
146-
if (secrets.aws_region) {
147-
process.env.AWS_REGION = secrets.aws_region;
148-
}
149-
150-
// Set custom GCP variables so they can be retrieved from process.env.
151-
if (secrets.gcp_workload_audience) {
152-
process.env.GCP_WORKLOAD_AUDIENCE = secrets.gcp_workload_audience;
153-
}
154-
if (secrets.gcs_bucket_name) {
155-
process.env.GCS_BUCKET_NAME = secrets.gcs_bucket_name;
156-
}
157-
if (secrets.gcp_service_account_impersonation_url) {
158-
process.env.GCP_SERVICE_ACCOUNT_IMPERSONATION_URL =
159-
secrets.gcp_service_account_impersonation_url;
160-
}
133+
const configMapping = {
134+
aws_access_key_id: 'AWS_ACCESS_KEY_ID',
135+
aws_secret_access_key: 'AWS_SECRET_ACCESS_KEY',
136+
aws_region: 'AWS_REGION',
137+
gcp_workload_audience: 'GCP_WORKLOAD_AUDIENCE',
138+
gcs_bucket_name: 'GCS_BUCKET_NAME',
139+
gcp_service_account_impersonation_url:
140+
'GCP_SERVICE_ACCOUNT_IMPERSONATION_URL',
141+
};
161142
} catch (error) {
162143
console.error(`Error reading secrets file: ${error.message}`);
163144
}
164145
}
165146

166147
async function main() {
167-
// Reads the secrets.json if running locally.
168148
loadConfigFromFile();
169149

170150
const gcpAudience = process.env.GCP_WORKLOAD_AUDIENCE;

auth/customcredentials/okta/customCredentialSupplierOkta.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const path = require('path');
2525
*/
2626
class OktaClientCredentialsSupplier {
2727
constructor(domain, clientId, clientSecret) {
28-
// Ensure domain URL is clean
2928
const cleanDomain = domain.endsWith('/') ? domain.slice(0, -1) : domain;
3029
this.oktaTokenUrl = `${cleanDomain}/oauth2/default/v1/token`;
3130

@@ -42,7 +41,6 @@ class OktaClientCredentialsSupplier {
4241
* @returns {Promise<string>} A promise that resolves with the Okta Access token.
4342
*/
4443
async getSubjectToken() {
45-
// Check if the current token is still valid (with a 60-second buffer).
4644
const isTokenValid =
4745
this.accessToken && Date.now() < this.expiryTime - 60 * 1000;
4846

@@ -151,37 +149,24 @@ function loadConfigFromFile() {
151149

152150
try {
153151
const secrets = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
154-
155152
if (!secrets) {
156153
return;
157154
}
158155

159-
// Map JSON keys (snake_case) to Environment Variables (UPPER_CASE)
160-
if (secrets.gcp_workload_audience) {
161-
process.env.GCP_WORKLOAD_AUDIENCE = secrets.gcp_workload_audience;
162-
}
163-
if (secrets.gcs_bucket_name) {
164-
process.env.GCS_BUCKET_NAME = secrets.gcs_bucket_name;
165-
}
166-
if (secrets.gcp_service_account_impersonation_url) {
167-
process.env.GCP_SERVICE_ACCOUNT_IMPERSONATION_URL =
168-
secrets.gcp_service_account_impersonation_url;
169-
}
170-
if (secrets.okta_domain) {
171-
process.env.OKTA_DOMAIN = secrets.okta_domain;
172-
}
173-
if (secrets.okta_client_id) {
174-
process.env.OKTA_CLIENT_ID = secrets.okta_client_id;
175-
}
176-
if (secrets.okta_client_secret) {
177-
process.env.OKTA_CLIENT_SECRET = secrets.okta_client_secret;
178-
}
156+
const configMapping = {
157+
gcp_workload_audience: 'GCP_WORKLOAD_AUDIENCE',
158+
gcs_bucket_name: 'GCS_BUCKET_NAME',
159+
gcp_service_account_impersonation_url:
160+
'GCP_SERVICE_ACCOUNT_IMPERSONATION_URL',
161+
okta_domain: 'OKTA_DOMAIN',
162+
okta_client_id: 'OKTA_CLIENT_ID',
163+
okta_client_secret: 'OKTA_CLIENT_SECRET',
164+
};
179165
} catch (error) {
180166
console.error(`Error reading secrets file: ${error.message}`);
181167
}
182168
}
183169

184-
// Load the configuration from the file when the module is loaded.
185170
loadConfigFromFile();
186171

187172
async function main() {

0 commit comments

Comments
 (0)