Skip to content
Merged
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
30 changes: 17 additions & 13 deletions pm-stack/console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,18 @@ the route if the console does not come up with its sign-in settings.

## Configuration

| Variable | Default | Meaning |
| --------------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------- |
| `PM_CONSOLE_PORT`, `PM_CONSOLE_BIND` | `4872`, `127.0.0.1` | Listener; the address must be loopback |
| `PM_CONSOLE_OWNER_EMAILS` | none (nobody) | Comma-separated owner addresses |
| `PM_CONSOLE_ACCESS_TEAM_DOMAIN`, `PM_CONSOLE_ACCESS_AUD` | unset | The Access issuer; set together |
| `PM_CONSOLE_ISSUERS_FILE` | unset | JSON list of further issuers |
| `PM_CONSOLE_CERT_NAMES` | none | Certificate common names that are the owner |
| `PM_CONSOLE_DASHBOARD_URL` | `http://127.0.0.1:4870` | The dashboard behind the gate; loopback, or empty |
| `PM_CONSOLE_UNITS`, `PM_CONSOLE_PROTECTED_UNITS` | the stack's units | Units the console manages; units it will not stop |
| `PM_CONSOLE_ANDROID_PACKAGE`, `PM_CONSOLE_ANDROID_SHA256` | `today.developing.pmconsole`, none | The Android app's identity in the asset-links file |
| `PM_CONSOLE_REPO`, `PM_CONSOLE_QUEUE` | `developing-today/code`, `repo-health-code-pilot` | What it operates on |
| Variable | Default | Meaning |
| --------------------------------------------------------- | -------------------------------------------------- | -------------------------------------------------- |
| `PM_CONSOLE_PORT`, `PM_CONSOLE_BIND` | `4872`, `127.0.0.1` | Listener; the address must be loopback |
| `PM_CONSOLE_OWNER_EMAILS` | none (nobody) | Comma-separated owner addresses |
| `PM_CONSOLE_ACCESS_TEAM_DOMAIN`, `PM_CONSOLE_ACCESS_AUD` | unset | The Access issuer; set together |
| `PM_CONSOLE_ISSUERS_FILE` | unset | JSON list of further issuers |
| `PM_CONSOLE_CERT_NAMES` | none | Certificate common names that are the owner |
| `PM_CONSOLE_DASHBOARD_URL` | `http://127.0.0.1:4870` | The dashboard behind the gate; loopback, or empty |
| `PM_CONSOLE_UNITS`, `PM_CONSOLE_PROTECTED_UNITS` | the stack's units | Units the console manages; units it will not stop |
| `PM_CONSOLE_ANDROID_PACKAGE`, `PM_CONSOLE_ANDROID_SHA256` | `today.developing.pmconsole`, none | The Android app's identity in the asset-links file |
| `PM_CONSOLE_ANDROID_APK` | `~/.local/state/pm-console/android/pm-console.apk` | The signed build the console hands to the owner |
| `PM_CONSOLE_REPO`, `PM_CONSOLE_QUEUE` | `developing-today/code`, `repo-health-code-pilot` | What it operates on |

A malformed port, a listen address or dashboard address that is not
loopback, or a malformed issuer stops the process at boot and names the
Expand All @@ -105,5 +106,8 @@ variable.

`pm-stack/console/android/` is a Trusted Web Activity: the same site in
Chrome without browser chrome, so sign-in is Chrome's own and nothing is
reimplemented. The signed APK is built by the `pm-console-android`
workflow and attached to the release of the same name. See its README.
reimplemented. The `pm-console-android` workflow builds it and the host
signs it. Once the signed file is on the host, the console's header
shows "Android app": a download for the signed-in owner at
`/console/pm-console.apk`, so the phone gets the app from the site it
opens. See its README.
18 changes: 15 additions & 3 deletions pm-stack/console/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ reinstall; nothing else depends on it.

## Install

Download `pm-console.apk` from the
[release](https://github.com/developing-today/code/releases/tag/pm-console-android)
on the phone and open it. Later builds install over it.
On the phone, open `https://pm.security.cab/console/`, sign in, and tap
"Android app" in the header. That downloads the signed build from the
host (`/console/pm-console.apk`, owner only); open it to install. Later
builds install over it.

The console serves whatever signed build is at
`~/.local/state/pm-console/android/pm-console.apk` on the host, so
putting a new build in place is the whole release:

```bash
install -m 644 /tmp/pm-apk/pm-console.apk ~/.local/state/pm-console/android/pm-console.apk
```

The same file can also be attached to the GitHub release
`pm-console-android` for a copy outside the host.
5 changes: 4 additions & 1 deletion pm-stack/console/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,8 @@ async function show(name) {
}

for (const [name, [label]] of Object.entries(VIEWS)) tabs.append(h("button", { "data-view": name, onclick: () => show(name) }, label));
api("me").then((me) => (document.getElementById("who").textContent = me.email || me.certName || me.via)).catch(() => {});
api("me").then((me) => {
document.getElementById("who").textContent = me.email || me.certName || me.via;
document.getElementById("apk").hidden = !me.androidApp;
}).catch(() => {});
show(location.hash.slice(1));
3 changes: 3 additions & 0 deletions pm-stack/console/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<header class="bar">
<a class="brand" href="/console/">Console</a>
<a class="quiet" href="/">Dashboard</a>
<a id="apk" class="quiet" href="/console/pm-console.apk" hidden
>Android app</a
>
<span class="grow"></span>
<span id="who" class="quiet"></span>
</header>
Expand Down
4 changes: 4 additions & 0 deletions pm-stack/console/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export interface Config {
/** Android app identity served at /.well-known/assetlinks.json. */
androidPackage: string;
androidFingerprints: string[];
/** The signed APK the console hands to the signed-in owner, if the
* file exists. */
androidApk: string;
}

export class ConfigError extends Error {}
Expand Down Expand Up @@ -207,5 +210,6 @@ export function readConfig(
dashboardUrl,
androidPackage: env.PM_CONSOLE_ANDROID_PACKAGE ?? "today.developing.pmconsole",
androidFingerprints: fingerprints,
androidApk: env.PM_CONSOLE_ANDROID_APK ?? `${home}/.local/state/pm-console/android/pm-console.apk`,
};
}
22 changes: 21 additions & 1 deletion pm-stack/console/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const STATIC = new Map([
["app.css", "text/css; charset=utf-8"],
["icon.svg", "image/svg+xml"],
]);
const APK_NAME = "pm-console.apk";
const SECURITY = {
"Content-Security-Policy":
"default-src 'self'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'none'; form-action 'self'",
Expand Down Expand Up @@ -204,6 +205,20 @@ export async function handle(req: Request, deps: Deps, peer: string | null = nul
}
const type = method === "GET" ? STATIC.get(rest) : undefined;
if (type !== undefined) return file(rest, type);
// The Android app, for the signed-in owner: the phone gets it from the
// site it opens, and nothing has to be published anywhere else.
if (method === "GET" && rest === APK_NAME) {
const apk = Bun.file(deps.cfg.androidApk);
if (!(await apk.exists())) return json({ error: "no Android build is installed on this host" }, 404);
return new Response(apk, {
headers: {
"Content-Type": "application/vnd.android.package-archive",
"Content-Disposition": `attachment; filename="${APK_NAME}"`,
"Cache-Control": "no-store",
...SECURITY,
},
});
}
if (!rest.startsWith("api/")) return json({ error: "not found" }, 404);

let seg: string[];
Expand All @@ -219,7 +234,12 @@ export async function handle(req: Request, deps: Deps, peer: string | null = nul
if (seg.length === 1) {
switch (seg[0]) {
case "me":
return json({ ...who, repo: deps.cfg.repo, queue: deps.cfg.queue });
return json({
...who,
repo: deps.cfg.repo,
queue: deps.cfg.queue,
androidApp: await Bun.file(deps.cfg.androidApk).exists(),
});
case "overview":
return json(await ops.overview());
case "queue":
Expand Down
30 changes: 29 additions & 1 deletion pm-stack/console/test/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// compared with literal URLs, so no spelling of a path can pass as one
// of the public files unless the dashboard is asked for exactly that file.
import { beforeEach, describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
AuthError,
authorize,
Expand Down Expand Up @@ -1164,6 +1166,32 @@ describe("HTTP surface", () => {
expect([failed.status, await failed.json()]).toEqual([502, { error: "the dashboard did not answer" }]);
});

test("the Android app is handed to the signed-in owner only, when a build is on the host", async () => {
const dir = mkdtempSync(join(tmpdir(), "pm-console-test-"));
try {
const cfg = readConfig({ HOME: "/h", PM_CONSOLE_ANDROID_APK: join(dir, "pm-console.apk") });
expect(readConfig({ HOME: "/h" }).androidApk).toBe("/h/.local/state/pm-console/android/pm-console.apk");
const owner = { ...deps("owner"), cfg };
// No build yet: the page is told so, and the link answers 404.
expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(false);
expect((await handle(get("/console/pm-console.apk"), owner)).status).toBe(404);
writeFileSync(join(dir, "pm-console.apk"), "PK\u0003\u0004 signed apk bytes");
expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(true);
const res = await handle(get("/console/pm-console.apk"), owner);
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("application/vnd.android.package-archive");
expect(res.headers.get("content-disposition")).toBe('attachment; filename="pm-console.apk"');
expect(await res.text()).toBe("PK\u0003\u0004 signed apk bytes");
// Not for anyone else, and not by another method or name.
expect((await handle(get("/console/pm-console.apk"), { ...deps(401), cfg })).status).toBe(401);
expect((await handle(get("/console/pm-console.apk"), { ...deps(403), cfg })).status).toBe(403);
expect((await handle(post("/console/pm-console.apk", {}), owner)).status).toBe(404);
expect(owner.sent).toEqual([]); // never passed to the dashboard
} finally {
rmSync(dir, { recursive: true, force: true });
}
});

test("an action that cannot be recorded is not taken; one already taken is reported as it went", async () => {
const r = recorder();
const order: string[] = [];
Expand Down