-
Notifications
You must be signed in to change notification settings - Fork 4
Generate env files from EJSON with local overrides #580
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
Open
kieran-osgood-shopify
wants to merge
3
commits into
main
Choose a base branch
from
kieran-osgood/ejson-generate-env-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Generates .env and e2e/.env from committed EJSON secrets. --check reports stale | ||
| # output without writing. A missing private key is supported for contributors who | ||
| # use their own config; other decrypt failures preserve existing files and fail. | ||
|
|
||
| ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| KEYDIR="${EJSON_KEYDIR:-/opt/ejson/keys}" | ||
|
|
||
| # name:source:destination | ||
| PAIRS=( | ||
| "demo:config/secrets/demo.ejson:.env" | ||
| "e2e:config/secrets/e2e.ejson:e2e/.env" | ||
| ) | ||
|
|
||
| mode="generate" | ||
| if [[ "${1:-}" == "--check" ]]; then | ||
| mode="check" | ||
| elif [[ $# -gt 0 ]]; then | ||
| echo "Usage: scripts/generate_env_files [--check]" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| public_key_of() { | ||
| sed -n 's/.*"_public_key"[[:space:]]*:[[:space:]]*"\([0-9a-f]\{64\}\)".*/\1/p' "$1" | head -n 1 | ||
| } | ||
|
|
||
| generated_header() { | ||
| local name="$1" | ||
| local source_rel="$2" | ||
| local dest_rel="$3" | ||
|
|
||
| cat <<EOF | ||
| # Generated by scripts/generate_env_files from ${source_rel}. | ||
| # Do not edit this file: the next \`dev up\` overwrites it. | ||
| # | ||
| # Shared changes: \`dev secrets edit ${name}\`, then commit ${source_rel}. | ||
| EOF | ||
|
|
||
| if [[ "${dest_rel}" == ".env" ]]; then | ||
| echo "# Gitignored overrides: .env.local." | ||
| fi | ||
|
|
||
| echo | ||
| } | ||
|
|
||
| if ! command -v ejson2env >/dev/null 2>&1; then | ||
| echo "generate_env_files: ejson2env is not installed, so no env file can be generated." >&2 | ||
| echo "generate_env_files: run \`dev up\` to install it." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| stale=() | ||
| generated=() | ||
| skipped=() | ||
|
|
||
| for pair in "${PAIRS[@]}"; do | ||
| name="${pair%%:*}" | ||
| rest="${pair#*:}" | ||
| source_rel="${rest%%:*}" | ||
| dest_rel="${rest#*:}" | ||
|
|
||
| source_path="${ROOT_DIR}/${source_rel}" | ||
| dest_path="${ROOT_DIR}/${dest_rel}" | ||
|
|
||
| if [[ ! -f "${source_path}" ]]; then | ||
| echo "generate_env_files: ${source_rel} does not exist, so ${dest_rel} is left alone." | ||
| skipped+=("${dest_rel}") | ||
| continue | ||
| fi | ||
|
|
||
| public_key="$(public_key_of "${source_path}")" | ||
| if [[ -z "${public_key}" ]]; then | ||
| echo "generate_env_files: ${source_rel} has no _public_key, so ${dest_rel} cannot be generated." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [[ ! -f "${KEYDIR}/${public_key}" ]]; then | ||
| echo "generate_env_files: no private key for ${public_key} in ${KEYDIR}, so ${dest_rel} is left alone." | ||
| skipped+=("${dest_rel}") | ||
| continue | ||
| fi | ||
|
|
||
| rendered="$(mktemp)" | ||
|
|
||
| if ! { | ||
| generated_header "${name}" "${source_rel}" "${dest_rel}" | ||
| ejson2env --quiet --keydir "${KEYDIR}" "${source_path}" | ||
| } >"${rendered}" 2>/dev/null; then | ||
| rm -f "${rendered}" | ||
| echo "generate_env_files: could not decrypt ${source_rel}, so ${dest_rel} is unchanged." >&2 | ||
| echo "generate_env_files: check that the private key for ${public_key} is the current one." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [[ "${mode}" == "check" ]]; then | ||
| if [[ ! -f "${dest_path}" ]] || ! cmp -s "${dest_path}" "${rendered}"; then | ||
| stale+=("${dest_rel}") | ||
| fi | ||
|
|
||
| rm -f "${rendered}" | ||
| continue | ||
| fi | ||
|
|
||
| mkdir -p "$(dirname "${dest_path}")" | ||
| mv "${rendered}" "${dest_path}" | ||
| generated+=("${dest_rel}") | ||
| done | ||
|
|
||
| if [[ "${mode}" == "check" ]]; then | ||
| if [[ "${#stale[@]}" -gt 0 ]]; then | ||
| echo "generate_env_files: missing or out of date: ${stale[*]}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "generate_env_files: generated env files are up to date." | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [[ "${#generated[@]}" -gt 0 ]]; then | ||
| echo "generate_env_files: wrote ${generated[*]}" | ||
| fi | ||
|
|
||
| if [[ "${#skipped[@]}" -gt 0 ]]; then | ||
| echo "generate_env_files: your own values in ${skipped[*]} stay under your control." | ||
| fi |
This file contains hidden or 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,45 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Preserves a hand-written .env as .env.local before .env becomes generated. | ||
| # Existing .env.local files are never overwritten. | ||
|
|
||
| ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
|
|
||
| ROOT_ENV="${ROOT_DIR}/.env" | ||
| ROOT_ENV_LOCAL="${ROOT_DIR}/.env.local" | ||
|
|
||
| mode="migrate" | ||
| if [[ "${1:-}" == "--check" ]]; then | ||
| mode="check" | ||
| elif [[ $# -gt 0 ]]; then | ||
| echo "Usage: scripts/migrate_env_to_local [--check]" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| nothing_to_do() { | ||
| [[ "${mode}" == "check" ]] && exit 0 | ||
|
|
||
| echo "migrate_env_to_local: $1" | ||
| exit 0 | ||
| } | ||
|
|
||
| if [[ -e "${ROOT_ENV_LOCAL}" ]]; then | ||
| nothing_to_do ".env.local already exists, so nothing is copied." | ||
| fi | ||
|
|
||
| if [[ ! -f "${ROOT_ENV}" ]]; then | ||
| nothing_to_do "no .env to migrate." | ||
| fi | ||
|
|
||
| if [[ "${mode}" == "check" ]]; then | ||
| echo "migrate_env_to_local: .env has no .env.local yet" | ||
| exit 1 | ||
| fi | ||
|
|
||
| cp "${ROOT_ENV}" "${ROOT_ENV_LOCAL}" | ||
|
|
||
| echo "migrate_env_to_local: copied .env to .env.local, because .env is now generated." | ||
| echo "migrate_env_to_local: .env.local wins over .env, so your values keep working." | ||
| echo "migrate_env_to_local: delete .env.local to follow the shared config instead." |
This file contains hidden or 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.
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.
This sets the domain for sample app to shared staff store instead of the e2e test store