Repository navigation
Expand file tree
/
Copy pathbootstrap-github-access
More file actions
executable file
·105 lines (94 loc) · 2.94 KB
/
Copy pathbootstrap-github-access
File metadata and controls
executable file
·105 lines (94 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# bootstrap-github-access
#
# Upload this host's SSH public key to GitHub. The token comes from
# Bitwarden item github-token. bw prompts for the master password.
set +x
API="https://api.github.com/user/keys"
TITLE="$(hostname)-$(date +%Y%m%d)"
KEYFILE=""
github_token=""
load_bwsecrets() {
local lib="${BWSECRETS_LIB:-}"
if [[ -z "$lib" ]]; then
lib="$(dirname "$(readlink -f "$0")")/lib/bwsecrets.sh"
[[ -f "$lib" ]] || lib="$HOME/bin/lib/bwsecrets.sh"
fi
# shellcheck disable=SC1090
source "$lib"
}
load_bwsecrets
trap 'unset github_token; bw_lock' EXIT
bw_require || exit 1
load_vault_tokens || exit 1
if [[ -f ~/.ssh/id_ed25519.pub ]]; then
KEYFILE=~/.ssh/id_ed25519.pub
elif [[ -f ~/.ssh/id_rsa.pub ]]; then
KEYFILE=~/.ssh/id_rsa.pub
else
echo "No SSH public key found. Generating one."
ssh_key_generate "$HOME/.ssh/id_ed25519" ed25519 || exit 1
KEYFILE=~/.ssh/id_ed25519.pub
fi
PUBKEY="$(<"$KEYFILE")"
LOCAL_FP="$(ssh-keygen -lf "$KEYFILE" | awk '{print $2}')"
EXISTING="$(curl_with_token Authorization "Bearer $github_token" \
-sS -H "Accept: application/vnd.github+json" "$API")"
key_exists=false
if command -v jq >/dev/null 2>&1; then
if echo "$EXISTING" | jq -r '.[].key' 2>/dev/null \
| ssh-keygen -lf /dev/stdin 2>/dev/null \
| awk '{print $2}' | grep -q "$LOCAL_FP"; then
key_exists=true
fi
else
local_key_data="$(awk '{print $2}' < "$KEYFILE")"
if echo "$EXISTING" | grep -qF "$local_key_data"; then
key_exists=true
fi
fi
if $key_exists; then
echo "Key already exists on GitHub (fingerprint $LOCAL_FP)."
else
echo "Uploading SSH key to GitHub..."
resp="$(secret_tmp)" || exit 1
hdr="$(secret_tmp)" || exit 1
code="$(curl_with_token Authorization "Bearer $github_token" \
-sS -o "$resp" -D "$hdr" -w "%{http_code}" \
-H "Accept: application/vnd.github+json" \
-d "{\"title\":\"${TITLE}\",\"key\":\"${PUBKEY}\"}" \
"$API")" || code="000"
if [[ "$code" == "201" ]]; then
echo "Key uploaded successfully."
else
echo "Upload failed (HTTP $code)." >&2
python3 -c '
import json, sys
from pathlib import Path
body = Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace")
try:
message = json.loads(body).get("message") or ""
except Exception:
message = ""
if message:
print(f"GitHub: {message}", file=sys.stderr)
scopes = ""
for line in Path(sys.argv[2]).read_text(encoding="utf-8", errors="replace").splitlines():
if line.lower().startswith("x-accepted-oauth-scopes:"):
scopes = line.split(":", 1)[1].strip()
if scopes:
print(f"The token needs one of these scopes: {scopes}", file=sys.stderr)
' "$resp" "$hdr" || true
secret_shred "$resp" "$hdr"
exit 1
fi
secret_shred "$resp" "$hdr"
fi
if $key_exists || [[ "${code:-}" == "201" ]]; then
git config --global url."git@github.com:".insteadOf "https://github.com/"
ssh -o StrictHostKeyChecking=accept-new -T git@github.com || true
else
echo "SSH key was not uploaded. GitHub access still requires Bitwarden."
fi
unset github_token
bw_lock