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
|
|
@ -23,6 +23,18 @@ export interface LocalIdentity {
|
|||
keys: IdentityKeyPair;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
/**
|
||||
* Set on an API-key identity (keys/<prefix>.json): what kind of key it is.
|
||||
* A "machine" identity holds the machine's OWN keypair, registered on the
|
||||
* key; a "user" one holds no keypair and borrows this machine's identity.json.
|
||||
*/
|
||||
keyKind?: "user" | "machine";
|
||||
keyId?: string;
|
||||
keyName?: string;
|
||||
/** Machine keys: the one team the key is scoped to. */
|
||||
team?: string;
|
||||
/** The public key is on the server, so no command needs to register it again. */
|
||||
registered?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -178,8 +190,84 @@ export async function updateIdentity(
|
|||
return next;
|
||||
}
|
||||
|
||||
/** Require a logged-in identity (token present), or throw with guidance. */
|
||||
/** `LOGICSRC_API_KEY`, the headless credential for deploy boxes and CI. */
|
||||
export function envApiKey(): string | undefined {
|
||||
const value = process.env.LOGICSRC_API_KEY?.trim();
|
||||
return value ? value : undefined;
|
||||
}
|
||||
|
||||
/** The display prefix the server stores for a key (and names its identity file after). */
|
||||
export function apiKeyPrefix(token: string): string {
|
||||
return token.slice(0, 12);
|
||||
}
|
||||
|
||||
/** Where an API key's own identity lives: `<home>/keys/<prefix>.json`, mode 0600. */
|
||||
export function keyIdentityPath(token: string): string {
|
||||
const prefix = apiKeyPrefix(token).replace(/[^A-Za-z0-9_-]/g, "_");
|
||||
return join(logicsrcHome(), "keys", `${prefix}.json`);
|
||||
}
|
||||
|
||||
/** Set by `logicsrc login --api-key`: which key identity commands use when no env key is set. */
|
||||
export function activeKeyPath(): string {
|
||||
return join(logicsrcHome(), "active-key.json");
|
||||
}
|
||||
|
||||
export function readActiveKeyIdentity(): LocalIdentity | undefined {
|
||||
const pointer = activeKeyPath();
|
||||
if (!existsSync(pointer)) return undefined;
|
||||
try {
|
||||
const { file } = JSON.parse(readFileSync(pointer, "utf8")) as { file?: string };
|
||||
return file ? readIdentity(file) : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeKeyIdentity(identity: LocalIdentity, file: string): void {
|
||||
writeSecure(file, { ...identity, updatedAt: new Date().toISOString() });
|
||||
}
|
||||
|
||||
export function setActiveKey(file: string): void {
|
||||
writeSecure(activeKeyPath(), { file });
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an API-key identity file into what commands authenticate with.
|
||||
* A user key borrows this machine's identity.json keypair (it acts as the
|
||||
* person); a machine key carries its own.
|
||||
*/
|
||||
function fromKeyIdentity(stored: LocalIdentity, token: string): LocalIdentity & { apiToken: string; email: string } {
|
||||
if (stored.keyKind === "user") {
|
||||
const own = readIdentity();
|
||||
if (!own?.keys?.secretKey) {
|
||||
throw new Error("This API key acts as a person, and this machine has no identity key. Run: logicsrc login");
|
||||
}
|
||||
return { ...own, apiUrl: stored.apiUrl, apiToken: token, email: stored.email ?? own.email ?? "" };
|
||||
}
|
||||
return { ...stored, apiToken: token, email: stored.email ?? "" };
|
||||
}
|
||||
|
||||
/**
|
||||
* Require a logged-in identity (token present), or throw with guidance.
|
||||
*
|
||||
* Precedence: `LOGICSRC_API_KEY` (headless), then a key chosen with
|
||||
* `logicsrc login --api-key`, then the person's own `logicsrc login`. The CLI
|
||||
* prepares an API-key identity before any command runs (ensureApiKeyIdentity),
|
||||
* so reading it here stays synchronous.
|
||||
*/
|
||||
export function requireAuth(file = identityPath()): LocalIdentity & { apiToken: string; email: string } {
|
||||
const token = file === identityPath() ? envApiKey() : undefined;
|
||||
if (token) {
|
||||
const stored = readIdentity(keyIdentityPath(token));
|
||||
if (!stored) {
|
||||
throw new Error("LOGICSRC_API_KEY is set but its identity is not prepared yet. Run any logicsrc command with it once (e.g. logicsrc whoami).");
|
||||
}
|
||||
return fromKeyIdentity(stored, token);
|
||||
}
|
||||
if (file === identityPath()) {
|
||||
const active = readActiveKeyIdentity();
|
||||
if (active?.apiToken) return fromKeyIdentity(active, active.apiToken);
|
||||
}
|
||||
const identity = readIdentity(file);
|
||||
if (!identity?.apiToken || !identity.email) {
|
||||
throw new Error('Not logged in. Run "logicsrc login" first.');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue