Skip to content

Commit 7195767

Browse files
authored
Merge pull request #42 from DocSpring/nathan/provider-deploy-key
Push the provider sync with a deploy key instead of a PAT
2 parents 8bb8303 + 3f8a2ae commit 7195767

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,34 @@ jobs:
262262

263263
- name: Commit, push and tag the provider
264264
env:
265-
PUSH_TOKEN: ${{ secrets.TF_PROVIDER_GITHUB_TOKEN }}
265+
# A deploy key with write access to terraform-provider-logstruct only. Deploy keys don't expire: the PAT
266+
# this replaced had expired by the 0.2.0 release.
267+
DEPLOY_KEY: ${{ secrets.TF_PROVIDER_DEPLOY_KEY }}
266268
run: |
269+
if [[ -z "$DEPLOY_KEY" ]]; then
270+
echo "::error::TF_PROVIDER_DEPLOY_KEY isn't set in the release environment."
271+
exit 1
272+
fi
273+
mkdir -p ~/.ssh && chmod 700 ~/.ssh
274+
printf '%s\n' "$DEPLOY_KEY" > ~/.ssh/provider_deploy_key
275+
chmod 600 ~/.ssh/provider_deploy_key
276+
# GitHub's published host keys, so the key is only ever offered to github.com
277+
curl -fsS https://api.github.com/meta | jq -r '.ssh_keys[] | "github.com " + .' > ~/.ssh/github_known_hosts
278+
export GIT_SSH_COMMAND="ssh -i $HOME/.ssh/provider_deploy_key -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$HOME/.ssh/github_known_hosts"
279+
REMOTE=git@github.com:DocSpring/terraform-provider-logstruct.git
280+
# Stop before changing anything if GitHub doesn't accept the key
281+
if ! git ls-remote "$REMOTE" HEAD >/dev/null; then
282+
echo "::error::GitHub didn't accept TF_PROVIDER_DEPLOY_KEY for terraform-provider-logstruct: check the repo's deploy key (with write access) matches the secret."
283+
exit 1
284+
fi
267285
git config --global user.name "DocSpring Bot"
268286
git config --global user.email "docspring-bot@docspring.com"
269-
git clone https://github.com/DocSpring/terraform-provider-logstruct.git terraform-provider-logstruct
287+
git clone "$REMOTE" terraform-provider-logstruct
270288
cd terraform-provider-logstruct
271-
# The token is passed to the two pushes only, not stored in the clone's config
272-
AUTH="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$PUSH_TOKEN" | base64 -w0)"
273289
if [[ -s ../provider-changes.patch ]]; then
274290
git apply --index ../provider-changes.patch
275291
git commit -m "Sync catalog from logstruct $TAG_NAME"
276-
git -c "http.https://github.com/.extraheader=$AUTH" push origin HEAD:main
292+
git push origin HEAD:main
277293
else
278294
echo "No provider changes to commit."
279295
fi
@@ -282,4 +298,4 @@ jobs:
282298
exit 0
283299
fi
284300
git tag -a "$TAG_NAME" -m "Release $TAG_NAME (logstruct sync)"
285-
git -c "http.https://github.com/.extraheader=$AUTH" push origin "$TAG_NAME"
301+
git push origin "$TAG_NAME"

‎AGENTS.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ For template file changes (tests, rake tasks, etc.), just run `scripts/rails_tes
127127

128128
## Required secrets
129129

130-
- `TF_PROVIDER_GITHUB_TOKEN` (a `release` environment secret): PAT with write access to
131-
`DocSpring/terraform-provider-logstruct` for syncing/tagging.
130+
- `TF_PROVIDER_DEPLOY_KEY` (a `release` environment secret): the private half of a deploy key with write access to
131+
`DocSpring/terraform-provider-logstruct`, for syncing and tagging it. To replace it, generate an ed25519 key, add the
132+
public half as a write deploy key on the provider repo and set the private half as this secret.
133+
- The provider repo's own release (GoReleaser, on its `v*` tags) signs with the GPG key in its `GPG_PRIVATE_KEY`
134+
secret. The Terraform Registry only accepts releases signed by a key registered for the DocSpring namespace, so a
135+
new signing key must be added to the registry before the next provider release.
132136
- RubyGems needs no secret: the `logstruct` gem has a trusted publisher for this repository's Release workflow and its
133137
`release` environment.
134138

‎test/workflows/release_workflow_test.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,15 @@ def all_needs(job_id)
4242

4343
assert_equal({"publish_gem" => "release", "publish_provider" => "release"}, environments)
4444
end
45+
46+
test "the provider push uses the deploy key and checks it before changing anything" do
47+
steps = jobs.fetch("publish_provider").fetch("steps")
48+
env = steps.filter_map { |step| step["env"] }.reduce({}, :merge)
49+
run = steps.map { |step| step["run"].to_s }.join("\n")
50+
51+
assert_equal "${{ secrets.TF_PROVIDER_DEPLOY_KEY }}", env["DEPLOY_KEY"]
52+
refute_match(/TF_PROVIDER_GITHUB_TOKEN|x-access-token/, run)
53+
assert_operator run.index("git ls-remote"), :<, run.index("git clone")
54+
end
4555
end
4656
end

0 commit comments

Comments
 (0)