diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65a791b..eb81739 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,6 +17,16 @@ jobs: node-version: 24 - run: node --test "spdx-check/test/*.test.mjs" + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 24 + - run: npx prettier@3.10.1 --check . + - run: for f in quay-login/*.mjs; do node --check "$f"; done + dogfood: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index 6a1e80a..6c980c7 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,5 @@ Reusable GitHub Actions workflows for the ai-openshell organization. - [`spdx-check`](spdx-check/): fails the job if source files lack an `SPDX-License-Identifier: Apache-2.0` header. +- [`quay-login`](quay-login/): logs docker, podman or helm in to quay.io as a + robot account, using the job's GitHub OIDC token instead of a secret. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..6296eb7 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1794427683, + "narHash": "sha256-o7ey9gEilsxaifqJWnBDlYSKbNWYQYtKwDyXeZU2kaU=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cd489559845d81f673a12885576016c65cbe9363", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1794890597, + "narHash": "sha256-lME2lHoDHAaSHP3/okgiVoJ0icVEvsQH8LeU3We7tcc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "04906337b6d53e8bb0eaf0470ae9f070a4d4eb55", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1791244623, + "narHash": "sha256-uuanbkFk8a313Zfrxdcz8Us/yQtaxNnKJtX8v0ZPpd8=", + "owner": "nix-systems", + "repo": "default", + "rev": "d36eeed142f88b5a636f2e3856a53c61654cf29b", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..36c3eb1 --- /dev/null +++ b/flake.nix @@ -0,0 +1,20 @@ +{ + description = "Reusable GitHub Actions for the ai-openshell organization"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { packages = [ pkgs.nodejs_24 ]; }; + } + ); +} diff --git a/quay-login/README.md b/quay-login/README.md new file mode 100644 index 0000000..86a1123 --- /dev/null +++ b/quay-login/README.md @@ -0,0 +1,45 @@ +# quay-login + +Logs docker, podman or helm in to quay.io as a robot account, with no stored +secret. The step asks GitHub for the job's OIDC token and trades it at Quay's +robot federation endpoint for a robot token that lasts one hour. It's a +JavaScript action with no dependencies. + +```yaml +jobs: + publish: + runs-on: ubuntu-latest + environment: release + permissions: + contents: read + id-token: write + steps: + - uses: ai-openshell/workflows/quay-login@main + with: + robot: ai-openshell+console_release + clients: docker helm + - run: docker push quay.io/ai-openshell/console:${{ github.sha }} +``` + +## Inputs + +- `robot` (required): the robot account, written `namespace+name`. +- `clients` (optional, default `docker`): any of `docker`, `podman` and + `helm`, separated by spaces or newlines. A client that isn't installed fails + the step. +- `audience` (optional, default `quay`): the audience of the GitHub token. It + must be one the robot's federation lists. + +## Trust + +Quay gives out a robot token only when the robot's federation trusts the +GitHub token's issuer (`https://token.actions.githubusercontent.com`) and its +subject, compared exactly. Bind each robot to a GitHub environment, for +example `repo:ai-openshell/console:environment:release`. A job outside that +environment then gets nothing, whatever its workflow file says. + +The job needs `permissions: id-token: write`. Pull requests from forks never +get an OIDC token, so the step fails there. + +Both tokens are masked in the job log. The robot token expires after an hour, +so a job that runs longer must log in again. diff --git a/quay-login/action.yml b/quay-login/action.yml new file mode 100644 index 0000000..6de334e --- /dev/null +++ b/quay-login/action.yml @@ -0,0 +1,17 @@ +name: Quay login +description: Log docker, podman or helm in to quay.io as a robot account, using the job's GitHub OIDC token instead of a stored secret. +inputs: + robot: + description: Quay robot account to log in as, written namespace+name. + required: true + clients: + description: Clients to log in (docker, podman, helm), separated by whitespace or newlines. + required: false + default: docker + audience: + description: Audience of the GitHub OIDC token. It must be one the robot's federation trusts. + required: false + default: quay +runs: + using: node24 + main: index.mjs diff --git a/quay-login/clients.mjs b/quay-login/clients.mjs new file mode 100644 index 0000000..7442e1d --- /dev/null +++ b/quay-login/clients.mjs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Apache-2.0 + +import { execFileSync } from "node:child_process"; +import { REGISTRY } from "./quay.mjs"; + +export const CLIENTS = { + docker: ["docker", "login"], + podman: ["podman", "login"], + helm: ["helm", "registry", "login"], +}; + +export function login(client, username, password) { + const [command, ...args] = CLIENTS[client]; + // TODO: say " is not installed" instead of a bare ENOENT. + execFileSync( + command, + [...args, REGISTRY, "--username", username, "--password-stdin"], + { input: password, stdio: ["pipe", "inherit", "inherit"] }, + ); +} diff --git a/quay-login/index.mjs b/quay-login/index.mjs new file mode 100644 index 0000000..71aefb2 --- /dev/null +++ b/quay-login/index.mjs @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Apache-2.0 + +import { CLIENTS, login } from "./clients.mjs"; +import { requestIdToken } from "./oidc.mjs"; +import { exchangeForRobotToken } from "./quay.mjs"; + +function parseClients(value = "") { + const clients = value.split(/\s+/).filter(Boolean); + const unknown = clients.filter((client) => !Object.hasOwn(CLIENTS, client)); + if (unknown.length > 0) { + const known = Object.keys(CLIENTS).join(", "); + throw new Error( + `Unknown clients: ${unknown.join(", ")} (expected ${known})`, + ); + } + return clients; +} + +// Masked before anything else can print it. +function mask(secret) { + console.log(`::add-mask::${secret}`); +} + +async function main() { + const robot = process.env.INPUT_ROBOT; + const clients = parseClients(process.env.INPUT_CLIENTS); + const jwt = await requestIdToken(process.env.INPUT_AUDIENCE); + mask(jwt); + const token = await exchangeForRobotToken(robot, jwt); + mask(token); + clients.forEach((client) => login(client, robot, token)); +} + +main().catch((error) => { + console.log(`::error::${error.message}`); + process.exitCode = 1; +}); diff --git a/quay-login/oidc.mjs b/quay-login/oidc.mjs new file mode 100644 index 0000000..73e8428 --- /dev/null +++ b/quay-login/oidc.mjs @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: Apache-2.0 + +export async function requestIdToken(audience) { + const { + ACTIONS_ID_TOKEN_REQUEST_URL: url, + ACTIONS_ID_TOKEN_REQUEST_TOKEN: bearer, + } = process.env; + if (!url || !bearer) { + throw new Error( + "No GitHub OIDC token available; the job needs 'permissions: id-token: write'", + ); + } + const request = new URL(url); + request.searchParams.set("audience", audience); + const response = await fetch(request, { + headers: { Authorization: `bearer ${bearer}` }, + }); + // TODO: report GitHub's status and body when the request fails. + const { value } = await response.json(); + return value; +} diff --git a/quay-login/quay.mjs b/quay-login/quay.mjs new file mode 100644 index 0000000..b91919c --- /dev/null +++ b/quay-login/quay.mjs @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: Apache-2.0 + +export const REGISTRY = "quay.io"; + +const FEDERATION_URL = `https://${REGISTRY}/oauth2/federation/robot/token`; + +// Quay hands out a one-hour robot token only when the robot trusts the JWT's +// issuer and subject exactly. +export async function exchangeForRobotToken(robot, jwt) { + const basic = Buffer.from(`${robot}:${jwt}`).toString("base64"); + const response = await fetch(FEDERATION_URL, { + headers: { Authorization: `Basic ${basic}` }, + }); + // Quay's body says why (unknown robot, untrusted subject) and never echoes the JWT. + if (!response.ok) { + const reason = await response.text(); + throw new Error(`${REGISTRY} refused the exchange for ${robot}: ${reason}`); + } + const { token } = await response.json(); + return token; +}