@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 340 lines 11 KB
Raw
1 import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import { hasAlchemyLabels } from "../Labels.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 findOwnedOccurrence,
12 hasOwnershipMarker,
13 ignoreGone,
14 listProjectOccurrences,
15 missingGet,
16 occurrenceAttrs,
17 occurrenceKind,
18 parseDescription,
19 projectParent,
20 reconcileOccurrence,
21 replaceOnIdentity,
22 retryTransient,
23 } from "./internal.ts";
24
25 export type OccurrenceProps = {
26 /**
27 * Analysis note this occurrence attaches to, as
28 * `projects/{project}/notes/{note}` or a bare note id. Immutable —
29 * changing it replaces the occurrence.
30 */
31 noteName: string;
32 /**
33 * URI of the analyzed resource, for example
34 * `https://gcr.io/project/image@sha256:abc`. Immutable — changing it
35 * replaces the occurrence.
36 */
37 resourceUri: string;
38 /**
39 * Remediation notes. Alchemy stamps ownership into this field for
40 * `list` / nuke because occurrences have no labels API.
41 */
42 remediation?: string;
43 /**
44 * DSSE envelope stored on the occurrence.
45 */
46 envelope?: containeranalysis.Envelope;
47 /**
48 * Attestation details. Used when no other kind is set.
49 */
50 attestation?: containeranalysis.AttestationOccurrence;
51 /**
52 * Build provenance details.
53 */
54 build?: containeranalysis.BuildOccurrence;
55 /**
56 * Discovery details.
57 */
58 discovery?: containeranalysis.DiscoveryOccurrence;
59 /**
60 * Image basis details.
61 */
62 image?: containeranalysis.ImageOccurrence;
63 /**
64 * Package installation details.
65 */
66 package?: containeranalysis.PackageOccurrence;
67 /**
68 * Vulnerability details.
69 */
70 vulnerability?: containeranalysis.VulnerabilityOccurrence;
71 /**
72 * Compliance details.
73 */
74 compliance?: containeranalysis.ComplianceOccurrence;
75 /**
76 * Deployment details.
77 */
78 deployment?: containeranalysis.DeploymentOccurrence;
79 /**
80 * Upgrade details.
81 */
82 upgrade?: containeranalysis.UpgradeOccurrence;
83 /**
84 * DSSE attestation details.
85 */
86 dsseAttestation?: containeranalysis.DSSEAttestationOccurrence;
87 /**
88 * SBOM reference details.
89 */
90 sbomReference?: containeranalysis.SBOMReferenceOccurrence;
91 /**
92 * Secret details.
93 */
94 secret?: containeranalysis.SecretOccurrence;
95 /**
96 * AI skill analysis details.
97 */
98 aiSkillAnalysis?: containeranalysis.AISkillAnalysisOccurrence;
99 };
100
101 export type Occurrence = Resource<
102 "GCP.ContainerAnalysis.Occurrence",
103 OccurrenceProps,
104 {
105 /** Full resource name `projects/{project}/occurrences/{occurrence}`. */
106 name: string;
107 /** Occurrence id (last path segment, server-assigned). */
108 occurrenceId: string;
109 /** Project id. */
110 project: string;
111 /** Note resource name. */
112 noteName: string;
113 /** Analyzed resource URI. */
114 resourceUri: string;
115 /** User remediation with the Alchemy ownership prefix stripped. */
116 remediation: string | undefined;
117 /** Server-reported analysis kind. */
118 kind: string | undefined;
119 /** DSSE envelope, if any. */
120 envelope: containeranalysis.Envelope | undefined;
121 /** Attestation details. */
122 attestation: containeranalysis.AttestationOccurrence | undefined;
123 /** Build provenance details. */
124 build: containeranalysis.BuildOccurrence | undefined;
125 /** Discovery details. */
126 discovery: containeranalysis.DiscoveryOccurrence | undefined;
127 /** Image basis details. */
128 image: containeranalysis.ImageOccurrence | undefined;
129 /** Package installation details. */
130 package: containeranalysis.PackageOccurrence | undefined;
131 /** Vulnerability details. */
132 vulnerability: containeranalysis.VulnerabilityOccurrence | undefined;
133 /** Compliance details. */
134 compliance: containeranalysis.ComplianceOccurrence | undefined;
135 /** Deployment details. */
136 deployment: containeranalysis.DeploymentOccurrence | undefined;
137 /** Upgrade details. */
138 upgrade: containeranalysis.UpgradeOccurrence | undefined;
139 /** DSSE attestation details. */
140 dsseAttestation: containeranalysis.DSSEAttestationOccurrence | undefined;
141 /** SBOM reference details. */
142 sbomReference: containeranalysis.SBOMReferenceOccurrence | undefined;
143 /** Secret details. */
144 secret: containeranalysis.SecretOccurrence | undefined;
145 /** AI skill analysis details. */
146 aiSkillAnalysis: containeranalysis.AISkillAnalysisOccurrence | undefined;
147 /** RFC3339 creation timestamp. */
148 createTime: string | undefined;
149 /** RFC3339 last-update timestamp. */
150 updateTime: string | undefined;
151 },
152 never,
153 Providers
154 >;
155
156 /**
157 * A project-scoped Container Analysis (Grafeas) occurrence. Occurrences
158 * attach a note to a concrete resource URI such as a container image
159 * digest.
160 *
161 * The occurrence id is assigned by the API. `noteName` and `resourceUri`
162 * are identity — changing either replaces the occurrence. Occurrences have
163 * no labels field, so Alchemy stamps ownership into `remediation` for
164 * `list` / nuke.
165 *
166 * ### Creating an Occurrence
167 * **Example:** Attest an image
168 * ```typescript
169 * const note = yield* GCP.ContainerAnalysis.Note("Authority", {
170 * attestation: { hint: { humanReadableName: "QA" } },
171 * });
172 * const occurrence = yield* GCP.ContainerAnalysis.Occurrence("Signed", {
173 * noteName: note.name,
174 * resourceUri: "https://example.com/image@sha256:abc",
175 * attestation: {
176 * serializedPayload: btoa("payload"),
177 * signatures: [
178 * { publicKeyId: "https://example.com/keys/qa", signature: btoa("sig") },
179 * ],
180 * },
181 * });
182 * ```
183 *
184 * ### Updating an Occurrence
185 * Re-declare the same logical id with changed props; the engine keeps the
186 * physical resource and updates it in place.
187 *
188 * **Example:** Change remediation text
189 * ```typescript
190 * const occurrence = yield* GCP.ContainerAnalysis.Occurrence("Signed", {
191 * noteName: note.name,
192 * resourceUri: "https://example.com/image@sha256:abc",
193 * remediation: "rebuild from a patched base",
194 * attestation: { hint: { humanReadableName: "QA" } },
195 * });
196 * ```
197 *
198 * @resource
199 * @category ContainerAnalysis
200 */
201 export const Occurrence = Resource<Occurrence>("GCP.ContainerAnalysis.Occurrence");
202
203 const getByName = missingGet(containeranalysis.getProjectsOccurrences);
204
205 const toPublicAttrs = (
206 occurrence: containeranalysis.Occurrence,
207 project: string,
208 ): Occurrence["Attributes"] => {
209 const attrs = occurrenceAttrs(occurrence, project);
210 return {
211 name: attrs.name,
212 occurrenceId: attrs.occurrenceId,
213 project: attrs.project,
214 noteName: attrs.noteName,
215 resourceUri: attrs.resourceUri,
216 remediation: attrs.remediation,
217 kind: attrs.kind,
218 envelope: attrs.envelope,
219 attestation: attrs.attestation,
220 build: attrs.build,
221 discovery: attrs.discovery,
222 image: attrs.image,
223 package: attrs.package,
224 vulnerability: attrs.vulnerability,
225 compliance: attrs.compliance,
226 deployment: attrs.deployment,
227 upgrade: attrs.upgrade,
228 dsseAttestation: attrs.dsseAttestation,
229 sbomReference: attrs.sbomReference,
230 secret: attrs.secret,
231 aiSkillAnalysis: attrs.aiSkillAnalysis,
232 createTime: attrs.createTime,
233 updateTime: attrs.updateTime,
234 };
235 };
236
237 export const OccurrenceProvider = () =>
238 Provider.succeed(Occurrence, {
239 stables: ["name", "occurrenceId", "project", "noteName", "resourceUri", "kind", "createTime"],
240
241 diff: Effect.fn(function* ({ news, olds, output }) {
242 if (!isResolved(news)) return undefined;
243 const previousKind =
244 occurrenceKind(olds ?? { noteName: "", resourceUri: "" }) ?? output?.kind;
245 const nextKind = occurrenceKind(news) ?? previousKind;
246 return replaceOnIdentity({
247 previousParent: olds?.noteName ?? output?.noteName,
248 nextParent: news.noteName,
249 extra:
250 ((olds?.resourceUri ?? output?.resourceUri) !== undefined &&
251 news.resourceUri !== (olds?.resourceUri ?? output?.resourceUri)) ||
252 (previousKind !== undefined && nextKind !== undefined && previousKind !== nextKind),
253 });
254 }),
255
256 read: Effect.fn(function* ({ id, output }) {
257 const env = yield* GcpEnvironment.current;
258 const name = output?.name;
259 if (name !== undefined && name.length > 0) {
260 const existing = yield* getByName(name);
261 if (existing === undefined) return undefined;
262 const attrs = toPublicAttrs(existing, env.project);
263 const { labels } = parseDescription(existing.remediation);
264 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
265 }
266 const items = yield* listProjectOccurrences(env.project);
267 const owned = yield* findOwnedOccurrence(id, items, env.project);
268 if (owned === undefined) return undefined;
269 return {
270 name: owned.name,
271 occurrenceId: owned.occurrenceId,
272 project: owned.project,
273 noteName: owned.noteName,
274 resourceUri: owned.resourceUri,
275 remediation: owned.remediation,
276 kind: owned.kind,
277 envelope: owned.envelope,
278 attestation: owned.attestation,
279 build: owned.build,
280 discovery: owned.discovery,
281 image: owned.image,
282 package: owned.package,
283 vulnerability: owned.vulnerability,
284 compliance: owned.compliance,
285 deployment: owned.deployment,
286 upgrade: owned.upgrade,
287 dsseAttestation: owned.dsseAttestation,
288 sbomReference: owned.sbomReference,
289 secret: owned.secret,
290 aiSkillAnalysis: owned.aiSkillAnalysis,
291 createTime: owned.createTime,
292 updateTime: owned.updateTime,
293 };
294 }),
295
296 list: () =>
297 Effect.gen(function* () {
298 const env = yield* GcpEnvironment.current;
299 const items = yield* listProjectOccurrences(env.project);
300 return items
301 .filter((item) => hasOwnershipMarker(item.remediation))
302 .map((item) => toPublicAttrs(item, env.project));
303 }),
304
305 reconcile: Effect.fn(function* ({ id, news, output }) {
306 const env = yield* GcpEnvironment.current;
307 const parent = projectParent(env.project);
308 const current = yield* reconcileOccurrence({
309 id,
310 name: output?.name,
311 parent,
312 project: env.project,
313 location: undefined,
314 news,
315 ops: {
316 get: getByName,
317 create: (request) =>
318 retryTransient(
319 containeranalysis.createProjectsOccurrences({
320 parent: request.parent,
321 body: request.body,
322 }),
323 ),
324 patch: (request) =>
325 retryTransient(
326 containeranalysis.patchProjectsOccurrences({
327 name: request.name,
328 updateMask: request.updateMask,
329 body: request.body,
330 }),
331 ),
332 },
333 });
334 return toPublicAttrs(current, env.project);
335 }),
336
337 delete: Effect.fn(function* ({ output }) {
338 yield* ignoreGone(containeranalysis.deleteProjectsOccurrences({ name: output.name }));
339 }),
340 });