Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This Bash script pulls `authorized_keys` files from remote URLs and updates SSH
- Works with:
- ✅ Public URLs (method: `raw`)
- ✅ Private GitHub repositories via GitHub API (method: `api`, requires token)
- ✅ GitHub user public keys (method: `ghuser`)
- Safe: Only updates keys if they’ve changed
- Logs activity per user

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

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

**Example `users.conf`:**
```bash
declare -A USER_KEYS=(
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main"
["alice"]="ghuser:alice-github-username"
)
```

## Usage

1. Edit the `users.conf` file to define users and their key URLs.
1. Edit the `users.conf` file to define users and their key URLs or GitHub usernames.
2. If using the `api` method, export your GitHub token:
```bash
export GITHUB_TOKEN=your_token_here
Expand All @@ -42,10 +45,9 @@ declare -A USER_KEYS=(
chmod +x sync-ssh-keys.sh
```
4. Add to root's crontab:

```cron
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
```
```cron
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
```

## Implementation Notes

Expand Down
14 changes: 9 additions & 5 deletions sync-ssh-keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -euo pipefail

# shellcheck disable=SC2034 # planned to be used in a future release
SCRIPT_VERSION="0.0.5"
SCRIPT_VERSION="0.0.6"

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

fetch_key_file() {
local METHOD="$1"
local URL="$2"
local TARGET="$2"
local OUTFILE="$3"

if [[ "$METHOD" == "raw" ]]; then
curl -fsSL "$URL" -o "$OUTFILE"
curl -fsSL "$TARGET" -o "$OUTFILE"
return $?
elif [[ "$METHOD" == "api" ]]; then
: "${GITHUB_TOKEN:?GITHUB_TOKEN is required for API access}"
curl -fsSL -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
"$URL" -o "$OUTFILE"
"$TARGET" -o "$OUTFILE"
return $?
elif [[ "$METHOD" == "ghuser" ]]; then
# TARGET is the GitHub username
curl -fsSL "https://github.com/${TARGET}.keys" -o "$OUTFILE"
return $?
else
log_message "Error: Unsupported method '$METHOD' encountered for URL '$URL'. Halting execution."
log_message "Error: Unsupported method '$METHOD' encountered for URL '$TARGET'. Halting execution."
exit 2
fi
}
Expand Down
2 changes: 1 addition & 1 deletion users.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare -A USER_KEYS=(
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main"
["admin"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/admin.authorized_keys?ref=main"
["alice"]="ghuser:alice-github-username"
)