-
Notifications
You must be signed in to change notification settings - Fork 0
Remote configuration reconciliation via GitHub App token #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [Unit] | ||
| Description=Reconcile ci-fleet controller with remote desired-state repository | ||
| Documentation=https://github.com/RandomDevelopment/ci-fleet | ||
| After=docker.service network-online.target ci-fleet-drift.service | ||
| Wants=docker.service network-online.target ci-fleet-drift.service | ||
|
|
||
| [Service] | ||
| Type=oneshot | ||
| User=root | ||
| WorkingDirectory=/opt/ci-fleet/manager/current | ||
| ExecStart=/opt/ci-fleet/manager/current/scripts/remote-reconcile.sh | ||
| Restart=no | ||
| # 0=noop, 3=drift/invalid — timer retries | ||
| SuccessExitStatus=0 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [Unit] | ||
| Description=Reconcile ci-fleet controller every five minutes | ||
| Documentation=https://github.com/RandomDevelopment/ci-fleet | ||
|
|
||
| [Timer] | ||
| OnBootSec=10min | ||
| OnUnitActiveSec=5min | ||
| AccuracySec=30s | ||
| Persistent=true | ||
|
|
||
| [Install] | ||
| WantedBy=timers.target | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env bash | ||
| # Generate a short-lived GitHub App installation token. | ||
| # Uses openssl (existing dependency) for JWT signing and curl for the API exchange. | ||
| # Usage: | ||
| # github-app-token.sh --app-id ID --install-id ID --key-path PATH | ||
| # github-app-token.sh --env-file PATH # reads APP_ID/INSTALL_ID/KEY_PATH from env file | ||
| set -Eeuo pipefail | ||
|
|
||
| app_id= | ||
| install_id= | ||
| key_path= | ||
| env_file= | ||
|
|
||
| while (($#)); do | ||
| case "$1" in | ||
| --app-id) app_id=$2; shift 2 ;; | ||
| --install-id) install_id=$2; shift 2 ;; | ||
| --key-path) key_path=$2; shift 2 ;; | ||
| --env-file) env_file=$2; shift 2 ;; | ||
| --help|-h) echo "usage: $(basename "$0") --app-id ID --install-id ID --key-path PATH"; exit 0 ;; | ||
| *) echo "ERROR: unknown argument: $1" >&2; exit 2 ;; | ||
| esac | ||
| done | ||
|
|
||
| if [[ -n "$env_file" ]]; then | ||
| [[ -f "$env_file" ]] || { echo "ERROR: env file not found: $env_file" >&2; exit 2; } | ||
| while IFS='=' read -r name value; do | ||
| case "$name" in | ||
| CI_FLEET_GITHUB_APP_CLIENT_ID) app_id=$value ;; | ||
| CI_FLEET_GITHUB_APP_INSTALLATION_ID) install_id=$value ;; | ||
| CI_FLEET_GITHUB_APP_PRIVATE_KEY_FILE) key_path=$value ;; | ||
| esac | ||
| done < <(grep -E '^(CI_FLEET_GITHUB_APP_CLIENT_ID|CI_FLEET_GITHUB_APP_INSTALLATION_ID|CI_FLEET_GITHUB_APP_PRIVATE_KEY_FILE)=' "$env_file" || true) | ||
| fi | ||
|
|
||
| [[ -n "$app_id" && -n "$install_id" && -n "$key_path" ]] || { echo "ERROR: --app-id, --install-id, and --key-path are required" >&2; exit 2; } | ||
| [[ -f "$key_path" ]] || { echo "ERROR: private key file not found: $key_path" >&2; exit 2; } | ||
|
|
||
| # Generate JWT | ||
| now=$(date -u +%s) | ||
| exp=$((now + 540)) # 9 minutes (GitHub max is 10, leave buffer) | ||
| header='{"alg":"RS256","typ":"JWT"}' | ||
| payload=$(printf '{"iat":%d,"exp":%d,"iss":"%s"}' "$now" "$exp" "$app_id") | ||
|
|
||
| b64url() { openssl base64 -e | tr '+/' '-_' | tr -d '=\n'; } | ||
| b64header=$(printf '%s' "$header" | b64url) | ||
| b64payload=$(printf '%s' "$payload" | b64url) | ||
| signature=$(printf '%s.%s' "$b64header" "$b64payload" | openssl dgst -sha256 -sign "$key_path" | b64url) | ||
| jwt="${b64header}.${b64payload}.${signature}" | ||
|
|
||
| # Exchange JWT for installation token | ||
| response=$(curl -sS -X POST \ | ||
| -H "Authorization: Bearer ${jwt}" \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "https://api.github.com/app/installations/${install_id}/access_tokens" 2>/dev/null) || { | ||
| echo "ERROR: token exchange request failed" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| token=$(printf '%s' "$response" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('token',''))" 2>/dev/null) || token= | ||
| if [[ -z "$token" ]]; then | ||
| msg=$(printf '%s' "$response" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('message',''))" 2>/dev/null) | ||
| echo "ERROR: token exchange rejected: ${msg:-unknown}" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| printf '%s' "$token" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds a controller-lifecycle timer, but a repo-wide check of
health.pyshows thatcollect_snapshotmonitors only the health, cleanup, and drift timers and only cleanup and drift services, with no consumer for reconciliation state. Consequently, a missing/disabled reconcile timer or persistent token and fetch failures can leave ordinary health reports and external heartbeats healthy indefinitely while reviewed desired state is no longer being applied; register this timer, service result, or reconcile state in the health contract.Useful? React with 👍 / 👎.