Skip to content

Commit aca6029

Browse files
locus313Copilot
andauthored
Move user config into separate file (#3)
* Move user config into separate file * update * change version check command Co-authored-by: Copilot <[email protected]> * add error handing for users.conf Co-authored-by: Copilot <[email protected]> * update version check command * change version check command Co-authored-by: Copilot <[email protected]> * update version check command * revert version check command --------- Co-authored-by: Copilot <[email protected]>
1 parent 97a9b35 commit aca6029

File tree

5 files changed

+106
-11
lines changed

5 files changed

+106
-11
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Check Script Version
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
check-version:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Extract SCRIPT_VERSION from sync-ssh-keys.sh
16+
id: get_version
17+
run: |
18+
VERSION=$(awk -F'"' '/SCRIPT_VERSION/ {print $2; exit}' sync-ssh-keys.sh)
19+
echo "version=$VERSION" >> $GITHUB_OUTPUT
20+
21+
- name: Fetch tags
22+
run: git fetch --tags
23+
24+
- name: Check if version tag exists
25+
run: |
26+
TAG="v${{ steps.get_version.outputs.version }}"
27+
if git tag --list | grep -q "^$TAG$"; then
28+
echo "Error: Tag $TAG already exists. Please bump SCRIPT_VERSION."
29+
exit 1
30+
else
31+
echo "Tag $TAG does not exist. Good to merge."
32+
fi

.github/workflows/release.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Create Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Extract version from script
16+
id: get_version
17+
run: |
18+
VERSION=$(awk -F'"' '/SCRIPT_VERSION/ {print $2; exit}' sync-ssh-keys.sh)
19+
if [[ ! $VERSION =~ ^[0-9]+(\.[0-9]+)*$ ]]; then
20+
echo "Error: Invalid version format: $VERSION" >&2
21+
exit 1
22+
fi
23+
echo "version=$VERSION" >> $GITHUB_OUTPUT
24+
25+
- name: Create tag if needed
26+
run: |
27+
TAG="v${{ steps.get_version.outputs.version }}"
28+
if git rev-parse "$TAG" >/dev/null 2>&1; then
29+
echo "Tag $TAG already exists."
30+
else
31+
git config user.name "github-actions"
32+
git config user.email "[email protected]"
33+
git tag "$TAG"
34+
git push origin "$TAG"
35+
fi
36+
37+
- name: Create release zip
38+
run: |
39+
zip ssh-key-sync.zip sync-ssh-keys.sh users.conf
40+
41+
- name: Create GitHub Release
42+
uses: softprops/action-gh-release@v2
43+
with:
44+
tag_name: v${{ steps.get_version.outputs.version }}
45+
generate_release_notes: true
46+
files: ssh-key-sync.zip

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ This Bash script pulls `authorized_keys` files from remote URLs and updates SSH
1414

1515
## ⚙️ Configuration
1616

17-
Edit the `USER_KEYS` associative array in `sync-ssh-keys.sh` to define users and their key sources.
17+
User configuration is stored in a separate `users.conf` file in the same directory as the script.
18+
Edit `users.conf` to define users and their key sources.
1819
Each entry uses the format:
1920
`["username"]="method:url"`
2021

2122
- **raw:** Fetches directly from a public URL.
2223
- **api:** Fetches from a private GitHub repo using the GitHub API (requires `GITHUB_TOKEN` environment variable).
2324

24-
**Example:**
25+
**Example `users.conf`:**
2526
```bash
2627
declare -A USER_KEYS=(
2728
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
@@ -31,19 +32,24 @@ declare -A USER_KEYS=(
3132

3233
## Usage
3334

34-
1. Edit the `USER_KEYS` array in `sync-ssh-keys.sh` to define users and their key URLs.
35+
1. Edit the `users.conf` file to define users and their key URLs.
3536
2. If using the `api` method, export your GitHub token:
3637
```bash
3738
export GITHUB_TOKEN=your_token_here
3839
```
39-
3. Add to root's crontab:
40+
3. Make sure the script is executable:
41+
```bash
42+
chmod +x sync-ssh-keys.sh
43+
```
44+
4. Add to root's crontab:
4045

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

4550
## Implementation Notes
4651

47-
- The script uses a helper function `fetch_key_file` to fetch keys using the appropriate method.
52+
- The script sources `users.conf` for configuration.
53+
- Uses a helper function `fetch_key_file` to fetch keys using the appropriate method.
4854
- Only updates a user's `authorized_keys` if the remote file has changed.
4955
- Logs all actions with timestamps.

sync-ssh-keys.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#!/bin/bash
22
set -euo pipefail
33

4-
# === Configuration: user -> remote key file URL ===
5-
declare -A USER_KEYS=(
6-
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
7-
["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main"
8-
["admin"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/admin.authorized_keys?ref=main"
9-
)
4+
SCRIPT_VERSION="0.0.3"
5+
6+
# === Load user configuration ===
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
if [ ! -f "$SCRIPT_DIR/users.conf" ]; then
9+
echo "Error: Configuration file 'users.conf' not found in $SCRIPT_DIR. Halting execution." >&2
10+
exit 1
11+
fi
12+
if ! source "$SCRIPT_DIR/users.conf"; then
13+
echo "Error: Failed to load configuration file 'users.conf'. Please check the file for syntax errors. Halting execution." >&2
14+
exit 1
15+
fi
1016

1117
log_message() {
1218
local TIMESTAMP

users.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare -A USER_KEYS=(
2+
["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys"
3+
["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"
5+
)

0 commit comments

Comments
 (0)