Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

feat: Add github-upload-public-key module #241

Merged
merged 23 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions github-upload-public-key/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ display_name: Github Upload Public Key
description: Automates uploading Coder public key to Github so users don't have to.
icon: ../.icons/github.svg
maintainer_github: f0ssel
verified: false
tags: [helper]
verified: true
tags: [helper, git]
---

# github-upload-public-key

<!-- Describes what this module does -->
Templates that utilize Github External Auth can automatically ensure that the Coder public key is uploaded to Github so that users can clone repositories without needing to upload the public key themselves.

```tf
module "github-upload-public-key" {
Expand All @@ -19,5 +19,16 @@ module "github-upload-public-key" {
}
```

<!-- Add a screencast or screenshot here put them in .images directory -->
<!-- TODO: Add examples -->
# Requirements

Github External Auth must be enabled in the workspace for this module to work. The Github app that is configured for external auth must have both read and write permissions to "Git SSH keys" in order to upload the public key. Additionally, a Coder admin must also have the `admin:public_key` scope added to the external auth configuration of the Coder deployment. For example:

```
CODER_EXTERNAL_AUTH_0_ID="USER_DEFINED_ID"
CODER_EXTERNAL_AUTH_0_TYPE=github
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
CODER_EXTERNAL_AUTH_0_SCOPES="repo,workflow,admin:public_key"
```

Note that the default scopes if not provided are `repo,workflow`.
10 changes: 8 additions & 2 deletions github-upload-public-key/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ if [ -z "$CODER_OWNER_SESSION_TOKEN" ]; then
exit 1
fi

echo "Fetching GitHub token..."
GITHUB_TOKEN=$(coder external-auth access-token github)
if [ $? -ne 0 ]; then
echo "Failed to fetch GitHub token!"
exit 1
fi
if [ -z "$GITHUB_TOKEN" ]; then
echo "No GITHUB_TOKEN in the workspace environment!"
echo "The user must be authenticated with Github before this script can be run."
echo "No GitHub token found!"
exit 1
fi
echo "GitHub token found!"

echo "Fetching Coder public SSH key..."
PUBLIC_KEY_RESPONSE=$(curl -L -s \
Expand Down