Skip to content

Commit 6ca93ca

Browse files
locus313Copilot
andauthored
Add method for using keys from github profile (#6)
* Add method for using keys from github profile * bumb version * add error handling Co-authored-by: Copilot <[email protected]> * update indentation * Updateparameter name * change return to exit * remove usage of log_message --------- Co-authored-by: Copilot <[email protected]>
1 parent 513cfb5 commit 6ca93ca

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This Bash script pulls `authorized_keys` files from remote URLs and updates SSH
99
- Works with:
1010
- ✅ Public URLs (method: `raw`)
1111
- ✅ Private GitHub repositories via GitHub API (method: `api`, requires token)
12+
- ✅ GitHub user public keys (method: `ghuser`)
1213
- Safe: Only updates keys if they’ve changed
1314
- Logs activity per user
1415

@@ -21,18 +22,20 @@ Each entry uses the format:
2122

2223
- **raw:** Fetches directly from a public URL.
2324
- **api:** Fetches from a private GitHub repo using the GitHub API (requires `GITHUB_TOKEN` environment variable).
25+
- **ghuser:** Fetches public keys from a GitHub user's profile (provide the GitHub username after the colon).
2426

2527
**Example `users.conf`:**
2628
```bash
2729
declare -A USER_KEYS=(
2830
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
2931
["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main"
32+
["alice"]="ghuser:alice-github-username"
3033
)
3134
```
3235

3336
## Usage
3437

35-
1. Edit the `users.conf` file to define users and their key URLs.
38+
1. Edit the `users.conf` file to define users and their key URLs or GitHub usernames.
3639
2. If using the `api` method, export your GitHub token:
3740
```bash
3841
export GITHUB_TOKEN=your_token_here
@@ -42,10 +45,9 @@ declare -A USER_KEYS=(
4245
chmod +x sync-ssh-keys.sh
4346
```
4447
4. Add to root's crontab:
45-
46-
```cron
47-
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
48-
```
48+
```cron
49+
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
50+
```
4951

5052
## Implementation Notes
5153

sync-ssh-keys.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
# shellcheck disable=SC2034 # planned to be used in a future release
5-
SCRIPT_VERSION="0.0.5"
5+
SCRIPT_VERSION="0.0.6"
66

77
# === Load user configuration ===
88
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -23,20 +23,24 @@ log_message() {
2323

2424
fetch_key_file() {
2525
local METHOD="$1"
26-
local URL="$2"
26+
local TARGET="$2"
2727
local OUTFILE="$3"
2828

2929
if [[ "$METHOD" == "raw" ]]; then
30-
curl -fsSL "$URL" -o "$OUTFILE"
30+
curl -fsSL "$TARGET" -o "$OUTFILE"
3131
return $?
3232
elif [[ "$METHOD" == "api" ]]; then
3333
: "${GITHUB_TOKEN:?GITHUB_TOKEN is required for API access}"
3434
curl -fsSL -H "Authorization: token $GITHUB_TOKEN" \
3535
-H "Accept: application/vnd.github.v3.raw" \
36-
"$URL" -o "$OUTFILE"
36+
"$TARGET" -o "$OUTFILE"
37+
return $?
38+
elif [[ "$METHOD" == "ghuser" ]]; then
39+
# TARGET is the GitHub username
40+
curl -fsSL "https://github.com/${TARGET}.keys" -o "$OUTFILE"
3741
return $?
3842
else
39-
log_message "Error: Unsupported method '$METHOD' encountered for URL '$URL'. Halting execution."
43+
log_message "Error: Unsupported method '$METHOD' encountered for URL '$TARGET'. Halting execution."
4044
exit 2
4145
fi
4246
}

users.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare -A USER_KEYS=(
22
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
33
["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main"
4-
["admin"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/admin.authorized_keys?ref=main"
4+
["alice"]="ghuser:alice-github-username"
55
)

0 commit comments

Comments
 (0)