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

Commit daed803

Browse files
committed
pr review
1 parent a239212 commit daed803

File tree

3 files changed

+35
-48
lines changed

3 files changed

+35
-48
lines changed

github-upload-public-key/main.test.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,37 @@ describe("github-upload-public-key", async () => {
1919
});
2020

2121
it("creates new key if one does not exist", async () => {
22-
const { instance, id } = await setupContainer();
22+
const { instance, id, server } = await setupContainer();
2323
await writeCoder(id, "echo foo");
24-
let exec = await execContainer(id, ["bash", "-c", instance.script]);
24+
let exec = await execContainer(id, [
25+
"env",
26+
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1),
27+
"GITHUB_API_URL=" + server.url.toString().slice(0, -1),
28+
"CODER_OWNER_SESSION_TOKEN=foo",
29+
"CODER_EXTERNAL_AUTH_ID=github",
30+
"bash",
31+
"-c",
32+
instance.script,
33+
]);
2534
expect(exec.stdout).toContain("Coder public SSH key uploaded to GitHub!");
2635
expect(exec.exitCode).toBe(0);
2736
// we need to increase timeout to pull the container
2837
}, 15000);
2938

3039
it("does nothing if one already exists", async () => {
31-
const { instance, id } = await setupContainer();
40+
const { instance, id, server } = await setupContainer();
3241
// use keyword to make server return a existing key
3342
await writeCoder(id, "echo findkey");
34-
let exec = await execContainer(id, ["bash", "-c", instance.script]);
43+
let exec = await execContainer(id, [
44+
"env",
45+
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1),
46+
"GITHUB_API_URL=" + server.url.toString().slice(0, -1),
47+
"CODER_OWNER_SESSION_TOKEN=foo",
48+
"CODER_EXTERNAL_AUTH_ID=github",
49+
"bash",
50+
"-c",
51+
instance.script,
52+
]);
3553
expect(exec.stdout).toContain(
3654
"Coder public SSH key is already uploaded to GitHub!",
3755
);
@@ -46,15 +64,11 @@ const setupContainer = async (
4664
const server = await setupServer();
4765
const state = await runTerraformApply(import.meta.dir, {
4866
agent_id: "foo",
49-
// trim the trailing slash on the URL
50-
access_url: server.url.toString().slice(0, -1),
51-
owner_session_token: "bar",
52-
github_api_url: server.url.toString().slice(0, -1),
5367
...vars,
5468
});
5569
const instance = findResourceInstance(state, "coder_script");
5670
const id = await runContainer(image);
57-
return { id, instance };
71+
return { id, instance, server };
5872
};
5973

6074
const setupServer = async (): Promise<Server> => {

github-upload-public-key/main.tf

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,13 @@ variable "github_api_url" {
2626
default = "https://api.github.com"
2727
}
2828

29-
// Optional variables mostly for testing purposes, will normally come from data.coder_workspace.me
30-
variable "access_url" {
31-
type = string
32-
description = "The access URL of the workspace."
33-
default = ""
34-
}
35-
36-
variable "owner_session_token" {
37-
type = string
38-
description = "The owner session token of the workspace."
39-
default = ""
40-
}
41-
4229
data "coder_workspace" "me" {}
4330

4431
resource "coder_script" "github_upload_public_key" {
4532
agent_id = var.agent_id
4633
script = templatefile("${path.module}/run.sh", {
47-
CODER_OWNER_SESSION_TOKEN : var.owner_session_token != "" ? var.owner_session_token : data.coder_workspace.me.owner_session_token,
48-
CODER_ACCESS_URL : var.access_url != "" ? var.access_url : data.coder_workspace.me.access_url,
34+
CODER_OWNER_SESSION_TOKEN : data.coder_workspace.me.owner_session_token,
35+
CODER_ACCESS_URL : data.coder_workspace.me.access_url,
4936
CODER_EXTERNAL_AUTH_ID : var.external_auth_id,
5037
GITHUB_API_URL : var.github_api_url,
5138
})

github-upload-public-key/run.sh

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,34 @@
22

33
set -e
44

5-
CODER_ACCESS_URL="${CODER_ACCESS_URL}"
6-
CODER_OWNER_SESSION_TOKEN="${CODER_OWNER_SESSION_TOKEN}"
7-
CODER_EXTERNAL_AUTH_ID="${CODER_EXTERNAL_AUTH_ID}"
8-
GITHUB_API_URL="${GITHUB_API_URL}"
9-
105
if [ -z "$CODER_ACCESS_URL" ]; then
11-
echo "No coder access url specified!"
6+
echo "No CODER_ACCESS_URL specified!"
127
exit 1
138
fi
149

1510
if [ -z "$CODER_OWNER_SESSION_TOKEN" ]; then
16-
echo "No coder owner session token specified!"
11+
echo "No CODER_OWNER_SESSION_TOKEN specified!"
1712
exit 1
1813
fi
1914

2015
if [ -z "$CODER_EXTERNAL_AUTH_ID" ]; then
21-
echo "No GitHub external auth id specified!"
16+
echo "No CODER_EXTERNAL_AUTH_ID specified!"
2217
exit 1
2318
fi
2419

2520
if [ -z "$GITHUB_API_URL" ]; then
26-
echo "No GitHub API URL specified!"
21+
echo "No GITHUB_API_URL specified!"
2722
exit 1
2823
fi
2924

3025
echo "Fetching GitHub token..."
3126
GITHUB_TOKEN=$(coder external-auth access-token $CODER_EXTERNAL_AUTH_ID)
3227
if [ $? -ne 0 ]; then
33-
echo "Failed to fetch GitHub token!"
34-
exit 1
35-
fi
36-
if [ -z "$GITHUB_TOKEN" ]; then
37-
echo "No GitHub token found!"
28+
printf "Authenticate with Github to automatically upload Coder public key:\n$GITHUB_TOKEN\n"
3829
exit 1
3930
fi
40-
echo "GitHub token found!"
4131

42-
echo "Fetching Coder public SSH key..."
32+
echo "Fetching public key from Coder..."
4333
PUBLIC_KEY_RESPONSE=$(
4434
curl -L -s \
4535
-w "\n%%{http_code}" \
@@ -55,16 +45,13 @@ if [ "$PUBLIC_KEY_RESPONSE_STATUS" -ne 200 ]; then
5545
echo "$PUBLIC_KEY_BODY"
5646
exit 1
5747
fi
58-
5948
PUBLIC_KEY=$(jq -r '.public_key' <<< "$PUBLIC_KEY_BODY")
60-
echo "Coder public SSH key found!"
61-
6249
if [ -z "$PUBLIC_KEY" ]; then
6350
echo "No Coder public SSH key found!"
6451
exit 1
6552
fi
6653

67-
echo "Fetching GitHub public SSH keys..."
54+
echo "Fetching public keys from GitHub..."
6855
GITHUB_KEYS_RESPONSE=$(
6956
curl -L -s \
7057
-w "\n%%{http_code}" \
@@ -85,12 +72,11 @@ fi
8572
GITHUB_MATCH=$(jq -r --arg PUBLIC_KEY "$PUBLIC_KEY" '.[] | select(.key == $PUBLIC_KEY) | .key' <<< "$GITHUB_KEYS_RESPONSE_BODY")
8673

8774
if [ "$PUBLIC_KEY" = "$GITHUB_MATCH" ]; then
88-
echo "Coder public SSH key is already uploaded to GitHub!"
75+
echo "Your Coder public key is already on GitHub!"
8976
exit 0
9077
fi
9178

92-
echo "Coder public SSH key not found in GitHub keys!"
93-
echo "Uploading Coder public SSH key to GitHub..."
79+
echo "Your Coder public key is not in GitHub. Adding it now..."
9480
CODER_PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces"
9581
UPLOAD_RESPONSE=$(
9682
curl -L -s \
@@ -111,4 +97,4 @@ if [ "$UPLOAD_RESPONSE_STATUS" -ne 201 ]; then
11197
exit 1
11298
fi
11399

114-
echo "Coder public SSH key uploaded to GitHub!"
100+
echo "Your Coder public key has been added to GitHub!"

0 commit comments

Comments
 (0)