@leed-ai/git-gateway-worker 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +49 -2
- package/dist/audit/audit-recorder.d.ts +2 -0
- package/dist/audit/audit-recorder.js +53 -0
- package/dist/audit/audit-recorder.js.map +1 -0
- package/dist/audit/push-report.d.ts +13 -0
- package/dist/audit/push-report.js +66 -0
- package/dist/audit/push-report.js.map +1 -0
- package/dist/audit-recorder-K1Ns3tfC.d.ts +31 -0
- package/dist/auth/authenticate.d.ts +14 -0
- package/dist/auth/authenticate.js +59 -0
- package/dist/auth/authenticate.js.map +1 -0
- package/dist/auth/authorize.d.ts +12 -0
- package/dist/auth/authorize.js +57 -0
- package/dist/auth/authorize.js.map +1 -0
- package/dist/auth/read-token-auth.d.ts +60 -0
- package/dist/auth/read-token-auth.js +83 -0
- package/dist/auth/read-token-auth.js.map +1 -0
- package/dist/cache/decision-cache.d.ts +2 -0
- package/dist/cache/decision-cache.js +45 -0
- package/dist/cache/decision-cache.js.map +1 -0
- package/dist/commit-workflow-schemas-BWlg7bZR.d.ts +95 -0
- package/dist/decision-cache-87U32iSj.d.ts +24 -0
- package/dist/gateway-app.d.ts +69 -0
- package/dist/gateway-app.js +80 -0
- package/dist/gateway-app.js.map +1 -0
- package/dist/gateway-context-Boh9I0mk.d.ts +54 -0
- package/dist/gateway-env-DnsBU4Pq.d.ts +40 -0
- package/dist/gateway-worker.d.ts +13 -0
- package/dist/gateway-worker.js +22 -0
- package/dist/gateway-worker.js.map +1 -0
- package/dist/git-route-Spxi09Rm.d.ts +22 -0
- package/dist/health.d.ts +6 -0
- package/dist/health.js +16 -0
- package/dist/health.js.map +1 -0
- package/dist/logger-BZ1zyKLb.d.ts +27 -0
- package/dist/logging/logger.d.ts +2 -0
- package/dist/logging/logger.js +15 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/on-push-DTREOB__.d.ts +62 -0
- package/dist/policy/branch-policy.d.ts +24 -0
- package/dist/policy/branch-policy.js +125 -0
- package/dist/policy/branch-policy.js.map +1 -0
- package/dist/policy/enforce-branch-policy.d.ts +30 -0
- package/dist/policy/enforce-branch-policy.js +214 -0
- package/dist/policy/enforce-branch-policy.js.map +1 -0
- package/dist/policy/fast-forward.d.ts +32 -0
- package/dist/policy/fast-forward.js +95 -0
- package/dist/policy/fast-forward.js.map +1 -0
- package/dist/policy/ref-pattern.d.ts +7 -0
- package/dist/policy/ref-pattern.js +25 -0
- package/dist/policy/ref-pattern.js.map +1 -0
- package/dist/policy/rule-branch-policy.d.ts +17 -0
- package/dist/policy/rule-branch-policy.js +69 -0
- package/dist/policy/rule-branch-policy.js.map +1 -0
- package/dist/proxy/defer.d.ts +8 -0
- package/dist/proxy/defer.js +15 -0
- package/dist/proxy/defer.js.map +1 -0
- package/dist/proxy/forward-read.d.ts +11 -0
- package/dist/proxy/forward-read.js +53 -0
- package/dist/proxy/forward-read.js.map +1 -0
- package/dist/proxy/gateway-deps.d.ts +39 -0
- package/dist/proxy/gateway-deps.js +0 -0
- package/dist/proxy/gateway-env.d.ts +2 -0
- package/dist/proxy/gateway-env.js +0 -0
- package/dist/proxy/git-responses.d.ts +25 -0
- package/dist/proxy/git-responses.js +83 -0
- package/dist/proxy/git-responses.js.map +1 -0
- package/dist/proxy/git-route.d.ts +2 -0
- package/dist/proxy/git-route.js +34 -0
- package/dist/proxy/git-route.js.map +1 -0
- package/dist/proxy/push-events.d.ts +28 -0
- package/dist/proxy/push-events.js +65 -0
- package/dist/proxy/push-events.js.map +1 -0
- package/dist/proxy/push-guard.d.ts +43 -0
- package/dist/proxy/push-guard.js +81 -0
- package/dist/proxy/push-guard.js.map +1 -0
- package/dist/proxy/push-options.d.ts +16 -0
- package/dist/proxy/push-options.js +33 -0
- package/dist/proxy/push-options.js.map +1 -0
- package/dist/proxy/receive-pack.d.ts +17 -0
- package/dist/proxy/receive-pack.js +244 -0
- package/dist/proxy/receive-pack.js.map +1 -0
- package/dist/proxy/request-log.d.ts +13 -0
- package/dist/proxy/request-log.js +41 -0
- package/dist/proxy/request-log.js.map +1 -0
- package/dist/proxy/resolve-route.d.ts +9 -0
- package/dist/proxy/resolve-route.js +22 -0
- package/dist/proxy/resolve-route.js.map +1 -0
- package/dist/proxy/upstream-call.d.ts +23 -0
- package/dist/proxy/upstream-call.js +63 -0
- package/dist/proxy/upstream-call.js.map +1 -0
- package/dist/proxy/upstream.d.ts +2 -0
- package/dist/proxy/upstream.js +68 -0
- package/dist/proxy/upstream.js.map +1 -0
- package/dist/push-event-f-5TsFUY.d.ts +15 -0
- package/dist/push-policy/changed-paths.d.ts +26 -0
- package/dist/push-policy/changed-paths.js +117 -0
- package/dist/push-policy/changed-paths.js.map +1 -0
- package/dist/push-policy/pushed-objects.d.ts +30 -0
- package/dist/push-policy/pushed-objects.js +76 -0
- package/dist/push-policy/pushed-objects.js.map +1 -0
- package/dist/push-policy/run-push-policy.d.ts +35 -0
- package/dist/push-policy/run-push-policy.js +77 -0
- package/dist/push-policy/run-push-policy.js.map +1 -0
- package/dist/push-policy/tree-diff.d.ts +21 -0
- package/dist/push-policy/tree-diff.js +78 -0
- package/dist/push-policy/tree-diff.js.map +1 -0
- package/dist/push-policy/uncheckable.d.ts +14 -0
- package/dist/push-policy/uncheckable.js +25 -0
- package/dist/push-policy/uncheckable.js.map +1 -0
- package/dist/rpc/branch-methods.d.ts +16 -0
- package/dist/rpc/branch-methods.js +43 -0
- package/dist/rpc/branch-methods.js.map +1 -0
- package/dist/rpc/commit-attempt.d.ts +27 -0
- package/dist/rpc/commit-attempt.js +85 -0
- package/dist/rpc/commit-attempt.js.map +1 -0
- package/dist/rpc/commit-graph.d.ts +20 -0
- package/dist/rpc/commit-graph.js +38 -0
- package/dist/rpc/commit-graph.js.map +1 -0
- package/dist/rpc/commit-method.d.ts +13 -0
- package/dist/rpc/commit-method.js +30 -0
- package/dist/rpc/commit-method.js.map +1 -0
- package/dist/rpc/commit-source.d.ts +12 -0
- package/dist/rpc/commit-source.js +9 -0
- package/dist/rpc/commit-source.js.map +1 -0
- package/dist/rpc/gateway-context.d.ts +2 -0
- package/dist/rpc/gateway-context.js +30 -0
- package/dist/rpc/gateway-context.js.map +1 -0
- package/dist/rpc/gateway-rpc.d.ts +10 -0
- package/dist/rpc/gateway-rpc.js +48 -0
- package/dist/rpc/gateway-rpc.js.map +1 -0
- package/dist/rpc/gateway-service.d.ts +24 -0
- package/dist/rpc/gateway-service.js +63 -0
- package/dist/rpc/gateway-service.js.map +1 -0
- package/dist/rpc/history-page.d.ts +35 -0
- package/dist/rpc/history-page.js +122 -0
- package/dist/rpc/history-page.js.map +1 -0
- package/dist/rpc/lifecycle-methods.d.ts +12 -0
- package/dist/rpc/lifecycle-methods.js +39 -0
- package/dist/rpc/lifecycle-methods.js.map +1 -0
- package/dist/rpc/paint-down.d.ts +17 -0
- package/dist/rpc/paint-down.js +96 -0
- package/dist/rpc/paint-down.js.map +1 -0
- package/dist/rpc/promote-attempt.d.ts +21 -0
- package/dist/rpc/promote-attempt.js +55 -0
- package/dist/rpc/promote-attempt.js.map +1 -0
- package/dist/rpc/push-ref.d.ts +21 -0
- package/dist/rpc/push-ref.js +47 -0
- package/dist/rpc/push-ref.js.map +1 -0
- package/dist/rpc/read-methods.d.ts +17 -0
- package/dist/rpc/read-methods.js +94 -0
- package/dist/rpc/read-methods.js.map +1 -0
- package/dist/rpc/read-token.d.ts +39 -0
- package/dist/rpc/read-token.js +107 -0
- package/dist/rpc/read-token.js.map +1 -0
- package/dist/rpc/repo-access.d.ts +36 -0
- package/dist/rpc/repo-access.js +75 -0
- package/dist/rpc/repo-access.js.map +1 -0
- package/dist/rpc/sync-attempt.d.ts +23 -0
- package/dist/rpc/sync-attempt.js +105 -0
- package/dist/rpc/sync-attempt.js.map +1 -0
- package/dist/sync-workflow-schemas-DCNmNE5-.d.ts +57 -0
- package/dist/upstream-DvqMCj5N.d.ts +15 -0
- package/dist/workflow-starter-BqhwX6oA.d.ts +45 -0
- package/dist/workflows/commit-workflow-schemas.d.ts +2 -0
- package/dist/workflows/commit-workflow-schemas.js +84 -0
- package/dist/workflows/commit-workflow-schemas.js.map +1 -0
- package/dist/workflows/commit-workflow.d.ts +16 -0
- package/dist/workflows/commit-workflow.js +22 -0
- package/dist/workflows/commit-workflow.js.map +1 -0
- package/dist/workflows/on-push.d.ts +2 -0
- package/dist/workflows/on-push.js +90 -0
- package/dist/workflows/on-push.js.map +1 -0
- package/dist/workflows/push-event.d.ts +2 -0
- package/dist/workflows/push-event.js +28 -0
- package/dist/workflows/push-event.js.map +1 -0
- package/dist/workflows/run-commit-workflow.d.ts +15 -0
- package/dist/workflows/run-commit-workflow.js +49 -0
- package/dist/workflows/run-commit-workflow.js.map +1 -0
- package/dist/workflows/run-sync-workflow.d.ts +14 -0
- package/dist/workflows/run-sync-workflow.js +67 -0
- package/dist/workflows/run-sync-workflow.js.map +1 -0
- package/dist/workflows/step-errors.d.ts +15 -0
- package/dist/workflows/step-errors.js +24 -0
- package/dist/workflows/step-errors.js.map +1 -0
- package/dist/workflows/step-runner.d.ts +31 -0
- package/dist/workflows/step-runner.js +6 -0
- package/dist/workflows/step-runner.js.map +1 -0
- package/dist/workflows/sync-branch-workflow.d.ts +15 -0
- package/dist/workflows/sync-branch-workflow.js +21 -0
- package/dist/workflows/sync-branch-workflow.js.map +1 -0
- package/dist/workflows/sync-workflow-schemas.d.ts +2 -0
- package/dist/workflows/sync-workflow-schemas.js +104 -0
- package/dist/workflows/sync-workflow-schemas.js.map +1 -0
- package/dist/workflows/workflow-instance-id.d.ts +12 -0
- package/dist/workflows/workflow-instance-id.js +33 -0
- package/dist/workflows/workflow-instance-id.js.map +1 -0
- package/dist/workflows/workflow-runtime.d.ts +7 -0
- package/dist/workflows/workflow-runtime.js +11 -0
- package/dist/workflows/workflow-runtime.js.map +1 -0
- package/dist/workflows/workflow-starter.d.ts +2 -0
- package/dist/workflows/workflow-starter.js +40 -0
- package/dist/workflows/workflow-starter.js.map +1 -0
- package/package.json +381 -4
- package/src/audit/audit-recorder.ts +74 -0
- package/src/audit/push-report.ts +60 -0
- package/src/auth/authenticate.ts +68 -0
- package/src/auth/authorize.ts +57 -0
- package/src/auth/read-token-auth.ts +119 -0
- package/src/cache/decision-cache.ts +57 -0
- package/src/gateway-app.ts +147 -0
- package/src/gateway-worker.ts +32 -0
- package/src/health.ts +12 -0
- package/src/logging/logger.ts +40 -0
- package/src/policy/branch-policy.ts +119 -0
- package/src/policy/enforce-branch-policy.ts +213 -0
- package/src/policy/fast-forward.ts +136 -0
- package/src/policy/ref-pattern.ts +24 -0
- package/src/policy/rule-branch-policy.ts +76 -0
- package/src/proxy/defer.ts +18 -0
- package/src/proxy/forward-read.ts +52 -0
- package/src/proxy/gateway-deps.ts +43 -0
- package/src/proxy/gateway-env.ts +43 -0
- package/src/proxy/git-responses.ts +89 -0
- package/src/proxy/git-route.ts +49 -0
- package/src/proxy/push-events.ts +91 -0
- package/src/proxy/push-guard.ts +109 -0
- package/src/proxy/push-options.ts +38 -0
- package/src/proxy/receive-pack.ts +257 -0
- package/src/proxy/request-log.ts +44 -0
- package/src/proxy/resolve-route.ts +24 -0
- package/src/proxy/upstream-call.ts +71 -0
- package/src/proxy/upstream.ts +76 -0
- package/src/push-policy/changed-paths.ts +143 -0
- package/src/push-policy/pushed-objects.ts +88 -0
- package/src/push-policy/run-push-policy.ts +99 -0
- package/src/push-policy/tree-diff.ts +107 -0
- package/src/push-policy/uncheckable.ts +27 -0
- package/src/rpc/branch-methods.ts +57 -0
- package/src/rpc/commit-attempt.ts +87 -0
- package/src/rpc/commit-graph.ts +44 -0
- package/src/rpc/commit-method.ts +36 -0
- package/src/rpc/commit-source.ts +15 -0
- package/src/rpc/gateway-context.ts +77 -0
- package/src/rpc/gateway-rpc.ts +65 -0
- package/src/rpc/gateway-service.ts +85 -0
- package/src/rpc/history-page.ts +163 -0
- package/src/rpc/lifecycle-methods.ts +50 -0
- package/src/rpc/paint-down.ts +120 -0
- package/src/rpc/promote-attempt.ts +52 -0
- package/src/rpc/push-ref.ts +54 -0
- package/src/rpc/read-methods.ts +111 -0
- package/src/rpc/read-token.ts +135 -0
- package/src/rpc/repo-access.ts +112 -0
- package/src/rpc/sync-attempt.ts +84 -0
- package/src/workflows/commit-workflow-schemas.ts +95 -0
- package/src/workflows/commit-workflow.ts +39 -0
- package/src/workflows/on-push.ts +128 -0
- package/src/workflows/push-event.ts +41 -0
- package/src/workflows/run-commit-workflow.ts +45 -0
- package/src/workflows/run-sync-workflow.ts +52 -0
- package/src/workflows/step-errors.ts +26 -0
- package/src/workflows/step-runner.ts +36 -0
- package/src/workflows/sync-branch-workflow.ts +40 -0
- package/src/workflows/sync-workflow-schemas.ts +122 -0
- package/src/workflows/workflow-instance-id.ts +26 -0
- package/src/workflows/workflow-runtime.ts +13 -0
- package/src/workflows/workflow-starter.ts +69 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { isCoreError } from '@leed-ai/git-gateway-core/core-error';
|
|
3
|
+
import { PktLineReader } from '@leed-ai/git-gateway-core/pkt-line/pkt-line-reader';
|
|
4
|
+
import { parseReceivePackRequest } from '@leed-ai/git-gateway-core/receive-pack/parse-request';
|
|
5
|
+
import type { ReceivePackRequest } from '@leed-ai/git-gateway-core/schemas/receive-pack';
|
|
6
|
+
import type { MiddlewareHandler } from 'hono';
|
|
7
|
+
import { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
|
|
8
|
+
import { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
9
|
+
|
|
10
|
+
import { rejectedOutcomes, unknownOutcomes } from '../audit/push-report.ts';
|
|
11
|
+
import { deferWith } from '../proxy/defer.ts';
|
|
12
|
+
import type { GatewayDeps } from '../proxy/gateway-deps.ts';
|
|
13
|
+
import type { GatewayEnv } from '../proxy/gateway-env.ts';
|
|
14
|
+
import { errResponse, NO_CACHE, refusePush, textResponse, type RefRefusal } from '../proxy/git-responses.ts';
|
|
15
|
+
import { disallowedPushOptions, pushOptionsReason } from '../proxy/push-options.ts';
|
|
16
|
+
import { judgeWithPushPolicy, pushPolicyNeeds } from '../push-policy/run-push-policy.ts';
|
|
17
|
+
import { judgePush, reasonLine } from './branch-policy.ts';
|
|
18
|
+
|
|
19
|
+
/** The reason given for an update refused only because another update of the same push was. */
|
|
20
|
+
export const COLLATERAL_REASON = 'not updated: another update in this push was refused';
|
|
21
|
+
|
|
22
|
+
/** Most bytes of a refused push read and discarded before it is answered. */
|
|
23
|
+
export const MAX_DRAIN_BYTES = 16 * 1024 * 1024;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Reads and discards what remains of a refused push, so a client that is still sending gets its
|
|
27
|
+
* answer instead of a reset connection — but at most `limit` bytes, after which the rest is
|
|
28
|
+
* cancelled. Read errors (a client that hung up) are swallowed: the answer is already decided.
|
|
29
|
+
*/
|
|
30
|
+
export async function drain(stream: ReadableStream<Uint8Array>, limit = MAX_DRAIN_BYTES): Promise<void> {
|
|
31
|
+
const reader = stream.getReader();
|
|
32
|
+
let read = 0;
|
|
33
|
+
try {
|
|
34
|
+
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
35
|
+
read += next.value.length;
|
|
36
|
+
if (read > limit) {
|
|
37
|
+
await reader.cancel();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
42
|
+
await reader.cancel().catch(() => undefined);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Every update of a push refused: the given refusals, and the collateral reason for the rest. */
|
|
47
|
+
export function refuseAll(request: ReceivePackRequest, refusals: readonly RefRefusal[]): RefRefusal[] {
|
|
48
|
+
const byRef = new Map(refusals.map((refusal) => [refusal.ref, refusal.reason]));
|
|
49
|
+
return request.commands.map((command) => ({
|
|
50
|
+
ref: command.ref,
|
|
51
|
+
reason: byRef.get(command.ref) ?? COLLATERAL_REASON,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The request body as receive-pack bytes: gunzipped when the client compressed it, and null for
|
|
57
|
+
* git's auth probe (a body of just a flush packet, sent before a large chunked push).
|
|
58
|
+
*/
|
|
59
|
+
async function receivePackBody(request: Request): Promise<ReadableStream<Uint8Array> | null> {
|
|
60
|
+
const raw = request.body ?? new Blob([]).stream();
|
|
61
|
+
const body =
|
|
62
|
+
request.headers.get('Content-Encoding') === 'gzip'
|
|
63
|
+
? raw.pipeThrough(new DecompressionStream('gzip'))
|
|
64
|
+
: raw;
|
|
65
|
+
const reader = new PktLineReader(body);
|
|
66
|
+
const first = await reader.read();
|
|
67
|
+
if (first?.type === 'flush' && (await reader.read()) === null) return null;
|
|
68
|
+
return reader.replay();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Branch-policy middleware for git-receive-pack: reads only the leading update commands of the
|
|
73
|
+
* push (never the pack), refuses a push carrying a push option outside
|
|
74
|
+
* `SmartHttpConfig.allowedPushOptions` (the storage host would act on it with the gateway's own
|
|
75
|
+
* credential), asks the BranchPolicy about every ref, and proves fast-forwards it can
|
|
76
|
+
* from storage, then runs the push policy (when there is one) — at once when it needs no changed
|
|
77
|
+
* paths, or after the pack when it does. A push is forwarded only when every update is allowed —
|
|
78
|
+
* otherwise the client is answered in-protocol ("ng <ref> <reason>" per ref) and nothing reaches storage. Updates whose
|
|
79
|
+
* ancestry depends on commits in the pack are left for the receive-pack handler to prove as the
|
|
80
|
+
* pack streams past.
|
|
81
|
+
*/
|
|
82
|
+
export function enforceBranchPolicy(deps: GatewayDeps): MiddlewareHandler<GatewayEnv> {
|
|
83
|
+
return async (context, next) => {
|
|
84
|
+
const requestId = context.get('requestId');
|
|
85
|
+
const principal = context.get('principal');
|
|
86
|
+
const git = context.get('git');
|
|
87
|
+
const defer = deferWith(context);
|
|
88
|
+
let parsed;
|
|
89
|
+
try {
|
|
90
|
+
const body = await receivePackBody(context.req.raw);
|
|
91
|
+
if (body === null) {
|
|
92
|
+
return new Response(null, {
|
|
93
|
+
status: 200,
|
|
94
|
+
headers: {
|
|
95
|
+
'Content-Type': 'application/x-git-receive-pack-result',
|
|
96
|
+
...NO_CACHE,
|
|
97
|
+
'X-Request-Id': requestId,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
parsed = await parseReceivePackRequest(body, { maxHeadBytes: deps.config.maxPushHeadBytes });
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (!isCoreError(error)) throw error;
|
|
104
|
+
const reason = reasonLine(`malformed push: ${error.message}`);
|
|
105
|
+
deps.audit.record({ requestId, principal, git, outcome: 'denied', reason, refs: [] }, defer, requestId);
|
|
106
|
+
const response = errResponse(git.service, 'rpc', reason);
|
|
107
|
+
response.headers.set('X-Request-Id', requestId);
|
|
108
|
+
return response;
|
|
109
|
+
}
|
|
110
|
+
const refusedOptions = disallowedPushOptions(parsed.request, deps.config.allowedPushOptions);
|
|
111
|
+
if (refusedOptions.length > 0) {
|
|
112
|
+
const reason = reasonLine(pushOptionsReason(refusedOptions));
|
|
113
|
+
const refusals = parsed.request.commands.map((command) => ({ ref: command.ref, reason }));
|
|
114
|
+
deps.audit.record(
|
|
115
|
+
{
|
|
116
|
+
requestId,
|
|
117
|
+
principal,
|
|
118
|
+
git,
|
|
119
|
+
outcome: 'denied',
|
|
120
|
+
reason,
|
|
121
|
+
refs: rejectedOutcomes(parsed.request, new Map(), reason),
|
|
122
|
+
},
|
|
123
|
+
defer,
|
|
124
|
+
requestId,
|
|
125
|
+
);
|
|
126
|
+
await drain(parsed.body);
|
|
127
|
+
deps.logger.log({
|
|
128
|
+
level: 'info',
|
|
129
|
+
message: 'push refused for its push options',
|
|
130
|
+
requestId,
|
|
131
|
+
repo: git.repo,
|
|
132
|
+
reason,
|
|
133
|
+
});
|
|
134
|
+
const response = refusePush(parsed.request.capabilities, refusals);
|
|
135
|
+
response.headers.set('X-Request-Id', requestId);
|
|
136
|
+
return response;
|
|
137
|
+
}
|
|
138
|
+
/** Answers 503 when a policy hook failed; nothing reaches storage. */
|
|
139
|
+
const unavailable = async (error: unknown, what: string): Promise<Response> => {
|
|
140
|
+
deps.logger.log({
|
|
141
|
+
level: 'error',
|
|
142
|
+
message: `${what} failed`,
|
|
143
|
+
requestId,
|
|
144
|
+
repo: git.repo,
|
|
145
|
+
error: error instanceof Error ? error.message : String(error),
|
|
146
|
+
});
|
|
147
|
+
const reason = `The ${what} is unavailable; try again later`;
|
|
148
|
+
deps.audit.record(
|
|
149
|
+
{ requestId, principal, git, outcome: 'failed', reason, refs: unknownOutcomes(parsed.request) },
|
|
150
|
+
defer,
|
|
151
|
+
requestId,
|
|
152
|
+
);
|
|
153
|
+
await drain(parsed.body);
|
|
154
|
+
return textResponse(503, reason, { 'X-Request-Id': requestId });
|
|
155
|
+
};
|
|
156
|
+
/** Refuses the whole push in-protocol: the given refusals, and the collateral reason for the rest. */
|
|
157
|
+
const refuse = async (given: readonly RefRefusal[], by: string): Promise<Response> => {
|
|
158
|
+
const refusals = refuseAll(parsed.request, given);
|
|
159
|
+
const reasons = new Map(refusals.map((refusal) => [refusal.ref, refusal.reason]));
|
|
160
|
+
const reason = given[0]?.reason ?? COLLATERAL_REASON;
|
|
161
|
+
deps.audit.record(
|
|
162
|
+
{
|
|
163
|
+
requestId,
|
|
164
|
+
principal,
|
|
165
|
+
git,
|
|
166
|
+
outcome: 'denied',
|
|
167
|
+
reason,
|
|
168
|
+
refs: rejectedOutcomes(parsed.request, reasons, COLLATERAL_REASON),
|
|
169
|
+
},
|
|
170
|
+
defer,
|
|
171
|
+
requestId,
|
|
172
|
+
);
|
|
173
|
+
await drain(parsed.body);
|
|
174
|
+
deps.logger.log({ level: 'info', message: `push refused by ${by}`, requestId, repo: git.repo, reason });
|
|
175
|
+
const response = refusePush(parsed.request.capabilities, refusals);
|
|
176
|
+
response.headers.set('X-Request-Id', requestId);
|
|
177
|
+
return response;
|
|
178
|
+
};
|
|
179
|
+
let verdict;
|
|
180
|
+
try {
|
|
181
|
+
verdict = await judgePush(deps, git, principal, parsed.request);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
return unavailable(error, 'branch policy');
|
|
184
|
+
}
|
|
185
|
+
if (verdict.refusals.length > 0) return refuse(verdict.refusals, 'branch policy');
|
|
186
|
+
let policyUpdates: readonly RefUpdate[] | null = null;
|
|
187
|
+
const { pushPolicy } = deps;
|
|
188
|
+
if (pushPolicy !== undefined) {
|
|
189
|
+
const updates = parsed.request.commands.map((command) => RefUpdate.parse(command));
|
|
190
|
+
const push = { principal, repo: git.repo, updates };
|
|
191
|
+
const policyDeps = { pushPolicy, backend: deps.backend, config: deps.config };
|
|
192
|
+
let refusals: readonly RefRefusal[] = [];
|
|
193
|
+
try {
|
|
194
|
+
const needs = await pushPolicyNeeds(policyDeps, push);
|
|
195
|
+
// Changed paths come from the pack, so the check waits for it; a push of deletions only
|
|
196
|
+
// brings no commits and is checked now.
|
|
197
|
+
if (needs.changedPaths && updates.some((update) => update.newSha !== ZERO_SHA))
|
|
198
|
+
policyUpdates = updates;
|
|
199
|
+
else
|
|
200
|
+
refusals = await judgeWithPushPolicy(
|
|
201
|
+
policyDeps,
|
|
202
|
+
push,
|
|
203
|
+
updates.map(() => (needs.changedPaths ? [] : null)),
|
|
204
|
+
);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
return unavailable(error, 'push policy');
|
|
207
|
+
}
|
|
208
|
+
if (refusals.length > 0) return refuse(refusals, 'push policy');
|
|
209
|
+
}
|
|
210
|
+
context.set('push', { ...parsed, pending: verdict.pending, policyUpdates });
|
|
211
|
+
return next();
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
|
|
3
|
+
|
|
4
|
+
/** Whether an update is a fast-forward: "yes", "no", or "unknown" when it cannot be shown either way. */
|
|
5
|
+
export type FastForwardVerdict = 'yes' | 'no' | 'unknown';
|
|
6
|
+
|
|
7
|
+
/** Where the ancestry walk finds commits. */
|
|
8
|
+
export interface AncestrySources {
|
|
9
|
+
/** The repository the push targets. */
|
|
10
|
+
readonly repo: string;
|
|
11
|
+
readonly backend: Pick<StorageBackend, 'readCommit' | 'isAncestor'>;
|
|
12
|
+
/** Commits in the pushed pack (not yet in storage), by id, with their parents. */
|
|
13
|
+
readonly packCommits?: ReadonlyMap<string, readonly string[]>;
|
|
14
|
+
/**
|
|
15
|
+
* Most calls made to the backend before giving up with "unknown": commits read by the walk, or
|
|
16
|
+
* `isAncestor` questions when the backend answers them.
|
|
17
|
+
*/
|
|
18
|
+
readonly maxReads: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Decides whether moving a ref from `oldSha` to `newSha` is a fast-forward — whether `oldSha` is
|
|
23
|
+
* `newSha` or one of its ancestors.
|
|
24
|
+
*
|
|
25
|
+
* When the backend offers `isAncestor`, the commits of the pushed pack are walked from `newSha`
|
|
26
|
+
* and the backend is asked once for each commit the walk leaves the pack at (the commits storage
|
|
27
|
+
* already has): "yes" when `oldSha` is one of the pack commits or an ancestor of any of those,
|
|
28
|
+
* "no" when it is neither. Otherwise parents are walked breadth-first from `newSha`, taking each
|
|
29
|
+
* commit from the pushed pack when it is there and from the backend otherwise, until `oldSha` is
|
|
30
|
+
* found ("yes") or the history is exhausted ("no").
|
|
31
|
+
*
|
|
32
|
+
* A commit found in neither place, a backend failure, or more than `maxReads` backend calls gives
|
|
33
|
+
* "unknown": the caller cannot prove the update safe and must treat it as a force push.
|
|
34
|
+
*/
|
|
35
|
+
export async function proveFastForward(
|
|
36
|
+
oldSha: string,
|
|
37
|
+
newSha: string,
|
|
38
|
+
sources: AncestrySources,
|
|
39
|
+
): Promise<FastForwardVerdict> {
|
|
40
|
+
if (oldSha === newSha) return 'yes';
|
|
41
|
+
const { backend, repo } = sources;
|
|
42
|
+
if (backend.isAncestor === undefined) return walkHistory(oldSha, newSha, sources);
|
|
43
|
+
return askBackend(
|
|
44
|
+
oldSha,
|
|
45
|
+
newSha,
|
|
46
|
+
sources,
|
|
47
|
+
async (ancestor, descendant) => backend.isAncestor?.(repo, ancestor, descendant) ?? null,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Walks the pack's commits from `newSha`; returns "yes" on meeting `oldSha`, else where the walk left the pack. */
|
|
52
|
+
function walkPack(
|
|
53
|
+
oldSha: string,
|
|
54
|
+
newSha: string,
|
|
55
|
+
packCommits: ReadonlyMap<string, readonly string[]> | undefined,
|
|
56
|
+
): 'yes' | string[] {
|
|
57
|
+
const seen = new Set<string>([newSha]);
|
|
58
|
+
const stored: string[] = [];
|
|
59
|
+
let frontier = [newSha];
|
|
60
|
+
while (frontier.length > 0) {
|
|
61
|
+
const next: string[] = [];
|
|
62
|
+
for (const sha of frontier) {
|
|
63
|
+
const parents = packCommits?.get(sha);
|
|
64
|
+
if (parents === undefined) {
|
|
65
|
+
stored.push(sha);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
for (const parent of parents) {
|
|
69
|
+
if (parent === oldSha) return 'yes';
|
|
70
|
+
if (!seen.has(parent)) {
|
|
71
|
+
seen.add(parent);
|
|
72
|
+
next.push(parent);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
frontier = next;
|
|
77
|
+
}
|
|
78
|
+
return stored;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function askBackend(
|
|
82
|
+
oldSha: string,
|
|
83
|
+
newSha: string,
|
|
84
|
+
sources: AncestrySources,
|
|
85
|
+
isAncestor: (ancestor: string, descendant: string) => Promise<boolean | null>,
|
|
86
|
+
): Promise<FastForwardVerdict> {
|
|
87
|
+
const stored = walkPack(oldSha, newSha, sources.packCommits);
|
|
88
|
+
if (stored === 'yes') return 'yes';
|
|
89
|
+
if (stored.length > sources.maxReads) return 'unknown';
|
|
90
|
+
for (const sha of stored) {
|
|
91
|
+
let answer: boolean | null;
|
|
92
|
+
try {
|
|
93
|
+
answer = await isAncestor(oldSha, sha);
|
|
94
|
+
} catch {
|
|
95
|
+
return 'unknown';
|
|
96
|
+
}
|
|
97
|
+
if (answer === null) return 'unknown';
|
|
98
|
+
if (answer) return 'yes';
|
|
99
|
+
}
|
|
100
|
+
return 'no';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function walkHistory(
|
|
104
|
+
oldSha: string,
|
|
105
|
+
newSha: string,
|
|
106
|
+
sources: AncestrySources,
|
|
107
|
+
): Promise<FastForwardVerdict> {
|
|
108
|
+
const seen = new Set<string>([newSha]);
|
|
109
|
+
let frontier = [newSha];
|
|
110
|
+
let reads = 0;
|
|
111
|
+
while (frontier.length > 0) {
|
|
112
|
+
const next: string[] = [];
|
|
113
|
+
for (const sha of frontier) {
|
|
114
|
+
let parents = sources.packCommits?.get(sha);
|
|
115
|
+
if (parents === undefined) {
|
|
116
|
+
reads += 1;
|
|
117
|
+
if (reads > sources.maxReads) return 'unknown';
|
|
118
|
+
try {
|
|
119
|
+
parents = (await sources.backend.readCommit(sources.repo, sha))?.parents;
|
|
120
|
+
} catch {
|
|
121
|
+
return 'unknown';
|
|
122
|
+
}
|
|
123
|
+
if (parents === undefined) return 'unknown';
|
|
124
|
+
}
|
|
125
|
+
for (const parent of parents) {
|
|
126
|
+
if (parent === oldSha) return 'yes';
|
|
127
|
+
if (!seen.has(parent)) {
|
|
128
|
+
seen.add(parent);
|
|
129
|
+
next.push(parent);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
frontier = next;
|
|
134
|
+
}
|
|
135
|
+
return 'no';
|
|
136
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { RefPattern } from '@leed-ai/git-gateway-contract/proxy/branch-rule';
|
|
3
|
+
|
|
4
|
+
const SPECIAL = /[.+?^${}()|[\]\\]/g;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Compiles a {@link RefPattern} glob into a matcher over whole fully qualified refs: "**" matches
|
|
8
|
+
* any run of characters including "/", "*" any run inside one path component, everything else
|
|
9
|
+
* itself.
|
|
10
|
+
*/
|
|
11
|
+
export function compileRefPattern(pattern: string): (ref: string) => boolean {
|
|
12
|
+
const glob = RefPattern.parse(pattern);
|
|
13
|
+
let source = '';
|
|
14
|
+
for (let index = 0; index < glob.length; index += 1) {
|
|
15
|
+
const char = glob[index] ?? '';
|
|
16
|
+
if (char === '*' && glob[index + 1] === '*') {
|
|
17
|
+
source += '.*';
|
|
18
|
+
index += 1;
|
|
19
|
+
} else if (char === '*') source += '[^/]*';
|
|
20
|
+
else source += char.replace(SPECIAL, String.raw`\$&`);
|
|
21
|
+
}
|
|
22
|
+
const regex = new RegExp(`^${source}$`, 's');
|
|
23
|
+
return (ref) => regex.test(ref);
|
|
24
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import {
|
|
3
|
+
BranchPolicyConfig,
|
|
4
|
+
type BranchPermissions,
|
|
5
|
+
type BranchPolicyConfigInput,
|
|
6
|
+
type PrincipalSelector,
|
|
7
|
+
} from '@leed-ai/git-gateway-contract/proxy/branch-rule';
|
|
8
|
+
import type { BranchPolicy } from '@leed-ai/git-gateway-contract/proxy/gateway-hooks';
|
|
9
|
+
import type { PolicyResult, RefDecision } from '@leed-ai/git-gateway-contract/proxy/policy-result';
|
|
10
|
+
import type { Principal } from '@leed-ai/git-gateway-contract/proxy/principal';
|
|
11
|
+
import type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
|
|
12
|
+
import { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
13
|
+
|
|
14
|
+
import { compileRefPattern } from './ref-pattern.ts';
|
|
15
|
+
|
|
16
|
+
interface CompiledRule extends BranchPermissions {
|
|
17
|
+
readonly matches: (ref: string) => boolean;
|
|
18
|
+
readonly message: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Whether a principal is selected (a null selector selects everyone). */
|
|
22
|
+
function selects(selector: PrincipalSelector | null, principal: Principal): boolean {
|
|
23
|
+
if (selector === null) return true;
|
|
24
|
+
return (
|
|
25
|
+
selector.subjects.includes(principal.subject) ||
|
|
26
|
+
principal.roles.some((role) => selector.roles.includes(role))
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A short ref name for messages: "main" for refs/heads/main, "tag v1" for refs/tags/v1. */
|
|
31
|
+
function describeRef(ref: string): string {
|
|
32
|
+
if (ref.startsWith('refs/heads/')) return ref.slice('refs/heads/'.length);
|
|
33
|
+
return `tag ${ref.slice('refs/tags/'.length)}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The reference {@link BranchPolicy}: an ordered list of {@link BranchRule}s, first match wins,
|
|
38
|
+
* with a fallback for refs no rule matches. Each rule says who may push the refs it matches,
|
|
39
|
+
* whether they may be created or deleted, and whether force pushes are allowed; an update that is
|
|
40
|
+
* allowed but not forced comes back "fast-forward-only" for the gateway to prove.
|
|
41
|
+
*/
|
|
42
|
+
export class RuleBranchPolicy implements BranchPolicy {
|
|
43
|
+
readonly #rules: readonly CompiledRule[];
|
|
44
|
+
readonly #fallback: CompiledRule;
|
|
45
|
+
|
|
46
|
+
constructor(config: BranchPolicyConfigInput = {}) {
|
|
47
|
+
const parsed = BranchPolicyConfig.parse(config);
|
|
48
|
+
this.#rules = parsed.rules.map((rule) => ({ ...rule, matches: compileRefPattern(rule.pattern) }));
|
|
49
|
+
this.#fallback = { ...parsed.fallback, matches: () => true, message: null };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
check(principal: Principal, _repo: string, updates: readonly RefUpdate[]): PolicyResult {
|
|
53
|
+
return { refs: updates.map((update) => this.#decide(principal, update)) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#decide(principal: Principal, update: RefUpdate): RefDecision {
|
|
57
|
+
const rule = this.#rules.find((candidate) => candidate.matches(update.ref)) ?? this.#fallback;
|
|
58
|
+
const name = describeRef(update.ref);
|
|
59
|
+
const deny = (what: string): RefDecision => ({
|
|
60
|
+
ref: update.ref,
|
|
61
|
+
decision: 'deny',
|
|
62
|
+
reason: rule.message ?? `${what} ${name} is not allowed`,
|
|
63
|
+
});
|
|
64
|
+
if (!selects(rule.pushers, principal)) return deny('pushing to');
|
|
65
|
+
if (update.oldSha === ZERO_SHA)
|
|
66
|
+
return rule.create ? { ref: update.ref, decision: 'allow' } : deny('creating');
|
|
67
|
+
if (update.newSha === ZERO_SHA)
|
|
68
|
+
return rule.delete ? { ref: update.ref, decision: 'allow' } : deny('deleting');
|
|
69
|
+
if (rule.forcePush) return { ref: update.ref, decision: 'allow' };
|
|
70
|
+
return {
|
|
71
|
+
ref: update.ref,
|
|
72
|
+
decision: 'fast-forward-only',
|
|
73
|
+
reason: rule.message ?? `force-pushing ${name} is not allowed`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Context } from 'hono';
|
|
3
|
+
|
|
4
|
+
import type { Defer } from '../audit/audit-recorder.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Work to run after the response: handed to the runtime's `waitUntil` when the request has an
|
|
8
|
+
* execution context (in a Worker), otherwise left to run on its own. The work must not reject.
|
|
9
|
+
*/
|
|
10
|
+
export function deferWith(context: Context): Defer {
|
|
11
|
+
return (work) => {
|
|
12
|
+
try {
|
|
13
|
+
context.executionCtx.waitUntil(work);
|
|
14
|
+
} catch {
|
|
15
|
+
// No execution context (tests, Node): the promise runs to completion on its own.
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Handler } from 'hono';
|
|
3
|
+
|
|
4
|
+
import { deferWith } from './defer.ts';
|
|
5
|
+
import type { GatewayDeps } from './gateway-deps.ts';
|
|
6
|
+
import type { GatewayEnv } from './gateway-env.ts';
|
|
7
|
+
import { textResponse } from './git-responses.ts';
|
|
8
|
+
import { callUpstream, streamToClient } from './upstream-call.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Forwards an authorized request that writes nothing — either ref advertisement, or a fetch's
|
|
12
|
+
* git-upload-pack — to the storage host and streams the answer back. Request and response bodies
|
|
13
|
+
* stream through without being buffered or parsed, so protocol v2 (requested with the Git-Protocol
|
|
14
|
+
* header) passes through to a host that speaks it.
|
|
15
|
+
*/
|
|
16
|
+
export function forwardRead(deps: GatewayDeps): Handler<GatewayEnv> {
|
|
17
|
+
return async (context) => {
|
|
18
|
+
const requestId = context.get('requestId');
|
|
19
|
+
const principal = context.get('principal');
|
|
20
|
+
const git = context.get('git');
|
|
21
|
+
const defer = deferWith(context);
|
|
22
|
+
const body = git.endpoint === 'info-refs' ? null : context.req.raw.body;
|
|
23
|
+
let result;
|
|
24
|
+
try {
|
|
25
|
+
result = await callUpstream(deps, context, body);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
deps.logger.log({
|
|
28
|
+
level: 'error',
|
|
29
|
+
message: 'storage host unreachable',
|
|
30
|
+
requestId,
|
|
31
|
+
repo: git.repo,
|
|
32
|
+
error: error instanceof Error ? error.message : String(error),
|
|
33
|
+
});
|
|
34
|
+
const reason = 'The storage backend is unreachable; try again later';
|
|
35
|
+
deps.audit.record({ requestId, principal, git, outcome: 'failed', reason, refs: [] }, defer, requestId);
|
|
36
|
+
return textResponse(502, reason, { 'X-Request-Id': requestId });
|
|
37
|
+
}
|
|
38
|
+
deps.audit.record(
|
|
39
|
+
{
|
|
40
|
+
requestId,
|
|
41
|
+
principal,
|
|
42
|
+
git,
|
|
43
|
+
outcome: result.ok ? 'allowed' : 'failed',
|
|
44
|
+
reason: result.ok ? null : result.reason,
|
|
45
|
+
refs: [],
|
|
46
|
+
},
|
|
47
|
+
defer,
|
|
48
|
+
requestId,
|
|
49
|
+
);
|
|
50
|
+
return result.ok ? streamToClient(result.response, result.response.body, requestId) : result.response;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type {
|
|
3
|
+
Authenticator,
|
|
4
|
+
Authorizer,
|
|
5
|
+
BranchPolicy,
|
|
6
|
+
PushPolicy,
|
|
7
|
+
} from '@leed-ai/git-gateway-contract/proxy/gateway-hooks';
|
|
8
|
+
import type { Principal } from '@leed-ai/git-gateway-contract/proxy/principal';
|
|
9
|
+
import type { SmartHttpConfig } from '@leed-ai/git-gateway-contract/proxy/smart-http-config';
|
|
10
|
+
import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
|
|
11
|
+
|
|
12
|
+
import type { AuditRecorder } from '../audit/audit-recorder.ts';
|
|
13
|
+
import type { DecisionCache } from '../cache/decision-cache.ts';
|
|
14
|
+
import type { Logger } from '../logging/logger.ts';
|
|
15
|
+
import type { OnPushHook } from '../workflows/on-push.ts';
|
|
16
|
+
import type { UpstreamFetch } from './upstream.ts';
|
|
17
|
+
|
|
18
|
+
/** A cached authentication or authorization decision. */
|
|
19
|
+
export type CachedDecision =
|
|
20
|
+
| { readonly kind: 'principal'; readonly principal: Principal }
|
|
21
|
+
| { readonly kind: 'allowed'; readonly allowed: boolean };
|
|
22
|
+
|
|
23
|
+
/** Everything the gateway's middleware and handlers share, resolved from the app options. */
|
|
24
|
+
export interface GatewayDeps {
|
|
25
|
+
readonly backend: StorageBackend;
|
|
26
|
+
readonly authenticator: Authenticator;
|
|
27
|
+
readonly authorizer: Authorizer;
|
|
28
|
+
readonly branchPolicy: BranchPolicy;
|
|
29
|
+
/** The deployment's rules over what a push changes, run after the branch policy; undefined: none. */
|
|
30
|
+
readonly pushPolicy: PushPolicy | undefined;
|
|
31
|
+
readonly audit: AuditRecorder;
|
|
32
|
+
readonly logger: Logger;
|
|
33
|
+
readonly cache: DecisionCache<CachedDecision>;
|
|
34
|
+
readonly config: SmartHttpConfig;
|
|
35
|
+
/** How long decisions are cached, in milliseconds (GatewayConfig.authCacheSeconds). */
|
|
36
|
+
readonly cacheTtlMs: number;
|
|
37
|
+
readonly fetch: UpstreamFetch;
|
|
38
|
+
readonly now: () => Date;
|
|
39
|
+
/** Run after every accepted push (the sync rules and the deployment's hook); undefined: none. */
|
|
40
|
+
readonly onPush: OnPushHook | undefined;
|
|
41
|
+
/** Receives onPush failures after they are logged. */
|
|
42
|
+
readonly onPushError: (error: unknown) => void;
|
|
43
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Principal } from '@leed-ai/git-gateway-contract/proxy/principal';
|
|
3
|
+
import type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
|
|
4
|
+
import type { ReceivePackRequest } from '@leed-ai/git-gateway-core/schemas/receive-pack';
|
|
5
|
+
|
|
6
|
+
import type { GitRequest } from './git-route.ts';
|
|
7
|
+
|
|
8
|
+
/** A fast-forward-only update whose ancestry is proven only once the pushed pack has been scanned. */
|
|
9
|
+
export interface PendingFastForward {
|
|
10
|
+
readonly update: RefUpdate;
|
|
11
|
+
/** The policy's reason, shown to the user if the update turns out not to be a fast-forward. */
|
|
12
|
+
readonly reason: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A push the branch policy let through, ready to forward. */
|
|
16
|
+
export interface AllowedPush {
|
|
17
|
+
readonly request: ReceivePackRequest;
|
|
18
|
+
/** The whole request body from its first byte. */
|
|
19
|
+
readonly body: ReadableStream<Uint8Array>;
|
|
20
|
+
/** Where the pack starts in `body`. */
|
|
21
|
+
readonly headLength: number;
|
|
22
|
+
/** Updates still to be proven fast-forwards from the pack as it streams past. */
|
|
23
|
+
readonly pending: readonly PendingFastForward[];
|
|
24
|
+
/**
|
|
25
|
+
* Every update of the push, when the push policy is still to check them with changed paths
|
|
26
|
+
* computed from the pack; null when there is nothing left for it to check.
|
|
27
|
+
*/
|
|
28
|
+
readonly policyUpdates: readonly RefUpdate[] | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The Hono environment of the gateway app: what each middleware leaves for the next. */
|
|
32
|
+
export interface GatewayEnv {
|
|
33
|
+
Variables: {
|
|
34
|
+
/** Set first: the id of this request, also returned as X-Request-Id. */
|
|
35
|
+
requestId: string;
|
|
36
|
+
/** Set by route resolution: the repository and endpoint addressed. */
|
|
37
|
+
git: GitRequest;
|
|
38
|
+
/** Set by authentication. */
|
|
39
|
+
principal: Principal;
|
|
40
|
+
/** Set by the branch-policy middleware on a receive-pack request it lets through. */
|
|
41
|
+
push: AllowedPush;
|
|
42
|
+
};
|
|
43
|
+
}
|