-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c2bb94
commit 7aba4e4
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const getSecretValue = async () => { | ||
console.log('Fetching secrets...'); | ||
|
||
const httpPort = process.env.PARAMETERS_SECRETS_EXTENSION_HTTP_PORT || 2773; | ||
const url = `http://localhost:${httpPort}/secretsmanager/get?secretId=${encodeURIComponent( | ||
process.env.SECRET_ARN | ||
)}`; | ||
const options = { | ||
headers: { | ||
'X-Aws-Parameters-Secrets-Token': process.env.AWS_SESSION_TOKEN, | ||
}, | ||
method: 'GET', | ||
}; | ||
|
||
const response = await fetch(url, options); | ||
|
||
if (!response.ok) { | ||
const json = await response.json().catch((err) => err.message); | ||
console.error('Invalid response - response:', JSON.stringify(response)); | ||
console.error('Invalid response - json:', json); | ||
throw new Error(`Invalid ${response.status} response`); | ||
} | ||
|
||
const result = await response.json(); | ||
|
||
if (!result) { | ||
throw new Error('Error getting secret', result); | ||
} | ||
|
||
return JSON.parse(result.SecretString); | ||
}; | ||
|
||
const transformSecrets = (secrets) => { | ||
Object.keys(secrets).forEach((key) => { | ||
process.env[key] = secrets[key]; | ||
}); | ||
}; | ||
|
||
/** | ||
* Middleware that gets the secrets from Lambda layer and transform into environment variables. | ||
* | ||
*/ | ||
const secretsToEnv = async () => { | ||
if (!process.env.SECRET_ARN) { | ||
return; | ||
} | ||
console.log('Secrets to env'); | ||
|
||
try { | ||
const secrets = await getSecretValue(); | ||
transformSecrets(secrets); | ||
|
||
return secrets; | ||
} catch (err) { | ||
throw err; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
secretsToEnv, | ||
}; |