|
| 1 | +# Copilot Instructions for `ssh-key-sync` |
| 2 | + |
| 3 | +This document provides comprehensive guidance for AI coding agents and contributors working with the `ssh-key-sync` repository. It ensures consistency, quality, and alignment with project standards. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +`ssh-key-sync` is a Bash script-based utility for synchronizing SSH `authorized_keys` files for multiple users. It supports fetching keys from various sources, including public URLs, private GitHub repositories, and GitHub user profiles. The configuration is externalized in a `users.conf` file. |
| 8 | + |
| 9 | +## Key Components |
| 10 | + |
| 11 | +### Scripts |
| 12 | +- **`sync-ssh-keys.sh`**: The main script that performs the synchronization. It includes: |
| 13 | + - Support for multiple fetch methods (`raw`, `api`, `ghuser`). |
| 14 | + - Logging and error handling. |
| 15 | + - Configuration loading from `users.conf`. |
| 16 | + - A helper function `fetch_key_file` to handle key retrieval logic. |
| 17 | + |
| 18 | +### Configuration |
| 19 | +- **`users.conf`**: Defines users and their key sources. Example structure: |
| 20 | + ```bash |
| 21 | + CONF_GITHUB_TOKEN="your_github_token_here" |
| 22 | + |
| 23 | + declare -A USER_KEYS=( |
| 24 | + ["ubuntu"]="raw:https://example.com/ssh-keys/ubuntu.authorized_keys" |
| 25 | + ["devuser"]="api:https://api.github.com/repos/yourorg/ssh-keys/contents/keys/devuser.authorized_keys?ref=main" |
| 26 | + ["alice"]="ghuser:alice-github-username" |
| 27 | + ) |
| 28 | + ``` |
| 29 | + |
| 30 | +## Developer Workflows |
| 31 | + |
| 32 | +### Running the Script |
| 33 | +1. Ensure `sync-ssh-keys.sh` is executable: |
| 34 | + ```bash |
| 35 | + chmod +x sync-ssh-keys.sh |
| 36 | + ``` |
| 37 | +2. Run the script manually: |
| 38 | + ```bash |
| 39 | + ./sync-ssh-keys.sh |
| 40 | + ``` |
| 41 | + |
| 42 | +### Configuration |
| 43 | +- Edit `users.conf` to define users and their key sources. |
| 44 | +- If using the `api` method, ensure `CONF_GITHUB_TOKEN` is set in `users.conf` or export `GITHUB_TOKEN` in the environment. |
| 45 | + |
| 46 | +### Automating with Cron |
| 47 | +- Add the script to root's crontab: |
| 48 | + ```cron |
| 49 | + */15 * * * * /path/to/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1 |
| 50 | + ``` |
| 51 | + |
| 52 | +### Logging |
| 53 | +- Logs are printed to the console with timestamps. |
| 54 | +- Example log message: |
| 55 | + ``` |
| 56 | + 2025-07-20 12:00:00: Fetching key file for user 'ubuntu' from https://example.com/ssh-keys/ubuntu.authorized_keys (method: raw) |
| 57 | + ``` |
| 58 | + |
| 59 | +## Coding Conventions |
| 60 | + |
| 61 | +- Use meaningful variable and function names. |
| 62 | +- Follow the existing code style in `sync-ssh-keys.sh`. |
| 63 | +- Add comments for complex logic. |
| 64 | +- Use environment variables for sensitive data (e.g., `GITHUB_TOKEN`). |
| 65 | +- Ensure temporary files are cleaned up using `trap`. |
| 66 | + |
| 67 | +## Integration Points |
| 68 | + |
| 69 | +### GitHub API |
| 70 | +- Used for fetching keys from private repositories (`api` method). |
| 71 | +- Requires a GitHub token (`GITHUB_TOKEN` or `CONF_GITHUB_TOKEN`). |
| 72 | + |
| 73 | +### System Integration |
| 74 | +- The script ensures the `.ssh` directory and `authorized_keys` file exist for each user. |
| 75 | +- Updates file permissions and ownership as needed. |
| 76 | + |
| 77 | +## Contribution Guidelines |
| 78 | + |
| 79 | +- Include a clear description of changes in pull requests. |
| 80 | +- Reference related issues. |
| 81 | +- Ensure the script passes linting (e.g., using `shellcheck`). |
| 82 | +- Test changes locally before submission. |
| 83 | + |
| 84 | +## Examples |
| 85 | + |
| 86 | +### Adding a New User |
| 87 | +1. Add the user to `users.conf`: |
| 88 | + ```bash |
| 89 | + ["newuser"]="ghuser:newuser-github-username" |
| 90 | + ``` |
| 91 | +2. Run the script to sync keys: |
| 92 | + ```bash |
| 93 | + ./sync-ssh-keys.sh |
| 94 | + ``` |
| 95 | + |
| 96 | +### Fetch Methods |
| 97 | +- **`raw`**: Fetches keys from a public URL. |
| 98 | +- **`api`**: Fetches keys from a private GitHub repository using the GitHub API. |
| 99 | +- **`ghuser`**: Fetches public keys from a GitHub user's profile. |
0 commit comments