-
Notifications
You must be signed in to change notification settings - Fork 0
Automate GitHub App and runner-group bootstrap #75
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
Draft
Nickfost
wants to merge
10
commits into
main
Choose a base branch
from
feat/issue-27-github-bootstrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f59bdaa
feat: automate GitHub App bootstrap
89d79ac
fix: make GitHub bootstrap recoverable and scoped
831844a
fix: separate App and runner repository scopes
4fbb184
fix: close GitHub bootstrap verification gaps
e3ea10a
fix: roll back unverified runner groups
7255a7c
fix: verify bootstrap routing before mutation
73ab465
fix: complete bootstrap install preflight
5b7eebb
fix: serialize and bound GitHub bootstrap
fb6b12c
fix: close bootstrap lifecycle races
113bad0
fix: preserve ambiguous recovery state
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| #!/usr/bin/env python3 | ||
| import argparse | ||
| import ctypes | ||
| import html | ||
| import http.server | ||
| import os | ||
| import secrets | ||
| import signal | ||
| import urllib.parse | ||
| from typing import cast | ||
|
|
||
|
|
||
| class Server(http.server.HTTPServer): | ||
| organization: str | ||
| state: str | ||
| manifest: str | ||
| output: str | ||
| handoff: str | ||
| done: bool | ||
|
|
||
|
|
||
| class Callback(http.server.BaseHTTPRequestHandler): | ||
| server_version = "ci-fleet-bootstrap" | ||
| sys_version = "" | ||
|
|
||
| @property | ||
| def app_server(self) -> Server: | ||
| return cast(Server, self.server) | ||
|
|
||
| def log_message(self, format: str, *args: object) -> None: | ||
| del format, args | ||
|
|
||
| def reply(self, status: int, body: str) -> None: | ||
| data = body.encode() | ||
| self.send_response(status) | ||
| self.send_header("Content-Type", "text/html; charset=utf-8") | ||
| self.send_header("Content-Length", str(len(data))) | ||
| self.send_header("Cache-Control", "no-store") | ||
| self.send_header("X-Content-Type-Options", "nosniff") | ||
| self.send_header("X-Frame-Options", "DENY") | ||
| self.send_header("Content-Security-Policy", "default-src 'none'; form-action https://github.com; frame-ancestors 'none'") | ||
| self.end_headers() | ||
| self.wfile.write(data) | ||
|
|
||
| def do_GET(self) -> None: | ||
| parsed = urllib.parse.urlsplit(self.path) | ||
| if parsed.path == "/" and not parsed.query: | ||
| action = f"https://github.com/organizations/{urllib.parse.quote(self.app_server.organization, safe='')}/settings/apps/new?state={urllib.parse.quote(self.app_server.state, safe='')}" | ||
| body = ( | ||
| "<!doctype html><meta name=viewport content='width=device-width'>" | ||
| "<title>Register ci-fleet GitHub App</title>" | ||
| "<h1>Register this host's ci-fleet App</h1>" | ||
| f"<form action='{html.escape(action, quote=True)}' method=post>" | ||
| f"<input type=hidden name=manifest value='{html.escape(self.app_server.manifest, quote=True)}'>" | ||
| "<button type=submit>Continue to GitHub</button></form>" | ||
| ) | ||
| self.reply(200, body) | ||
| return | ||
| if parsed.path == "/next" and not parsed.query: | ||
| try: | ||
| target = open(self.app_server.handoff, encoding="utf-8").read().strip() | ||
| except FileNotFoundError: | ||
| self.reply(200, "<meta http-equiv=refresh content='1;url=/next'><p>Preparing the installation approval link…</p>") | ||
| return | ||
| if not target.startswith("https://github.com/apps/") or not target.endswith("/installations/new"): | ||
| self.reply(500, "Invalid installation target") | ||
| return | ||
| self.reply(200, f"<h1>App created</h1><p><a href='{html.escape(target, quote=True)}'>Continue to GitHub installation approval</a></p>") | ||
| self.app_server.done = True | ||
| return | ||
| if parsed.path != "/callback": | ||
| self.reply(404, "Not found") | ||
| return | ||
| try: | ||
| values = urllib.parse.parse_qs(parsed.query, strict_parsing=True) | ||
| except ValueError: | ||
| self.reply(400, "Invalid callback") | ||
| return | ||
| if set(values) != {"code", "state"} or any(len(value) != 1 for value in values.values()): | ||
| self.reply(400, "Invalid callback") | ||
| return | ||
| if not secrets.compare_digest(values["state"][0], self.app_server.state): | ||
| self.reply(403, "Invalid callback state") | ||
| return | ||
| code = values["code"][0] | ||
| if not code or len(code) > 512 or any(character.isspace() for character in code): | ||
| self.reply(400, "Invalid callback") | ||
| return | ||
| try: | ||
| descriptor = os.open(self.app_server.output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) | ||
| except FileExistsError: | ||
| self.reply(409, "Callback already consumed") | ||
| return | ||
| with os.fdopen(descriptor, "w", encoding="utf-8") as handle: | ||
| handle.write(code) | ||
| self.reply(200, "<meta http-equiv=refresh content='1;url=/next'><h1>Registration received</h1><p>Preparing installation approval. No value needs to be copied.</p>") | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--bind", required=True) | ||
| parser.add_argument("--parent-pid", required=True, type=int) | ||
| parser.add_argument("--port", required=True, type=int) | ||
| parser.add_argument("--organization", required=True) | ||
| parser.add_argument("--state", required=True) | ||
| parser.add_argument("--manifest", required=True) | ||
| parser.add_argument("--output", required=True) | ||
| parser.add_argument("--handoff", required=True) | ||
| parser.add_argument("--timeout", type=int, default=600) | ||
| args = parser.parse_args() | ||
| if not 1 <= args.port <= 65535 or not 30 <= args.timeout <= 1800: | ||
| parser.error("invalid port or timeout") | ||
| if os.getppid() != args.parent_pid: | ||
| return 2 | ||
| if ctypes.CDLL(None, use_errno=True).prctl(1, signal.SIGTERM) != 0: | ||
| raise OSError(ctypes.get_errno(), "cannot bind callback lifetime to bootstrap") | ||
| if os.getppid() != args.parent_pid: | ||
| return 2 | ||
| server = Server((args.bind, args.port), Callback) | ||
| server.organization = args.organization | ||
| server.state = args.state | ||
| server.manifest = args.manifest | ||
| server.output = args.output | ||
| server.handoff = args.handoff | ||
| server.done = False | ||
| server.timeout = 1 | ||
| deadline = __import__("time").monotonic() + args.timeout | ||
| while __import__("time").monotonic() < deadline and not server.done: | ||
| server.handle_request() | ||
| server.server_close() | ||
| return 0 if server.done else 2 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
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.
Uh oh!
There was an error while loading. Please reload this page.