The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { isTransientGcpError } from "../Errors.ts"; |
| 8 | import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 9 | |
| 10 | export const MAX_ID_LENGTH = 63; |
| 11 | |
| 12 | export class ResourceNotResolved extends Data.TaggedError( |
| 13 | "GCP.ContainerAnalysis.ResourceNotResolved", |
| 14 | )<{ |
| 15 | resourceName: string; |
| 16 | }> {} |
| 17 | |
| 18 | export const lastSegment = (value: string) => { |
| 19 | const trimmed = value.replace(/\/+$/, ""); |
| 20 | const parts = trimmed.split("/"); |
| 21 | return parts[parts.length - 1] || trimmed; |
| 22 | }; |
| 23 | |
| 24 | export const rfc1035 = (name: string, fallback = "note"): string => { |
| 25 | let next = name |
| 26 | .toLowerCase() |
| 27 | .replace(/[^a-z0-9-]/g, "-") |
| 28 | .replace(/-+/g, "-") |
| 29 | .replace(/^-+|-+$/g, ""); |
| 30 | if (!/^[a-z]/.test(next)) next = `n${next}`; |
| 31 | next = next.slice(0, MAX_ID_LENGTH).replace(/-+$/g, ""); |
| 32 | if (next.length === 0) return fallback; |
| 33 | if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_ID_LENGTH - 1)}0`; |
| 34 | return next.slice(0, MAX_ID_LENGTH); |
| 35 | }; |
| 36 | |
| 37 | export const toPhysicalId = ( |
| 38 | id: string, |
| 39 | explicit: string | undefined, |
| 40 | existing: string | undefined, |
| 41 | fallback = "note", |
| 42 | ) => |
| 43 | Effect.gen(function* () { |
| 44 | if (explicit !== undefined) return rfc1035(explicit, fallback); |
| 45 | if (existing !== undefined) return existing; |
| 46 | return rfc1035( |
| 47 | yield* createPhysicalName({ |
| 48 | id, |
| 49 | maxLength: MAX_ID_LENGTH, |
| 50 | lowercase: true, |
| 51 | }), |
| 52 | fallback, |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | export const normalizeLocation = (location: string | undefined, fallback: string) => |
| 57 | lastSegment(location ?? fallback).toLowerCase(); |
| 58 | |
| 59 | export const projectParent = (project: string) => `projects/${project}`; |
| 60 | |
| 61 | export const locationParent = (project: string, location: string) => |
| 62 | `projects/${project}/locations/${location}`; |
| 63 | |
| 64 | export const parseName = (name: string, collection: string) => { |
| 65 | const parts = name.split("/").filter((part) => part.length > 0); |
| 66 | const collectionAt = parts.lastIndexOf(collection); |
| 67 | const locationsAt = parts.lastIndexOf("locations"); |
| 68 | const projectsAt = parts.lastIndexOf("projects"); |
| 69 | return { |
| 70 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 71 | location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1] : undefined, |
| 72 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 73 | parent: |
| 74 | collectionAt > 0 |
| 75 | ? parts.slice(0, collectionAt).join("/") |
| 76 | : parts.slice(0, Math.max(0, parts.length - 1)).join("/"), |
| 77 | }; |
| 78 | }; |
| 79 | |
| 80 | export const expandNoteName = (value: string, project: string, location?: string) => { |
| 81 | if (value.includes("/")) return value.replace(/\/+$/, ""); |
| 82 | return location !== undefined |
| 83 | ? `${locationParent(project, location)}/notes/${value}` |
| 84 | : `${projectParent(project)}/notes/${value}`; |
| 85 | }; |
| 86 | |
| 87 | export const encodeDescription = ( |
| 88 | labels: Record<string, string>, |
| 89 | description: string | undefined, |
| 90 | ): string => { |
| 91 | const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`; |
| 92 | return description ? `${marker}\n${description}` : marker; |
| 93 | }; |
| 94 | |
| 95 | export const parseDescription = ( |
| 96 | description: string | undefined, |
| 97 | ): { |
| 98 | labels: Record<string, string>; |
| 99 | description: string | undefined; |
| 100 | } => { |
| 101 | if (!description?.startsWith("[alchemy ")) { |
| 102 | return { labels: {}, description }; |
| 103 | } |
| 104 | const end = description.indexOf("]"); |
| 105 | if (end < 0) return { labels: {}, description }; |
| 106 | const labels: Record<string, string> = {}; |
| 107 | for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) { |
| 108 | const eq = part.indexOf("="); |
| 109 | if (eq > 0) { |
| 110 | labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 111 | } |
| 112 | } |
| 113 | const rest = description.slice(end + 1).replace(/^\n/, ""); |
| 114 | return { labels, description: rest.length > 0 ? rest : undefined }; |
| 115 | }; |
| 116 | |
| 117 | export const hasOwnershipMarker = (description: string | undefined) => |
| 118 | Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-")); |
| 119 | |
| 120 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 121 | (left ?? "") === (right ?? ""); |
| 122 | |
| 123 | export const sameJson = (left: unknown, right: unknown) => |
| 124 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 125 | |
| 126 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 127 | fields.filter((field): field is string => field !== undefined).join(","); |
| 128 | |
| 129 | export const replaceOnIdentity = (input: { |
| 130 | previousId?: string; |
| 131 | nextId?: string; |
| 132 | previousParent?: string; |
| 133 | nextParent?: string; |
| 134 | extra?: boolean; |
| 135 | }) => { |
| 136 | if (input.extra === true) { |
| 137 | return { action: "replace" as const, deleteFirst: false }; |
| 138 | } |
| 139 | const idChanged = |
| 140 | input.previousId !== undefined && |
| 141 | input.nextId !== undefined && |
| 142 | input.previousId !== input.nextId; |
| 143 | const parentChanged = |
| 144 | (input.previousParent ?? "") !== "" && |
| 145 | (input.nextParent ?? "") !== "" && |
| 146 | (input.previousParent ?? "") !== (input.nextParent ?? ""); |
| 147 | if (!idChanged && !parentChanged) return undefined; |
| 148 | return { action: "replace" as const, deleteFirst: false }; |
| 149 | }; |
| 150 | |
| 151 | export const retryTransient = <A, E extends { readonly _tag: string }, R>( |
| 152 | effect: Effect.Effect<A, E, R>, |
| 153 | ) => |
| 154 | effect.pipe( |
| 155 | Effect.retry({ |
| 156 | while: isTransientGcpError, |
| 157 | times: 8, |
| 158 | schedule: Schedule.exponential("250 millis"), |
| 159 | }), |
| 160 | ); |
| 161 | |
| 162 | export const ignoreGone = <A, E extends { readonly _tag: string }, R>( |
| 163 | effect: Effect.Effect<A, E, R>, |
| 164 | ) => |
| 165 | effect.pipe( |
| 166 | Effect.catchIf( |
| 167 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 168 | () => Effect.void, |
| 169 | ), |
| 170 | ); |
| 171 | |
| 172 | export const missingGet = |
| 173 | <A, E extends { readonly _tag: string }, R>( |
| 174 | effect: (input: { name: string }) => Effect.Effect<A, E, R>, |
| 175 | ) => |
| 176 | (name: string) => |
| 177 | name.length === 0 |
| 178 | ? Effect.undefined |
| 179 | : effect({ name }).pipe( |
| 180 | Effect.catchIf( |
| 181 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 182 | () => Effect.undefined, |
| 183 | ), |
| 184 | ); |
| 185 | |
| 186 | const emptyList = <A>() => Effect.succeed<A[]>([]); |
| 187 | |
| 188 | export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>( |
| 189 | pages: Stream.Stream<Page, E, R>, |
| 190 | items: (page: Page) => readonly Item[] | null | undefined, |
| 191 | ) => |
| 192 | pages.pipe( |
| 193 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 194 | Stream.runCollect, |
| 195 | Effect.map((chunk) => Array.from(chunk) as Item[]), |
| 196 | Effect.catchIf( |
| 197 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 198 | () => emptyList<Item>(), |
| 199 | ), |
| 200 | ); |
| 201 | |
| 202 | export type NoteNews = { |
| 203 | shortDescription?: string; |
| 204 | longDescription?: string; |
| 205 | expirationTime?: string; |
| 206 | relatedUrl?: containeranalysis.RelatedUrlList; |
| 207 | relatedNoteNames?: string[]; |
| 208 | attestation?: containeranalysis.AttestationNote; |
| 209 | build?: containeranalysis.BuildNote; |
| 210 | discovery?: containeranalysis.DiscoveryNote; |
| 211 | image?: containeranalysis.ImageNote; |
| 212 | package?: containeranalysis.PackageNote; |
| 213 | vulnerability?: containeranalysis.VulnerabilityNote; |
| 214 | compliance?: containeranalysis.ComplianceNote; |
| 215 | deployment?: containeranalysis.DeploymentNote; |
| 216 | upgrade?: containeranalysis.UpgradeNote; |
| 217 | vulnerabilityAssessment?: containeranalysis.VulnerabilityAssessmentNote; |
| 218 | secret?: containeranalysis.SecretNote; |
| 219 | dsseAttestation?: containeranalysis.DSSEAttestationNote; |
| 220 | sbomReference?: containeranalysis.SBOMReferenceNote; |
| 221 | aiSkillAnalysis?: containeranalysis.AISkillAnalysisNote; |
| 222 | }; |
| 223 | |
| 224 | export type OccurrenceNews = { |
| 225 | noteName: string; |
| 226 | resourceUri: string; |
| 227 | remediation?: string; |
| 228 | envelope?: containeranalysis.Envelope; |
| 229 | attestation?: containeranalysis.AttestationOccurrence; |
| 230 | build?: containeranalysis.BuildOccurrence; |
| 231 | discovery?: containeranalysis.DiscoveryOccurrence; |
| 232 | image?: containeranalysis.ImageOccurrence; |
| 233 | package?: containeranalysis.PackageOccurrence; |
| 234 | vulnerability?: containeranalysis.VulnerabilityOccurrence; |
| 235 | compliance?: containeranalysis.ComplianceOccurrence; |
| 236 | deployment?: containeranalysis.DeploymentOccurrence; |
| 237 | upgrade?: containeranalysis.UpgradeOccurrence; |
| 238 | dsseAttestation?: containeranalysis.DSSEAttestationOccurrence; |
| 239 | sbomReference?: containeranalysis.SBOMReferenceOccurrence; |
| 240 | secret?: containeranalysis.SecretOccurrence; |
| 241 | aiSkillAnalysis?: containeranalysis.AISkillAnalysisOccurrence; |
| 242 | }; |
| 243 | |
| 244 | const DEFAULT_ATTESTATION_PAYLOAD = "YWxjaGVteQ=="; |
| 245 | |
| 246 | export const noteKind = (news: NoteNews): string | undefined => { |
| 247 | if (news.attestation !== undefined) return "ATTESTATION"; |
| 248 | if (news.build !== undefined) return "BUILD"; |
| 249 | if (news.discovery !== undefined) return "DISCOVERY"; |
| 250 | if (news.image !== undefined) return "IMAGE"; |
| 251 | if (news.package !== undefined) return "PACKAGE"; |
| 252 | if (news.vulnerability !== undefined) return "VULNERABILITY"; |
| 253 | if (news.compliance !== undefined) return "COMPLIANCE"; |
| 254 | if (news.deployment !== undefined) return "DEPLOYMENT"; |
| 255 | if (news.upgrade !== undefined) return "UPGRADE"; |
| 256 | if (news.vulnerabilityAssessment !== undefined) { |
| 257 | return "VULNERABILITY_ASSESSMENT"; |
| 258 | } |
| 259 | if (news.secret !== undefined) return "SECRET"; |
| 260 | if (news.dsseAttestation !== undefined) return "DSSE_ATTESTATION"; |
| 261 | if (news.sbomReference !== undefined) return "SBOM_REFERENCE"; |
| 262 | if (news.aiSkillAnalysis !== undefined) return "AI_SKILL_ANALYSIS"; |
| 263 | return undefined; |
| 264 | }; |
| 265 | |
| 266 | export const occurrenceKind = (news: OccurrenceNews): string | undefined => { |
| 267 | if (news.attestation !== undefined) return "ATTESTATION"; |
| 268 | if (news.build !== undefined) return "BUILD"; |
| 269 | if (news.discovery !== undefined) return "DISCOVERY"; |
| 270 | if (news.image !== undefined) return "IMAGE"; |
| 271 | if (news.package !== undefined) return "PACKAGE"; |
| 272 | if (news.vulnerability !== undefined) return "VULNERABILITY"; |
| 273 | if (news.compliance !== undefined) return "COMPLIANCE"; |
| 274 | if (news.deployment !== undefined) return "DEPLOYMENT"; |
| 275 | if (news.upgrade !== undefined) return "UPGRADE"; |
| 276 | if (news.secret !== undefined) return "SECRET"; |
| 277 | if (news.dsseAttestation !== undefined) return "DSSE_ATTESTATION"; |
| 278 | if (news.sbomReference !== undefined) return "SBOM_REFERENCE"; |
| 279 | if (news.aiSkillAnalysis !== undefined) return "AI_SKILL_ANALYSIS"; |
| 280 | return undefined; |
| 281 | }; |
| 282 | |
| 283 | export const noteKindFields = (news: NoteNews): containeranalysis.Note => { |
| 284 | const body: containeranalysis.Note = { |
| 285 | attestation: news.attestation, |
| 286 | build: news.build, |
| 287 | discovery: news.discovery, |
| 288 | image: news.image, |
| 289 | package: news.package, |
| 290 | vulnerability: news.vulnerability, |
| 291 | compliance: news.compliance, |
| 292 | deployment: news.deployment, |
| 293 | upgrade: news.upgrade, |
| 294 | vulnerabilityAssessment: news.vulnerabilityAssessment, |
| 295 | secret: news.secret, |
| 296 | dsseAttestation: news.dsseAttestation, |
| 297 | sbomReference: news.sbomReference, |
| 298 | aiSkillAnalysis: news.aiSkillAnalysis, |
| 299 | }; |
| 300 | if (noteKind(news) === undefined) { |
| 301 | body.attestation = { hint: { humanReadableName: "alchemy" } }; |
| 302 | } |
| 303 | return body; |
| 304 | }; |
| 305 | |
| 306 | export const occurrenceKindFields = (news: OccurrenceNews): containeranalysis.Occurrence => { |
| 307 | const body: containeranalysis.Occurrence = { |
| 308 | attestation: news.attestation, |
| 309 | build: news.build, |
| 310 | discovery: news.discovery, |
| 311 | image: news.image, |
| 312 | package: news.package, |
| 313 | vulnerability: news.vulnerability, |
| 314 | compliance: news.compliance, |
| 315 | deployment: news.deployment, |
| 316 | upgrade: news.upgrade, |
| 317 | dsseAttestation: news.dsseAttestation, |
| 318 | sbomReference: news.sbomReference, |
| 319 | secret: news.secret, |
| 320 | aiSkillAnalysis: news.aiSkillAnalysis, |
| 321 | envelope: news.envelope, |
| 322 | }; |
| 323 | if (occurrenceKind(news) === undefined) { |
| 324 | body.attestation = { |
| 325 | serializedPayload: DEFAULT_ATTESTATION_PAYLOAD, |
| 326 | signatures: [ |
| 327 | { |
| 328 | publicKeyId: "https://alchemy.local/keys/test", |
| 329 | signature: DEFAULT_ATTESTATION_PAYLOAD, |
| 330 | }, |
| 331 | ], |
| 332 | }; |
| 333 | } |
| 334 | return body; |
| 335 | }; |
| 336 | |
| 337 | export type NoteAttrs = { |
| 338 | name: string; |
| 339 | noteId: string; |
| 340 | project: string; |
| 341 | location: string | undefined; |
| 342 | shortDescription: string | undefined; |
| 343 | longDescription: string | undefined; |
| 344 | kind: string | undefined; |
| 345 | expirationTime: string | undefined; |
| 346 | relatedUrl: containeranalysis.RelatedUrlList; |
| 347 | relatedNoteNames: string[]; |
| 348 | attestation: containeranalysis.AttestationNote | undefined; |
| 349 | build: containeranalysis.BuildNote | undefined; |
| 350 | discovery: containeranalysis.DiscoveryNote | undefined; |
| 351 | image: containeranalysis.ImageNote | undefined; |
| 352 | package: containeranalysis.PackageNote | undefined; |
| 353 | vulnerability: containeranalysis.VulnerabilityNote | undefined; |
| 354 | compliance: containeranalysis.ComplianceNote | undefined; |
| 355 | deployment: containeranalysis.DeploymentNote | undefined; |
| 356 | upgrade: containeranalysis.UpgradeNote | undefined; |
| 357 | vulnerabilityAssessment: containeranalysis.VulnerabilityAssessmentNote | undefined; |
| 358 | secret: containeranalysis.SecretNote | undefined; |
| 359 | dsseAttestation: containeranalysis.DSSEAttestationNote | undefined; |
| 360 | sbomReference: containeranalysis.SBOMReferenceNote | undefined; |
| 361 | aiSkillAnalysis: containeranalysis.AISkillAnalysisNote | undefined; |
| 362 | createTime: string | undefined; |
| 363 | updateTime: string | undefined; |
| 364 | }; |
| 365 | |
| 366 | export const noteAttrs = (note: containeranalysis.Note, project: string): NoteAttrs => { |
| 367 | const name = note.name ?? ""; |
| 368 | const parsed = parseName(name, "notes"); |
| 369 | const description = parseDescription(note.longDescription); |
| 370 | return { |
| 371 | name, |
| 372 | noteId: parsed.id, |
| 373 | project: parsed.project || project, |
| 374 | location: parsed.location, |
| 375 | shortDescription: note.shortDescription, |
| 376 | longDescription: description.description, |
| 377 | kind: note.kind, |
| 378 | expirationTime: note.expirationTime, |
| 379 | relatedUrl: note.relatedUrl ?? [], |
| 380 | relatedNoteNames: note.relatedNoteNames ?? [], |
| 381 | attestation: note.attestation, |
| 382 | build: note.build, |
| 383 | discovery: note.discovery, |
| 384 | image: note.image, |
| 385 | package: note.package, |
| 386 | vulnerability: note.vulnerability, |
| 387 | compliance: note.compliance, |
| 388 | deployment: note.deployment, |
| 389 | upgrade: note.upgrade, |
| 390 | vulnerabilityAssessment: note.vulnerabilityAssessment, |
| 391 | secret: note.secret, |
| 392 | dsseAttestation: note.dsseAttestation, |
| 393 | sbomReference: note.sbomReference, |
| 394 | aiSkillAnalysis: note.aiSkillAnalysis, |
| 395 | createTime: note.createTime, |
| 396 | updateTime: note.updateTime, |
| 397 | }; |
| 398 | }; |
| 399 | |
| 400 | export type OccurrenceAttrs = { |
| 401 | name: string; |
| 402 | occurrenceId: string; |
| 403 | project: string; |
| 404 | location: string | undefined; |
| 405 | noteName: string; |
| 406 | resourceUri: string; |
| 407 | remediation: string | undefined; |
| 408 | kind: string | undefined; |
| 409 | envelope: containeranalysis.Envelope | undefined; |
| 410 | attestation: containeranalysis.AttestationOccurrence | undefined; |
| 411 | build: containeranalysis.BuildOccurrence | undefined; |
| 412 | discovery: containeranalysis.DiscoveryOccurrence | undefined; |
| 413 | image: containeranalysis.ImageOccurrence | undefined; |
| 414 | package: containeranalysis.PackageOccurrence | undefined; |
| 415 | vulnerability: containeranalysis.VulnerabilityOccurrence | undefined; |
| 416 | compliance: containeranalysis.ComplianceOccurrence | undefined; |
| 417 | deployment: containeranalysis.DeploymentOccurrence | undefined; |
| 418 | upgrade: containeranalysis.UpgradeOccurrence | undefined; |
| 419 | dsseAttestation: containeranalysis.DSSEAttestationOccurrence | undefined; |
| 420 | sbomReference: containeranalysis.SBOMReferenceOccurrence | undefined; |
| 421 | secret: containeranalysis.SecretOccurrence | undefined; |
| 422 | aiSkillAnalysis: containeranalysis.AISkillAnalysisOccurrence | undefined; |
| 423 | createTime: string | undefined; |
| 424 | updateTime: string | undefined; |
| 425 | }; |
| 426 | |
| 427 | export const occurrenceAttrs = ( |
| 428 | occurrence: containeranalysis.Occurrence, |
| 429 | project: string, |
| 430 | ): OccurrenceAttrs => { |
| 431 | const name = occurrence.name ?? ""; |
| 432 | const parsed = parseName(name, "occurrences"); |
| 433 | const remediation = parseDescription(occurrence.remediation); |
| 434 | return { |
| 435 | name, |
| 436 | occurrenceId: parsed.id, |
| 437 | project: parsed.project || project, |
| 438 | location: parsed.location, |
| 439 | noteName: occurrence.noteName ?? "", |
| 440 | resourceUri: occurrence.resourceUri ?? "", |
| 441 | remediation: remediation.description, |
| 442 | kind: occurrence.kind, |
| 443 | envelope: occurrence.envelope, |
| 444 | attestation: occurrence.attestation, |
| 445 | build: occurrence.build, |
| 446 | discovery: occurrence.discovery, |
| 447 | image: occurrence.image, |
| 448 | package: occurrence.package, |
| 449 | vulnerability: occurrence.vulnerability, |
| 450 | compliance: occurrence.compliance, |
| 451 | deployment: occurrence.deployment, |
| 452 | upgrade: occurrence.upgrade, |
| 453 | dsseAttestation: occurrence.dsseAttestation, |
| 454 | sbomReference: occurrence.sbomReference, |
| 455 | secret: occurrence.secret, |
| 456 | aiSkillAnalysis: occurrence.aiSkillAnalysis, |
| 457 | createTime: occurrence.createTime, |
| 458 | updateTime: occurrence.updateTime, |
| 459 | }; |
| 460 | }; |
| 461 | |
| 462 | export type NoteOps<EG = never, RG = never, EC = never, RC = never, EP = never, RP = never> = { |
| 463 | get: (name: string) => Effect.Effect<containeranalysis.Note | undefined, EG, RG>; |
| 464 | create: (input: { |
| 465 | parent: string; |
| 466 | noteId: string; |
| 467 | body: containeranalysis.Note; |
| 468 | }) => Effect.Effect<containeranalysis.Note | undefined, EC, RC>; |
| 469 | patch: (input: { |
| 470 | name: string; |
| 471 | updateMask: string; |
| 472 | body: containeranalysis.Note; |
| 473 | }) => Effect.Effect<containeranalysis.Note, EP, RP>; |
| 474 | }; |
| 475 | |
| 476 | export const reconcileNote = <EG, RG, EC, RC, EP, RP>(input: { |
| 477 | id: string; |
| 478 | name: string; |
| 479 | parent: string; |
| 480 | noteId: string; |
| 481 | news: NoteNews; |
| 482 | ops: NoteOps<EG, RG, EC, RC, EP, RP>; |
| 483 | }) => |
| 484 | Effect.gen(function* () { |
| 485 | const ownership = yield* createInternalLabels(input.id); |
| 486 | const desiredLong = encodeDescription(ownership, input.news.longDescription); |
| 487 | const kinds = noteKindFields({ |
| 488 | ...input.news, |
| 489 | attestation: |
| 490 | input.news.attestation ?? |
| 491 | (noteKind(input.news) === undefined |
| 492 | ? { hint: { humanReadableName: input.noteId } } |
| 493 | : undefined), |
| 494 | }); |
| 495 | |
| 496 | let current = yield* input.ops.get(input.name); |
| 497 | |
| 498 | if (current === undefined) { |
| 499 | const created = yield* input.ops.create({ |
| 500 | parent: input.parent, |
| 501 | noteId: input.noteId, |
| 502 | body: { |
| 503 | shortDescription: input.news.shortDescription, |
| 504 | longDescription: desiredLong, |
| 505 | expirationTime: input.news.expirationTime, |
| 506 | relatedUrl: input.news.relatedUrl, |
| 507 | relatedNoteNames: input.news.relatedNoteNames, |
| 508 | ...kinds, |
| 509 | }, |
| 510 | }); |
| 511 | current = created ?? undefined; |
| 512 | } |
| 513 | |
| 514 | if (current === undefined) { |
| 515 | return yield* new ResourceNotResolved({ resourceName: input.name }); |
| 516 | } |
| 517 | |
| 518 | const currentName = current.name ?? input.name; |
| 519 | const shortChanged = !sameText(current.shortDescription, input.news.shortDescription); |
| 520 | const longChanged = !sameText(current.longDescription, desiredLong); |
| 521 | const expirationChanged = !sameText(current.expirationTime, input.news.expirationTime); |
| 522 | const relatedUrlChanged = !sameJson(current.relatedUrl ?? [], input.news.relatedUrl ?? []); |
| 523 | const relatedNotesChanged = !sameJson( |
| 524 | [...(current.relatedNoteNames ?? [])].slice().sort(), |
| 525 | [...(input.news.relatedNoteNames ?? [])].slice().sort(), |
| 526 | ); |
| 527 | const attestationChanged = !sameJson(current.attestation, kinds.attestation); |
| 528 | const buildChanged = !sameJson(current.build, kinds.build); |
| 529 | const discoveryChanged = !sameJson(current.discovery, kinds.discovery); |
| 530 | const imageChanged = !sameJson(current.image, kinds.image); |
| 531 | const packageChanged = !sameJson(current.package, kinds.package); |
| 532 | const vulnerabilityChanged = !sameJson(current.vulnerability, kinds.vulnerability); |
| 533 | const complianceChanged = !sameJson(current.compliance, kinds.compliance); |
| 534 | const deploymentChanged = !sameJson(current.deployment, kinds.deployment); |
| 535 | const upgradeChanged = !sameJson(current.upgrade, kinds.upgrade); |
| 536 | const assessmentChanged = !sameJson( |
| 537 | current.vulnerabilityAssessment, |
| 538 | kinds.vulnerabilityAssessment, |
| 539 | ); |
| 540 | const secretChanged = !sameJson(current.secret, kinds.secret); |
| 541 | const dsseChanged = !sameJson(current.dsseAttestation, kinds.dsseAttestation); |
| 542 | const sbomChanged = !sameJson(current.sbomReference, kinds.sbomReference); |
| 543 | const skillChanged = !sameJson(current.aiSkillAnalysis, kinds.aiSkillAnalysis); |
| 544 | |
| 545 | const updateMask = updateMaskOf( |
| 546 | shortChanged ? "shortDescription" : undefined, |
| 547 | longChanged ? "longDescription" : undefined, |
| 548 | expirationChanged ? "expirationTime" : undefined, |
| 549 | relatedUrlChanged ? "relatedUrl" : undefined, |
| 550 | relatedNotesChanged ? "relatedNoteNames" : undefined, |
| 551 | attestationChanged && kinds.attestation !== undefined ? "attestation" : undefined, |
| 552 | buildChanged && kinds.build !== undefined ? "build" : undefined, |
| 553 | discoveryChanged && kinds.discovery !== undefined ? "discovery" : undefined, |
| 554 | imageChanged && kinds.image !== undefined ? "image" : undefined, |
| 555 | packageChanged && kinds.package !== undefined ? "package" : undefined, |
| 556 | vulnerabilityChanged && kinds.vulnerability !== undefined ? "vulnerability" : undefined, |
| 557 | complianceChanged && kinds.compliance !== undefined ? "compliance" : undefined, |
| 558 | deploymentChanged && kinds.deployment !== undefined ? "deployment" : undefined, |
| 559 | upgradeChanged && kinds.upgrade !== undefined ? "upgrade" : undefined, |
| 560 | assessmentChanged && kinds.vulnerabilityAssessment !== undefined |
| 561 | ? "vulnerabilityAssessment" |
| 562 | : undefined, |
| 563 | secretChanged && kinds.secret !== undefined ? "secret" : undefined, |
| 564 | dsseChanged && kinds.dsseAttestation !== undefined ? "dsseAttestation" : undefined, |
| 565 | sbomChanged && kinds.sbomReference !== undefined ? "sbomReference" : undefined, |
| 566 | skillChanged && kinds.aiSkillAnalysis !== undefined ? "aiSkillAnalysis" : undefined, |
| 567 | ); |
| 568 | |
| 569 | if (updateMask.length > 0) { |
| 570 | current = yield* input.ops.patch({ |
| 571 | name: currentName, |
| 572 | updateMask, |
| 573 | body: { |
| 574 | shortDescription: input.news.shortDescription, |
| 575 | longDescription: desiredLong, |
| 576 | expirationTime: input.news.expirationTime, |
| 577 | relatedUrl: input.news.relatedUrl, |
| 578 | relatedNoteNames: input.news.relatedNoteNames, |
| 579 | ...kinds, |
| 580 | }, |
| 581 | }); |
| 582 | } |
| 583 | |
| 584 | return current; |
| 585 | }); |
| 586 | |
| 587 | export type OccurrenceOps< |
| 588 | EG = never, |
| 589 | RG = never, |
| 590 | EC = never, |
| 591 | RC = never, |
| 592 | EP = never, |
| 593 | RP = never, |
| 594 | > = { |
| 595 | get: (name: string) => Effect.Effect<containeranalysis.Occurrence | undefined, EG, RG>; |
| 596 | create: (input: { |
| 597 | parent: string; |
| 598 | body: containeranalysis.Occurrence; |
| 599 | }) => Effect.Effect<containeranalysis.Occurrence | undefined, EC, RC>; |
| 600 | patch: (input: { |
| 601 | name: string; |
| 602 | updateMask: string; |
| 603 | body: containeranalysis.Occurrence; |
| 604 | }) => Effect.Effect<containeranalysis.Occurrence, EP, RP>; |
| 605 | }; |
| 606 | |
| 607 | export const reconcileOccurrence = <EG, RG, EC, RC, EP, RP>(input: { |
| 608 | id: string; |
| 609 | name: string | undefined; |
| 610 | parent: string; |
| 611 | project: string; |
| 612 | location: string | undefined; |
| 613 | news: OccurrenceNews; |
| 614 | ops: OccurrenceOps<EG, RG, EC, RC, EP, RP>; |
| 615 | }) => |
| 616 | Effect.gen(function* () { |
| 617 | const ownership = yield* createInternalLabels(input.id); |
| 618 | const desiredRemediation = encodeDescription(ownership, input.news.remediation); |
| 619 | const noteName = expandNoteName(input.news.noteName, input.project, input.location); |
| 620 | const kinds = occurrenceKindFields(input.news); |
| 621 | |
| 622 | let current = |
| 623 | input.name !== undefined && input.name.length > 0 |
| 624 | ? yield* input.ops.get(input.name) |
| 625 | : undefined; |
| 626 | |
| 627 | if (current === undefined) { |
| 628 | const created = yield* input.ops.create({ |
| 629 | parent: input.parent, |
| 630 | body: { |
| 631 | noteName, |
| 632 | resourceUri: input.news.resourceUri, |
| 633 | remediation: desiredRemediation, |
| 634 | ...kinds, |
| 635 | }, |
| 636 | }); |
| 637 | current = created ?? undefined; |
| 638 | } |
| 639 | |
| 640 | if (current === undefined) { |
| 641 | return yield* new ResourceNotResolved({ |
| 642 | resourceName: input.name ?? `${input.parent}/occurrences`, |
| 643 | }); |
| 644 | } |
| 645 | |
| 646 | const currentName = current.name ?? input.name ?? ""; |
| 647 | const remediationChanged = !sameText(current.remediation, desiredRemediation); |
| 648 | const envelopeChanged = !sameJson(current.envelope, kinds.envelope); |
| 649 | const attestationChanged = !sameJson(current.attestation, kinds.attestation); |
| 650 | const buildChanged = !sameJson(current.build, kinds.build); |
| 651 | const discoveryChanged = !sameJson(current.discovery, kinds.discovery); |
| 652 | const imageChanged = !sameJson(current.image, kinds.image); |
| 653 | const packageChanged = !sameJson(current.package, kinds.package); |
| 654 | const vulnerabilityChanged = !sameJson(current.vulnerability, kinds.vulnerability); |
| 655 | const complianceChanged = !sameJson(current.compliance, kinds.compliance); |
| 656 | const deploymentChanged = !sameJson(current.deployment, kinds.deployment); |
| 657 | const upgradeChanged = !sameJson(current.upgrade, kinds.upgrade); |
| 658 | const dsseChanged = !sameJson(current.dsseAttestation, kinds.dsseAttestation); |
| 659 | const sbomChanged = !sameJson(current.sbomReference, kinds.sbomReference); |
| 660 | const secretChanged = !sameJson(current.secret, kinds.secret); |
| 661 | const skillChanged = !sameJson(current.aiSkillAnalysis, kinds.aiSkillAnalysis); |
| 662 | |
| 663 | const updateMask = updateMaskOf( |
| 664 | remediationChanged ? "remediation" : undefined, |
| 665 | envelopeChanged && kinds.envelope !== undefined ? "envelope" : undefined, |
| 666 | attestationChanged && kinds.attestation !== undefined ? "attestation" : undefined, |
| 667 | buildChanged && kinds.build !== undefined ? "build" : undefined, |
| 668 | discoveryChanged && kinds.discovery !== undefined ? "discovery" : undefined, |
| 669 | imageChanged && kinds.image !== undefined ? "image" : undefined, |
| 670 | packageChanged && kinds.package !== undefined ? "package" : undefined, |
| 671 | vulnerabilityChanged && kinds.vulnerability !== undefined ? "vulnerability" : undefined, |
| 672 | complianceChanged && kinds.compliance !== undefined ? "compliance" : undefined, |
| 673 | deploymentChanged && kinds.deployment !== undefined ? "deployment" : undefined, |
| 674 | upgradeChanged && kinds.upgrade !== undefined ? "upgrade" : undefined, |
| 675 | dsseChanged && kinds.dsseAttestation !== undefined ? "dsseAttestation" : undefined, |
| 676 | sbomChanged && kinds.sbomReference !== undefined ? "sbomReference" : undefined, |
| 677 | secretChanged && kinds.secret !== undefined ? "secret" : undefined, |
| 678 | skillChanged && kinds.aiSkillAnalysis !== undefined ? "aiSkillAnalysis" : undefined, |
| 679 | ); |
| 680 | |
| 681 | if (updateMask.length > 0) { |
| 682 | current = yield* input.ops.patch({ |
| 683 | name: currentName, |
| 684 | updateMask, |
| 685 | body: { |
| 686 | noteName, |
| 687 | resourceUri: input.news.resourceUri, |
| 688 | remediation: desiredRemediation, |
| 689 | ...kinds, |
| 690 | }, |
| 691 | }); |
| 692 | } |
| 693 | |
| 694 | return current; |
| 695 | }); |
| 696 | |
| 697 | export const ownedNote = (id: string, note: containeranalysis.Note, project: string) => |
| 698 | Effect.gen(function* () { |
| 699 | const attrs = noteAttrs(note, project); |
| 700 | const { labels } = parseDescription(note.longDescription); |
| 701 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : undefined; |
| 702 | }); |
| 703 | |
| 704 | export const ownedOccurrence = ( |
| 705 | id: string, |
| 706 | occurrence: containeranalysis.Occurrence, |
| 707 | project: string, |
| 708 | ) => |
| 709 | Effect.gen(function* () { |
| 710 | const attrs = occurrenceAttrs(occurrence, project); |
| 711 | const { labels } = parseDescription(occurrence.remediation); |
| 712 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : undefined; |
| 713 | }); |
| 714 | |
| 715 | export const listProjectNotes = (project: string) => |
| 716 | collectPages( |
| 717 | containeranalysis.listProjectsNotes.pages({ |
| 718 | parent: projectParent(project), |
| 719 | pageSize: 1000, |
| 720 | }), |
| 721 | (page) => page.notes, |
| 722 | ); |
| 723 | |
| 724 | export const listLocationNotes = (project: string, location: string) => |
| 725 | collectPages( |
| 726 | containeranalysis.listProjectsLocationsNotes.pages({ |
| 727 | parent: locationParent(project, location), |
| 728 | pageSize: 1000, |
| 729 | returnPartialSuccess: location === "-", |
| 730 | }), |
| 731 | (page) => page.notes, |
| 732 | ); |
| 733 | |
| 734 | export const listProjectOccurrences = (project: string) => |
| 735 | collectPages( |
| 736 | containeranalysis.listProjectsOccurrences.pages({ |
| 737 | parent: projectParent(project), |
| 738 | pageSize: 1000, |
| 739 | }), |
| 740 | (page) => page.occurrences, |
| 741 | ); |
| 742 | |
| 743 | export const listLocationOccurrences = (project: string, location: string) => |
| 744 | collectPages( |
| 745 | containeranalysis.listProjectsLocationsOccurrences.pages({ |
| 746 | parent: locationParent(project, location), |
| 747 | pageSize: 1000, |
| 748 | returnPartialSuccess: location === "-", |
| 749 | }), |
| 750 | (page) => page.occurrences, |
| 751 | ); |
| 752 | |
| 753 | export const findOwnedNote = ( |
| 754 | id: string, |
| 755 | notes: readonly containeranalysis.Note[], |
| 756 | project: string, |
| 757 | ) => |
| 758 | Effect.gen(function* () { |
| 759 | for (const note of notes) { |
| 760 | const owned = yield* ownedNote(id, note, project); |
| 761 | if (owned !== undefined) return owned; |
| 762 | } |
| 763 | return undefined; |
| 764 | }); |
| 765 | |
| 766 | export const findOwnedOccurrence = ( |
| 767 | id: string, |
| 768 | occurrences: readonly containeranalysis.Occurrence[], |
| 769 | project: string, |
| 770 | ) => |
| 771 | Effect.gen(function* () { |
| 772 | for (const occurrence of occurrences) { |
| 773 | const owned = yield* ownedOccurrence(id, occurrence, project); |
| 774 | if (owned !== undefined) return owned; |
| 775 | } |
| 776 | return undefined; |
| 777 | }); |