agents get a per-agent repo limit in their permissions record, orgs are locked
max_repos (default 0, 0..=100) lives in the agent's agent-permissions record and is edited on its permissions panel. The controller applies each agent's own value as max_repo_creation at CreateForgeUser and to existing accounts on change, restart and every five minutes; agent accounts may not create organizations.
This commit is contained in:
parent
dc0d08b7fb
commit
1edeb5c55d
23 changed files with 763 additions and 58 deletions
|
|
@ -119,7 +119,7 @@ checkpoints**, not about sandboxing the agent from its own tools:
|
||||||
- **Operator merges, not the agent** — an agent may _push_ branches, but a
|
- **Operator merges, not the agent** — an agent may _push_ branches, but a
|
||||||
**human (the operator) merges the PR**, keeping a person in the loop on the
|
**human (the operator) merges the PR**, keeping a person in the loop on the
|
||||||
highest-value action. On the **internal forge this is technically enforced,
|
highest-value action. On the **internal forge this is technically enforced,
|
||||||
not just convention**: agents can't create repos (`max_repo_creation = 0`),
|
not just convention**: agents can't create repos by default (next bullet),
|
||||||
and `main` on an `agent-configs/<name>` repo carries swarm-controller's
|
and `main` on an `agent-configs/<name>` repo carries swarm-controller's
|
||||||
branch protection: merge and approval allowlisted to the `operators` team,
|
branch protection: merge and approval allowlisted to the `operators` team,
|
||||||
which swarm-controller converges on every config repo. An operator's merge
|
which swarm-controller converges on every config repo. An operator's merge
|
||||||
|
|
@ -129,6 +129,15 @@ checkpoints**, not about sandboxing the agent from its own tools:
|
||||||
settings nor merge its own PR. It's **not** set up for external VCS (GitHub
|
settings nor merge its own PR. It's **not** set up for external VCS (GitHub
|
||||||
etc.), though — there, operator-merge is process + accepted risk, not a
|
etc.), though — there, operator-merge is process + accepted risk, not a
|
||||||
technical control.
|
technical control.
|
||||||
|
- **Repo creation is a per-agent setting, default 0** — the _repositories_ badge
|
||||||
|
in an agent's card sets how many repos that agent's forge account may create
|
||||||
|
(`max_repos` in its permissions record, Forgejo's `max_repo_creation`, 0 to
|
||||||
|
100). The operator must raise it for that agent before it may create any. Agents never create organizations: the
|
||||||
|
controller turns that off on every agent account. A repo an agent creates is
|
||||||
|
its own, so the operator-merge gate doesn't cover it, and the agent can add a
|
||||||
|
CI workflow to it. CI workflows run unsandboxed on the CI runner, which is
|
||||||
|
arbitrary code on the runner host. Allow repo creation only for agents you
|
||||||
|
would trust with that.
|
||||||
- **Operator sign-off** — config changes are config PRs an operator merges
|
- **Operator sign-off** — config changes are config PRs an operator merges
|
||||||
on the forge (see
|
on the forge (see
|
||||||
[`persistence.md`](persistence.md#config-changes)), and
|
[`persistence.md`](persistence.md#config-changes)), and
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ by a test). swarm-controller mints every agent token with it:
|
||||||
| `write:repository` | Create, clone, push, delete repos; merge PRs. |
|
| `write:repository` | Create, clone, push, delete repos; merge PRs. |
|
||||||
| `write:issue` | Open / comment / review issues **and** pull requests (Forgejo namespaces PR conversation under issues). |
|
| `write:issue` | Open / comment / review issues **and** pull requests (Forgejo namespaces PR conversation under issues). |
|
||||||
| `write:user` | Edit own profile, create repos under own user. |
|
| `write:user` | Edit own profile, create repos under own user. |
|
||||||
| `write:organization` | Create + manage orgs (lets agents share a forge namespace). |
|
| `write:organization` | Manage orgs the agent belongs to. The forge refuses to create one: agent accounts may not. |
|
||||||
| `read:user` | Token-owner endpoint used for self-identification at harness startup. |
|
| `read:user` | Token-owner endpoint used for self-identification at harness startup. |
|
||||||
| `write:misc` | Hooks, attachments, the rest of the long tail. |
|
| `write:misc` | Hooks, attachments, the rest of the long tail. |
|
||||||
| `read:notification` | Poll `GET /notifications` for unread events. |
|
| `read:notification` | Poll `GET /notifications` for unread events. |
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ Deadline: hive-forge stops after HIVE_FORGE_DEADLINE seconds (default 900), prin
|
||||||
* `comment-show` — Print the body (or full JSON) of a single comment by id
|
* `comment-show` — Print the body (or full JSON) of a single comment by id
|
||||||
* `comment-edit` — Edit an existing comment by id
|
* `comment-edit` — Edit an existing comment by id
|
||||||
* `clone` — Clone a forge repo (default `-r`/`HIVE_FORGE_REPO`) with credentials autoinjected. Pairs with `pr create --agit`
|
* `clone` — Clone a forge repo (default `-r`/`HIVE_FORGE_REPO`) with credentials autoinjected. Pairs with `pr create --agit`
|
||||||
* `repo-create` — Create a forge repo under the current user (or `--org`). Prints the repo URL. Agent accounts can't create repos (limit 0)
|
* `repo-create` — Create a forge repo under the current user (or `--org`). Prints the repo URL. Agent accounts create repos only up to their own limit (0 by default)
|
||||||
* `repo-add-collaborator` — Add a collaborator to the active repo (`-r`/`HIVE_FORGE_REPO`) with a permission level. Companion to `repo-create`
|
* `repo-add-collaborator` — Add a collaborator to the active repo (`-r`/`HIVE_FORGE_REPO`) with a permission level. Companion to `repo-create`
|
||||||
* `repo-labels` — List every label defined on the repo (name + description), optionally filtered by a name substring
|
* `repo-labels` — List every label defined on the repo (name + description), optionally filtered by a name substring
|
||||||
* `repo-search` — Search the forge for repositories by keyword, topic, or description
|
* `repo-search` — Search the forge for repositories by keyword, topic, or description
|
||||||
|
|
@ -1008,7 +1008,7 @@ Clone a forge repo (default `-r`/`HIVE_FORGE_REPO`) with credentials autoinjecte
|
||||||
|
|
||||||
## `hive-forge repo-create`
|
## `hive-forge repo-create`
|
||||||
|
|
||||||
Create a forge repo under the current user (or `--org`). Prints the repo URL. Agent accounts can't create repos (limit 0)
|
Create a forge repo under the current user (or `--org`). Prints the repo URL. Agent accounts create repos only up to their own limit (0 by default)
|
||||||
|
|
||||||
**Usage:** `hive-forge repo-create [OPTIONS] <NAME>`
|
**Usage:** `hive-forge repo-create [OPTIONS] <NAME>`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -224,9 +224,11 @@ plain comment show under `last comment`, not `reviews`.
|
||||||
|
|
||||||
### Repo management
|
### Repo management
|
||||||
|
|
||||||
Agent accounts can't create repos: swarm-controller sets their
|
Agent accounts can't create repos unless the operator allows it for that
|
||||||
repo-creation limit to 0 (see `docs/2-handbook/agents/security.md`), so
|
agent: swarm-controller sets the account's repo-creation limit to the
|
||||||
an operator merge stays the only path to main. Ask the operator to
|
agent's own `max_repos`, which is 0 until the operator raises it (see
|
||||||
|
`docs/2-handbook/agents/security.md`), so an operator merge stays the only
|
||||||
|
path to main. Ask the operator to
|
||||||
create the repo (for example `agents/<name>`) and add you as a
|
create the repo (for example `agents/<name>`) and add you as a
|
||||||
collaborator. Forgejo also disables push-to-create.
|
collaborator. Forgejo also disables push-to-create.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -817,6 +817,21 @@ The agent's **internet** access is its own row beside it: a badge reading
|
||||||
restricted, log or unrestricted. Selecting it lists the three modes; picking one does a
|
restricted, log or unrestricted. Selecting it lists the three modes; picking one does a
|
||||||
`PUT` of the mode with the stored permissions beside it.
|
`PUT` of the mode with the stored permissions beside it.
|
||||||
|
|
||||||
|
The **repositories** row is how many repositories the agent's forge account may
|
||||||
|
create: a badge reading none or `up to N`, in the warning tone above 0. Selecting
|
||||||
|
it opens a number box, 0 to 100, and **save** does a `PUT` of `max_repos` with the
|
||||||
|
stored permissions beside it. A `PUT` that leaves `max_repos` out, and the `DELETE`
|
||||||
|
of **use the default**, keep it. Nothing stored means 0, and the box warns that an
|
||||||
|
agent that may create repositories can add a CI workflow to them, CI workflows run
|
||||||
|
unsandboxed on the CI runner, and that makes it arbitrary code on the runner host.
|
||||||
|
The controller sets the value as Forgejo's `max_repo_creation` on that agent's
|
||||||
|
account: on a new one at creation, and on an existing one within seconds of a save
|
||||||
|
and again after every restart. It also turns off creating organizations on every
|
||||||
|
agent account, whatever the number. Forgejo never reports `max_repo_creation`
|
||||||
|
back, so the controller's log (`agent max repos`) is the record of what it applied
|
||||||
|
to each agent. The controller tries an agent whose account the forge refuses again
|
||||||
|
every five minutes.
|
||||||
|
|
||||||
A saved change isn't instant. The hive reads the grant about every 30 seconds
|
A saved change isn't instant. The hive reads the grant about every 30 seconds
|
||||||
and the agent takes it at its next turn. A change to `manage_root_agent`
|
and the agent takes it at its next turn. A change to `manage_root_agent`
|
||||||
restarts the agent after its current turn. The card reads
|
restarts the agent after its current turn. The card reads
|
||||||
|
|
|
||||||
|
|
@ -340,6 +340,15 @@ explicit `unrestricted_internet: false`; read the pair with
|
||||||
`DELETE` keeps it. A hive that doesn't read the field sees `false` and enforces
|
`DELETE` keeps it. A hive that doesn't read the field sees `false` and enforces
|
||||||
the list. The proxy's handling is under _Log mode_ in [the egress proxy](../../4-implementation/networking/network.md#egress-proxy-squid).
|
the list. The proxy's handling is under _Log mode_ in [the egress proxy](../../4-implementation/networking/network.md#egress-proxy-squid).
|
||||||
|
|
||||||
|
**Repository limit** — the record also carries `max_repos`, how many repositories
|
||||||
|
the agent's forge account may create, 0 to 100. An absent value is 0. Only the
|
||||||
|
controller reads it: it sets the account's Forgejo `max_repo_creation` from it at
|
||||||
|
`CreateForgeUser`, and again for an existing account within seconds of a change,
|
||||||
|
at every start and every five minutes (`forge::agent_max_repos`). It also turns
|
||||||
|
off creating organizations on every agent account. A `PUT` that leaves the field
|
||||||
|
out, a `DELETE`, and a re-create keep the stored value; the route refuses more
|
||||||
|
than 100.
|
||||||
|
|
||||||
**How a change applies** —
|
**How a change applies** —
|
||||||
- The agent's harness reads its own key every 10 seconds and builds every
|
- The agent's harness reads its own key every 10 seconds and builds every
|
||||||
turn's `--tools`, `--allowedTools` and MCP config from the last value, so a
|
turn's `--tools`, `--allowedTools` and MCP config from the last value, so a
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import { AgentLimits } from "./AgentLimits.js";
|
||||||
import {
|
import {
|
||||||
AgentInternet,
|
AgentInternet,
|
||||||
AgentPermissions,
|
AgentPermissions,
|
||||||
|
AgentRepos,
|
||||||
useGrant,
|
useGrant,
|
||||||
} from "./AgentPermissions.js";
|
} from "./AgentPermissions.js";
|
||||||
import { AllowedDomains } from "./AllowedDomains.js";
|
import { AllowedDomains } from "./AllowedDomains.js";
|
||||||
|
|
@ -223,6 +224,10 @@ function GrantRows({ agent }: { agent: string }) {
|
||||||
<dd>
|
<dd>
|
||||||
<AllowedDomains key={agent} agent={agent} />
|
<AllowedDomains key={agent} agent={agent} />
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt>repositories</dt>
|
||||||
|
<dd>
|
||||||
|
{g.grant === null ? g.loading : <AgentRepos agent={agent} g={g} />}
|
||||||
|
</dd>
|
||||||
<dt>permissions</dt>
|
<dt>permissions</dt>
|
||||||
<dd>
|
<dd>
|
||||||
{g.grant === null ? (
|
{g.grant === null ? (
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
// <AgentPermissions> and <AgentInternet> — one agent's grant in an opened agent
|
// <AgentPermissions>, <AgentInternet> and <AgentRepos> — one agent's grant in an
|
||||||
// card: the permissions it holds, and how far its traffic may leave the host.
|
// opened agent card: the permissions it holds, how far its traffic may leave
|
||||||
// Each is a badge that opens a control under it, which saves as soon as it
|
// the host, and how many repositories its forge account may create. Each is a badge that opens a control under it, which saves as soon as it
|
||||||
// changes.
|
// changes.
|
||||||
//
|
//
|
||||||
// Both read `GET /api/agents/{name}/permissions`, which answers the stored
|
// Both read `GET /api/agents/{name}/permissions`, which answers the stored
|
||||||
// grant as `{ permissions?, unrestricted_internet?, internet_log? }` (`{}` when
|
// grant as `{ permissions?, unrestricted_internet?, internet_log?, max_repos? }` (`{}` when
|
||||||
// the swarm sets none, so the agent runs on the default set). The permissions
|
// the swarm sets none, so the agent runs on the default set). The permissions
|
||||||
// control offers the catalogue from `GET /api/permissions` as checkboxes and
|
// control offers the catalogue from `GET /api/permissions` as checkboxes and
|
||||||
// PUTs the ticked ones, a PUT replacing the stored value whole. A selection
|
// PUTs the ticked ones, a PUT replacing the stored value whole. A selection
|
||||||
// equal to the default set DELETEs instead, as the create form sends nothing
|
// equal to the default set DELETEs instead, as the create form sends nothing
|
||||||
// for it: the agent then follows the default if that changes. The internet
|
// for it: the agent then follows the default if that changes. The internet
|
||||||
// control PUTs the mode with the stored list beside it. The route's refusal
|
// control PUTs the mode with the stored list beside it, and the repositories
|
||||||
|
// control the limit the same way. The route's refusal
|
||||||
// shows as it arrives, in the control.
|
// shows as it arrives, in the control.
|
||||||
//
|
//
|
||||||
// Their state belongs to one agent: callers key them by agent name, so
|
// Their state belongs to one agent: callers key them by agent name, so
|
||||||
|
|
@ -23,6 +24,7 @@ import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
|
||||||
import { Button } from "../../ui/button/Button.js";
|
import { Button } from "../../ui/button/Button.js";
|
||||||
import { BadgeSkeleton } from "../../ui/skeleton/BadgeSkeleton.js";
|
import { BadgeSkeleton } from "../../ui/skeleton/BadgeSkeleton.js";
|
||||||
import { Hint } from "../../ui/hint/Hint.js";
|
import { Hint } from "../../ui/hint/Hint.js";
|
||||||
|
import { TextField } from "../../ui/text-field/TextField.js";
|
||||||
import { MultiselectFilterOptions } from "../../ui/multiselect-filter/MultiselectFilter.js";
|
import { MultiselectFilterOptions } from "../../ui/multiselect-filter/MultiselectFilter.js";
|
||||||
import {
|
import {
|
||||||
requestPermissions,
|
requestPermissions,
|
||||||
|
|
@ -34,6 +36,10 @@ import {
|
||||||
internetBody,
|
internetBody,
|
||||||
internetMode,
|
internetMode,
|
||||||
internetSummary,
|
internetSummary,
|
||||||
|
MAX_REPOS,
|
||||||
|
maxReposBody,
|
||||||
|
parseMaxRepos,
|
||||||
|
reposSummary,
|
||||||
type InternetMode,
|
type InternetMode,
|
||||||
} from "./permissions.js";
|
} from "./permissions.js";
|
||||||
import "./AgentLimits.css";
|
import "./AgentLimits.css";
|
||||||
|
|
@ -43,6 +49,7 @@ interface Grant {
|
||||||
permissions?: string[];
|
permissions?: string[];
|
||||||
unrestricted_internet?: boolean;
|
unrestricted_internet?: boolean;
|
||||||
internet_log?: boolean;
|
internet_log?: boolean;
|
||||||
|
max_repos?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mirrors swarm-controller's `Catalogue` (`GET /api/permissions`).
|
// Mirrors swarm-controller's `Catalogue` (`GET /api/permissions`).
|
||||||
|
|
@ -302,3 +309,72 @@ export function AgentInternet({ agent, g }: { agent: string; g: GrantState }) {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function AgentRepos({ agent, g }: { agent: string; g: GrantState }) {
|
||||||
|
const { grant, busy, saveError, loading, send } = g;
|
||||||
|
// The box as typed; null shows the stored limit.
|
||||||
|
const [typed, setTyped] = useState<string | null>(null);
|
||||||
|
if (grant === null) return loading;
|
||||||
|
|
||||||
|
const stored = grant.max_repos ?? 0;
|
||||||
|
const text = typed ?? String(stored);
|
||||||
|
const parsed = parseMaxRepos(text);
|
||||||
|
|
||||||
|
async function save(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (parsed === null) return;
|
||||||
|
const ok = await send({
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: JSON.stringify(maxReposBody(grant?.permissions, parsed)),
|
||||||
|
});
|
||||||
|
// The grant was read again, or the change refused: show what is stored.
|
||||||
|
if (ok) setTyped(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div class="ui-agent-limits">
|
||||||
|
<BadgePopover
|
||||||
|
label="repositories"
|
||||||
|
value={reposSummary(grant.max_repos)}
|
||||||
|
tone={stored === 0 ? "neutral" : "warning"}
|
||||||
|
title={`how many repositories ${agent}'s forge account may create`}
|
||||||
|
popoverLabel={`${agent} repository limit`}
|
||||||
|
>
|
||||||
|
<p class="ui-badge-popover-help">
|
||||||
|
How many repositories {agent}'s forge account may create, 0 to{" "}
|
||||||
|
{MAX_REPOS}. 0 means none. The swarm applies a change to the account
|
||||||
|
within seconds.
|
||||||
|
</p>
|
||||||
|
<p class="ui-badge-popover-help" role="note">
|
||||||
|
<strong>Warning:</strong> an agent that may create repositories can
|
||||||
|
add a CI workflow to them, and CI workflows run unsandboxed on the CI
|
||||||
|
runner. That is arbitrary code on the runner host. Only allow this for
|
||||||
|
agents you would trust with that.
|
||||||
|
</p>
|
||||||
|
<form onSubmit={(e) => void save(e)}>
|
||||||
|
<TextField
|
||||||
|
id={`max-repos-${agent}`}
|
||||||
|
label="repositories"
|
||||||
|
type="number"
|
||||||
|
value={text}
|
||||||
|
onInput={setTyped}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="primary"
|
||||||
|
type="submit"
|
||||||
|
disabled={busy || parsed === null || parsed === stored}
|
||||||
|
>
|
||||||
|
{busy ? "saving…" : "save"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
{saveError ? (
|
||||||
|
<ApiErrorPanel
|
||||||
|
context="the repository limit was not saved"
|
||||||
|
problem={saveError}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</BadgePopover>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,3 +69,38 @@ export function internetBody(
|
||||||
...(mode === "log" ? { internet_log: true as const } : {}),
|
...(mode === "log" ? { internet_log: true as const } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The most `max_repos` may be set to; the route refuses more. */
|
||||||
|
export const MAX_REPOS = 100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The repository limit a typed value stands for, or null when it is not a
|
||||||
|
* whole number from 0 to {@link MAX_REPOS}.
|
||||||
|
*/
|
||||||
|
export function parseMaxRepos(text: string): number | null {
|
||||||
|
const t = text.trim();
|
||||||
|
if (!/^\d+$/.test(t)) return null;
|
||||||
|
const n = Number(t);
|
||||||
|
return n <= MAX_REPOS ? n : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What the repositories badge shows: an absent limit is none. */
|
||||||
|
export function reposSummary(stored: number | undefined): string {
|
||||||
|
return stored === undefined || stored === 0 ? "none" : `up to ${stored}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The `PUT` body that sets the repository limit. A `PUT` without
|
||||||
|
* `permissions` resets a stored list to the default, so the stored list goes
|
||||||
|
* along, as in {@link internetBody}; the internet fields are left out, which
|
||||||
|
* keeps them.
|
||||||
|
*/
|
||||||
|
export function maxReposBody(
|
||||||
|
stored: string[] | undefined,
|
||||||
|
n: number,
|
||||||
|
): { permissions?: string[]; max_repos: number } {
|
||||||
|
return {
|
||||||
|
...(stored === undefined ? {} : { permissions: stored }),
|
||||||
|
max_repos: n,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,10 @@ import {
|
||||||
internetBody,
|
internetBody,
|
||||||
internetMode,
|
internetMode,
|
||||||
internetSummary,
|
internetSummary,
|
||||||
|
MAX_REPOS,
|
||||||
|
maxReposBody,
|
||||||
|
parseMaxRepos,
|
||||||
|
reposSummary,
|
||||||
} from "../src/agent/detail/permissions.ts";
|
} from "../src/agent/detail/permissions.ts";
|
||||||
|
|
||||||
const all = ["messaging", "meta", "inbox", "execution", "manage_root_agent"];
|
const all = ["messaging", "meta", "inbox", "execution", "manage_root_agent"];
|
||||||
|
|
@ -79,3 +83,29 @@ test("the log mode keeps the old flag false and names internet_log; the others l
|
||||||
assert.equal("internet_log" in internetBody([], "restricted"), false);
|
assert.equal("internet_log" in internetBody([], "restricted"), false);
|
||||||
assert.equal("internet_log" in internetBody([], "unrestricted"), false);
|
assert.equal("internet_log" in internetBody([], "unrestricted"), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("a whole number from 0 to the most is a repository limit", () => {
|
||||||
|
assert.equal(parseMaxRepos("0"), 0);
|
||||||
|
assert.equal(parseMaxRepos(" 7 "), 7);
|
||||||
|
assert.equal(parseMaxRepos(String(MAX_REPOS)), MAX_REPOS);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("anything else is not: empty, negative, fractional, above the most, text", () => {
|
||||||
|
for (const t of ["", " ", "-1", "1.5", "1e2", String(MAX_REPOS + 1), "x"]) {
|
||||||
|
assert.equal(parseMaxRepos(t), null, JSON.stringify(t));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the repositories badge tells none from a limit", () => {
|
||||||
|
assert.equal(reposSummary(undefined), "none");
|
||||||
|
assert.equal(reposSummary(0), "none");
|
||||||
|
assert.equal(reposSummary(3), "up to 3");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the limit's body carries the stored list, so the PUT does not reset it", () => {
|
||||||
|
assert.deepEqual(maxReposBody(["meta"], 3), {
|
||||||
|
permissions: ["meta"],
|
||||||
|
max_repos: 3,
|
||||||
|
});
|
||||||
|
assert.deepEqual(maxReposBody(undefined, 0), { max_repos: 0 });
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ enum Verb {
|
||||||
/// credentials autoinjected. Pairs with `pr create --agit`.
|
/// credentials autoinjected. Pairs with `pr create --agit`.
|
||||||
Clone(verbs::clone::Args),
|
Clone(verbs::clone::Args),
|
||||||
/// Create a forge repo under the current user (or `--org`). Prints
|
/// Create a forge repo under the current user (or `--org`). Prints
|
||||||
/// the repo URL. Agent accounts can't create repos (limit 0).
|
/// the repo URL. Agent accounts create repos only up to their own limit (0 by default).
|
||||||
RepoCreate(verbs::repo_create::Args),
|
RepoCreate(verbs::repo_create::Args),
|
||||||
/// Add a collaborator to the active repo (`-r`/`HIVE_FORGE_REPO`)
|
/// Add a collaborator to the active repo (`-r`/`HIVE_FORGE_REPO`)
|
||||||
/// with a permission level. Companion to `repo-create`.
|
/// with a permission level. Companion to `repo-create`.
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,9 @@
|
||||||
//!
|
//!
|
||||||
//! Wraps `POST /api/v1/user/repos` (or `/orgs/{org}/repos`). For accounts
|
//! Wraps `POST /api/v1/user/repos` (or `/orgs/{org}/repos`). For accounts
|
||||||
//! that may create repos (operators, non-agent users). Agent accounts
|
//! that may create repos (operators, non-agent users). Agent accounts
|
||||||
//! cannot: swarm-controller sets their repo-creation limit to 0, and
|
//! cannot unless the operator allows it: swarm-controller sets their
|
||||||
//! Forgejo reports that as a bare 500 on the user path, which
|
//! repo-creation limit to the agent's own `max_repos` (0 by default), and Forgejo
|
||||||
|
//! reports a used-up limit as a bare 500 on the user path, which
|
||||||
//! `explain_create_error` turns into a message naming the operator path.
|
//! `explain_create_error` turns into a message naming the operator path.
|
||||||
//! The instance has push-to-create disabled, so `git push` to a
|
//! The instance has push-to-create disabled, so `git push` to a
|
||||||
//! non-existent repo 403s. Pairs with `clone` and `repo-add-collaborator`.
|
//! non-existent repo 403s. Pairs with `clone` and `repo-add-collaborator`.
|
||||||
|
|
@ -57,9 +58,10 @@ fn explain_create_error(org: Option<&str>, err: ForgejoError) -> anyhow::Error {
|
||||||
}
|
}
|
||||||
anyhow::anyhow!(
|
anyhow::anyhow!(
|
||||||
"this account most likely cannot create repositories (the forge answered 500, which is \
|
"this account most likely cannot create repositories (the forge answered 500, which is \
|
||||||
how Forgejo reports a repo-creation limit of 0). On hyperhive, agent accounts are \
|
how Forgejo reports a used-up repo-creation limit, 0 included). On hyperhive, agent \
|
||||||
locked to 0 by design, so an operator merge stays the only path to main. Ask the \
|
accounts are locked to 0 unless the operator raises its limit, so an operator merge stays \
|
||||||
operator to create the repo (for example `agents/<name>`) and add you as a collaborator."
|
the only path to main. Ask the operator to create the repo (for example \
|
||||||
|
`agents/<name>`) and add you as a collaborator."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,14 @@ pub struct Permissions {
|
||||||
/// out keeps the stored value.
|
/// out keeps the stored value.
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub internet_log: Option<bool>,
|
pub internet_log: Option<bool>,
|
||||||
|
/// How many repositories the agent's forge account may create, 0 to 100.
|
||||||
|
/// Absent in a `GET` means the swarm never set it, which counts as 0.
|
||||||
|
/// Absent in a `PUT` keeps the stored value. An agent that may create
|
||||||
|
/// repositories can add a CI workflow to them, and CI workflows run
|
||||||
|
/// unsandboxed on the CI runner.
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
#[cfg_attr(feature = "schema", schema(example = 0, minimum = 0, maximum = 100))]
|
||||||
|
pub max_repos: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -47,6 +55,7 @@ mod tests {
|
||||||
permissions: Some(vec![]),
|
permissions: Some(vec![]),
|
||||||
unrestricted_internet: None,
|
unrestricted_internet: None,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
},
|
},
|
||||||
r#"{"permissions":[]}"#,
|
r#"{"permissions":[]}"#,
|
||||||
),
|
),
|
||||||
|
|
@ -55,6 +64,7 @@ mod tests {
|
||||||
permissions: None,
|
permissions: None,
|
||||||
unrestricted_internet: Some(false),
|
unrestricted_internet: Some(false),
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
},
|
},
|
||||||
r#"{"unrestricted_internet":false}"#,
|
r#"{"unrestricted_internet":false}"#,
|
||||||
),
|
),
|
||||||
|
|
@ -63,6 +73,7 @@ mod tests {
|
||||||
permissions: None,
|
permissions: None,
|
||||||
unrestricted_internet: Some(false),
|
unrestricted_internet: Some(false),
|
||||||
internet_log: Some(true),
|
internet_log: Some(true),
|
||||||
|
max_repos: None,
|
||||||
},
|
},
|
||||||
r#"{"unrestricted_internet":false,"internet_log":true}"#,
|
r#"{"unrestricted_internet":false,"internet_log":true}"#,
|
||||||
),
|
),
|
||||||
|
|
@ -71,9 +82,17 @@ mod tests {
|
||||||
permissions: Some(vec![Permission::Messaging, Permission::ManageRootAgent]),
|
permissions: Some(vec![Permission::Messaging, Permission::ManageRootAgent]),
|
||||||
unrestricted_internet: None,
|
unrestricted_internet: None,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
},
|
},
|
||||||
r#"{"permissions":["messaging","manage_root_agent"]}"#,
|
r#"{"permissions":["messaging","manage_root_agent"]}"#,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
Permissions {
|
||||||
|
max_repos: Some(3),
|
||||||
|
..Permissions::default()
|
||||||
|
},
|
||||||
|
r#"{"max_repos":3}"#,
|
||||||
|
),
|
||||||
] {
|
] {
|
||||||
assert_eq!(serde_json::to_string(&value).unwrap(), wire);
|
assert_eq!(serde_json::to_string(&value).unwrap(), wire);
|
||||||
assert_eq!(serde_json::from_str::<Permissions>(wire).unwrap(), value);
|
assert_eq!(serde_json::from_str::<Permissions>(wire).unwrap(), value);
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,8 @@ use swarm_controller_api::permissions::Permissions;
|
||||||
use swarm_queue_client::agent_permissions;
|
use swarm_queue_client::agent_permissions;
|
||||||
use swarm_secret_client::observability::Grant;
|
use swarm_secret_client::observability::Grant;
|
||||||
use swarm_wire::agent_permissions::{
|
use swarm_wire::agent_permissions::{
|
||||||
AgentPermissions, Import, ImportReply, hive_from_import_subject, import_subject_wildcard,
|
AgentPermissions, DEFAULT_MAX_REPOS, Import, ImportReply, MAX_REPOS, hive_from_import_subject,
|
||||||
|
import_subject_wildcard,
|
||||||
};
|
};
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
|
@ -41,8 +42,8 @@ use crate::wanted::WantedWriter;
|
||||||
const MAX_ATTEMPTS: usize = 5;
|
const MAX_ATTEMPTS: usize = 5;
|
||||||
|
|
||||||
/// The permissions to store for `req`, whose list is already checked. The list
|
/// The permissions to store for `req`, whose list is already checked. The list
|
||||||
/// replaces the stored one; `unrestricted_internet` left out keeps the one in
|
/// replaces the stored one; `unrestricted_internet` and `max_repos` left out
|
||||||
/// `current`. `internet_log` is stored only as `true`, and a request that sets
|
/// keep the ones in `current`. `internet_log` is stored only as `true`, and a request that sets
|
||||||
/// `unrestricted_internet` without it turns it off: an agent that moves to
|
/// `unrestricted_internet` without it turns it off: an agent that moves to
|
||||||
/// unrestricted and back to restricted does not come back logging.
|
/// unrestricted and back to restricted does not come back logging.
|
||||||
fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions {
|
fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions {
|
||||||
|
|
@ -55,6 +56,7 @@ fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions
|
||||||
permissions: req.permissions,
|
permissions: req.permissions,
|
||||||
unrestricted_internet: req.unrestricted_internet.or(current.unrestricted_internet),
|
unrestricted_internet: req.unrestricted_internet.or(current.unrestricted_internet),
|
||||||
internet_log,
|
internet_log,
|
||||||
|
max_repos: req.max_repos.or(current.max_repos),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,12 +80,49 @@ pub(crate) fn unique(perms: &[Permission]) -> Result<(), String> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Woken when an agent's `max_repos` changes, so the forge pass applies it at
|
||||||
|
/// once instead of at its next tick. A wake with no pass waiting is kept for
|
||||||
|
/// the next one.
|
||||||
|
pub(crate) static MAX_REPOS_CHANGED: tokio::sync::Notify = tokio::sync::Notify::const_new();
|
||||||
|
|
||||||
|
/// The repository limit `record` sets: its `max_repos`, or
|
||||||
|
/// [`DEFAULT_MAX_REPOS`] when it has none. A value above [`MAX_REPOS`], as a
|
||||||
|
/// hand edit could leave, reads as the default with a warning: the stricter
|
||||||
|
/// reading.
|
||||||
|
fn max_repos_of(agent: &str, record: &AgentPermissions) -> u32 {
|
||||||
|
match record.max_repos {
|
||||||
|
None => DEFAULT_MAX_REPOS,
|
||||||
|
Some(n) if n <= MAX_REPOS => n,
|
||||||
|
Some(n) => {
|
||||||
|
tracing::warn!(
|
||||||
|
agent,
|
||||||
|
n,
|
||||||
|
"the stored repository limit is out of range; using the default"
|
||||||
|
);
|
||||||
|
DEFAULT_MAX_REPOS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `agent`'s repository limit: what its record sets, or [`DEFAULT_MAX_REPOS`]
|
||||||
|
/// when it has no record. A record that does not decode reads as none. Opens
|
||||||
|
/// the bucket, which the controller creates at start anyway.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// The queue could not be read.
|
||||||
|
pub(crate) async fn max_repos(client: &async_nats::Client, agent: &str) -> anyhow::Result<u32> {
|
||||||
|
let store = agent_permissions::open_or_create(client).await?;
|
||||||
|
let (record, _, _) = read_or_default::<AgentPermissions>(&store, agent).await?;
|
||||||
|
Ok(max_repos_of(agent, &record))
|
||||||
|
}
|
||||||
|
|
||||||
/// The stored permissions as the API shows them.
|
/// The stored permissions as the API shows them.
|
||||||
fn view(v: AgentPermissions) -> Permissions {
|
fn view(v: AgentPermissions) -> Permissions {
|
||||||
Permissions {
|
Permissions {
|
||||||
permissions: v.permissions,
|
permissions: v.permissions,
|
||||||
unrestricted_internet: v.unrestricted_internet,
|
unrestricted_internet: v.unrestricted_internet,
|
||||||
internet_log: v.internet_log,
|
internet_log: v.internet_log,
|
||||||
|
max_repos: v.max_repos,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -249,6 +288,12 @@ async fn put_with(
|
||||||
if let Some(perms) = &req.permissions {
|
if let Some(perms) = &req.permissions {
|
||||||
unique(perms).map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?;
|
unique(perms).map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?;
|
||||||
}
|
}
|
||||||
|
if let Some(n) = req.max_repos.filter(|n| *n > MAX_REPOS) {
|
||||||
|
return Err(refuse(error_problem(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
&format!("max_repos {n} is more than the {MAX_REPOS} an agent may be given"),
|
||||||
|
)));
|
||||||
|
}
|
||||||
let client = queue(&state).map_err(|r| refuse(refused(r)))?;
|
let client = queue(&state).map_err(|r| refuse(refused(r)))?;
|
||||||
if !live(&state).await.map_err(refuse)?.contains(&agent) {
|
if !live(&state).await.map_err(refuse)?.contains(&agent) {
|
||||||
return Err(refuse(error_problem(
|
return Err(refuse(error_problem(
|
||||||
|
|
@ -283,6 +328,9 @@ async fn put_with(
|
||||||
.map_err(|e| queue_problem(&e))?
|
.map_err(|e| queue_problem(&e))?
|
||||||
{
|
{
|
||||||
tracing::info!(%agent, ?value, "set agent permissions");
|
tracing::info!(%agent, ?value, "set agent permissions");
|
||||||
|
if value.max_repos != current.max_repos {
|
||||||
|
MAX_REPOS_CHANGED.notify_one();
|
||||||
|
}
|
||||||
if !revoking {
|
if !revoking {
|
||||||
mirror(leaves, &agent, &before, &after)
|
mirror(leaves, &agent, &before, &after)
|
||||||
.await
|
.await
|
||||||
|
|
@ -333,12 +381,15 @@ async fn delete_with(
|
||||||
let (current, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
|
let (current, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| queue_problem(&e))?;
|
.map_err(|e| queue_problem(&e))?;
|
||||||
// Only the flags outlive a clear: dropping `unrestricted_internet`
|
// Only the flags and the repository limit outlive a clear: dropping
|
||||||
// would turn an agent created with `false` on, and dropping
|
// `unrestricted_internet` would turn an agent created with `false` on,
|
||||||
// `internet_log` would turn a logging agent into an enforced one.
|
// dropping `internet_log` would turn a logging agent into an enforced
|
||||||
|
// one, and a limit is an operator's decision about repositories, not
|
||||||
|
// part of the permission list.
|
||||||
let kept = AgentPermissions {
|
let kept = AgentPermissions {
|
||||||
unrestricted_internet: current.unrestricted_internet,
|
unrestricted_internet: current.unrestricted_internet,
|
||||||
internet_log: current.internet_log,
|
internet_log: current.internet_log,
|
||||||
|
max_repos: current.max_repos,
|
||||||
..AgentPermissions::default()
|
..AgentPermissions::default()
|
||||||
};
|
};
|
||||||
mirror(leaves, &agent, &held(¤t), &held(&kept))
|
mirror(leaves, &agent, &held(¤t), &held(&kept))
|
||||||
|
|
@ -438,6 +489,7 @@ pub(crate) fn at_create(
|
||||||
permissions: perms,
|
permissions: perms,
|
||||||
unrestricted_internet,
|
unrestricted_internet,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -467,6 +519,7 @@ fn merged_at_create(
|
||||||
} else {
|
} else {
|
||||||
current.internet_log
|
current.internet_log
|
||||||
},
|
},
|
||||||
|
max_repos: value.max_repos.or(current.max_repos),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -810,7 +863,7 @@ pub(crate) mod tests {
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use hive_types::Permission;
|
use hive_types::Permission;
|
||||||
use swarm_secret_client::observability::Grant;
|
use swarm_secret_client::observability::Grant;
|
||||||
use swarm_wire::agent_permissions::{Import, ImportReply};
|
use swarm_wire::agent_permissions::{Import, ImportReply, MAX_REPOS};
|
||||||
|
|
||||||
fn stored(json: &str) -> swarm_wire::agent_permissions::AgentPermissions {
|
fn stored(json: &str) -> swarm_wire::agent_permissions::AgentPermissions {
|
||||||
serde_json::from_str(json).expect("decodes")
|
serde_json::from_str(json).expect("decodes")
|
||||||
|
|
@ -821,6 +874,7 @@ pub(crate) mod tests {
|
||||||
permissions: None,
|
permissions: None,
|
||||||
unrestricted_internet,
|
unrestricted_internet,
|
||||||
internet_log,
|
internet_log,
|
||||||
|
max_repos: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -886,6 +940,99 @@ pub(crate) mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An agent's repository limit is its own: a change to one agent's leaves
|
||||||
|
/// every other agent at its limit, a `PUT` that leaves the field out and a
|
||||||
|
/// `DELETE` keep it, and a limit above the range is refused.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_agents_repo_limit_is_its_own_and_survives_other_changes() {
|
||||||
|
use crate::wanted::WantedWriter;
|
||||||
|
use swarm_wire::wanted::AgentState;
|
||||||
|
|
||||||
|
let nats = TestNats::start();
|
||||||
|
let client = nats.client().await;
|
||||||
|
let state = crate::agent_limits::tests::declared(&client).await;
|
||||||
|
WantedWriter::new(client.clone())
|
||||||
|
.set("pr1ma", "iris", AgentState::Up)
|
||||||
|
.await
|
||||||
|
.expect("declare iris");
|
||||||
|
let limit = |n| Permissions {
|
||||||
|
max_repos: Some(n),
|
||||||
|
..Permissions::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 0);
|
||||||
|
// A change wakes the forge pass; drain stale wakes first.
|
||||||
|
while tokio::time::timeout(
|
||||||
|
std::time::Duration::from_millis(1),
|
||||||
|
super::MAX_REPOS_CHANGED.notified(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
{}
|
||||||
|
assert_eq!(
|
||||||
|
put(&state, "atlas", limit(3))
|
||||||
|
.await
|
||||||
|
.expect("set")
|
||||||
|
.0
|
||||||
|
.max_repos,
|
||||||
|
Some(3)
|
||||||
|
);
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
|
||||||
|
tokio::time::timeout(
|
||||||
|
std::time::Duration::from_secs(5),
|
||||||
|
super::MAX_REPOS_CHANGED.notified(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("the change wakes the forge pass");
|
||||||
|
assert_eq!(
|
||||||
|
super::max_repos(&client, "iris").await.unwrap(),
|
||||||
|
0,
|
||||||
|
"another agent's limit is not changed"
|
||||||
|
);
|
||||||
|
let _ = put(&state, "iris", limit(7)).await.expect("set");
|
||||||
|
assert_eq!(super::max_repos(&client, "iris").await.unwrap(), 7);
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
|
||||||
|
|
||||||
|
// A PUT without the field, and a DELETE, keep the limit.
|
||||||
|
let _ = put(&state, "atlas", granted()).await.expect("set");
|
||||||
|
assert_eq!(get(&state, "atlas").await.max_repos, Some(3));
|
||||||
|
delete_agent_permissions(State(state.clone()), Path("atlas".to_owned()))
|
||||||
|
.await
|
||||||
|
.expect("cleared");
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
|
||||||
|
|
||||||
|
let problem = put(&state, "atlas", limit(MAX_REPOS + 1))
|
||||||
|
.await
|
||||||
|
.expect_err("over the range");
|
||||||
|
assert_eq!(problem.status, Some(StatusCode::BAD_REQUEST), "{problem:?}");
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
|
||||||
|
let _ = put(&state, "atlas", limit(MAX_REPOS))
|
||||||
|
.await
|
||||||
|
.expect("control: the top of the range is stored");
|
||||||
|
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), MAX_REPOS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A stored limit above the range, as a hand edit could leave it, reads as
|
||||||
|
/// none rather than as that many repositories.
|
||||||
|
#[test]
|
||||||
|
fn a_stored_limit_above_the_range_reads_as_none() {
|
||||||
|
let read = |json: &str| super::max_repos_of("atlas", &stored(json));
|
||||||
|
assert_eq!(read(r#"{"max_repos":5000}"#), 0);
|
||||||
|
assert_eq!(read("{}"), 0);
|
||||||
|
assert_eq!(read(r#"{"max_repos":7}"#), 7, "control: in range");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A create keeps the stored limit and does not set one.
|
||||||
|
#[test]
|
||||||
|
fn a_create_keeps_the_repo_limit() {
|
||||||
|
let current = stored(r#"{"max_repos":4}"#);
|
||||||
|
assert_eq!(
|
||||||
|
super::merged_at_create(&stored("{}"), ¤t, true).max_repos,
|
||||||
|
Some(4)
|
||||||
|
);
|
||||||
|
assert_eq!(super::at_create(None, None).expect("valid").max_repos, None);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn published(client: &async_nats::Client, agent: &str) -> Option<String> {
|
pub(crate) async fn published(client: &async_nats::Client, agent: &str) -> Option<String> {
|
||||||
let store = swarm_queue_client::agent_permissions::open_or_create(client)
|
let store = swarm_queue_client::agent_permissions::open_or_create(client)
|
||||||
.await
|
.await
|
||||||
|
|
@ -902,6 +1049,7 @@ pub(crate) mod tests {
|
||||||
permissions: Some(vec![Permission::ManageRootAgent]),
|
permissions: Some(vec![Permission::ManageRootAgent]),
|
||||||
unrestricted_internet: None,
|
unrestricted_internet: None,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -947,6 +1095,7 @@ pub(crate) mod tests {
|
||||||
permissions: Some(Vec::new()),
|
permissions: Some(Vec::new()),
|
||||||
unrestricted_internet: None,
|
unrestricted_internet: None,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
};
|
};
|
||||||
let _ = put(&state, "atlas", none.clone()).await.expect("set");
|
let _ = put(&state, "atlas", none.clone()).await.expect("set");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
@ -975,6 +1124,7 @@ pub(crate) mod tests {
|
||||||
permissions: None,
|
permissions: None,
|
||||||
unrestricted_internet: Some(false),
|
unrestricted_internet: Some(false),
|
||||||
internet_log: Some(true),
|
internet_log: Some(true),
|
||||||
|
max_repos: None,
|
||||||
};
|
};
|
||||||
let stored = put(&state, "atlas", log.clone()).await.expect("set").0;
|
let stored = put(&state, "atlas", log.clone()).await.expect("set").0;
|
||||||
assert_eq!(stored, log);
|
assert_eq!(stored, log);
|
||||||
|
|
@ -997,6 +1147,7 @@ pub(crate) mod tests {
|
||||||
permissions: None,
|
permissions: None,
|
||||||
unrestricted_internet: Some(true),
|
unrestricted_internet: Some(true),
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
};
|
};
|
||||||
let _ = put(&state, "atlas", on.clone()).await.expect("set");
|
let _ = put(&state, "atlas", on.clone()).await.expect("set");
|
||||||
let off = Permissions {
|
let off = Permissions {
|
||||||
|
|
@ -1144,6 +1295,7 @@ pub(crate) mod tests {
|
||||||
permissions: Some(perms.to_vec()),
|
permissions: Some(perms.to_vec()),
|
||||||
unrestricted_internet: None,
|
unrestricted_internet: None,
|
||||||
internet_log: None,
|
internet_log: None,
|
||||||
|
max_repos: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ use crate::webhook::DeliveryKind;
|
||||||
use swarm_secret_client::stored_secret::StoredSecret;
|
use swarm_secret_client::stored_secret::StoredSecret;
|
||||||
use swarm_wire::review_classifier::Commit;
|
use swarm_wire::review_classifier::Commit;
|
||||||
|
|
||||||
|
pub mod agent_max_repos;
|
||||||
pub mod agent_token;
|
pub mod agent_token;
|
||||||
mod api;
|
mod api;
|
||||||
mod base_pulls;
|
mod base_pulls;
|
||||||
|
|
@ -749,27 +750,29 @@ impl Client {
|
||||||
/// unconditionally, 422 only after a follow-up `user_get` confirms the
|
/// unconditionally, 422 only after a follow-up `user_get` confirms the
|
||||||
/// user (see [`folds_into_success`] — not every 422 means it exists).
|
/// user (see [`folds_into_success`] — not every 422 means it exists).
|
||||||
///
|
///
|
||||||
/// Then locks the account out of creating repos
|
/// Then limits the account ([`Self::limit_agent_account`]) to `max_repos`
|
||||||
/// ([`Self::disable_repo_creation`]), whether it was just created or
|
/// repositories and no organizations, whether it was just created or
|
||||||
/// already existed. Nothing else does that for a swarm-created agent, so
|
/// already existed. Nothing else does that for a swarm-created agent, so
|
||||||
/// a failure fails the node. Leaves an existing account's email as it
|
/// a failure fails the node. Leaves an existing account's email as it
|
||||||
/// finds it.
|
/// finds it.
|
||||||
pub async fn ensure_agent_user(&self, agent: &str) -> Result<()> {
|
pub async fn ensure_agent_user(&self, agent: &str, max_repos: u32) -> Result<()> {
|
||||||
self.create_agent_user(agent).await?;
|
self.create_agent_user(agent).await?;
|
||||||
self.disable_repo_creation(agent).await
|
self.limit_agent_account(agent, max_repos).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set `max_repo_creation = 0` on `agent`'s account. Agents create repos
|
/// Set `max_repo_creation = max_repos` and `allow_create_organization =
|
||||||
/// through the hive, never with their own token — a write-scoped token
|
/// false` on `agent`'s account. At 0 an agent creates repos only through
|
||||||
/// can otherwise create and own a repo and self-merge in it, bypassing
|
/// the hive, never with its own token; above 0 a write-scoped token can
|
||||||
/// the operator-only-merge policy. Existing repos are untouched; push,
|
/// create and own that many repos and self-merge in them, bypassing the
|
||||||
/// PRs and clone keep working.
|
/// operator-only-merge policy. Existing repos are untouched; push, PRs and
|
||||||
|
/// clone keep working. An account that may not create organizations cannot
|
||||||
|
/// get around the limit by making an organization to own repos in.
|
||||||
///
|
///
|
||||||
/// The PATCH itself is unconditional rather than verify-then-patch:
|
/// The PATCH itself is unconditional rather than verify-then-patch:
|
||||||
/// Forgejo's API never returns `max_repo_creation` (it is a field of
|
/// Forgejo's API never returns `max_repo_creation` (it is a field of
|
||||||
/// `EditUserOption` only, not of `User`), so there is no current value
|
/// `EditUserOption` only, not of `User`), so there is no current value
|
||||||
/// to compare against, and setting 0 on an account already at 0 is a
|
/// to compare against, and setting the same value again is a no-op. But
|
||||||
/// no-op. But `create_agent_user`'s idempotent fold only proves *some*
|
/// `create_agent_user`'s idempotent fold only proves *some*
|
||||||
/// account named `agent` exists — a 409 folds with no read at all, and
|
/// account named `agent` exists — a 409 folds with no read at all, and
|
||||||
/// a 422's follow-up `user_get` only confirms the username, not that
|
/// a 422's follow-up `user_get` only confirms the username, not that
|
||||||
/// swarm-controller created it — so before that PATCH this reads the
|
/// swarm-controller created it — so before that PATCH this reads the
|
||||||
|
|
@ -781,31 +784,35 @@ impl Client {
|
||||||
/// name collided with an account this node didn't create, so it fails
|
/// name collided with an account this node didn't create, so it fails
|
||||||
/// the node rather than sending the PATCH (or silently skipping) to an
|
/// the node rather than sending the PATCH (or silently skipping) to an
|
||||||
/// account nobody verified is agent-controlled.
|
/// account nobody verified is agent-controlled.
|
||||||
async fn disable_repo_creation(&self, agent: &str) -> Result<()> {
|
pub(crate) async fn limit_agent_account(&self, agent: &str, max_repos: u32) -> Result<()> {
|
||||||
let user = self
|
let user = self
|
||||||
.api
|
.api
|
||||||
.call(|api| api.user_get(agent).into_future())
|
.call(|api| api.user_get(agent).into_future())
|
||||||
.await
|
.await
|
||||||
.with_context(|| format!("read forge user {agent} before locking it down"))?;
|
.with_context(|| format!("read forge user {agent} before limiting it"))?;
|
||||||
let expected = agent_email(agent);
|
let expected = agent_email(agent);
|
||||||
let legacy = legacy_agent_email(agent);
|
let legacy = legacy_agent_email(agent);
|
||||||
if !matches!(user.email.as_deref(), Some(e) if e == expected || e == legacy) {
|
if !matches!(user.email.as_deref(), Some(e) if e == expected || e == legacy) {
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"forge account `{agent}` exists but is not this agent's (email {:?}, expected \
|
"forge account `{agent}` exists but is not this agent's (email {:?}, expected \
|
||||||
`{expected}` or the legacy `{legacy}`); refusing to lock it down",
|
`{expected}` or the legacy `{legacy}`); refusing to limit it",
|
||||||
user.email
|
user.email
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.api
|
self.api
|
||||||
.call(|api| {
|
.call(|api| {
|
||||||
api.admin_edit_user(agent, repo_creation_lockdown(agent))
|
api.admin_edit_user(agent, agent_account_lockdown(agent, max_repos))
|
||||||
.into_future()
|
.into_future()
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!("disable repo creation for forge user {agent} (max_repo_creation = 0)")
|
format!("limit forge user {agent} (max_repo_creation = {max_repos})")
|
||||||
})?;
|
})?;
|
||||||
tracing::info!(%agent, "swarm forge: disabled repo creation (max_repo_creation = 0)");
|
tracing::info!(
|
||||||
|
%agent,
|
||||||
|
max_repos,
|
||||||
|
"swarm forge: limited the account (max_repo_creation, no organizations)"
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2167,7 +2174,7 @@ fn initial_flake_nix() -> &'static str {
|
||||||
|
|
||||||
/// Canonical email for an agent's Forgejo account, set by
|
/// Canonical email for an agent's Forgejo account, set by
|
||||||
/// [`Client::create_agent_user`] at creation and checked by
|
/// [`Client::create_agent_user`] at creation and checked by
|
||||||
/// [`Client::disable_repo_creation`] before it sends the account's PATCH. Same
|
/// [`Client::limit_agent_account`] before it sends the account's PATCH. Same
|
||||||
/// format string as `hive-c0re::forge::users::agent_email` — not called
|
/// format string as `hive-c0re::forge::users::agent_email` — not called
|
||||||
/// across the crate boundary (see this module's doc comment), but both
|
/// across the crate boundary (see this module's doc comment), but both
|
||||||
/// creators use this exact value, so it doubles as the
|
/// creators use this exact value, so it doubles as the
|
||||||
|
|
@ -2179,7 +2186,7 @@ fn agent_email(agent: &str) -> String {
|
||||||
|
|
||||||
/// The email older hive-created agent accounts carry, and keep: nothing
|
/// The email older hive-created agent accounts carry, and keep: nothing
|
||||||
/// rewrites it to [`agent_email`]. Accepted by
|
/// rewrites it to [`agent_email`]. Accepted by
|
||||||
/// [`Client::disable_repo_creation`]'s guard alongside [`agent_email`] so
|
/// [`Client::limit_agent_account`]'s guard alongside [`agent_email`] so
|
||||||
/// such an account is still recognised as the agent's own.
|
/// such an account is still recognised as the agent's own.
|
||||||
fn legacy_agent_email(agent: &str) -> String {
|
fn legacy_agent_email(agent: &str) -> String {
|
||||||
format!("{agent}@hive.local")
|
format!("{agent}@hive.local")
|
||||||
|
|
@ -2215,6 +2222,17 @@ fn repo_creation_lockdown(agent: &str) -> EditUserOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [`repo_creation_lockdown`] for an agent's account: `max_repos` repositories
|
||||||
|
/// instead of none, and no organizations. The service accounts that share the
|
||||||
|
/// lockdown stay at none and are not touched on organizations.
|
||||||
|
fn agent_account_lockdown(agent: &str, max_repos: u32) -> EditUserOption {
|
||||||
|
EditUserOption {
|
||||||
|
allow_create_organization: Some(false),
|
||||||
|
max_repo_creation: Some(i64::from(max_repos)),
|
||||||
|
..repo_creation_lockdown(agent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether a create-style call failed because the object already
|
/// Whether a create-style call failed because the object already
|
||||||
/// exists. Forgejo signals this as HTTP 409 (conflict) or 422
|
/// exists. Forgejo signals this as HTTP 409 (conflict) or 422
|
||||||
/// (validation) — match both defensively, same as
|
/// (validation) — match both defensively, same as
|
||||||
|
|
@ -2842,10 +2860,11 @@ mod tests {
|
||||||
async fn a_created_agent_user_gets_repo_creation_disabled() {
|
async fn a_created_agent_user_gets_repo_creation_disabled() {
|
||||||
let (client, seen) = stub_forge((201, "{}"), own_account_get("alice"), (200, "{}")).await;
|
let (client, seen) = stub_forge((201, "{}"), own_account_get("alice"), (200, "{}")).await;
|
||||||
|
|
||||||
client.ensure_agent_user("alice").await.unwrap();
|
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||||
|
|
||||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
|
assert_eq!(body["allow_create_organization"], false);
|
||||||
let set: Vec<&str> = body
|
let set: Vec<&str> = body
|
||||||
.as_object()
|
.as_object()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
@ -2853,11 +2872,35 @@ mod tests {
|
||||||
.filter(|(_, v)| !v.is_null())
|
.filter(|(_, v)| !v.is_null())
|
||||||
.map(|(k, _)| k.as_str())
|
.map(|(k, _)| k.as_str())
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(set, ["login_name", "max_repo_creation", "source_id"]);
|
assert_eq!(
|
||||||
|
set,
|
||||||
|
[
|
||||||
|
"allow_create_organization",
|
||||||
|
"login_name",
|
||||||
|
"max_repo_creation",
|
||||||
|
"source_id"
|
||||||
|
]
|
||||||
|
);
|
||||||
assert_eq!(body["login_name"], "alice");
|
assert_eq!(body["login_name"], "alice");
|
||||||
assert_eq!(body["source_id"], 0);
|
assert_eq!(body["source_id"], 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The agent's limit is what goes out as `max_repo_creation`, on a created
|
||||||
|
/// account and an existing one alike, and organizations stay refused at any
|
||||||
|
/// limit.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_agents_limit_is_what_the_account_is_set_to() {
|
||||||
|
for create in [(201, "{}"), (409, r#"{"message":"user already exists"}"#)] {
|
||||||
|
let (client, seen) = stub_forge(create, own_account_get("alice"), (200, "{}")).await;
|
||||||
|
|
||||||
|
client.ensure_agent_user("alice", 3).await.unwrap();
|
||||||
|
|
||||||
|
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||||
|
assert_eq!(body["max_repo_creation"], 3, "{create:?}");
|
||||||
|
assert_eq!(body["allow_create_organization"], false, "{create:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An account that already existed is locked down too: the node is
|
/// An account that already existed is locked down too: the node is
|
||||||
/// re-run, and an account created before this lockdown existed must
|
/// re-run, and an account created before this lockdown existed must
|
||||||
/// not keep repo creation just because the create step folded to
|
/// not keep repo creation just because the create step folded to
|
||||||
|
|
@ -2872,7 +2915,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
client.ensure_agent_user("alice").await.unwrap();
|
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||||
|
|
||||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
|
|
@ -2891,7 +2934,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let err = client.ensure_agent_user("alice").await.unwrap_err();
|
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
|
||||||
|
|
||||||
let msg = format!("{err:#}");
|
let msg = format!("{err:#}");
|
||||||
assert!(msg.contains("max_repo_creation"), "{msg}");
|
assert!(msg.contains("max_repo_creation"), "{msg}");
|
||||||
|
|
@ -2915,7 +2958,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let err = client.ensure_agent_user("alice").await.unwrap_err();
|
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
|
||||||
|
|
||||||
let msg = format!("{err:#}");
|
let msg = format!("{err:#}");
|
||||||
assert!(msg.contains("alice"), "{msg}");
|
assert!(msg.contains("alice"), "{msg}");
|
||||||
|
|
@ -2945,7 +2988,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
client.ensure_agent_user("alice").await.unwrap();
|
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||||
|
|
||||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
|
|
@ -2963,7 +3006,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
client.ensure_agent_user("alice").await.unwrap();
|
client.ensure_agent_user("alice", 0).await.unwrap();
|
||||||
|
|
||||||
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
|
|
|
||||||
223
swarm-controller/src/forge/agent_max_repos.rs
Normal file
223
swarm-controller/src/forge/agent_max_repos.rs
Normal file
|
|
@ -0,0 +1,223 @@
|
||||||
|
//! Each existing agent's forge account at its own repository limit.
|
||||||
|
//!
|
||||||
|
//! The limit is the `max_repos` of the agent's record in `agent-permissions`
|
||||||
|
//! (`swarm_wire::agent_permissions`). A new account gets it from its
|
||||||
|
//! `CreateForgeUser` node. This pass reaches the others: at start, when a
|
||||||
|
//! change of an agent's `max_repos` wakes it
|
||||||
|
//! ([`crate::agent_permissions::MAX_REPOS_CHANGED`]), and every
|
||||||
|
//! [`RECONCILE_INTERVAL`] over every agent that holds a store identity.
|
||||||
|
//!
|
||||||
|
//! Forgejo never returns an account's `max_repo_creation`, so nothing can be
|
||||||
|
//! compared against the forge. The pass remembers what it applied to each agent
|
||||||
|
//! in this process and applies again only what differs, so a restart applies
|
||||||
|
//! every agent's limit once, and an agent whose edit failed is retried at the
|
||||||
|
//! next tick. The decision is pure ([`plan`]), so the tests pin it.
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::Client;
|
||||||
|
|
||||||
|
/// How often [`spawn`] re-checks every agent.
|
||||||
|
const RECONCILE_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);
|
||||||
|
|
||||||
|
/// The agents of `desired` whose account is not known to be at their limit,
|
||||||
|
/// with that limit.
|
||||||
|
pub fn plan(
|
||||||
|
desired: &BTreeMap<String, u32>,
|
||||||
|
applied: &BTreeMap<String, u32>,
|
||||||
|
) -> Vec<(String, u32)> {
|
||||||
|
desired
|
||||||
|
.iter()
|
||||||
|
.filter(|(agent, limit)| applied.get(*agent) != Some(limit))
|
||||||
|
.map(|(agent, limit)| (agent.clone(), *limit))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One pass: set each limit of `todo` on its agent's account, recording the
|
||||||
|
/// ones the forge took in `applied`. An agent the forge refuses is logged and
|
||||||
|
/// left out, so the next pass plans it again.
|
||||||
|
async fn apply(client: &Client, todo: &[(String, u32)], applied: &mut BTreeMap<String, u32>) {
|
||||||
|
for (agent, limit) in todo {
|
||||||
|
match client.limit_agent_account(agent, *limit).await {
|
||||||
|
Ok(()) => {
|
||||||
|
applied.insert(agent.clone(), *limit);
|
||||||
|
}
|
||||||
|
Err(e) => tracing::warn!(
|
||||||
|
%agent,
|
||||||
|
limit,
|
||||||
|
error = %format!("{e:#}"),
|
||||||
|
"agent max repos: the forge account was not updated; retrying next pass"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every roster agent's limit. An agent whose record can't be read is left
|
||||||
|
/// out, so nothing is applied for it this pass.
|
||||||
|
async fn desired(queue: &async_nats::Client, roster: &[String]) -> BTreeMap<String, u32> {
|
||||||
|
let mut limits = BTreeMap::new();
|
||||||
|
for agent in roster {
|
||||||
|
match crate::agent_permissions::max_repos(queue, agent).await {
|
||||||
|
Ok(limit) => {
|
||||||
|
limits.insert(agent.clone(), limit);
|
||||||
|
}
|
||||||
|
Err(e) => tracing::warn!(
|
||||||
|
%agent,
|
||||||
|
error = %format!("{e:#}"),
|
||||||
|
"agent max repos: the agent's limit was not read; skipping it this pass"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
limits
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Apply every agent's limit now and again on each wake and tick. A pass that
|
||||||
|
/// fails is logged and retried on the next tick; it never stops the daemon.
|
||||||
|
pub fn spawn(client: Arc<Client>, queue: async_nats::Client) {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut ticker = tokio::time::interval(RECONCILE_INTERVAL);
|
||||||
|
let mut applied = BTreeMap::new();
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = ticker.tick() => {}
|
||||||
|
() = crate::agent_permissions::MAX_REPOS_CHANGED.notified() => {}
|
||||||
|
}
|
||||||
|
let roster = match super::docs_grant::read_roster().await {
|
||||||
|
Ok(roster) => roster,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(error = %format!("{e:#}"), "agent max repos: no roster; retrying next tick");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
applied.retain(|agent, _| roster.contains(agent));
|
||||||
|
let todo = plan(&desired(&queue, &roster).await, &applied);
|
||||||
|
if todo.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
tracing::info!(agents = todo.len(), "agent max repos: applying the limits");
|
||||||
|
super::within_forge_deadline("agent max repos", apply(&client, &todo, &mut applied))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn limits(of: &[(&str, u32)]) -> BTreeMap<String, u32> {
|
||||||
|
of.iter().map(|(a, n)| ((*a).to_owned(), *n)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An agent at another value, or not yet applied, is planned with its own
|
||||||
|
/// limit; one already at its limit is not.
|
||||||
|
#[test]
|
||||||
|
fn plan_names_the_agents_not_at_their_limit() {
|
||||||
|
let desired = limits(&[("atlas", 2), ("iris", 0), ("sock", 5)]);
|
||||||
|
let applied = limits(&[("atlas", 2), ("iris", 1)]);
|
||||||
|
assert_eq!(
|
||||||
|
plan(&desired, &applied),
|
||||||
|
[("iris".to_owned(), 0), ("sock".to_owned(), 5)]
|
||||||
|
);
|
||||||
|
// Control: everything applied at its limit leaves nothing to do.
|
||||||
|
assert!(plan(&desired, &desired).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A stand-in forge on a loopback port that answers each agent's own
|
||||||
|
/// email and records every admin edit as (login, body).
|
||||||
|
async fn stub_agents() -> (
|
||||||
|
Client,
|
||||||
|
Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>>,
|
||||||
|
) {
|
||||||
|
use axum::http::{Method, StatusCode, Uri, header::CONTENT_TYPE};
|
||||||
|
|
||||||
|
let edits: Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>> = Arc::default();
|
||||||
|
let recorded = Arc::clone(&edits);
|
||||||
|
let app = axum::Router::new().fallback(
|
||||||
|
move |method: Method, uri: Uri, body: axum::body::Bytes| {
|
||||||
|
let recorded = Arc::clone(&recorded);
|
||||||
|
async move {
|
||||||
|
let login = uri.path().rsplit('/').next().unwrap_or_default().to_owned();
|
||||||
|
let body = match method {
|
||||||
|
Method::GET => format!(r#"{{"email":"{login}@hyperhive.local"}}"#),
|
||||||
|
Method::PATCH => {
|
||||||
|
let json = serde_json::from_slice(&body).unwrap_or_default();
|
||||||
|
recorded.lock().unwrap().push((login, json));
|
||||||
|
"{}".to_owned()
|
||||||
|
}
|
||||||
|
_ => "{}".to_owned(),
|
||||||
|
};
|
||||||
|
(StatusCode::OK, [(CONTENT_TYPE, "application/json")], body)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
|
||||||
|
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||||
|
let api = crate::forge::api::tests::fixed(url.clone(), "stub-token");
|
||||||
|
(Client { api, url }, edits)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Each agent gets its own limit, none is skipped and none takes another's;
|
||||||
|
/// each is recorded as applied, and a second plan has nothing left.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_pass_sets_each_agent_its_own_limit() {
|
||||||
|
let (client, edits) = stub_agents().await;
|
||||||
|
let desired = limits(&[("atlas", 4), ("iris", 0), ("sock", 9)]);
|
||||||
|
let mut applied = BTreeMap::new();
|
||||||
|
|
||||||
|
apply(&client, &plan(&desired, &applied), &mut applied).await;
|
||||||
|
|
||||||
|
let mut seen: Vec<(String, i64, bool)> = edits
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.map(|(login, body)| {
|
||||||
|
(
|
||||||
|
login.clone(),
|
||||||
|
body["max_repo_creation"].as_i64().unwrap(),
|
||||||
|
body["allow_create_organization"] == false,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
seen.sort();
|
||||||
|
assert_eq!(
|
||||||
|
seen,
|
||||||
|
[
|
||||||
|
("atlas".to_owned(), 4, true),
|
||||||
|
("iris".to_owned(), 0, true),
|
||||||
|
("sock".to_owned(), 9, true)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
assert_eq!(applied, desired);
|
||||||
|
assert!(plan(&desired, &applied).is_empty());
|
||||||
|
// One agent's changed limit plans that agent alone.
|
||||||
|
let changed = limits(&[("atlas", 4), ("iris", 1), ("sock", 9)]);
|
||||||
|
assert_eq!(plan(&changed, &applied), [("iris".to_owned(), 1)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An agent whose edit the forge refuses is not recorded, so the next pass
|
||||||
|
/// plans it again.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_refused_agent_is_planned_again() {
|
||||||
|
let (client, edits) = stub_agents().await;
|
||||||
|
let mut applied = BTreeMap::new();
|
||||||
|
apply(&client, &[("atlas".to_owned(), 2)], &mut applied).await;
|
||||||
|
// Control: an account that is the agent's own is recorded.
|
||||||
|
assert_eq!(applied.get("atlas"), Some(&2));
|
||||||
|
assert_eq!(edits.lock().unwrap().len(), 1);
|
||||||
|
|
||||||
|
let (broken, _seen) = super::super::tests::stub_forge(
|
||||||
|
(200, "{}"),
|
||||||
|
(200, r#"{"email":"someone@example.com"}"#),
|
||||||
|
(200, "{}"),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let mut applied = BTreeMap::new();
|
||||||
|
apply(&broken, &[("atlas".to_owned(), 2)], &mut applied).await;
|
||||||
|
assert!(applied.is_empty(), "a refused agent must not be recorded");
|
||||||
|
let desired = limits(&[("atlas", 2)]);
|
||||||
|
assert_eq!(plan(&desired, &applied), [("atlas".to_owned(), 2)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -134,7 +134,7 @@ impl Client {
|
||||||
|
|
||||||
/// The store's `hive-agent-*` cert-auth roles: exactly the agents that hold a
|
/// The store's `hive-agent-*` cert-auth roles: exactly the agents that hold a
|
||||||
/// store identity.
|
/// store identity.
|
||||||
async fn read_roster() -> Result<Vec<String>> {
|
pub(super) async fn read_roster() -> Result<Vec<String>> {
|
||||||
let store = crate::store::connect()
|
let store = crate::store::connect()
|
||||||
.await
|
.await
|
||||||
.context("logging in to the swarm secret store")?;
|
.context("logging in to the swarm secret store")?;
|
||||||
|
|
|
||||||
|
|
@ -591,7 +591,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn an_agent_called_hive_x_is_not_hive_xs_account() {
|
fn an_agent_called_hive_x_is_not_hive_xs_account() {
|
||||||
// Both ways round: the hive job refuses the agent's account, and the
|
// Both ways round: the hive job refuses the agent's account, and the
|
||||||
// agent's own guard (`disable_repo_creation`) refuses the hive's,
|
// agent's own guard (`limit_agent_account`) refuses the hive's,
|
||||||
// because neither accepts the other's email.
|
// because neither accepts the other's email.
|
||||||
let login = forge::hive_login("alpha");
|
let login = forge::hive_login("alpha");
|
||||||
for agents in [
|
for agents in [
|
||||||
|
|
@ -638,7 +638,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
client
|
client
|
||||||
.ensure_agent_user("hive-alpha")
|
.ensure_agent_user("hive-alpha", 0)
|
||||||
.await
|
.await
|
||||||
.expect_err("the hive's account is not the agent's");
|
.expect_err("the hive's account is not the agent's");
|
||||||
assert!(lockdown_patch(&seen, "hive-alpha").is_none());
|
assert!(lockdown_patch(&seen, "hive-alpha").is_none());
|
||||||
|
|
|
||||||
|
|
@ -38,9 +38,10 @@ use super::{
|
||||||
folds_into_success, is_ambiguous_validation_failure, is_confirmed_conflict,
|
folds_into_success, is_ambiguous_validation_failure, is_confirmed_conflict,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Forgejo org that owns agent-created repos. Agents can't create repos with
|
/// Forgejo org that owns agent-created repos. An agent can create repos with
|
||||||
/// their own token (`max_repo_creation = 0`); a repo an agent asks for is
|
/// its own token only up to its own `max_repos` (default 0, so none); a repo
|
||||||
/// created here and the agent added as a **write** member (not owner/admin).
|
/// an agent asks for is created here and the agent added as a **write**
|
||||||
|
/// member (not owner/admin).
|
||||||
/// Because the org, not the agent, owns the repo, perms stay centrally
|
/// Because the org, not the agent, owns the repo, perms stay centrally
|
||||||
/// managed and branch protection (referencing [`OPERATORS_TEAM`]) can block
|
/// managed and branch protection (referencing [`OPERATORS_TEAM`]) can block
|
||||||
/// the author from merging their own PR.
|
/// the author from merging their own PR.
|
||||||
|
|
|
||||||
|
|
@ -393,7 +393,13 @@ async fn run_node_work(
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
),
|
),
|
||||||
Some(client) => {
|
Some(client) => {
|
||||||
match forge_node("create_forge_user", client.ensure_agent_user(&agent)).await {
|
let max_repos = agent_max_repos_limit(deps.queue.as_ref(), &agent).await;
|
||||||
|
match forge_node(
|
||||||
|
"create_forge_user",
|
||||||
|
client.ensure_agent_user(&agent, max_repos),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(()) => Outcome::Done,
|
Ok(()) => Outcome::Done,
|
||||||
Err(e) => Outcome::Failed(format!("{e:#}")),
|
Err(e) => Outcome::Failed(format!("{e:#}")),
|
||||||
}
|
}
|
||||||
|
|
@ -554,6 +560,26 @@ async fn mint_forge_token(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The repository limit a new agent account is created at: the `max_repos` of
|
||||||
|
/// the agent's permissions record, or none (0) when there is no queue or it
|
||||||
|
/// cannot be read. The forge pass in `forge::agent_max_repos` raises the
|
||||||
|
/// account to its limit once the queue answers.
|
||||||
|
async fn agent_max_repos_limit(queue: Option<&async_nats::Client>, agent: &str) -> u32 {
|
||||||
|
let Some(queue) = queue else {
|
||||||
|
return swarm_wire::agent_permissions::DEFAULT_MAX_REPOS;
|
||||||
|
};
|
||||||
|
crate::agent_permissions::max_repos(queue, agent)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
tracing::warn!(
|
||||||
|
agent,
|
||||||
|
error = %format!("{e:#}"),
|
||||||
|
"swarm jobq: the agent's repository limit is unreadable; creating the account at none"
|
||||||
|
);
|
||||||
|
swarm_wire::agent_permissions::DEFAULT_MAX_REPOS
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// The `GrantDocsRead` arm, lifted out for the same reason.
|
/// The `GrantDocsRead` arm, lifted out for the same reason.
|
||||||
pub(crate) async fn grant_docs_read(
|
pub(crate) async fn grant_docs_read(
|
||||||
forge: Option<Arc<forge::Client>>,
|
forge: Option<Arc<forge::Client>>,
|
||||||
|
|
@ -2031,6 +2057,29 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A new agent's account is created at the limit of its own record, not
|
||||||
|
/// another agent's, and at none when it has no record or there is no queue.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_new_account_is_created_at_its_agents_limit() {
|
||||||
|
let nats = crate::test_nats::TestNats::start();
|
||||||
|
let client = nats.client().await;
|
||||||
|
assert_eq!(agent_max_repos_limit(Some(&client), "iris").await, 0);
|
||||||
|
let store = swarm_queue_client::agent_permissions::open_or_create(&client)
|
||||||
|
.await
|
||||||
|
.expect("bucket");
|
||||||
|
store
|
||||||
|
.put("iris", br#"{"max_repos":4}"#.to_vec().into())
|
||||||
|
.await
|
||||||
|
.expect("written");
|
||||||
|
store
|
||||||
|
.put("atlas", br#"{"max_repos":9}"#.to_vec().into())
|
||||||
|
.await
|
||||||
|
.expect("written");
|
||||||
|
assert_eq!(agent_max_repos_limit(Some(&client), "iris").await, 4);
|
||||||
|
assert_eq!(agent_max_repos_limit(Some(&client), "sock").await, 0);
|
||||||
|
assert_eq!(agent_max_repos_limit(None, "iris").await, 0);
|
||||||
|
}
|
||||||
|
|
||||||
fn build_state(
|
fn build_state(
|
||||||
sched: &hive_jobq::scheduler::Scheduler<SwarmNodeKind, super::SwarmResourceKind>,
|
sched: &hive_jobq::scheduler::Scheduler<SwarmNodeKind, super::SwarmResourceKind>,
|
||||||
rev: &str,
|
rev: &str,
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue