-
Notifications
You must be signed in to change notification settings - Fork 172
Decrypt secrets outside of the repo #1269
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
mokagio
wants to merge
17
commits into
trunk
Choose a base branch
from
ainfra-2731-decrypt-secrets-outside-of-repo-in-simplenote-macos
base: trunk
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
17 commits
Select commit
Hold shift + click to select a range
9892d0e
Generate the secret into the build dir, not the repo
mokagio 6459560
Compile SPCredentials.swift out of DerivedSources
mokagio 94b786c
Drop the manual credentials setup step
mokagio fcd149d
Use the env-lookup bash shebang in copy-secret
mokagio e4ae1b1
Quote the path expansions in copy-secret
mokagio ec768d4
Funnel both secret sources through one apply
mokagio bee3771
Trim the DerivedSources README to the surprise
mokagio 8c74cd0
Move `cp -v` explanation close to `cp -v` usage
mokagio 753f9a8
Remove now-outdated ignore rule for in-repo credentials
mokagio 800d08b
Add script description and reorder a few steps
mokagio 490bb9f
Introduce template + external credentials
mokagio 0a476b9
Decrypt secrets before the CI test build
mokagio dc96fb7
Run the CI tests as an external contributor
mokagio f194d6d
Condense verbose AI comment
mokagio d36a223
Drop the skip-if-unchanged check
mokagio 5321832
Stop advertising distributed testing credentials
mokagio 609051c
Default the input file list count under `set -u`
mokagio 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
| #!/bin/bash -euo pipefail | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Materialize secrets into the target's DERIVED_FILE_DIR so the decrypted | ||
| # credentials never land in the repo checkout. | ||
| # | ||
| # The compiled file comes from one of two sources, in order: | ||
| # | ||
| # 1. ${SECRETS_ROOT}, for internal contributors. `bundle exec fastlane run | ||
| # configure_apply` decrypts it there, outside the repo; this phase only | ||
| # reads it. | ||
| # 2. Simplenote/SPCredentials.external-contributors.swift — gitignored, so | ||
| # external contributors can keep their own Simperium credentials with | ||
| # little-to-no risk of committing them, starting from a copy of the | ||
| # committed template. | ||
| # | ||
| # If neither is present, the build will fail. | ||
|
|
||
| SECRETS_ROOT="${HOME}/.configure/simplenote-macos/secrets" | ||
| SECRETS_FILE="${SECRETS_ROOT}/SPCredentials.swift" | ||
|
Comment on lines
+10
to
+21
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, the paths here will soon change as we adopt |
||
| TEMPLATE_SECRETS_FILE="${SRCROOT}/Simplenote/SPCredentials.template.swift" | ||
| EXTERNAL_SECRETS_FILE="${SRCROOT}/Simplenote/SPCredentials.external-contributors.swift" | ||
|
|
||
| # To help the Xcode build system optimize the build, we want to ensure each of | ||
| # the secrets we want to copy is defined as an input file for the run script | ||
|
|
@@ -20,13 +42,14 @@ function ensure_is_in_input_files_list() { | |
| echo "error: Input file list verification needs a path to verify!" | ||
| exit 1 | ||
| fi | ||
| file_to_find=$1 | ||
|
|
||
| if [ $SCRIPT_INPUT_FILE_LIST_COUNT -eq 0 ]; then | ||
| if [ "${SCRIPT_INPUT_FILE_LIST_COUNT:-0}" -eq 0 ]; then | ||
| echo "error: No input file list given (.xcfilelist). Cannot continue." | ||
| exit 1 | ||
| fi | ||
|
|
||
| file_to_find=$1 | ||
|
|
||
| i=0 | ||
| found=false | ||
| while [[ $i -lt $SCRIPT_INPUT_FILE_LIST_COUNT && "$found" = false ]] | ||
|
|
@@ -35,55 +58,49 @@ function ensure_is_in_input_files_list() { | |
| file_list_resolved_var_name=SCRIPT_INPUT_FILE_LIST_${i} | ||
| # The following reads the processed xcfilelist line by line looking for | ||
| # the given file | ||
| while read input_file; do | ||
| while read -r input_file; do | ||
| if [ "$file_to_find" == "$input_file" ]; then | ||
| found=true | ||
| break | ||
| fi | ||
| done <"${!file_list_resolved_var_name}" | ||
| let i=i+1 | ||
| (( i=i+1 )) | ||
| done | ||
|
|
||
| if [ "$found" = false ]; then | ||
| echo "error: Could not find $file_to_find as an input to the build phase. Add $file_to_find to the input files list using the .xcfilelist." | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| SECRETS_ROOT="${HOME}/.configure/simplenote-macos/secrets" | ||
| SECRETS_FILE="${SECRETS_ROOT}/SPCredentials.swift" | ||
| EXAMPLE_SECRETS_FILE="${SRCROOT}/Simplenote/SPCredentials-demo.swift" | ||
| ensure_is_in_input_files_list "$SECRETS_FILE" | ||
| ensure_is_in_input_files_list "$EXTERNAL_SECRETS_FILE" | ||
|
|
||
| ensure_is_in_input_files_list $SECRETS_FILE | ||
| ensure_is_in_input_files_list $EXAMPLE_SECRETS_FILE | ||
| # The destination comes from the build phase's `outputPaths`, which Xcode | ||
| # exposes as SCRIPT_OUTPUT_FILE_N. Each consumer target writes into its own | ||
| # $(DERIVED_FILE_DIR), keeping the decrypted secret out of the checkout. | ||
| if [ "${SCRIPT_OUTPUT_FILE_COUNT:-0}" -lt 1 ]; then | ||
| echo "error: No output file given. Declare the destination in the build phase's output files list." | ||
| exit 1 | ||
| fi | ||
|
|
||
| SECRETS_DESTINATION_FILE="${SRCROOT}/Simplenote/Credentials/SPCredentials.swift" | ||
| mkdir -p $(dirname "$SECRETS_DESTINATION_FILE") | ||
| SECRETS_DESTINATION_FILE="${SCRIPT_OUTPUT_FILE_0}" | ||
| mkdir -p "$(dirname "$SECRETS_DESTINATION_FILE")" | ||
|
|
||
| if cmp --silent -- ${SECRETS_FILE} ${SECRETS_DESTINATION_FILE}; then | ||
| echo "☑️ Credentials were not modified. Skipping..." | ||
| apply() { | ||
| echo "Applying secrets from ${1}" | ||
| # `cp -v` names the destination, which differs per consumer target. | ||
| cp -v "$1" "$SECRETS_DESTINATION_FILE" | ||
| exit 0 | ||
| fi | ||
| } | ||
|
|
||
| if [ -f "$SECRETS_FILE" ]; then | ||
| echo "Applying Production Secrets" | ||
| cp -v "$SECRETS_FILE" "${SECRETS_DESTINATION_FILE}" | ||
| exit 0 | ||
| apply "$SECRETS_FILE" | ||
| fi | ||
|
|
||
| # No secrets file found. Use the example secrets file as a last resort, unless | ||
| # building for Release. | ||
|
|
||
| COULD_NOT_FIND_SECRET_MSG="Could not find secrets file at ${SECRETS_DESTINATION_FILE}. This is likely due to the source secrets being missing from ${SECRETS_ROOT}" | ||
| INTERNAL_CONTRIBUTOR_MSG="If you are an internal contributor, run \`bundle exec fastlane run configure_apply\` to update your secrets" | ||
| if [ -f "$EXTERNAL_SECRETS_FILE" ]; then | ||
| apply "$EXTERNAL_SECRETS_FILE" | ||
| fi | ||
|
|
||
| case $CONFIGURATION in | ||
| Release) | ||
| echo "error: $COULD_NOT_FIND_SECRET_MSG. Cannot continue Release build. $INTERNAL_CONTRIBUTOR_MSG and try again. External contributors should not need to perform a Release build." | ||
| exit 1 | ||
| ;; | ||
| *) | ||
| echo "warning: $COULD_NOT_FIND_SECRET_MSG. Falling back to $EXAMPLE_SECRETS_FILE. In a Release build, this would be an error. $INTERNAL_CONTRIBUTOR_MSG and try again. If you are an external contributor, you can ignore this warning." | ||
| echo "Applying Example Secrets" | ||
| cp -v "$EXAMPLE_SECRETS_FILE" "$SECRETS_DESTINATION_FILE" | ||
| ;; | ||
| esac | ||
| echo "error: No secrets found! Internal contributors: run \`bundle exec fastlane run configure_apply\`. External contributors: copy '${TEMPLATE_SECRETS_FILE}' to '${EXTERNAL_SECRETS_FILE}', fill in your own Simperium credentials, and build again." | ||
| exit 1 | ||
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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| # Inputs for the build phase run script marshalling the secrets for the app, | ||
| # currently running from the SimplenoteSecrets aggregate targets | ||
| # Inputs for the build phase run script marshalling the secrets for the app. | ||
| # Shared between the per-target "Copy Secret" build phases on the Simplenote and | ||
| # IntentsExtension targets — each phase writes its own SPCredentials.swift into | ||
| # $(DERIVED_FILE_DIR). | ||
| ${HOME}/.configure/simplenote-macos/secrets/SPCredentials.swift | ||
| ${SRCROOT}/Simplenote/SPCredentials-demo.swift | ||
| ${SRCROOT}/Simplenote/SPCredentials.external-contributors.swift | ||
| # Add the script itself as an input, so the build system will know to run it | ||
| # if it changes | ||
| ${SRCROOT}/Scripts/Build-Phases/copy-secret.sh |
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.
I considered keeping this just to avoid leaking. But given the repo is in maintenance, meaning it doesn't see a lot of active development, the chance of a leak are low and I prioritized keeping things tidy.
Additionally, worth remembering, that even if something from
SPCredentials.swiftleaked, its content are already de facto public because they are part of the shipped binary.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.
I get it, but at the same time, retaining one line (perhaps adding a comment) is cheap 🙂 anyway not a big deal