mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 09:24:46 +00:00
Machine API keys: headless teams pull for deploy boxes and CI (#230)
A person's lsk_ key acts as the person, with one identity key per user. Logging a fresh box in with `login --token` uploaded the box's new keypair as the person's identity and replaced the real one. Machine keys fix that: a key made for a box, scoped to one team (and optionally named vaults), read-only unless --read-write, optionally expiring, with its OWN identity key registered on the key row. A vault key reaches it as a grant sealed to that public key (credshare_key_grants), never through the person. Schema (005_machine_keys, SQLite + Postgres): api_keys gains kind, team_id, vault_scope, read_only (default 0, so existing person keys keep writing), expires_at, public_key; new credshare_key_grants; credshare_audit.actor_key_id. Server: each credshare route declares what a machine key may do (read / write / register); the rest are people-only. Machine keys see only their team and scoped vaults, read their grant from credshare_key_grants (audited as vault:read with the key id), are refused every write when read-only, and cannot mint/list/revoke keys, read /api/me or touch the personal vault. Expired and revoked keys are 401. New GET|POST /api/keys, DELETE /api/keys/:id, and GET|POST|DELETE /api/credshare/vaults/:id/key-grants. A re-key re-seals the machine grants it is given and drops the rest. CLI 0.8.0: `logicsrc keys create|list|revoke` (--team, --vault, --read-only / --read-write, --expires, --out 0600); LOGICSRC_API_KEY and `login --api-key` with a per-key identity in keys/<prefix>.json (0600), generated and registered on first use; `login --token` with a machine key takes that path instead of replacing the person's key; `teams grant … --key <name>`; `credentials rotate` re-seals machine keys. PWA: settings creates machine keys (team, vaults, read-only, expiry); a vault page grants or revokes a machine key in the browser after unlock. MCP: list_api_keys / create_api_key / revoke_api_key over /api/keys with a Bearer LOGICSRC_API_KEY. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4df342a064
commit
ade33efd17
35 changed files with 2220 additions and 106 deletions
|
|
@ -63,6 +63,62 @@ export interface RekeyResult {
|
|||
rekeyed: number;
|
||||
granted: string[];
|
||||
revoked: string[];
|
||||
/** Machine keys re-sealed to the new DEK, and the ones dropped (servers before machine keys omit these). */
|
||||
keysResealed?: string[];
|
||||
keysDropped?: string[];
|
||||
}
|
||||
|
||||
/** The credential a request was made with, as /api/credshare/me describes it. Null for a browser session. */
|
||||
export interface RemoteKeyInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
prefix: string;
|
||||
kind: "user" | "machine";
|
||||
/** Machine keys only: the one team the key may see. */
|
||||
team: string | null;
|
||||
/** Machine keys only: the vault names it may see, or null for every vault in the team. */
|
||||
vaults: string[] | null;
|
||||
readOnly: boolean;
|
||||
expiresAt: number | null;
|
||||
}
|
||||
|
||||
/** One of the caller's API keys, as GET /api/keys lists it. Never carries a secret. */
|
||||
export interface RemoteApiKey {
|
||||
id: string;
|
||||
name: string;
|
||||
prefix: string;
|
||||
kind: "user" | "machine";
|
||||
team: string | null;
|
||||
vaults: string[] | null;
|
||||
readOnly: boolean;
|
||||
expiresAt: number | null;
|
||||
expired: boolean;
|
||||
/** The machine's own identity public key, once it has run one command. */
|
||||
publicKey: string | null;
|
||||
createdAt: number;
|
||||
lastUsedAt: number | null;
|
||||
}
|
||||
|
||||
export interface CreateApiKeyInput {
|
||||
name: string;
|
||||
kind?: "user" | "machine";
|
||||
team?: string;
|
||||
vaults?: string[];
|
||||
readOnly?: boolean;
|
||||
/** ms epoch, an ISO date, or a duration such as 30d / 12h. */
|
||||
expiresAt?: number | string;
|
||||
}
|
||||
|
||||
/** A machine key holding a vault, with its public key so a re-key can re-seal to it. */
|
||||
export interface RemoteKeyGrant {
|
||||
keyId: string;
|
||||
name: string;
|
||||
prefix: string;
|
||||
owner: string | null;
|
||||
publicKey: string | null;
|
||||
readOnly: boolean;
|
||||
expiresAt: number | null;
|
||||
grantedAt: number;
|
||||
}
|
||||
|
||||
export class TeamApiError extends Error {
|
||||
|
|
@ -88,11 +144,15 @@ export class TeamClient {
|
|||
this.token = token;
|
||||
}
|
||||
|
||||
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
private request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
return this.requestAt<T>(`/api/credshare${path}`, method, body);
|
||||
}
|
||||
|
||||
private async requestAt<T>(fullPath: string, method: string, body?: unknown): Promise<T> {
|
||||
const headers: Record<string, string> = { accept: "application/json" };
|
||||
if (body !== undefined) headers["content-type"] = "application/json";
|
||||
if (this.token) headers["authorization"] = `Bearer ${this.token}`;
|
||||
const response = await fetch(`${this.apiUrl}/api/credshare${path}`, {
|
||||
const response = await fetch(`${this.apiUrl}${fullPath}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body === undefined ? undefined : JSON.stringify(body)
|
||||
|
|
@ -117,7 +177,7 @@ export class TeamClient {
|
|||
return this.request<{ email: string; publicKey: string }>("POST", "/keys", { publicKey });
|
||||
}
|
||||
me() {
|
||||
return this.request<{ user: RemoteUser; teams: RemoteTeam[] }>("GET", "/me");
|
||||
return this.request<{ user: RemoteUser; teams: RemoteTeam[]; key?: RemoteKeyInfo | null }>("GET", "/me");
|
||||
}
|
||||
logout() {
|
||||
return this.request<{ ok: boolean }>("POST", "/logout");
|
||||
|
|
@ -193,10 +253,33 @@ export class TeamClient {
|
|||
grants: Array<{ email: string; wrappedDek: string }>;
|
||||
secrets: Array<{ name: string; nonce: string; ciphertext: string; fingerprint: string }>;
|
||||
revoke: string[];
|
||||
keyGrants?: Array<{ keyId: string; wrappedDek: string }>;
|
||||
}
|
||||
) {
|
||||
return this.request<RekeyResult>("POST", `/vaults/${encodeURIComponent(vaultId)}/rekey`, body);
|
||||
}
|
||||
// ---- machine-key grants (the vault key sealed to a machine key's own public key) ----
|
||||
listKeyGrants(vaultId: string) {
|
||||
return this.request<{ keyGrants: RemoteKeyGrant[] }>("GET", `/vaults/${encodeURIComponent(vaultId)}/key-grants`);
|
||||
}
|
||||
putKeyGrant(vaultId: string, keyId: string, wrappedDek: string) {
|
||||
return this.request<{ ok: boolean; keyId: string; name: string }>("POST", `/vaults/${encodeURIComponent(vaultId)}/key-grants`, { keyId, wrappedDek });
|
||||
}
|
||||
deleteKeyGrant(vaultId: string, keyId: string) {
|
||||
return this.request<{ ok: boolean; revoked: string }>("DELETE", `/vaults/${encodeURIComponent(vaultId)}/key-grants/${encodeURIComponent(keyId)}`);
|
||||
}
|
||||
|
||||
// ---- API keys (/api/keys; a person's credential only, never a machine key) ----
|
||||
listApiKeys() {
|
||||
return this.requestAt<{ keys: RemoteApiKey[] }>("/api/keys", "GET");
|
||||
}
|
||||
createApiKey(input: CreateApiKeyInput) {
|
||||
return this.requestAt<{ key: RemoteApiKey; secret: string }>("/api/keys", "POST", input);
|
||||
}
|
||||
revokeApiKey(keyId: string) {
|
||||
return this.requestAt<{ ok: boolean; revoked: string }>(`/api/keys/${encodeURIComponent(keyId)}`, "DELETE");
|
||||
}
|
||||
|
||||
listAudit(vaultId: string) {
|
||||
return this.request<{ audit: Array<Record<string, unknown>> }>("GET", `/vaults/${encodeURIComponent(vaultId)}/audit`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue