Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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 ]; };
}
);
}
45 changes: 45 additions & 0 deletions quay-login/README.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions quay-login/action.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions quay-login/clients.mjs
Original file line number Diff line number Diff line change
@@ -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 "<command> is not installed" instead of a bare ENOENT.
execFileSync(
command,
[...args, REGISTRY, "--username", username, "--password-stdin"],
{ input: password, stdio: ["pipe", "inherit", "inherit"] },
);
}
37 changes: 37 additions & 0 deletions quay-login/index.mjs
Original file line number Diff line number Diff line change
@@ -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;
});
21 changes: 21 additions & 0 deletions quay-login/oidc.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions quay-login/quay.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
Loading