@leitwerk-dev/git-ssh 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +133 -43
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +114 -0
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
# Git SSH
|
|
2
2
|
|
|
3
3
|
Provides pinned, non-interactive Git-over-SSH credential profiles to repository processes. Configure profiles under `extensions.git-ssh.credentials`. Private keys and `known_hosts` are delivered only in authenticated `worker.start` IPC; ambient SSH configuration and agents are not used.
|
|
4
|
+
|
|
5
|
+
Dependent server extensions can use `gitSshIntegration.preflight()` before launch. The preflight verifies the repository's base ref with `git ls-remote`. When write access is required, it fetches that ref and performs a dry-run push to a unique probe branch. The check uses only the selected profile, does not create the probe branch, and never returns credential material.
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,25 @@ export declare const manifest: {
|
|
|
3
3
|
readonly id: "git-ssh";
|
|
4
4
|
readonly version: "0.1.0";
|
|
5
5
|
};
|
|
6
|
+
export interface GitSshAuthorizationPreflightInput {
|
|
7
|
+
credentialRef: string;
|
|
8
|
+
repoLocator: string;
|
|
9
|
+
baseBranch: string;
|
|
10
|
+
requireWrite: boolean;
|
|
11
|
+
}
|
|
12
|
+
export type GitSshAuthorizationPreflightResult = {
|
|
13
|
+
ok: true;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
access: "read" | "write";
|
|
17
|
+
detail: string;
|
|
18
|
+
};
|
|
19
|
+
export interface GitSshIntegration {
|
|
20
|
+
profiles(): readonly string[];
|
|
21
|
+
preflight(input: GitSshAuthorizationPreflightInput): Promise<GitSshAuthorizationPreflightResult>;
|
|
22
|
+
}
|
|
23
|
+
export declare const gitSshIntegration: import("@leitwerk-dev/process-sdk").CapabilityToken<GitSshIntegration>;
|
|
24
|
+
export declare function preflightGitSshAccess(input: Omit<GitSshAuthorizationPreflightInput, "credentialRef">, material: GitSshCredentialMaterial, gitBinary?: string): Promise<GitSshAuthorizationPreflightResult>;
|
|
6
25
|
declare function parseProfiles(raw: unknown): Map<string, GitSshCredentialMaterial>;
|
|
7
26
|
declare const extension: LeitwerkExtensionModule;
|
|
8
27
|
export { parseProfiles };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAGN,KAAK,wBAAwB,EAC7B,KAAK,uBAAuB,EAC5B,MAAM,2BAA2B,CAAC;AAInC,eAAO,MAAM,QAAQ;;;CAA+C,CAAC;AAErE,MAAM,WAAW,iCAAiC;IACjD,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,MAAM,kCAAkC,GAC3C;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GACZ;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IACjC,QAAQ,IAAI,SAAS,MAAM,EAAE,CAAC;IAC9B,SAAS,CAAC,KAAK,EAAE,iCAAiC,GAAG,OAAO,CAAC,kCAAkC,CAAC,CAAC;CACjG;AAED,eAAO,MAAM,iBAAiB,wEAE7B,CAAC;AAgBF,wBAAsB,qBAAqB,CAC1C,KAAK,EAAE,IAAI,CAAC,iCAAiC,EAAE,eAAe,CAAC,EAC/D,QAAQ,EAAE,wBAAwB,EAClC,SAAS,SAAQ,GACf,OAAO,CAAC,kCAAkC,CAAC,CAoD7C;AAED,iBAAS,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAiC1E;AAED,QAAA,MAAM,SAAS,EAAE,uBAyBhB,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC;AACzB,eAAe,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,137 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { execFile } from "child_process";
|
|
3
|
+
import { mkdtemp, rm, writeFile } from "fs/promises";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { join } from "path";
|
|
6
|
+
import { promisify } from "util";
|
|
7
|
+
import {
|
|
8
|
+
coreHostCapabilities,
|
|
9
|
+
createCapabilityToken
|
|
10
|
+
} from "@leitwerk-dev/process-sdk";
|
|
11
|
+
var execFileAsync = promisify(execFile);
|
|
12
|
+
var manifest = { id: "git-ssh", version: "0.1.0" };
|
|
13
|
+
var gitSshIntegration = createCapabilityToken(
|
|
14
|
+
"@leitwerk-dev/git-ssh.integration"
|
|
15
|
+
);
|
|
16
|
+
function gitFailureDetail(error) {
|
|
17
|
+
if (!error || typeof error !== "object") return "Git SSH access check failed";
|
|
18
|
+
const value = error;
|
|
19
|
+
const output = typeof value.stderr === "string" && value.stderr.trim() ? value.stderr : typeof value.stdout === "string" && value.stdout.trim() ? value.stdout : typeof value.message === "string" ? value.message : "Git SSH access check failed";
|
|
20
|
+
return output.replace(/\s+/g, " ").trim().slice(0, 500);
|
|
21
|
+
}
|
|
22
|
+
async function preflightGitSshAccess(input, material, gitBinary = "git") {
|
|
23
|
+
const directory = await mkdtemp(join(tmpdir(), "leitwerk-git-ssh-preflight-"));
|
|
24
|
+
try {
|
|
25
|
+
const keyPath = join(directory, "identity");
|
|
26
|
+
const knownHostsPath = join(directory, "known_hosts");
|
|
27
|
+
const sshPath = join(directory, "ssh");
|
|
28
|
+
await Promise.all([
|
|
29
|
+
writeFile(keyPath, `${material.privateKey.trim()}
|
|
30
|
+
`, { mode: 384 }),
|
|
31
|
+
writeFile(knownHostsPath, `${material.knownHosts.trim()}
|
|
32
|
+
`, { mode: 384 }),
|
|
33
|
+
writeFile(
|
|
34
|
+
sshPath,
|
|
35
|
+
`#!/bin/sh
|
|
36
|
+
exec ssh -F /dev/null -i "${keyPath}" -o IdentitiesOnly=yes -o UserKnownHostsFile="${knownHostsPath}" -o StrictHostKeyChecking=yes -o BatchMode=yes "$@"
|
|
37
|
+
`,
|
|
38
|
+
{ mode: 448 }
|
|
39
|
+
)
|
|
40
|
+
]);
|
|
41
|
+
const env = {
|
|
42
|
+
...process.env,
|
|
43
|
+
GIT_SSH: sshPath,
|
|
44
|
+
// This also overrides ambient core.sshCommand, which takes precedence over GIT_SSH.
|
|
45
|
+
GIT_SSH_COMMAND: `'${sshPath.replaceAll("'", "'\\''")}'`,
|
|
46
|
+
SSH_AUTH_SOCK: ""
|
|
47
|
+
};
|
|
48
|
+
const ref = `refs/heads/${input.baseBranch}`;
|
|
49
|
+
try {
|
|
50
|
+
await execFileAsync(gitBinary, ["ls-remote", "--exit-code", input.repoLocator, ref], {
|
|
51
|
+
env
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return { ok: false, access: "read", detail: gitFailureDetail(error) };
|
|
55
|
+
}
|
|
56
|
+
if (!input.requireWrite) return { ok: true };
|
|
57
|
+
try {
|
|
58
|
+
await execFileAsync(gitBinary, ["init", "--quiet", directory], { env });
|
|
59
|
+
await execFileAsync(
|
|
60
|
+
gitBinary,
|
|
61
|
+
["-C", directory, "fetch", "--quiet", "--depth=1", input.repoLocator, ref],
|
|
62
|
+
{ env }
|
|
63
|
+
);
|
|
64
|
+
const probeBranch = `refs/heads/leitwerk/preflight-${process.pid}-${Date.now()}`;
|
|
65
|
+
await execFileAsync(
|
|
66
|
+
gitBinary,
|
|
67
|
+
["-C", directory, "push", "--dry-run", input.repoLocator, `FETCH_HEAD:${probeBranch}`],
|
|
68
|
+
{ env }
|
|
69
|
+
);
|
|
70
|
+
return { ok: true };
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return { ok: false, access: "write", detail: gitFailureDetail(error) };
|
|
29
73
|
}
|
|
30
|
-
|
|
74
|
+
} finally {
|
|
75
|
+
await rm(directory, { recursive: true, force: true });
|
|
76
|
+
}
|
|
31
77
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
78
|
+
function parseProfiles(raw) {
|
|
79
|
+
const root = raw && typeof raw === "object" ? raw : {};
|
|
80
|
+
const credentials = root.credentials && typeof root.credentials === "object" ? root.credentials : {};
|
|
81
|
+
const profiles = /* @__PURE__ */ new Map();
|
|
82
|
+
for (const [ref, value] of Object.entries(credentials)) {
|
|
83
|
+
const profile = value && typeof value === "object" ? value : {};
|
|
84
|
+
const privateKey = typeof profile.private_key === "string" ? profile.private_key.trim() : "";
|
|
85
|
+
const knownHosts = typeof profile.known_hosts === "string" ? profile.known_hosts.trim() : "";
|
|
86
|
+
if (!privateKey.includes("BEGIN OPENSSH PRIVATE KEY"))
|
|
87
|
+
throw new Error(`Invalid git_ssh credential '${ref}': an OpenSSH private key is required`);
|
|
88
|
+
const encodedBody = privateKey.split(/\r?\n/).filter((line) => !line.startsWith("-----")).join("");
|
|
89
|
+
const decodedHeader = Buffer.from(encodedBody, "base64").subarray(0, 96).toString("latin1");
|
|
90
|
+
if (/ENCRYPTED/.test(privateKey) || decodedHeader.includes("bcrypt"))
|
|
91
|
+
throw new Error(
|
|
92
|
+
`Invalid git_ssh credential '${ref}': encrypted keys requiring a passphrase are unsupported`
|
|
93
|
+
);
|
|
94
|
+
const hostLines = knownHosts.split(/\r?\n/).map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
|
|
95
|
+
if (hostLines.length === 0 || hostLines.some((line) => line.split(/\s+/).length < 3))
|
|
96
|
+
throw new Error(
|
|
97
|
+
`Invalid git_ssh credential '${ref}': pinned known_hosts entries are required`
|
|
98
|
+
);
|
|
99
|
+
profiles.set(ref, { privateKey, knownHosts });
|
|
100
|
+
}
|
|
101
|
+
return profiles;
|
|
102
|
+
}
|
|
103
|
+
var extension = {
|
|
104
|
+
manifest,
|
|
105
|
+
setupServer(api, config) {
|
|
106
|
+
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
107
|
+
if (!deps || Array.isArray(deps)) return;
|
|
108
|
+
const profiles = parseProfiles(config);
|
|
109
|
+
deps.repositoryCredentials.register({
|
|
110
|
+
kind: "git_ssh",
|
|
111
|
+
resolve: (ref) => profiles.get(ref) ?? null
|
|
112
|
+
});
|
|
113
|
+
api.provide(gitSshIntegration, {
|
|
114
|
+
profiles: () => [...profiles.keys()].sort(),
|
|
115
|
+
async preflight(input) {
|
|
116
|
+
const material = profiles.get(input.credentialRef);
|
|
117
|
+
if (!material) {
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
access: "read",
|
|
121
|
+
detail: `Unknown git_ssh credential '${input.credentialRef}'`
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return preflightGitSshAccess(input, material);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var index_default = extension;
|
|
130
|
+
export {
|
|
131
|
+
index_default as default,
|
|
132
|
+
gitSshIntegration,
|
|
133
|
+
manifest,
|
|
134
|
+
parseProfiles,
|
|
135
|
+
preflightGitSshAccess
|
|
44
136
|
};
|
|
45
|
-
export { parseProfiles };
|
|
46
|
-
export default extension;
|
|
47
137
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,GAGpB,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAW,CAAC;AAErE,SAAS,aAAa,CAAC,GAAY;IAClC,MAAM,IAAI,GAAG,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAE,GAA+B,CAAC,CAAC,CAAC,EAAE,CAAC;IACpF,MAAM,WAAW,GAChB,IAAI,CAAC,WAAW,IAAI,OAAO,IAAI,CAAC,WAAW,KAAK,QAAQ;QACvD,CAAC,CAAE,IAAI,CAAC,WAAuC;QAC/C,CAAC,CAAC,EAAE,CAAC;IACP,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAoC,CAAC;IAC7D,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;QACxD,MAAM,OAAO,GAAG,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAE,KAAiC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7F,MAAM,UAAU,GAAG,OAAO,OAAO,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7F,MAAM,UAAU,GAAG,OAAO,OAAO,CAA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Q,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED,MAAM,SAAS,GAA4B;IAC1C,QAAQ;IACR,WAAW,CAAC,GAAG,EAAE,MAAM;QACtB,MAAM,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;YAAE,OAAO;QACzC,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC;YACnC,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI;SAC3C,CAAC,CAAC;IACJ,CAAC;CACD,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC;AACzB,eAAe,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { execFile } from \"node:child_process\";\nimport { mkdtemp, rm, writeFile } from \"node:fs/promises\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { promisify } from \"node:util\";\nimport {\n\tcoreHostCapabilities,\n\tcreateCapabilityToken,\n\ttype GitSshCredentialMaterial,\n\ttype LeitwerkExtensionModule,\n} from \"@leitwerk-dev/process-sdk\";\n\nconst execFileAsync = promisify(execFile);\n\nexport const manifest = { id: \"git-ssh\", version: \"0.1.0\" } as const;\n\nexport interface GitSshAuthorizationPreflightInput {\n\tcredentialRef: string;\n\trepoLocator: string;\n\tbaseBranch: string;\n\trequireWrite: boolean;\n}\n\nexport type GitSshAuthorizationPreflightResult =\n\t| { ok: true }\n\t| { ok: false; access: \"read\" | \"write\"; detail: string };\n\nexport interface GitSshIntegration {\n\tprofiles(): readonly string[];\n\tpreflight(input: GitSshAuthorizationPreflightInput): Promise<GitSshAuthorizationPreflightResult>;\n}\n\nexport const gitSshIntegration = createCapabilityToken<GitSshIntegration>(\n\t\"@leitwerk-dev/git-ssh.integration\",\n);\n\nfunction gitFailureDetail(error: unknown): string {\n\tif (!error || typeof error !== \"object\") return \"Git SSH access check failed\";\n\tconst value = error as { stderr?: unknown; stdout?: unknown; message?: unknown };\n\tconst output =\n\t\ttypeof value.stderr === \"string\" && value.stderr.trim()\n\t\t\t? value.stderr\n\t\t\t: typeof value.stdout === \"string\" && value.stdout.trim()\n\t\t\t\t? value.stdout\n\t\t\t\t: typeof value.message === \"string\"\n\t\t\t\t\t? value.message\n\t\t\t\t\t: \"Git SSH access check failed\";\n\treturn output.replace(/\\s+/g, \" \").trim().slice(0, 500);\n}\n\nexport async function preflightGitSshAccess(\n\tinput: Omit<GitSshAuthorizationPreflightInput, \"credentialRef\">,\n\tmaterial: GitSshCredentialMaterial,\n\tgitBinary = \"git\",\n): Promise<GitSshAuthorizationPreflightResult> {\n\tconst directory = await mkdtemp(join(tmpdir(), \"leitwerk-git-ssh-preflight-\"));\n\ttry {\n\t\tconst keyPath = join(directory, \"identity\");\n\t\tconst knownHostsPath = join(directory, \"known_hosts\");\n\t\tconst sshPath = join(directory, \"ssh\");\n\t\tawait Promise.all([\n\t\t\twriteFile(keyPath, `${material.privateKey.trim()}\\n`, { mode: 0o600 }),\n\t\t\twriteFile(knownHostsPath, `${material.knownHosts.trim()}\\n`, { mode: 0o600 }),\n\t\t\twriteFile(\n\t\t\t\tsshPath,\n\t\t\t\t`#!/bin/sh\\nexec ssh -F /dev/null -i \"${keyPath}\" -o IdentitiesOnly=yes -o UserKnownHostsFile=\"${knownHostsPath}\" -o StrictHostKeyChecking=yes -o BatchMode=yes \"$@\"\\n`,\n\t\t\t\t{ mode: 0o700 },\n\t\t\t),\n\t\t]);\n\t\tconst env = {\n\t\t\t...process.env,\n\t\t\tGIT_SSH: sshPath,\n\t\t\t// This also overrides ambient core.sshCommand, which takes precedence over GIT_SSH.\n\t\t\tGIT_SSH_COMMAND: `'${sshPath.replaceAll(\"'\", \"'\\\\''\")}'`,\n\t\t\tSSH_AUTH_SOCK: \"\",\n\t\t};\n\t\tconst ref = `refs/heads/${input.baseBranch}`;\n\t\ttry {\n\t\t\tawait execFileAsync(gitBinary, [\"ls-remote\", \"--exit-code\", input.repoLocator, ref], {\n\t\t\t\tenv,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\treturn { ok: false, access: \"read\", detail: gitFailureDetail(error) };\n\t\t}\n\t\tif (!input.requireWrite) return { ok: true };\n\n\t\ttry {\n\t\t\tawait execFileAsync(gitBinary, [\"init\", \"--quiet\", directory], { env });\n\t\t\tawait execFileAsync(\n\t\t\t\tgitBinary,\n\t\t\t\t[\"-C\", directory, \"fetch\", \"--quiet\", \"--depth=1\", input.repoLocator, ref],\n\t\t\t\t{ env },\n\t\t\t);\n\t\t\tconst probeBranch = `refs/heads/leitwerk/preflight-${process.pid}-${Date.now()}`;\n\t\t\tawait execFileAsync(\n\t\t\t\tgitBinary,\n\t\t\t\t[\"-C\", directory, \"push\", \"--dry-run\", input.repoLocator, `FETCH_HEAD:${probeBranch}`],\n\t\t\t\t{ env },\n\t\t\t);\n\t\t\treturn { ok: true };\n\t\t} catch (error) {\n\t\t\treturn { ok: false, access: \"write\", detail: gitFailureDetail(error) };\n\t\t}\n\t} finally {\n\t\tawait rm(directory, { recursive: true, force: true });\n\t}\n}\n\nfunction parseProfiles(raw: unknown): Map<string, GitSshCredentialMaterial> {\n\tconst root = raw && typeof raw === \"object\" ? (raw as Record<string, unknown>) : {};\n\tconst credentials =\n\t\troot.credentials && typeof root.credentials === \"object\"\n\t\t\t? (root.credentials as Record<string, unknown>)\n\t\t\t: {};\n\tconst profiles = new Map<string, GitSshCredentialMaterial>();\n\tfor (const [ref, value] of Object.entries(credentials)) {\n\t\tconst profile = value && typeof value === \"object\" ? (value as Record<string, unknown>) : {};\n\t\tconst privateKey = typeof profile.private_key === \"string\" ? profile.private_key.trim() : \"\";\n\t\tconst knownHosts = typeof profile.known_hosts === \"string\" ? profile.known_hosts.trim() : \"\";\n\t\tif (!privateKey.includes(\"BEGIN OPENSSH PRIVATE KEY\"))\n\t\t\tthrow new Error(`Invalid git_ssh credential '${ref}': an OpenSSH private key is required`);\n\t\tconst encodedBody = privateKey\n\t\t\t.split(/\\r?\\n/)\n\t\t\t.filter((line) => !line.startsWith(\"-----\"))\n\t\t\t.join(\"\");\n\t\tconst decodedHeader = Buffer.from(encodedBody, \"base64\").subarray(0, 96).toString(\"latin1\");\n\t\tif (/ENCRYPTED/.test(privateKey) || decodedHeader.includes(\"bcrypt\"))\n\t\t\tthrow new Error(\n\t\t\t\t`Invalid git_ssh credential '${ref}': encrypted keys requiring a passphrase are unsupported`,\n\t\t\t);\n\t\tconst hostLines = knownHosts\n\t\t\t.split(/\\r?\\n/)\n\t\t\t.map((line) => line.trim())\n\t\t\t.filter((line) => line && !line.startsWith(\"#\"));\n\t\tif (hostLines.length === 0 || hostLines.some((line) => line.split(/\\s+/).length < 3))\n\t\t\tthrow new Error(\n\t\t\t\t`Invalid git_ssh credential '${ref}': pinned known_hosts entries are required`,\n\t\t\t);\n\t\tprofiles.set(ref, { privateKey, knownHosts });\n\t}\n\treturn profiles;\n}\n\nconst extension: LeitwerkExtensionModule = {\n\tmanifest,\n\tsetupServer(api, config) {\n\t\tconst deps = api.get(coreHostCapabilities.serverSetup);\n\t\tif (!deps || Array.isArray(deps)) return;\n\t\tconst profiles = parseProfiles(config);\n\t\tdeps.repositoryCredentials.register({\n\t\t\tkind: \"git_ssh\",\n\t\t\tresolve: (ref) => profiles.get(ref) ?? null,\n\t\t});\n\t\tapi.provide(gitSshIntegration, {\n\t\t\tprofiles: () => [...profiles.keys()].sort(),\n\t\t\tasync preflight(input) {\n\t\t\t\tconst material = profiles.get(input.credentialRef);\n\t\t\t\tif (!material) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tok: false,\n\t\t\t\t\t\taccess: \"read\",\n\t\t\t\t\t\tdetail: `Unknown git_ssh credential '${input.credentialRef}'`,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn preflightGitSshAccess(input, material);\n\t\t\t},\n\t\t});\n\t},\n};\n\nexport { parseProfiles };\nexport default extension;\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,SAAS,IAAI,iBAAiB;AACvC,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B;AAAA,EACC;AAAA,EACA;AAAA,OAGM;AAEP,IAAM,gBAAgB,UAAU,QAAQ;AAEjC,IAAM,WAAW,EAAE,IAAI,WAAW,SAAS,QAAQ;AAkBnD,IAAM,oBAAoB;AAAA,EAChC;AACD;AAEA,SAAS,iBAAiB,OAAwB;AACjD,MAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;AAChD,QAAM,QAAQ;AACd,QAAM,SACL,OAAO,MAAM,WAAW,YAAY,MAAM,OAAO,KAAK,IACnD,MAAM,SACN,OAAO,MAAM,WAAW,YAAY,MAAM,OAAO,KAAK,IACrD,MAAM,SACN,OAAO,MAAM,YAAY,WACxB,MAAM,UACN;AACN,SAAO,OAAO,QAAQ,QAAQ,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG;AACvD;AAEA,eAAsB,sBACrB,OACA,UACA,YAAY,OACkC;AAC9C,QAAM,YAAY,MAAM,QAAQ,KAAK,OAAO,GAAG,6BAA6B,CAAC;AAC7E,MAAI;AACH,UAAM,UAAU,KAAK,WAAW,UAAU;AAC1C,UAAM,iBAAiB,KAAK,WAAW,aAAa;AACpD,UAAM,UAAU,KAAK,WAAW,KAAK;AACrC,UAAM,QAAQ,IAAI;AAAA,MACjB,UAAU,SAAS,GAAG,SAAS,WAAW,KAAK,CAAC;AAAA,GAAM,EAAE,MAAM,IAAM,CAAC;AAAA,MACrE,UAAU,gBAAgB,GAAG,SAAS,WAAW,K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R;AAEA,IAAM,YAAqC;AAAA,EAC1C;AAAA,EACA,YAAY,KAAK,QAAQ;AACxB,UAAM,OAAO,IAAI,IAAI,qBAAqB,WAAW;AACrD,QAAI,CAAC,QAAQ,MAAM,QAAQ,IAAI,EAAG;AAClC,UAAM,WAAW,cAAc,MAAM;AACrC,SAAK,sBAAsB,SAAS;AAAA,MACnC,MAAM;AAAA,MACN,SAAS,CAAC,QAAQ,SAAS,IAAI,GAAG,KAAK;AAAA,IACxC,CAAC;AACD,QAAI,QAAQ,mBAAmB;AAAA,MAC9B,UAAU,MAAM,CAAC,GAAG,SAAS,KAAK,CAAC,EAAE,KAAK;AAAA,MAC1C,MAAM,UAAU,OAAO;AACtB,cAAM,WAAW,SAAS,IAAI,MAAM,aAAa;AACjD,YAAI,CAAC,UAAU;AACd,iBAAO;AAAA,YACN,IAAI;AAAA,YACJ,QAAQ;AAAA,YACR,QAAQ,+BAA+B,MAAM,aAAa;AAAA,UAC3D;AAAA,QACD;AACA,eAAO,sBAAsB,OAAO,QAAQ;AAAA,MAC7C;AAAA,IACD,CAAC;AAAA,EACF;AACD;AAGA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leitwerk-dev/git-ssh",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"build": "tsup --silent"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@leitwerk-dev/process-sdk": "0.
|
|
28
|
+
"@leitwerk-dev/process-sdk": "0.2.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"tsup": "^8.5.1"
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
|
-
"node": ">=
|
|
38
|
+
"node": ">=26 <27"
|
|
39
39
|
},
|
|
40
40
|
"files": [
|
|
41
41
|
"dist/**",
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,111 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
1
6
|
import {
|
|
2
7
|
coreHostCapabilities,
|
|
8
|
+
createCapabilityToken,
|
|
3
9
|
type GitSshCredentialMaterial,
|
|
4
10
|
type LeitwerkExtensionModule,
|
|
5
11
|
} from "@leitwerk-dev/process-sdk";
|
|
6
12
|
|
|
13
|
+
const execFileAsync = promisify(execFile);
|
|
14
|
+
|
|
7
15
|
export const manifest = { id: "git-ssh", version: "0.1.0" } as const;
|
|
8
16
|
|
|
17
|
+
export interface GitSshAuthorizationPreflightInput {
|
|
18
|
+
credentialRef: string;
|
|
19
|
+
repoLocator: string;
|
|
20
|
+
baseBranch: string;
|
|
21
|
+
requireWrite: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type GitSshAuthorizationPreflightResult =
|
|
25
|
+
| { ok: true }
|
|
26
|
+
| { ok: false; access: "read" | "write"; detail: string };
|
|
27
|
+
|
|
28
|
+
export interface GitSshIntegration {
|
|
29
|
+
profiles(): readonly string[];
|
|
30
|
+
preflight(input: GitSshAuthorizationPreflightInput): Promise<GitSshAuthorizationPreflightResult>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const gitSshIntegration = createCapabilityToken<GitSshIntegration>(
|
|
34
|
+
"@leitwerk-dev/git-ssh.integration",
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
function gitFailureDetail(error: unknown): string {
|
|
38
|
+
if (!error || typeof error !== "object") return "Git SSH access check failed";
|
|
39
|
+
const value = error as { stderr?: unknown; stdout?: unknown; message?: unknown };
|
|
40
|
+
const output =
|
|
41
|
+
typeof value.stderr === "string" && value.stderr.trim()
|
|
42
|
+
? value.stderr
|
|
43
|
+
: typeof value.stdout === "string" && value.stdout.trim()
|
|
44
|
+
? value.stdout
|
|
45
|
+
: typeof value.message === "string"
|
|
46
|
+
? value.message
|
|
47
|
+
: "Git SSH access check failed";
|
|
48
|
+
return output.replace(/\s+/g, " ").trim().slice(0, 500);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function preflightGitSshAccess(
|
|
52
|
+
input: Omit<GitSshAuthorizationPreflightInput, "credentialRef">,
|
|
53
|
+
material: GitSshCredentialMaterial,
|
|
54
|
+
gitBinary = "git",
|
|
55
|
+
): Promise<GitSshAuthorizationPreflightResult> {
|
|
56
|
+
const directory = await mkdtemp(join(tmpdir(), "leitwerk-git-ssh-preflight-"));
|
|
57
|
+
try {
|
|
58
|
+
const keyPath = join(directory, "identity");
|
|
59
|
+
const knownHostsPath = join(directory, "known_hosts");
|
|
60
|
+
const sshPath = join(directory, "ssh");
|
|
61
|
+
await Promise.all([
|
|
62
|
+
writeFile(keyPath, `${material.privateKey.trim()}\n`, { mode: 0o600 }),
|
|
63
|
+
writeFile(knownHostsPath, `${material.knownHosts.trim()}\n`, { mode: 0o600 }),
|
|
64
|
+
writeFile(
|
|
65
|
+
sshPath,
|
|
66
|
+
`#!/bin/sh\nexec ssh -F /dev/null -i "${keyPath}" -o IdentitiesOnly=yes -o UserKnownHostsFile="${knownHostsPath}" -o StrictHostKeyChecking=yes -o BatchMode=yes "$@"\n`,
|
|
67
|
+
{ mode: 0o700 },
|
|
68
|
+
),
|
|
69
|
+
]);
|
|
70
|
+
const env = {
|
|
71
|
+
...process.env,
|
|
72
|
+
GIT_SSH: sshPath,
|
|
73
|
+
// This also overrides ambient core.sshCommand, which takes precedence over GIT_SSH.
|
|
74
|
+
GIT_SSH_COMMAND: `'${sshPath.replaceAll("'", "'\\''")}'`,
|
|
75
|
+
SSH_AUTH_SOCK: "",
|
|
76
|
+
};
|
|
77
|
+
const ref = `refs/heads/${input.baseBranch}`;
|
|
78
|
+
try {
|
|
79
|
+
await execFileAsync(gitBinary, ["ls-remote", "--exit-code", input.repoLocator, ref], {
|
|
80
|
+
env,
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return { ok: false, access: "read", detail: gitFailureDetail(error) };
|
|
84
|
+
}
|
|
85
|
+
if (!input.requireWrite) return { ok: true };
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
await execFileAsync(gitBinary, ["init", "--quiet", directory], { env });
|
|
89
|
+
await execFileAsync(
|
|
90
|
+
gitBinary,
|
|
91
|
+
["-C", directory, "fetch", "--quiet", "--depth=1", input.repoLocator, ref],
|
|
92
|
+
{ env },
|
|
93
|
+
);
|
|
94
|
+
const probeBranch = `refs/heads/leitwerk/preflight-${process.pid}-${Date.now()}`;
|
|
95
|
+
await execFileAsync(
|
|
96
|
+
gitBinary,
|
|
97
|
+
["-C", directory, "push", "--dry-run", input.repoLocator, `FETCH_HEAD:${probeBranch}`],
|
|
98
|
+
{ env },
|
|
99
|
+
);
|
|
100
|
+
return { ok: true };
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return { ok: false, access: "write", detail: gitFailureDetail(error) };
|
|
103
|
+
}
|
|
104
|
+
} finally {
|
|
105
|
+
await rm(directory, { recursive: true, force: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
9
109
|
function parseProfiles(raw: unknown): Map<string, GitSshCredentialMaterial> {
|
|
10
110
|
const root = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {};
|
|
11
111
|
const credentials =
|
|
@@ -51,6 +151,20 @@ const extension: LeitwerkExtensionModule = {
|
|
|
51
151
|
kind: "git_ssh",
|
|
52
152
|
resolve: (ref) => profiles.get(ref) ?? null,
|
|
53
153
|
});
|
|
154
|
+
api.provide(gitSshIntegration, {
|
|
155
|
+
profiles: () => [...profiles.keys()].sort(),
|
|
156
|
+
async preflight(input) {
|
|
157
|
+
const material = profiles.get(input.credentialRef);
|
|
158
|
+
if (!material) {
|
|
159
|
+
return {
|
|
160
|
+
ok: false,
|
|
161
|
+
access: "read",
|
|
162
|
+
detail: `Unknown git_ssh credential '${input.credentialRef}'`,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return preflightGitSshAccess(input, material);
|
|
166
|
+
},
|
|
167
|
+
});
|
|
54
168
|
},
|
|
55
169
|
};
|
|
56
170
|
|