pull/241/head
Garrett Delfosse 1 year ago
parent 428f386c4c
commit 5a33af28ac

@ -11,7 +11,7 @@ tags: [helper, git]
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. 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.
# Example # Example
```tf ```tf
module "github-upload-public-key" { module "github-upload-public-key" {
@ -33,4 +33,4 @@ CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
CODER_EXTERNAL_AUTH_0_SCOPES="repo,workflow,admin:public_key" CODER_EXTERNAL_AUTH_0_SCOPES="repo,workflow,admin:public_key"
``` ```
Note that the default scopes if not provided are `repo,workflow`. If the module is failing to complete after updating the external auth configuration, instruct users of the module to "Unlink" and "Link" their Github account in the External Auth user settings page to get the new scopes. Note that the default scopes if not provided are `repo,workflow`. If the module is failing to complete after updating the external auth configuration, instruct users of the module to "Unlink" and "Link" their Github account in the External Auth user settings page to get the new scopes.

@ -18,9 +18,9 @@ resource "coder_script" "github_upload_public_key" {
agent_id = var.agent_id agent_id = var.agent_id
script = templatefile("${path.module}/run.sh", { script = templatefile("${path.module}/run.sh", {
CODER_OWNER_SESSION_TOKEN : data.coder_workspace.me.owner_session_token, CODER_OWNER_SESSION_TOKEN : data.coder_workspace.me.owner_session_token,
CODER_ACCESS_URL : data.coder_workspace.me.access_url, CODER_ACCESS_URL : data.coder_workspace.me.access_url,
}) })
display_name = "Github Upload Public Key" display_name = "Github Upload Public Key"
icon = "/icon/github.svg" icon = "/icon/github.svg"
run_on_start = true run_on_start = true
} }

@ -28,11 +28,12 @@ fi
echo "GitHub token found!" echo "GitHub token found!"
echo "Fetching Coder public SSH key..." echo "Fetching Coder public SSH key..."
PUBLIC_KEY_RESPONSE=$(curl -L -s \ PUBLIC_KEY_RESPONSE=$(
-w "%{http_code}" \ curl -L -s \
-H 'accept: application/json' \ -w "%{http_code}" \
-H "cookie: coder_session_token=$CODER_OWNER_SESSION_TOKEN" \ -H 'accept: application/json' \
"$CODER_ACCESS_URL/api/v2/users/me/gitsshkey" -H "cookie: coder_session_token=$CODER_OWNER_SESSION_TOKEN" \
"$CODER_ACCESS_URL/api/v2/users/me/gitsshkey"
) )
PUBLIC_KEY_RESPONSE_STATUS=$(tail -n1 <<< "$PUBLIC_KEY_RESPONSE") PUBLIC_KEY_RESPONSE_STATUS=$(tail -n1 <<< "$PUBLIC_KEY_RESPONSE")
PUBLIC_KEY_BODY=$(sed \$d <<< "$PUBLIC_KEY_RESPONSE") PUBLIC_KEY_BODY=$(sed \$d <<< "$PUBLIC_KEY_RESPONSE")
@ -52,12 +53,13 @@ if [ -z "$PUBLIC_KEY" ]; then
fi fi
echo "Fetching GitHub public SSH keys..." echo "Fetching GitHub public SSH keys..."
GITHUB_KEYS_RESPONSE=$(curl -L -s \ GITHUB_KEYS_RESPONSE=$(
-w "%{http_code}" \ curl -L -s \
-H "Accept: application/vnd.github+json" \ -w "%{http_code}" \
-H "Authorization: Bearer $GITHUB_TOKEN" \ -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/user/keys -H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/user/keys
) )
GITHUB_KEYS_RESPONSE_STATUS=$(tail -n1 <<< "$GITHUB_KEYS_RESPONSE") GITHUB_KEYS_RESPONSE_STATUS=$(tail -n1 <<< "$GITHUB_KEYS_RESPONSE")
GITHUB_KEYS_RESPONSE_BODY=$(sed \$d <<< "$GITHUB_KEYS_RESPONSE") GITHUB_KEYS_RESPONSE_BODY=$(sed \$d <<< "$GITHUB_KEYS_RESPONSE")
@ -78,14 +80,15 @@ fi
echo "Coder public SSH key not found in GitHub keys!" echo "Coder public SSH key not found in GitHub keys!"
echo "Uploading Coder public SSH key to GitHub..." echo "Uploading Coder public SSH key to GitHub..."
CODER_PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces" CODER_PUBLIC_KEY_NAME="$CODER_ACCESS_URL Workspaces"
UPLOAD_RESPONSE=$(curl -L -s \ UPLOAD_RESPONSE=$(
-X POST \ curl -L -s \
-w "%{http_code}" \ -X POST \
-H "Accept: application/vnd.github+json" \ -w "%{http_code}" \
-H "Authorization: Bearer $GITHUB_TOKEN" \ -H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \ -H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/user/keys \ -H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"title\":\"$CODER_PUBLIC_KEY_NAME\",\"key\":\"$PUBLIC_KEY\"}" https://api.github.com/user/keys \
-d "{\"title\":\"$CODER_PUBLIC_KEY_NAME\",\"key\":\"$PUBLIC_KEY\"}"
) )
UPLOAD_RESPONSE_STATUS=$(tail -n1 <<< "$UPLOAD_RESPONSE") UPLOAD_RESPONSE_STATUS=$(tail -n1 <<< "$UPLOAD_RESPONSE")
UPLOAD_RESPONSE_BODY=$(sed \$d <<< "$UPLOAD_RESPONSE") UPLOAD_RESPONSE_BODY=$(sed \$d <<< "$UPLOAD_RESPONSE")
@ -96,4 +99,4 @@ if [ "$UPLOAD_RESPONSE_STATUS" -ne 201 ]; then
exit 1 exit 1
fi fi
echo "Coder public SSH key uploaded to GitHub!" echo "Coder public SSH key uploaded to GitHub!"

Loading…
Cancel
Save