The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as apigee from "@distilled.cloud/gcp/apigee_v1"; |
| 2 | import { unownedUnlessPrevious } 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 Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { lastSegment, orgParent, organizationFromName, sameJson, toResourceId } from "./names.ts"; |
| 12 | import { |
| 13 | createInternalLabels, |
| 14 | encodeOwnership, |
| 15 | hasAlchemyLabels, |
| 16 | hasOwnershipMarker, |
| 17 | parseOwnership, |
| 18 | } from "./ownership.ts"; |
| 19 | |
| 20 | const MAX_NAME_LENGTH = 63; |
| 21 | const DEFAULT_FEEDBACK_TYPE = "EXCLUDED_DETECTION"; |
| 22 | |
| 23 | export type SecurityFeedbackContext = { |
| 24 | /** |
| 25 | * Attribute the feedback constrains. |
| 26 | */ |
| 27 | attribute: apigee.GoogleCloudApigeeV1SecurityFeedbackFeedbackContextAttributeEnum | (string & {}); |
| 28 | /** |
| 29 | * Values of that attribute. |
| 30 | */ |
| 31 | values: string[]; |
| 32 | }; |
| 33 | |
| 34 | export type SecurityFeedbackProps = { |
| 35 | /** |
| 36 | * Apigee organization id. Defaults to the current GCP project id. |
| 37 | * Immutable — changing it replaces the feedback report. |
| 38 | */ |
| 39 | organization?: string; |
| 40 | /** |
| 41 | * Feedback id (the `{feedback}` segment of |
| 42 | * `organizations/{org}/securityFeedback/{feedback}`). If omitted, a |
| 43 | * unique name is generated from the stack, stage, and logical id. |
| 44 | * Immutable — changing it replaces the report. |
| 45 | */ |
| 46 | securityFeedbackId?: string; |
| 47 | /** |
| 48 | * Display name. |
| 49 | */ |
| 50 | displayName?: string; |
| 51 | /** |
| 52 | * Feedback type. |
| 53 | * @default "EXCLUDED_DETECTION" |
| 54 | */ |
| 55 | feedbackType?: apigee.GoogleCloudApigeeV1SecurityFeedbackFeedbackTypeEnum | (string & {}); |
| 56 | /** |
| 57 | * Reason for the feedback. |
| 58 | */ |
| 59 | reason?: apigee.GoogleCloudApigeeV1SecurityFeedbackReasonEnum | (string & {}); |
| 60 | /** |
| 61 | * Attribute/value pairs constraining the feedback. |
| 62 | */ |
| 63 | feedbackContexts: SecurityFeedbackContext[]; |
| 64 | /** |
| 65 | * Free-form comment. Alchemy ownership is stored in a `[alchemy …]` |
| 66 | * prefix and stripped from attributes. Feedback reports have no |
| 67 | * labels field. |
| 68 | */ |
| 69 | comment?: string; |
| 70 | }; |
| 71 | |
| 72 | export type SecurityFeedback = Resource< |
| 73 | "GCP.Apigee.SecurityFeedback", |
| 74 | SecurityFeedbackProps, |
| 75 | { |
| 76 | /** Full resource name `organizations/{org}/securityFeedback/{feedback}`. */ |
| 77 | name: string; |
| 78 | /** Feedback id (last path segment). */ |
| 79 | securityFeedbackId: string; |
| 80 | /** Apigee organization id. */ |
| 81 | organization: string; |
| 82 | /** Display name. */ |
| 83 | displayName: string | undefined; |
| 84 | /** Feedback type. */ |
| 85 | feedbackType: string | undefined; |
| 86 | /** Reason. */ |
| 87 | reason: string | undefined; |
| 88 | /** Constraining contexts. */ |
| 89 | feedbackContexts: SecurityFeedbackContext[]; |
| 90 | /** User comment with the Alchemy ownership prefix stripped. */ |
| 91 | comment: string | undefined; |
| 92 | /** RFC3339 creation timestamp. */ |
| 93 | createTime: string | undefined; |
| 94 | /** RFC3339 last-update timestamp. */ |
| 95 | updateTime: string | undefined; |
| 96 | }, |
| 97 | never, |
| 98 | Providers |
| 99 | >; |
| 100 | |
| 101 | /** |
| 102 | * An Advanced API Security customer feedback report. |
| 103 | * |
| 104 | * Feedback reports have no labels field, so Alchemy stamps ownership |
| 105 | * into the comment for `list` / nuke. Name and organization are |
| 106 | * identity — changing them replaces the report. Display name, type, |
| 107 | * reason, contexts, and comment update in place. |
| 108 | * |
| 109 | * ### Creating Feedback |
| 110 | * **Example:** Exclude detections from an environment |
| 111 | * ```typescript |
| 112 | * const feedback = yield* GCP.Apigee.SecurityFeedback("PenTest", { |
| 113 | * reason: "PENETRATION_TEST", |
| 114 | * feedbackContexts: [{ |
| 115 | * attribute: "ATTRIBUTE_ENVIRONMENTS", |
| 116 | * values: ["eval"], |
| 117 | * }], |
| 118 | * }); |
| 119 | * ``` |
| 120 | * |
| 121 | * @resource |
| 122 | * @category Apigee |
| 123 | */ |
| 124 | export const SecurityFeedback = Resource<SecurityFeedback>("GCP.Apigee.SecurityFeedback"); |
| 125 | |
| 126 | export class SecurityFeedbackNotResolved extends Data.TaggedError( |
| 127 | "GCP.Apigee.SecurityFeedbackNotResolved", |
| 128 | )<{ |
| 129 | resourceName: string; |
| 130 | }> {} |
| 131 | |
| 132 | const resourceName = (organization: string, feedbackId: string) => |
| 133 | `${orgParent(organization)}/securityFeedback/${feedbackId}`; |
| 134 | |
| 135 | const feedbackIdOf = (feedback: apigee.GoogleCloudApigeeV1SecurityFeedback) => |
| 136 | lastSegment(feedback.name ?? ""); |
| 137 | |
| 138 | const contextsOf = ( |
| 139 | contexts: |
| 140 | | readonly { |
| 141 | attribute?: SecurityFeedbackContext["attribute"]; |
| 142 | values?: readonly string[]; |
| 143 | }[] |
| 144 | | undefined, |
| 145 | ): SecurityFeedbackContext[] => |
| 146 | (contexts ?? []) |
| 147 | .filter( |
| 148 | ( |
| 149 | context, |
| 150 | ): context is { |
| 151 | attribute: SecurityFeedbackContext["attribute"]; |
| 152 | values?: readonly string[]; |
| 153 | } => context.attribute !== undefined, |
| 154 | ) |
| 155 | .map((context) => ({ |
| 156 | attribute: context.attribute, |
| 157 | values: [...(context.values ?? [])], |
| 158 | })); |
| 159 | |
| 160 | const toAttrs = (feedback: apigee.GoogleCloudApigeeV1SecurityFeedback, organization: string) => { |
| 161 | const securityFeedbackId = feedbackIdOf(feedback); |
| 162 | const name = feedback.name?.includes("/") |
| 163 | ? feedback.name |
| 164 | : resourceName(organization, securityFeedbackId); |
| 165 | const parsed = parseOwnership(feedback.comment); |
| 166 | return { |
| 167 | name, |
| 168 | securityFeedbackId, |
| 169 | organization: organizationFromName(name) ?? organization, |
| 170 | displayName: feedback.displayName, |
| 171 | feedbackType: feedback.feedbackType, |
| 172 | reason: feedback.reason, |
| 173 | feedbackContexts: contextsOf(feedback.feedbackContexts), |
| 174 | comment: parsed.text, |
| 175 | createTime: feedback.createTime, |
| 176 | updateTime: feedback.updateTime, |
| 177 | }; |
| 178 | }; |
| 179 | |
| 180 | const getByName = (name: string) => |
| 181 | apigee |
| 182 | .getOrganizationsSecurityFeedback({ name }) |
| 183 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined)); |
| 184 | |
| 185 | const toBody = ( |
| 186 | news: SecurityFeedbackProps, |
| 187 | comment: string, |
| 188 | ): apigee.GoogleCloudApigeeV1SecurityFeedback => ({ |
| 189 | displayName: news.displayName, |
| 190 | feedbackType: news.feedbackType ?? DEFAULT_FEEDBACK_TYPE, |
| 191 | reason: news.reason, |
| 192 | comment, |
| 193 | feedbackContexts: contextsOf(news.feedbackContexts), |
| 194 | }); |
| 195 | |
| 196 | export const SecurityFeedbackProvider = () => |
| 197 | Provider.succeed(SecurityFeedback, { |
| 198 | stables: ["name", "securityFeedbackId", "organization", "createTime"], |
| 199 | |
| 200 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 201 | if (!isResolved(news)) return undefined; |
| 202 | const previousId = olds?.securityFeedbackId ?? output?.securityFeedbackId; |
| 203 | const previousOrg = olds?.organization ?? output?.organization; |
| 204 | if ( |
| 205 | (previousId !== undefined && |
| 206 | news.securityFeedbackId !== undefined && |
| 207 | news.securityFeedbackId !== previousId) || |
| 208 | (previousOrg !== undefined && |
| 209 | news.organization !== undefined && |
| 210 | news.organization !== previousOrg) |
| 211 | ) { |
| 212 | return { action: "replace" as const, deleteFirst: true }; |
| 213 | } |
| 214 | return undefined; |
| 215 | }), |
| 216 | |
| 217 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 218 | const env = yield* GcpEnvironment.current; |
| 219 | const organization = organizationFromName(output?.name) ?? olds?.organization ?? env.project; |
| 220 | const securityFeedbackId = yield* toResourceId( |
| 221 | id, |
| 222 | olds?.securityFeedbackId, |
| 223 | output?.securityFeedbackId, |
| 224 | MAX_NAME_LENGTH, |
| 225 | ); |
| 226 | const name = output?.name ?? resourceName(organization, securityFeedbackId); |
| 227 | const existing = yield* getByName(name); |
| 228 | if (existing === undefined) return undefined; |
| 229 | const attrs = toAttrs(existing, organization); |
| 230 | const { labels } = parseOwnership(existing.comment); |
| 231 | return (yield* hasAlchemyLabels(id, labels)) |
| 232 | ? attrs |
| 233 | : unownedUnlessPrevious(attrs, previous, "name"); |
| 234 | }), |
| 235 | |
| 236 | list: () => |
| 237 | Effect.gen(function* () { |
| 238 | const env = yield* GcpEnvironment.current; |
| 239 | return yield* apigee.listOrganizationsSecurityFeedback |
| 240 | .pages({ |
| 241 | parent: orgParent(env.project), |
| 242 | pageSize: 100, |
| 243 | }) |
| 244 | .pipe( |
| 245 | Stream.flatMap((page) => Stream.fromIterable(page.securityFeedback ?? [])), |
| 246 | Stream.filter((feedback) => hasOwnershipMarker(feedback.comment)), |
| 247 | Stream.map((feedback) => toAttrs(feedback, env.project)), |
| 248 | Stream.runCollect, |
| 249 | Effect.map((chunk) => Array.from(chunk)), |
| 250 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 251 | Effect.succeed([] as SecurityFeedback["Attributes"][]), |
| 252 | ), |
| 253 | ); |
| 254 | }), |
| 255 | |
| 256 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 257 | const env = yield* GcpEnvironment.current; |
| 258 | const organization = news.organization ?? output?.organization ?? env.project; |
| 259 | const securityFeedbackId = yield* toResourceId( |
| 260 | id, |
| 261 | news.securityFeedbackId, |
| 262 | output?.securityFeedbackId, |
| 263 | MAX_NAME_LENGTH, |
| 264 | ); |
| 265 | const name = resourceName(organization, securityFeedbackId); |
| 266 | const ownership = yield* createInternalLabels(id); |
| 267 | const desiredComment = encodeOwnership(ownership, news.comment); |
| 268 | const desiredType = news.feedbackType ?? DEFAULT_FEEDBACK_TYPE; |
| 269 | |
| 270 | let current = yield* getByName(output?.name ?? name); |
| 271 | |
| 272 | if (current === undefined) { |
| 273 | const created = yield* apigee |
| 274 | .createOrganizationsSecurityFeedback({ |
| 275 | parent: orgParent(organization), |
| 276 | securityFeedbackId, |
| 277 | body: toBody(news, desiredComment), |
| 278 | }) |
| 279 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 280 | current = created ?? undefined; |
| 281 | } |
| 282 | |
| 283 | if (current === undefined) { |
| 284 | return yield* new SecurityFeedbackNotResolved({ resourceName: name }); |
| 285 | } |
| 286 | |
| 287 | const displayChanged = (current.displayName ?? "") !== (news.displayName ?? ""); |
| 288 | const typeChanged = (current.feedbackType ?? "") !== desiredType; |
| 289 | const reasonChanged = (current.reason ?? "") !== (news.reason ?? ""); |
| 290 | const commentChanged = (current.comment ?? "") !== desiredComment; |
| 291 | const contextsChanged = !sameJson( |
| 292 | contextsOf(current.feedbackContexts), |
| 293 | contextsOf(news.feedbackContexts), |
| 294 | ); |
| 295 | |
| 296 | const updateMask = [ |
| 297 | displayChanged ? "displayName" : undefined, |
| 298 | typeChanged ? "feedbackType" : undefined, |
| 299 | reasonChanged ? "reason" : undefined, |
| 300 | commentChanged ? "comment" : undefined, |
| 301 | contextsChanged ? "feedbackContexts" : undefined, |
| 302 | ].filter((field): field is string => field !== undefined); |
| 303 | |
| 304 | if (updateMask.length > 0) { |
| 305 | current = yield* apigee.patchOrganizationsSecurityFeedback({ |
| 306 | name: current.name?.includes("/") ? current.name : name, |
| 307 | updateMask: updateMask.join(","), |
| 308 | body: toBody(news, desiredComment), |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | return toAttrs(current, organization); |
| 313 | }), |
| 314 | |
| 315 | delete: Effect.fn(function* ({ output }) { |
| 316 | yield* apigee |
| 317 | .deleteOrganizationsSecurityFeedback({ name: output.name }) |
| 318 | .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void)); |
| 319 | }), |
| 320 | }); |