Repository navigation
Expand file tree
/
Copy pathmac-setup-ssh-github.sh
More file actions
executable file
·171 lines (140 loc) · 4.34 KB
/
Copy pathmac-setup-ssh-github.sh
File metadata and controls
executable file
·171 lines (140 loc) · 4.34 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
set -euo pipefail
###############################################################################
# mac-setup-ssh-github.sh
#
# Author: Miguel Barra <mbarra.git@gmail.com>
#
# Sets up a personal SSH key for GitHub:
# - Creates the key if it doesn't exist.
# - Adds it to the ssh-agent.
# - Writes ~/.ssh/config using that key for github.com.
# - Shows the public key at the end so you can copy it to GitHub.
#
# Optional variables (env):
# PERSONAL_EMAIL (default: mbarra.dev@icloud.com)
# KEY_TYPE (default: ed25519, can be rsa)
# CLEAN_AGENT (default: false; if true runs ssh-add -D first)
###############################################################################
# ---------- logging helpers ----------
info() { printf "\033[32m[INFO]\033[0m %s\n" "$*"; }
warn() { printf "\033[33m[WARN]\033[0m %s\n" "$*"; }
error() { printf "\033[31m[ERROR]\033[0m %s\n" "$*\n" >&2; exit 1; }
# ---------- variables ----------
PERSONAL_EMAIL="${PERSONAL_EMAIL:-mbarra.dev@icloud.com}"
KEY_TYPE="${KEY_TYPE:-ed25519}"
if [[ "${KEY_TYPE}" == "ed25519" ]]; then
PERSONAL_KEY="${PERSONAL_KEY:-${HOME}/.ssh/id_ed25519_github_personal}"
else
PERSONAL_KEY="${PERSONAL_KEY:-${HOME}/.ssh/id_rsa_github_personal}"
fi
CLEAN_AGENT="${CLEAN_AGENT:-false}"
info "Configuration:"
info " Personal key : ${PERSONAL_KEY} (${PERSONAL_EMAIL})"
info " Key type : ${KEY_TYPE}"
# ---------- main functions ----------
ensure_ssh_dir() {
if [[ ! -d "${HOME}/.ssh" ]]; then
info "Creating ~/.ssh directory..."
mkdir -p "${HOME}/.ssh"
chmod 700 "${HOME}/.ssh"
fi
}
generate_key_if_missing() {
local key_path="$1"
local email="$2"
if [[ -f "${key_path}" ]]; then
info "Key already exists: ${key_path}"
return
fi
info "Creating SSH key: ${key_path}"
if [[ "${KEY_TYPE}" == "ed25519" ]]; then
ssh-keygen -t ed25519 -C "${email}" -f "${key_path}" -N ""
else
ssh-keygen -t rsa -b 4096 -C "${email}" -f "${key_path}" -N ""
fi
}
start_ssh_agent_if_needed() {
# ssh-add -l exits 0 (has keys) or 1 (no keys) when it can reach an agent,
# and 2 when it can't. On macOS, launchd provides the agent in Terminal.
local rc=0
ssh-add -l >/dev/null 2>&1 || rc=$?
if [[ "${rc}" -eq 2 ]]; then
info "No reachable ssh-agent (SSH_AUTH_SOCK unset or stale). Starting one for this run..."
eval "$(ssh-agent -s)" >/dev/null
# Only lives for this run; ~/.ssh/config points ssh at the key anyway
trap 'ssh-agent -k >/dev/null 2>&1' EXIT
fi
}
add_key_to_agent() {
local key_path="$1"
local fingerprint
fingerprint="$(ssh-keygen -lf "${key_path}.pub" | awk '{print $2}')"
if ! ssh-add -l 2>/dev/null | grep -qF "${fingerprint}"; then
info "Adding key to ssh-agent: ${key_path}"
ssh-add "${key_path}"
else
info "Key is already in the ssh-agent: ${key_path}"
fi
}
clean_agent_if_requested() {
if [[ "${CLEAN_AGENT}" == "true" ]]; then
warn "Clearing current identities from the ssh-agent (ssh-add -D)..."
ssh-add -D || true
fi
}
write_ssh_config() {
local cfg="${HOME}/.ssh/config"
info "Updating ~/.ssh/config..."
# Backup if it exists
if [[ -f "${cfg}" && ! -f "${cfg}.bak" ]]; then
cp "${cfg}" "${cfg}.bak"
info "Backup created: ${cfg}.bak"
fi
# Remove previous github.com block
local tmp
tmp="$(mktemp)"
if [[ -f "${cfg}" ]]; then
awk '
BEGIN { skip=0 }
/^Host[[:space:]]+github\.com$/ { skip=1; next }
/^Host[[:space:]]+/ && skip==1 { skip=0 }
skip==0 { print }
' "${cfg}" > "${tmp}"
fi
cat >> "${tmp}" <<EOF
Host github.com
HostName github.com
User git
IdentityFile ${PERSONAL_KEY}
IdentitiesOnly yes
EOF
mv "${tmp}" "${cfg}"
chmod 600 "${cfg}"
info "~/.ssh/config updated."
}
show_public_key() {
echo
info "================ PUBLIC KEY FOR GITHUB ================"
if [[ -f "${PERSONAL_KEY}.pub" ]]; then
echo
echo ">>> PERSONAL key (${PERSONAL_KEY}.pub):"
cat "${PERSONAL_KEY}.pub"
echo
else
warn "Could not find ${PERSONAL_KEY}.pub"
fi
cat <<EOT
Paste this key into GitHub:
Settings -> SSH and GPG keys -> New SSH key
Suggested title: "Personal laptop"
EOT
}
# ---------- main ----------
ensure_ssh_dir
generate_key_if_missing "${PERSONAL_KEY}" "${PERSONAL_EMAIL}"
start_ssh_agent_if_needed
clean_agent_if_requested
add_key_to_agent "${PERSONAL_KEY}"
write_ssh_config
show_public_key