Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/key-admin.ts?version=5&format=json
147 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 5. The newest is 318, and 5 versions since then changed this file. Go to the newestCompare with 318

1// abso key new, list and revoke (abso issue 116). The admin token is read at
2// run time and sent only as a header. It is never an argument and never printed.
3
4import { readFileSync } from "node:fs";
5import { homedir } from "node:os";
6import { join } from "node:path";
7import { UsageError } from "./args.ts";
8import type { Config } from "./config.ts";
9
10function originOf(server: string): string {
11 try {
12 return new URL(server).origin;
13 } catch {
14 return server;
15 }
18/** The admin token goes only to a server named in AVCS_ADMIN_SERVERS (comma-separated): none is built in. */
19export function adminGoesTo(server: string, extra = ""): boolean {
20 const allowed = [
21 ...extra
22 .split(",")
23 .map((s) => s.trim())
24 .filter((s) => s !== ""),
25 ];
26 const origin = originOf(server);
27 return allowed.some((s) => originOf(s) === origin);
30export function adminToken(env: Readonly<Record<string, string | undefined>> = process.env): string {
31 const fromEnv = env.AVCS_ADMIN_TOKEN?.trim();
32 if (fromEnv) return fromEnv;
33 try {
34 const fromFile = readFileSync(join(homedir(), ".avcs", "admin-token"), "utf8").trim();
35 if (fromFile) return fromFile;
36 } catch {}
37 throw new UsageError("abso key needs the admin token in AVCS_ADMIN_TOKEN or ~/.avcs/admin-token");
40function scrub(text: string, secrets: readonly string[]): string {
41 let out = text.replace(/abso_sk_[A-Za-z0-9_-]+/g, "[redacted]");
42 for (const secret of secrets) if (secret.length > 0) out = out.split(secret).join("[redacted]");
43 return out;
46async function call(config: Config, admin: string, method: "GET" | "POST", path: string): Promise<unknown> {
47 if (!adminGoesTo(config.server, process.env.AVCS_ADMIN_SERVERS))
48 throw new UsageError(
49 `abso key will not send the admin token to ${config.server}; AVCS_ADMIN_SERVERS names the servers it may go to`,
50 );
51 const secrets = [admin, config.token ?? ""].filter((s) => s.length > 0);
52 let response: Response;
53 try {
54 response = await fetch(`${config.server.replace(/\/+$/, "")}${path}`, {
55 method,
56 headers: {
57 ...(config.token ? { authorization: `Bearer ${config.token}` } : {}),
58 "x-avcs-admin": admin,
59 },
60 signal: AbortSignal.timeout(30_000),
61 });
62 } catch (err) {
63 throw new UsageError(scrub(err instanceof Error ? err.message : String(err), secrets));
64 }
65 const text = await response.text();
66 if (!response.ok)
67 throw new UsageError(scrub(text.slice(0, 500) || `the server answered ${response.status}`, secrets));
68 try {
69 return JSON.parse(text) as unknown;
70 } catch {
71 throw new UsageError("the server's answer was not JSON");
72 }
75export interface IssuedKey {
76 id: string;
77 key: string;
78 agent: string;
81export async function keyNew(
82 config: Config,
83 admin: string,
84 agent: string,
85 project = config.project,
86 expires?: string,
87 role?: "owner" | "writer" | "reader",
88): Promise<IssuedKey> {
89 const query = `project=${encodeURIComponent(project)}&agent=${encodeURIComponent(agent)}${expires ? `&expires=${encodeURIComponent(expires)}` : ""}${role ? `&role=${role}` : ""}`;
90 const body = (await call(config, admin, "POST", `/admin/agent-keys?${query}`)) as Partial<IssuedKey>;
91 if (typeof body.id !== "string" || typeof body.key !== "string")
92 throw new UsageError("the server did not return a key");
93 return { id: body.id, key: body.key, agent: typeof body.agent === "string" ? body.agent : agent };
96/** What `abso key new` may show. The key is included only with --print. */
97export function issuedPublic(issued: IssuedKey, print: boolean): { id: string; agent: string; key?: string } {
98 return print ? { id: issued.id, agent: issued.agent, key: issued.key } : { id: issued.id, agent: issued.agent };
101export interface ListedKey {
102 id: string;
103 agent: string;
104 issuedBy: string;
105 at: number;
106 lastUsed: number | null;
107 revokedAt: number | null;
108 expiresAt?: number | null;
111export async function keyList(
112 config: Config,
113 admin: string,
114 agent?: string,
115 project = config.project,
116): Promise<ListedKey[]> {
117 const query = `project=${encodeURIComponent(project)}${agent ? `&agent=${encodeURIComponent(agent)}` : ""}`;
118 const body = (await call(config, admin, "GET", `/admin/agent-keys?${query}`)) as { keys?: ListedKey[] };
119 return body.keys ?? [];
122export async function keyRevoke(config: Config, admin: string, id: string, project = config.project): Promise<boolean> {
123 const body = (await call(
124 config,
125 admin,
126 "POST",
127 `/admin/agent-keys/revoke?project=${encodeURIComponent(project)}&id=${encodeURIComponent(id)}`,
128 )) as { revoked?: boolean };
129 return body.revoked === true;
132/** What `abso key list` prints. The key's value is not a field, so it cannot appear. */
133export function keyListText(keys: readonly ListedKey[]): string {
134 if (keys.length === 0) return "No keys.";
135 return keys
136 .map((k) => {
137 const when = new Date(k.at).toISOString().slice(0, 16).replace("T", " ");
138 const used =
139 k.lastUsed === null
140 ? "never used"
141 : `last used ${new Date(k.lastUsed).toISOString().slice(0, 16).replace("T", " ")}`;
142 const until = k.expiresAt ? `, until ${new Date(k.expiresAt).toISOString().slice(0, 10)}` : "";
143 const state = k.revokedAt === null ? used : "revoked";
144 return `${k.id} ${k.agent} ${k.issuedBy} ${when} ${state}${until}`;
145 })
146 .join("\n");