-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access Parameter store config locally via Dotenv #4745
Merged
Merged
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
33254f3
Add check-dotenv to check-env of make file
jamesgorrie 970a6b1
add Dotenv webpack to webpack.config.server
jamesgorrie dae8e25
move check-env to gen-env as dev step
jamesgorrie 9bbbf58
move aws-parameters to scripts/dev
jamesgorrie 25533c8
move aws-parameters to scripts/dev
jamesgorrie 556b3f3
add dotenv to the install lifecycle
jamesgorrie 0681749
remove unused config setup
jamesgorrie 03282f0
lint
jamesgorrie 1ea468a
Check NODE_ENV=production to exit if we can't generate .env file
jamesgorrie fb0b187
add namespace to log
jamesgorrie 05763a9
add approachable value
jamesgorrie 6704887
Remove stray §
jamesgorrie cf852c8
throw error on not having AWS access
jamesgorrie 9bc60be
Merge branch 'server-side-dotenv' of github.com:guardian/dotcom-rende…
jamesgorrie 3e76b3b
move gen-dotenv to build and dev targets
jamesgorrie c2c959b
Merge branch 'main' of github.com:guardian/dotcom-rendering into serv…
jamesgorrie ddf749d
Add process of adding env vars
jamesgorrie 6ba8589
re suppress echoing in make
jamesgorrie 3c74346
Merge branch 'server-side-dotenv' of github.com:guardian/dotcom-rende…
jamesgorrie bdfc282
refactor how we get aws SSM parameters for generating dotenv file
jamesgorrie 9086f77
Merge branch 'main' of github.com:guardian/dotcom-rendering into serv…
jamesgorrie fd2dfc1
Add documentation on new dotenv functionality in DCR
OllysCoding b13b39a
Merge branch 'main' of github.com:guardian/dotcom-rendering into serv…
OllysCoding 7bd2da3
Better grammar on dotenv architecture doc
jamesgorrie d5582b4
Merge branch 'main' of github.com:guardian/dotcom-rendering into serv…
jamesgorrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Values | ||
|
||
## Approachable | ||
|
||
The project should have as few barriers as possible for anyone to work on it, including designers, UX, people outside of the Guardian and more. | ||
|
||
### Lines in the sand | ||
|
||
#### `.env` shouldn't be required | ||
|
||
While we use a `.env` file for configuration and secrets for `dotcom-rendering` to function correctly in `PROD`, not having it shouldn't block someone from running the project altogether. | ||
jamesgorrie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
If you're considering adding something to the `.env` file - please consult with the dotcom team first. Wherever possible seeking solutions like passing the data from [Frontend](https://github.com/guardian/frontend) is preferred. | ||
|
||
e.g. [Fallback for images salt](#) (In development so not linked to) |
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
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,87 @@ | ||
const { CredentialsProviderError } = require('@aws-sdk/property-provider'); | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
const { prompt, log, warn } = require('../env/log'); | ||
const secrets = require('../secrets'); | ||
const { getAwsSsmParameters } = require('./get-aws-ssm-parameters'); | ||
|
||
const ENV_PATH = path.resolve(__dirname, '../../.env'); | ||
|
||
const checkEnv = async () => { | ||
try { | ||
const env = (await fs.readFile(ENV_PATH)).toString(); | ||
|
||
let valid = true; | ||
for (const secret of secrets) { | ||
const regex = new RegExp(`^${secret.key.replace('.', '\\.')}=.*`); | ||
if (!env.match(regex)) valid = false; | ||
} | ||
|
||
return valid; | ||
} catch (_err) { | ||
return false; | ||
} | ||
}; | ||
|
||
const genEnv = async () => { | ||
const env = process.env.NODE_ENV === 'production' ? 'prod' : 'dev'; | ||
const parameters = await getAwsSsmParameters(env); | ||
|
||
let envString = ''; | ||
for (const secret of secrets) { | ||
envString += `${secret.key}=${parameters[secret.key]}\n`; | ||
} | ||
|
||
await fs.writeFile(ENV_PATH, envString); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
(async () => { | ||
try { | ||
const validEnv = await checkEnv(); | ||
if (!validEnv) { | ||
log( | ||
'[scripts/dotenv] .env file is missing, attemting to generate it from AWS parameters...', | ||
); | ||
|
||
await genEnv(); | ||
|
||
log('[scripts/dotenv] .env file written successfully'); | ||
} else { | ||
log('[scripts/dotenv] valid .env file exists, moving on...'); | ||
} | ||
} catch (err) { | ||
if (err instanceof CredentialsProviderError) { | ||
const PROD = process.env.NODE_ENV === 'production'; | ||
if (PROD) { | ||
warn( | ||
'[scripts/dotenv] could not generate .env file from AWS Parameter Store. Exiting', | ||
); | ||
throw err; | ||
} | ||
|
||
prompt( | ||
'[scripts/dotenv] Could not load AWS credentials to generate .env file', | ||
"[scripts/dotenv] This won't stop dotcom-rendering from working, it will just vary from PROD by:", | ||
); | ||
|
||
for (const secret of secrets) { | ||
prompt( | ||
`[scripts/dotenv] * ${secret.key}: ${secret.missingMessage}`, | ||
); | ||
} | ||
|
||
prompt( | ||
'', | ||
'[scripts/dotenv] To get things working PROD like either:', | ||
'[scripts/dotenv] * Get your credentials from Janus', | ||
'[scripts/dotenv] * Ask a local engineer for a copy of the .env file', | ||
'[scripts/dotenv] Then try again.', | ||
); | ||
|
||
process.exit(0); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
})(); |
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,56 @@ | ||
const { | ||
GetParametersByPathCommand, | ||
SSMClient, | ||
} = require('@aws-sdk/client-ssm'); | ||
|
||
process.env.AWS_PROFILE = 'frontend'; | ||
const ssm = new SSMClient({ region: 'eu-west-1' }); | ||
const env = process.env.NODE_ENV === 'production' ? 'prod' : 'dev'; | ||
|
||
async function* scrollParameters(params) { | ||
let command = new GetParametersByPathCommand(params); | ||
let response = await ssm.send(command); | ||
|
||
while (true) { | ||
const parameters = response.Parameters; | ||
|
||
// SSM returns undefined if there are no parameters | ||
if (parameters === undefined) { | ||
break; | ||
} | ||
|
||
for (const parameter of parameters) { | ||
yield parameter; | ||
} | ||
|
||
if (!response.NextToken) { | ||
break; | ||
} | ||
|
||
command = new GetParametersByPathCommand({ | ||
Path: `/dotcom/${env}/`, | ||
Recursive: true, | ||
WithDecryption: true, | ||
NextToken: response.NextToken, | ||
}); | ||
response = ssm.send(command); | ||
} | ||
} | ||
|
||
async function getAwsSsmParameters() { | ||
const parameters = {}; | ||
const params = { | ||
Path: `/dotcom/${env}/`, | ||
Recursive: true, | ||
WithDecryption: true, | ||
}; | ||
|
||
for await (const parameter of scrollParameters(params)) { | ||
const key = parameter.Name.replace(`/dotcom/${env}/`, ''); | ||
parameters[key] = parameter.Value; | ||
} | ||
|
||
return parameters; | ||
} | ||
|
||
module.exports = { getAwsSsmParameters }; |
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,8 @@ | ||
const secrets = [ | ||
{ | ||
key: 'IMAGE_SALT', | ||
missingMessage: 'Images will fallback to a placeholder image', | ||
}, | ||
]; | ||
|
||
module.exports = secrets; |
1 change: 1 addition & 0 deletions
1
dotcom-rendering/scripts/webpack/@types/dotenv-webpack/index.d.ts
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 @@ | ||
declare module 'dotenv-webpack'; | ||
mxdvl marked this conversation as resolved.
Show resolved
Hide resolved
|
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 An important point not to be forgotten
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worth enforcing in some or other way? That might be how we access to config is the only way I can think of.