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

Commit b2e87ef

Browse files
committed
feat: Add github-upload-public-key module
1 parent c50c425 commit b2e87ef

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

github-upload-public-key/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
coder = {
6+
source = "coder/coder"
7+
version = ">= 0.12"
8+
}
9+
}
10+
}
11+
12+
variable "agent_id" {
13+
type = string
14+
description = "The ID of a Coder agent."
15+
}
16+
17+
resource "coder_script" "github_upload_public_key" {
18+
agent_id = var.agent_id
19+
script = templatefile("${path.module}/run.sh", {
20+
CODER_OWNER_SESSION_TOKEN : data.coder_workspace.me.owner_session_token,
21+
CODER_ACCESS_URL : data.coder_workspace.me.access_url
22+
})
23+
display_name = "Github Upload Public Key"
24+
icon = "/icon/github.svg"
25+
run_on_start = true
26+
start_blocks_login = true
27+
}

github-upload-public-key/run.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
CODER_ACCESS_URL="${CODER_ACCESS_URL}"
6+
CODER_OWNER_SESSION_TOKEN="${CODER_OWNER_SESSION_TOKEN}"
7+
8+
if [ -z "$CODER_ACCESS_URL" ]; then
9+
echo "No coder access url specified!"
10+
exit 1
11+
fi
12+
13+
if [ -z "$CODER_OWNER_SESSION_TOKEN" ]; then
14+
echo "No coder owner session token specified!"
15+
exit 1
16+
fi
17+
18+
if [ -z "$GITHUB_TOKEN" ]; then
19+
echo "No GITHUB_TOKEN in the workspace environment!"
20+
exit 1
21+
fi
22+
23+
PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces"
24+
25+
echo "Fetching Coder public SSH key..."
26+
PUBLIC_KEY=$(curl "$CODER_ACCESS_URL/api/v2/users/me/gitsshkey" \
27+
-H 'accept: application/json' \
28+
-H "cookie: coder_session_token=$CODER_OWNER_SESSION_TOKEN" \
29+
--fail \
30+
-s \
31+
| jq -r '.public_key'
32+
)
33+
34+
if [ -z "$PUBLIC_KEY" ]; then
35+
echo "No Coder public SSH key found!"
36+
exit 1
37+
fi
38+
39+
echo "Fetching GitHub public SSH keys..."
40+
GITHUB_MATCH=$(curl \
41+
-H "Accept: application/vnd.github+json" \
42+
-H "Authorization: Bearer $GITHUB_TOKEN" \
43+
-H "X-GitHub-Api-Version: 2022-11-28" \
44+
--fail \
45+
-s \
46+
https://api.github.com/user/keys \
47+
| jq -r --arg PUBLIC_KEY "$PUBLIC_KEY" '.[] | select(.key == $PUBLIC_KEY) | .key'
48+
)
49+
50+
if [ "$PUBLIC_KEY" = "$GITHUB_MATCH" ]; then
51+
echo "Coder public SSH key is already uploaded to GitHub!"
52+
exit 0
53+
fi
54+
echo "Coder public SSH key not found in GitHub keys!"
55+
echo "Uploading Coder public SSH key to GitHub..."
56+
curl -L \
57+
-X POST \
58+
-H "Accept: application/vnd.github+json" \
59+
-H "Authorization: Bearer $GITHUB_TOKEN" \
60+
-H "X-GitHub-Api-Version: 2022-11-28" \
61+
https://api.github.com/user/keys \
62+
-d "{\"title\":\"$PUBLIC_KEY_NAME\",\"key\":\"$PUBLIC_KEY\"}"
63+
64+
echo "Coder public SSH key uploaded to GitHub!"

0 commit comments

Comments
 (0)