diff --git a/CHANGELOG.md b/CHANGELOG.md index 7602460e..6ae1c1f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ## [Unreleased] +### Added +- **Context-lock now covers your SSH identity per project.** A new `[context.ssh]` block locks which key a repo pushes/deploys with — declare any of `identity` (the IdentityFile path), `fingerprint` (`SHA256:…`, machine-portable), or `host_alias` (the `~/.ssh/config` Host the remote uses). `kit context check` reads the repo's *effective* identity — a per-repo `core.sshCommand` `-i` override wins, otherwise it resolves the remote host through `ssh -G` — derives the key fingerprint via `ssh-keygen -lf`, and verifies it matches. Pushing a repo with the wrong account's key is a mismatch, not a silent pass — the SSH analog of the git-host remote lock. Pure parsers (`core.sshCommand` `-i`, `ssh -G` identityfile, keygen fingerprint, remote host) are unit-tested; the live `ssh -G` read smoke-tests on a real machine. + ## [1.13.0] - 2026-06-22 ### Added diff --git a/src/config.ts b/src/config.ts index 8f3b38e1..7bf69243 100644 --- a/src/config.ts +++ b/src/config.ts @@ -262,6 +262,8 @@ export interface ContextConfig { github?: { org?: string; remote?: string }; gitlab?: { group?: string; remote?: string }; bitbucket?: { workspace?: string; remote?: string }; + /** SSH identity this repo must push/deploy with. Declare any of these. */ + ssh?: { identity?: string; fingerprint?: string; host_alias?: string }; git?: { email?: string }; npm?: { registry?: string }; } @@ -512,6 +514,14 @@ const kitConfigSchema = z .object({ workspace: z.string().optional(), remote: z.string().optional() }) .passthrough() .optional(), + ssh: z + .object({ + identity: z.string().optional(), + fingerprint: z.string().optional(), + host_alias: z.string().optional(), + }) + .passthrough() + .optional(), git: z.object({ email: z.string().optional() }).passthrough().optional(), npm: z.object({ registry: z.string().optional() }).passthrough().optional(), }) diff --git a/src/context-lock.test.ts b/src/context-lock.test.ts index e9ddb16e..f3942c23 100644 --- a/src/context-lock.test.ts +++ b/src/context-lock.test.ts @@ -5,6 +5,10 @@ import { parseGithubRemote, parseGitlabRemote, parseBitbucketRemote, + parseRemoteHost, + parseSshCommandIdentity, + parseSshConfigIdentity, + parseKeygenFingerprint, planContext, parseGcloudProject, suggestContextToml, @@ -106,6 +110,42 @@ describe("context lock", () => { assert.equal(compareContext(declared, { bitbucket: { workspace: "other", remote: null } })[0]?.status, "mismatch"); }); + it("parses the SSH identity from core.sshCommand, ssh -G output, and a keygen fingerprint", () => { + assert.equal(parseSshCommandIdentity('ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes'), "~/.ssh/id_work"); + assert.equal(parseSshCommandIdentity('ssh -i "/home/me/.ssh/id work"'), "/home/me/.ssh/id work"); + assert.equal(parseSshCommandIdentity("ssh"), null); + assert.equal(parseSshConfigIdentity("user me\nidentityfile /home/me/.ssh/id_work\nidentityfile ~/.ssh/id_rsa"), "/home/me/.ssh/id_work"); + assert.equal(parseKeygenFingerprint("256 SHA256:abc123DEF+/ me@host (ED25519)"), "SHA256:abc123DEF+/"); + assert.equal(parseKeygenFingerprint("not a fingerprint"), null); + }); + + it("parses the literal remote host (the Host alias when one is used)", () => { + assert.equal(parseRemoteHost("git@github.com-work:acme/web.git"), "github.com-work"); + assert.equal(parseRemoteHost("https://gitlab.com/acme/web"), "gitlab.com"); + assert.equal(parseRemoteHost(null), null); + }); + + it("locks the SSH identity: wrong key/fingerprint/host-alias is a mismatch, missing is unknown", () => { + const byFp = { ssh: { fingerprint: "SHA256:GOOD" } }; + assert.equal( + compareContext(byFp, { ssh: { identity: null, fingerprint: "SHA256:GOOD", host_alias: null } })[0]?.status, + "ok", + ); + assert.equal( + compareContext(byFp, { ssh: { identity: null, fingerprint: "SHA256:EVIL", host_alias: null } })[0]?.status, + "mismatch", + ); + assert.equal( + compareContext(byFp, { ssh: { identity: null, fingerprint: null, host_alias: null } })[0]?.status, + "unknown", + ); + const byAlias = { ssh: { host_alias: "github.com-work" } }; + assert.equal( + compareContext(byAlias, { ssh: { identity: null, fingerprint: null, host_alias: "github.com-personal" } })[0]?.status, + "mismatch", + ); + }); + it("plans gcloud + git activation from the declared context (local config only)", () => { const steps = planContext({ gcloud: { config: "prod-cfg", account: "ops@example.com", project: "prod-project", region: "europe-west4" }, diff --git a/src/context-lock.ts b/src/context-lock.ts index 58be7c0a..ebcbb9bd 100644 --- a/src/context-lock.ts +++ b/src/context-lock.ts @@ -34,6 +34,7 @@ export interface LiveContext { github?: { org: string | null; remote: string | null }; gitlab?: { group: string | null; remote: string | null }; bitbucket?: { workspace: string | null; remote: string | null }; + ssh?: { identity: string | null; fingerprint: string | null; host_alias: string | null }; npm?: { registry: string | null }; vercel?: { orgId: string | null; projectId: string | null }; } @@ -68,6 +69,9 @@ const FIELD_SPECS: { { tool: "gitlab", field: "remote", declared: (d) => d.gitlab?.remote, live: (l) => l.gitlab?.remote ?? null }, { tool: "bitbucket", field: "workspace", declared: (d) => d.bitbucket?.workspace, live: (l) => l.bitbucket?.workspace ?? null }, { tool: "bitbucket", field: "remote", declared: (d) => d.bitbucket?.remote, live: (l) => l.bitbucket?.remote ?? null }, + { tool: "ssh", field: "identity", declared: (d) => d.ssh?.identity, live: (l) => l.ssh?.identity ?? null }, + { tool: "ssh", field: "fingerprint", declared: (d) => d.ssh?.fingerprint, live: (l) => l.ssh?.fingerprint ?? null }, + { tool: "ssh", field: "host_alias", declared: (d) => d.ssh?.host_alias, live: (l) => l.ssh?.host_alias ?? null }, { tool: "npm", field: "registry", declared: (d) => d.npm?.registry, live: (l) => l.npm?.registry ?? null }, { tool: "vercel", field: "team(orgId)", declared: (d) => d.vercel?.team, live: (l) => l.vercel?.orgId ?? null }, { tool: "vercel", field: "project(projectId)", declared: (d) => d.vercel?.project, live: (l) => l.vercel?.projectId ?? null }, @@ -180,6 +184,46 @@ export function parseBitbucketRemote(url: string | null): { workspace: string | return { workspace: m[1], remote: `bitbucket.org/${m[1]}/${m[2]}` }; } +/** The literal host token of a remote URL (the `Host` alias when one is used). */ +export function parseRemoteHost(url: string | null): string | null { + if (!url) return null; + const ssh = url.match(/^(?:ssh:\/\/)?[^@]+@([^:/]+)/); + if (ssh) return ssh[1]; + const https = url.match(/^https?:\/\/(?:[^@/]+@)?([^/]+)/); + if (https) return https[1]; + return null; +} + +/** The `-i ` identity from a git `core.sshCommand`, if set. */ +export function parseSshCommandIdentity(cmd: string | null): string | null { + if (!cmd) return null; + const m = cmd.match(/-i\s+("[^"]+"|'[^']+'|\S+)/); + return m ? m[1].replace(/^["']|["']$/g, "") : null; +} + +/** First configured IdentityFile from `ssh -G ` output (configured precede defaults). */ +export function parseSshConfigIdentity(sshGOutput: string | null): string | null { + if (!sshGOutput) return null; + for (const line of sshGOutput.split("\n")) { + const m = line.match(/^identityfile\s+(.+)$/); + if (m) return m[1].trim(); + } + return null; +} + +/** The SHA256 fingerprint from `ssh-keygen -lf ` output. */ +export function parseKeygenFingerprint(out: string | null): string | null { + if (!out) return null; + const m = out.match(/SHA256:[A-Za-z0-9+/=]+/); + return m ? m[0] : null; +} + +/** Expand a leading `~` to the home dir, for comparing declared vs live paths. */ +function expandHome(p: string | null): string | null { + if (!p) return null; + return p.startsWith("~") ? join(homedir(), p.slice(1)) : p; +} + /** Read the live context from each tool. Every read fails soft to null. */ export async function gatherLive(cwd: string = process.cwd()): Promise { const live: LiveContext = {}; @@ -204,6 +248,19 @@ export async function gatherLive(cwd: string = process.cwd()): Promise { if (!ctx) return []; - // Normalize a declared npm registry the same way the live read is normalized. - const declared: ContextConfig = ctx.npm?.registry - ? { ...ctx, npm: { ...ctx.npm, registry: ctx.npm.registry.replace(/\/+$/, "") } } - : ctx; + // Normalize declared values the same way the live reads are normalized: + // npm registry trailing slash, and ssh identity `~` expansion (compared as paths). + let declared: ContextConfig = ctx; + if (ctx.npm?.registry) { + declared = { ...declared, npm: { ...declared.npm, registry: ctx.npm.registry.replace(/\/+$/, "") } }; + } + if (declared.ssh?.identity) { + declared = { ...declared, ssh: { ...declared.ssh, identity: expandHome(declared.ssh.identity) ?? declared.ssh.identity } }; + } return compareContext(declared, await gatherLive(cwd)); }