Skip to content

Commit 91a4fe4

Browse files
committed
Crisper load config from file.
1 parent 9243b12 commit 91a4fe4

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

auth/customcredentials/aws/customCredentialSupplierAws.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,16 @@ async function authenticateWithAwsCredentials(
117117
* variables are only set for the current process.
118118
*/
119119
function loadConfigFromFile() {
120-
const secretsFile = 'custom-credentials-aws-secrets.json';
121-
const secretsPath = path.resolve(__dirname, secretsFile);
122-
123-
if (!fs.existsSync(secretsPath)) {
124-
return;
125-
}
120+
const secretsPath = path.resolve(
121+
__dirname,
122+
'custom-credentials-aws-secrets.json'
123+
);
124+
if (!fs.existsSync(secretsPath)) return;
126125

127126
try {
128127
const secrets = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
129-
if (!secrets) {
130-
return;
131-
}
132128

133-
const configMapping = {
129+
const envMap = {
134130
aws_access_key_id: 'AWS_ACCESS_KEY_ID',
135131
aws_secret_access_key: 'AWS_SECRET_ACCESS_KEY',
136132
aws_region: 'AWS_REGION',
@@ -139,6 +135,12 @@ function loadConfigFromFile() {
139135
gcp_service_account_impersonation_url:
140136
'GCP_SERVICE_ACCOUNT_IMPERSONATION_URL',
141137
};
138+
139+
for (const [jsonKey, envKey] of Object.entries(envMap)) {
140+
if (secrets[jsonKey]) {
141+
process.env[envKey] = secrets[jsonKey];
142+
}
143+
}
142144
} catch (error) {
143145
console.error(`Error reading secrets file: ${error.message}`);
144146
}

auth/customcredentials/okta/README.md

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,14 @@ This command downloads all required Node.js libraries.
5757
npm install
5858
```
5959

60-
### 2. Configure Credentials
61-
62-
For local development, this sample reads configuration from a JSON file.
63-
64-
1. Create a file named `custom-credentials-okta-secrets.json` in the project root.
65-
2. Add the following content, replacing the placeholder values with your configuration:
66-
67-
```json
68-
{
69-
"gcp_workload_audience": "//iam.googleapis.com/projects/YOUR_PROJECT_NUMBER/locations/global/workloadIdentityPools/YOUR_POOL/providers/YOUR_PROVIDER",
70-
"gcs_bucket_name": "your-bucket-name",
71-
"okta_domain": "https://dev-123456.okta.com",
72-
"okta_client_id": "your-okta-client-id",
73-
"okta_client_secret": "your-okta-client-secret",
74-
"gcp_service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/your-sa@your-project.iam.gserviceaccount.com:generateAccessToken"
75-
}
76-
```
60+
### 2. Configure Credentials for Local Development
61+
62+
1. Copy the example secrets file to a new file named `custom-credentials-okta-secrets.json` in the project root:
63+
```bash
64+
cp custom-credentials-okta-secrets.json.example custom-credentials-okta-secrets.json
65+
```
66+
2. Open `custom-credentials-okta-secrets.json` and fill in the required values for your AWS and Google Cloud configuration. Do not check your `custom-credentials-okta-secrets.json` file into version control.
7767

78-
**Note:** Do not check your secrets file into version control.
7968

8069
### 3. Run the Application
8170

auth/customcredentials/okta/customCredentialSupplierOkta.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,17 @@ async function authenticateWithOktaCredentials(
140140
* variables are only set for the current process.
141141
*/
142142
function loadConfigFromFile() {
143-
const secretsFile = 'custom-credentials-okta-secrets.json';
144-
const secretsPath = path.resolve(__dirname, secretsFile);
145-
146-
if (!fs.existsSync(secretsPath)) {
147-
return;
148-
}
143+
const secretsPath = path.resolve(
144+
__dirname,
145+
'custom-credentials-okta-secrets.json'
146+
);
147+
if (!fs.existsSync(secretsPath)) return;
149148

150149
try {
151150
const secrets = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
152-
if (!secrets) {
153-
return;
154-
}
155151

156-
const configMapping = {
152+
// Define the mapping: JSON Key -> Environment Variable
153+
const envMap = {
157154
gcp_workload_audience: 'GCP_WORKLOAD_AUDIENCE',
158155
gcs_bucket_name: 'GCS_BUCKET_NAME',
159156
gcp_service_account_impersonation_url:
@@ -162,6 +159,13 @@ function loadConfigFromFile() {
162159
okta_client_id: 'OKTA_CLIENT_ID',
163160
okta_client_secret: 'OKTA_CLIENT_SECRET',
164161
};
162+
163+
// Iterate and assign
164+
for (const [jsonKey, envKey] of Object.entries(envMap)) {
165+
if (secrets[jsonKey]) {
166+
process.env[envKey] = secrets[jsonKey];
167+
}
168+
}
165169
} catch (error) {
166170
console.error(`Error reading secrets file: ${error.message}`);
167171
}

0 commit comments

Comments
 (0)