@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 299 lines 10.2 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 listLocationOccurrences,
15 locationParent,
16 missingGet,
17 normalizeLocation,
18 occurrenceAttrs,
19 occurrenceKind,
20 parseDescription,
21 reconcileOccurrence,
22 replaceOnIdentity,
23 retryTransient,
24 } from "./internal.ts";
25 import type { OccurrenceProps } from "./Occurrence.ts";
26
27 export type LocationsOccurrenceProps = OccurrenceProps & {
28 /**
29 * Location (`us-central1`, `us-east1`, …). Immutable — changing it
30 * replaces the occurrence. `US-CENTRAL1` is accepted and normalized to
31 * `us-central1`.
32 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
33 */
34 location?: string;
35 };
36
37 export type LocationsOccurrence = Resource<
38 "GCP.ContainerAnalysis.LocationsOccurrence",
39 LocationsOccurrenceProps,
40 {
41 /** Full resource name `projects/{project}/locations/{location}/occurrences/{occurrence}`. */
42 name: string;
43 /** Occurrence id (last path segment, server-assigned). */
44 occurrenceId: string;
45 /** Project id. */
46 project: string;
47 /** Location id. */
48 location: string;
49 /** Note resource name. */
50 noteName: string;
51 /** Analyzed resource URI. */
52 resourceUri: string;
53 /** User remediation with the Alchemy ownership prefix stripped. */
54 remediation: string | undefined;
55 /** Server-reported analysis kind. */
56 kind: string | undefined;
57 /** DSSE envelope, if any. */
58 envelope: containeranalysis.Envelope | undefined;
59 /** Attestation details. */
60 attestation: containeranalysis.AttestationOccurrence | undefined;
61 /** Build provenance details. */
62 build: containeranalysis.BuildOccurrence | undefined;
63 /** Discovery details. */
64 discovery: containeranalysis.DiscoveryOccurrence | undefined;
65 /** Image basis details. */
66 image: containeranalysis.ImageOccurrence | undefined;
67 /** Package installation details. */
68 package: containeranalysis.PackageOccurrence | undefined;
69 /** Vulnerability details. */
70 vulnerability: containeranalysis.VulnerabilityOccurrence | undefined;
71 /** Compliance details. */
72 compliance: containeranalysis.ComplianceOccurrence | undefined;
73 /** Deployment details. */
74 deployment: containeranalysis.DeploymentOccurrence | undefined;
75 /** Upgrade details. */
76 upgrade: containeranalysis.UpgradeOccurrence | undefined;
77 /** DSSE attestation details. */
78 dsseAttestation: containeranalysis.DSSEAttestationOccurrence | undefined;
79 /** SBOM reference details. */
80 sbomReference: containeranalysis.SBOMReferenceOccurrence | undefined;
81 /** Secret details. */
82 secret: containeranalysis.SecretOccurrence | undefined;
83 /** AI skill analysis details. */
84 aiSkillAnalysis: containeranalysis.AISkillAnalysisOccurrence | undefined;
85 /** RFC3339 creation timestamp. */
86 createTime: string | undefined;
87 /** RFC3339 last-update timestamp. */
88 updateTime: string | undefined;
89 },
90 never,
91 Providers
92 >;
93
94 /**
95 * A location-scoped Container Analysis (Grafeas) occurrence. Same shape as
96 * {@link Occurrence}, stored at
97 * `projects/{project}/locations/{location}/occurrences/{occurrence}`.
98 *
99 * Location, `noteName`, and `resourceUri` are identity. Alchemy stamps
100 * ownership into `remediation` for `list` / nuke.
101 *
102 * ### Creating a Locations Occurrence
103 * **Example:** Regional attestation
104 * ```typescript
105 * const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", {
106 * location: "us-central1",
107 * attestation: { hint: { humanReadableName: "QA" } },
108 * });
109 * const occurrence = yield* GCP.ContainerAnalysis.LocationsOccurrence(
110 * "Signed",
111 * {
112 * location: "us-central1",
113 * noteName: note.name,
114 * resourceUri: "https://example.com/image@sha256:abc",
115 * attestation: {
116 * serializedPayload: btoa("payload"),
117 * signatures: [
118 * {
119 * publicKeyId: "https://example.com/keys/qa",
120 * signature: btoa("sig"),
121 * },
122 * ],
123 * },
124 * },
125 * );
126 * ```
127 *
128 * @resource
129 * @category ContainerAnalysis
130 */
131 export const LocationsOccurrence = Resource<LocationsOccurrence>(
132 "GCP.ContainerAnalysis.LocationsOccurrence",
133 );
134
135 const getByName = missingGet(containeranalysis.getProjectsLocationsOccurrences);
136
137 const toPublicAttrs = (
138 occurrence: containeranalysis.Occurrence,
139 project: string,
140 region: string,
141 ): LocationsOccurrence["Attributes"] => {
142 const attrs = occurrenceAttrs(occurrence, project);
143 return {
144 name: attrs.name,
145 occurrenceId: attrs.occurrenceId,
146 project: attrs.project,
147 location: attrs.location ?? region,
148 noteName: attrs.noteName,
149 resourceUri: attrs.resourceUri,
150 remediation: attrs.remediation,
151 kind: attrs.kind,
152 envelope: attrs.envelope,
153 attestation: attrs.attestation,
154 build: attrs.build,
155 discovery: attrs.discovery,
156 image: attrs.image,
157 package: attrs.package,
158 vulnerability: attrs.vulnerability,
159 compliance: attrs.compliance,
160 deployment: attrs.deployment,
161 upgrade: attrs.upgrade,
162 dsseAttestation: attrs.dsseAttestation,
163 sbomReference: attrs.sbomReference,
164 secret: attrs.secret,
165 aiSkillAnalysis: attrs.aiSkillAnalysis,
166 createTime: attrs.createTime,
167 updateTime: attrs.updateTime,
168 };
169 };
170
171 export const LocationsOccurrenceProvider = () =>
172 Provider.succeed(LocationsOccurrence, {
173 stables: [
174 "name",
175 "occurrenceId",
176 "project",
177 "location",
178 "noteName",
179 "resourceUri",
180 "kind",
181 "createTime",
182 ],
183
184 diff: Effect.fn(function* ({ news, olds, output }) {
185 if (!isResolved(news)) return undefined;
186 const env = yield* GcpEnvironment.current;
187 const previousKind =
188 occurrenceKind(olds ?? { noteName: "", resourceUri: "" }) ?? output?.kind;
189 const nextKind = occurrenceKind(news) ?? previousKind;
190 return replaceOnIdentity({
191 previousParent: locationParent(
192 "x",
193 normalizeLocation(olds?.location ?? output?.location, env.region),
194 ),
195 nextParent: locationParent(
196 "x",
197 normalizeLocation(news.location ?? olds?.location ?? output?.location, env.region),
198 ),
199 extra:
200 ((olds?.noteName ?? output?.noteName) !== undefined &&
201 news.noteName !== (olds?.noteName ?? output?.noteName)) ||
202 ((olds?.resourceUri ?? output?.resourceUri) !== undefined &&
203 news.resourceUri !== (olds?.resourceUri ?? output?.resourceUri)) ||
204 (previousKind !== undefined && nextKind !== undefined && previousKind !== nextKind),
205 });
206 }),
207
208 read: Effect.fn(function* ({ id, olds, output }) {
209 const env = yield* GcpEnvironment.current;
210 const name = output?.name;
211 if (name !== undefined && name.length > 0) {
212 const existing = yield* getByName(name);
213 if (existing === undefined) return undefined;
214 const attrs = toPublicAttrs(existing, env.project, env.region);
215 const { labels } = parseDescription(existing.remediation);
216 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
217 }
218 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
219 const items = yield* listLocationOccurrences(env.project, location);
220 const owned = yield* findOwnedOccurrence(id, items, env.project);
221 if (owned === undefined) return undefined;
222 return {
223 name: owned.name,
224 occurrenceId: owned.occurrenceId,
225 project: owned.project,
226 location: owned.location ?? location,
227 noteName: owned.noteName,
228 resourceUri: owned.resourceUri,
229 remediation: owned.remediation,
230 kind: owned.kind,
231 envelope: owned.envelope,
232 attestation: owned.attestation,
233 build: owned.build,
234 discovery: owned.discovery,
235 image: owned.image,
236 package: owned.package,
237 vulnerability: owned.vulnerability,
238 compliance: owned.compliance,
239 deployment: owned.deployment,
240 upgrade: owned.upgrade,
241 dsseAttestation: owned.dsseAttestation,
242 sbomReference: owned.sbomReference,
243 secret: owned.secret,
244 aiSkillAnalysis: owned.aiSkillAnalysis,
245 createTime: owned.createTime,
246 updateTime: owned.updateTime,
247 };
248 }),
249
250 list: () =>
251 Effect.gen(function* () {
252 const env = yield* GcpEnvironment.current;
253 const items = yield* listLocationOccurrences(env.project, env.region);
254 return items
255 .filter((item) => hasOwnershipMarker(item.remediation))
256 .map((item) => toPublicAttrs(item, env.project, env.region));
257 }),
258
259 reconcile: Effect.fn(function* ({ id, news, output }) {
260 const env = yield* GcpEnvironment.current;
261 const location = normalizeLocation(news.location ?? output?.location, env.region);
262 const parent = locationParent(env.project, location);
263 const current = yield* reconcileOccurrence({
264 id,
265 name: output?.name,
266 parent,
267 project: env.project,
268 location,
269 news,
270 ops: {
271 get: getByName,
272 create: (request) =>
273 retryTransient(
274 containeranalysis.createProjectsLocationsOccurrences({
275 parent: request.parent,
276 body: request.body,
277 }),
278 ),
279 patch: (request) =>
280 retryTransient(
281 containeranalysis.patchProjectsLocationsOccurrences({
282 name: request.name,
283 updateMask: request.updateMask,
284 body: request.body,
285 }),
286 ),
287 },
288 });
289 return toPublicAttrs(current, env.project, env.region);
290 }),
291
292 delete: Effect.fn(function* ({ output }) {
293 yield* ignoreGone(
294 containeranalysis.deleteProjectsLocationsOccurrences({
295 name: output.name,
296 }),
297 );
298 }),
299 });