diff --git a/docs/sites/colony/git.md b/docs/sites/colony/git.md index 866cb3c..1a43ba2 100644 --- a/docs/sites/colony/git.md +++ b/docs/sites/colony/git.md @@ -20,6 +20,15 @@ The Git forge at `git.nul.ie` (self-registration disabled), configured in `gitea`, on [`object`](shill/containers/object.md)) — the secret is spliced into `app.ini` at startup. - Mail goes out via `mail.nul.ie`, including the issue-reply incoming-mail poller. +- **Sign-in with Pocket ID:** `preStart` adds or updates the `id.nul.ie` OpenID Connect login + source. The client `gitea` and its secret `gitea/oidc-secret.txt` are declared on + [`middleman`](shill/containers/middleman.md#pocket-id). + - Registration stays disabled, so a Pocket ID sign-in can't create an account. The first one + links to an existing account after signing in with its password. + - It runs before the server, which only reads login sources at startup. If it fails, Gitea + starts without the source and logs a warning. + - The CLI only takes the secret as an argument, so it is briefly visible in the process list + while `preStart` runs. ### Gitea Actions runner diff --git a/nixos/boxes/colony/vms/git/gitea.nix b/nixos/boxes/colony/vms/git/gitea.nix index 2fa5cd2..f513300 100644 --- a/nixos/boxes/colony/vms/git/gitea.nix +++ b/nixos/boxes/colony/vms/git/gitea.nix @@ -1,8 +1,11 @@ { lib, pkgs, config, assignments, allAssignments, ... }: let - inherit (lib) mkMerge; + inherit (lib) mkMerge mkAfter getExe; inherit (lib.my.c) pubDomain; inherit (lib.my.c.colony) prefixes; + + # Pocket ID login source, client `gitea` (declared on `middleman`) + oidcSource = "id.${pubDomain}"; in { config = { @@ -48,6 +51,40 @@ in # The default /tmp is on the small tmpfs root, so keep them on the state volume. environment.TMPDIR = "${config.services.gitea.stateDir}/tmp"; } + { + # Gitea keeps login sources in its database: add the Pocket ID one, or bring it up to + # date. The server only reads them at startup, so this runs before it; a failure only + # warns, so Gitea still starts. The CLI only takes the secret as an argument. + preStart = mkAfter ( + let + gitea = getExe config.services.gitea.package; + in + '' + oidc_source_setup() { + local oidc_args=( + --name '${oidcSource}' + --provider openidConnect + --key gitea + --secret "$(< '${config.age.secrets."gitea/oidc-secret.txt".path}')" + --auto-discover-url 'https://${oidcSource}/.well-known/openid-configuration' + --scopes openid --scopes email --scopes profile + ) + + local list oidc_id + list="$(${gitea} admin auth list)" || return + oidc_id="$(${pkgs.gawk}/bin/awk '$2 == "${oidcSource}" { print $1 }' <<< "$list")" || return + if [ -n "$oidc_id" ]; then + ${gitea} admin auth update-oauth --id "$oidc_id" "''${oidc_args[@]}" + else + ${gitea} admin auth add-oauth "''${oidc_args[@]}" + fi + } + + if ! oidc_source_setup; then + echo "Couldn't set up the ${oidcSource} login source; starting without it" >&2 + fi + ''); + } ]; }; @@ -139,6 +176,8 @@ in "gitea/db.txt" = ownedByGit; "gitea/mail.txt" = ownedByGit; "gitea/minio.txt" = ownedByGit; + # Shared with `middleman`, which registers it with Pocket ID + "gitea/oidc-secret.txt" = ownedByGit; }; }; }; diff --git a/nixos/boxes/colony/vms/shill/containers/middleman/auth.nix b/nixos/boxes/colony/vms/shill/containers/middleman/auth.nix index 550c435..527ccf0 100644 --- a/nixos/boxes/colony/vms/shill/containers/middleman/auth.nix +++ b/nixos/boxes/colony/vms/shill/containers/middleman/auth.nix @@ -15,6 +15,8 @@ in "middleman/pocket-id-api-key" = { }; # `OAUTH2_PROXY_CLIENT_SECRET` and `OAUTH2_PROXY_COOKIE_SECRET` "middleman/oauth2-proxy.env" = { }; + # Client secrets shared with the boxes that run the apps + "gitea/oidc-secret.txt" = { }; }; pocket-id = { @@ -38,6 +40,14 @@ in key = "OAUTH2_PROXY_CLIENT_SECRET"; }; }; + + # Gitea's `id.nul.ie` login source (`git` VM) + gitea = { + name = "Gitea"; + callbackURLs = [ "https://git.${pubDomain}/user/oauth2/id.${pubDomain}/callback" ]; + launchURL = "https://git.${pubDomain}/"; + secret.file = config.age.secrets."gitea/oidc-secret.txt".path; + }; }; }; }; diff --git a/secrets/gitea/oidc-secret.txt.age b/secrets/gitea/oidc-secret.txt.age new file mode 100644 index 0000000..da427c8 --- /dev/null +++ b/secrets/gitea/oidc-secret.txt.age @@ -0,0 +1,15 @@ +-----BEGIN AGE ENCRYPTED FILE----- +YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IGpJOFJBZyBzU2lH +U05YNG5ISEJtTklOZmZRUUI3MWxuU1JRWG0vNEIxekt3NTdWYVdJCldDMENWenl1 +ZmZCTW1ySFBXZFEvbHZhR0NDOWpLSzhNRTZUcFU1NWZXUWcKLT4gc3NoLWVkMjU1 +MTkgVkZjdzVnIDdXSXdnK1VvQzJrbWMyRzltL3BZMEZPV2pEV1lSUW5ySXkxaDRC +RjRMVDAKRjVRSm9sMGRNUTBWYkdWNlEwKzNTTU8xRXBmV1JDREhtNThabFRIVERU +MAotPiBYMjU1MTkgdHFnZ1RKQlRDTmg0TksvaGgrUER6SU5MYWV6WlpEWXA3Qktv +MFZJTktTSQpyMHhRb0FNeGFFZkIvUUxjMWlVbkxRY2pKZnhSanlrUWFITWZHUE1i +dldvCi0+IFgyNTUxOSBGbkhrVlZYYjZIakZ5MnZ5ZDJXdnoycEVtL08zYjNjNUty +OTY2amsrWTJnCk9neE80T25naFBDV3pjZXh0QmFKNW9YZ1VHdXJsREJidHlNRUY5 +Y0haTDAKLT4gdEkuc210LWdyZWFzZSA0UT5jYyBkICUgIzArNQpCVE5aQnM1blNO +L0Nvb1hDMTRSOVNHUmlTM1lmZWcKLS0tIGFBTDN1ZUhSM21LaVFLZDBUQkFJcDJZ +OUVSYVp5Q2NIL01IUTNsSmQ3Y2MKAuYjogUq28f/Cdf5jd/RkWaPZEqA+qGAmTnx +fXgii0zfKVsAMGtro8wB4RdKYcvyW86k7zYaZobXHzR4ApCF+Zg= +-----END AGE ENCRYPTED FILE-----