Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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(),
})
Expand Down
40 changes: 40 additions & 0 deletions src/context-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
parseGithubRemote,
parseGitlabRemote,
parseBitbucketRemote,
parseRemoteHost,
parseSshCommandIdentity,
parseSshConfigIdentity,
parseKeygenFingerprint,
planContext,
parseGcloudProject,
suggestContextToml,
Expand Down Expand Up @@ -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" },
Expand Down
70 changes: 66 additions & 4 deletions src/context-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 <path>` 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 <host>` 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 <file>` 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<LiveContext> {
const live: LiveContext = {};
Expand All @@ -204,6 +248,19 @@ export async function gatherLive(cwd: string = process.cwd()): Promise<LiveConte
live.gitlab = parseGitlabRemote(remote);
live.bitbucket = parseBitbucketRemote(remote);

// SSH identity this repo would actually push with: a per-repo core.sshCommand
// override wins; otherwise resolve the remote host through `ssh -G`. The host
// token (a `Host` alias when one is used) is the third comparable signal.
const host = parseRemoteHost(remote);
const sshCommand = await run("git", ["config", "--get", "core.sshCommand"]);
let identity = parseSshCommandIdentity(sshCommand);
if (!identity && host) identity = parseSshConfigIdentity(await run("ssh", ["-G", host]));
identity = expandHome(identity);
const fingerprint = identity
? parseKeygenFingerprint(await run("ssh-keygen", ["-lf", identity]))
: null;
live.ssh = { identity, fingerprint, host_alias: host };

const registry = await run("npm", ["config", "get", "registry"]);
live.npm = { registry: registry ? registry.replace(/\/+$/, "") : null };

Expand All @@ -224,10 +281,15 @@ export async function checkContext(
cwd: string = process.cwd(),
): Promise<ContextFinding[]> {
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));
}

Expand Down
Loading