The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { DeleteNotConfirmed } from "../Errors.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | isUserManagedAclEntity, |
| 13 | listAlchemyBuckets, |
| 14 | normalizeEntity, |
| 15 | normalizeRole, |
| 16 | } from "./internal.ts"; |
| 17 | |
| 18 | export type ObjectAccessControlProps = { |
| 19 | /** |
| 20 | * Name of the bucket that contains the object. Immutable — changing it |
| 21 | * replaces the entry. |
| 22 | */ |
| 23 | bucketName: string; |
| 24 | /** |
| 25 | * Object name this ACL entry is attached to. Immutable — changing it |
| 26 | * replaces the entry. |
| 27 | */ |
| 28 | object: string; |
| 29 | /** |
| 30 | * Principal granted the role. Forms include `user-{email}`, |
| 31 | * `group-{email}`, `domain-{domain}`, `allUsers`, and |
| 32 | * `allAuthenticatedUsers`. Immutable — changing it replaces the entry. |
| 33 | */ |
| 34 | entity: string; |
| 35 | /** |
| 36 | * Access permission for the entity (`OWNER` or `READER`). |
| 37 | */ |
| 38 | role: string; |
| 39 | /** |
| 40 | * Object generation. When omitted, the live generation is used. |
| 41 | * Immutable — changing it replaces the entry. |
| 42 | */ |
| 43 | generation?: string; |
| 44 | }; |
| 45 | |
| 46 | export type ObjectAccessControl = Resource< |
| 47 | "GCP.Storage.ObjectAccessControl", |
| 48 | ObjectAccessControlProps, |
| 49 | { |
| 50 | /** Parent bucket name. */ |
| 51 | bucketName: string; |
| 52 | /** Object name. */ |
| 53 | object: string; |
| 54 | /** Principal granted the role. */ |
| 55 | entity: string; |
| 56 | /** Access permission (`OWNER` or `READER`). */ |
| 57 | role: string; |
| 58 | /** Object generation, if the entry is generation-scoped. */ |
| 59 | generation: string | undefined; |
| 60 | /** Server-assigned ACL id. */ |
| 61 | id: string | undefined; |
| 62 | /** Domain associated with the entity, if any. */ |
| 63 | domain: string | undefined; |
| 64 | /** Email associated with the entity, if any. */ |
| 65 | email: string | undefined; |
| 66 | /** Numeric entity id, if any. */ |
| 67 | entityId: string | undefined; |
| 68 | /** HTTP etag. */ |
| 69 | etag: string | undefined; |
| 70 | /** GCS self-link. */ |
| 71 | selfLink: string | undefined; |
| 72 | }, |
| 73 | never, |
| 74 | Providers |
| 75 | >; |
| 76 | |
| 77 | /** |
| 78 | * A Cloud Storage object ACL entry. |
| 79 | * |
| 80 | * Fine-grained object ACLs are identity `(bucket, object, entity)`. Role |
| 81 | * is mutable; changing `bucketName`, `object`, or `entity` replaces the |
| 82 | * entry. Entries have no labels field, so `list` / `pnpm nuke:gcp` |
| 83 | * discover them by enumerating objects in alchemy-labeled buckets and |
| 84 | * skipping project-team and numeric-owner defaults. |
| 85 | * |
| 86 | * Uniform bucket-level access buckets do not support object ACLs. |
| 87 | * |
| 88 | * ### Creating an Object ACL |
| 89 | * **Example:** Grant a service account reader access |
| 90 | * ```typescript |
| 91 | * const bucket = yield* GCP.Storage.Bucket("assets", { |
| 92 | * forceDestroy: true, |
| 93 | * }); |
| 94 | * const acl = yield* GCP.Storage.ObjectAccessControl("reader", { |
| 95 | * bucketName: bucket.bucketName, |
| 96 | * object: "hello.txt", |
| 97 | * entity: "user-app@project.iam.gserviceaccount.com", |
| 98 | * role: "READER", |
| 99 | * }); |
| 100 | * ``` |
| 101 | * |
| 102 | * **Example:** Update the role |
| 103 | * ```typescript |
| 104 | * const acl = yield* GCP.Storage.ObjectAccessControl("reader", { |
| 105 | * bucketName: bucket.bucketName, |
| 106 | * object: "hello.txt", |
| 107 | * entity: "user-app@project.iam.gserviceaccount.com", |
| 108 | * role: "OWNER", |
| 109 | * }); |
| 110 | * ``` |
| 111 | * |
| 112 | * @resource |
| 113 | * @category Storage |
| 114 | */ |
| 115 | export const ObjectAccessControl = Resource<ObjectAccessControl>("GCP.Storage.ObjectAccessControl"); |
| 116 | |
| 117 | export class ObjectAccessControlNotResolved extends Data.TaggedError( |
| 118 | "GCP.Storage.ObjectAccessControlNotResolved", |
| 119 | )<{ |
| 120 | bucketName: string; |
| 121 | object: string; |
| 122 | entity: string; |
| 123 | }> {} |
| 124 | |
| 125 | const toAttrs = (acl: storage.ObjectAccessControl, bucketName: string, object: string) => ({ |
| 126 | bucketName: acl.bucket ?? bucketName, |
| 127 | object: acl.object ?? object, |
| 128 | entity: acl.entity ?? "", |
| 129 | role: acl.role ?? "", |
| 130 | generation: acl.generation, |
| 131 | id: acl.id, |
| 132 | domain: acl.domain, |
| 133 | email: acl.email, |
| 134 | entityId: acl.entityId, |
| 135 | etag: acl.etag, |
| 136 | selfLink: acl.selfLink, |
| 137 | }); |
| 138 | |
| 139 | const getByEntity = (bucketName: string, object: string, entity: string, generation?: string) => |
| 140 | storage |
| 141 | .getObjectAccessControls({ |
| 142 | bucket: bucketName, |
| 143 | object, |
| 144 | entity, |
| 145 | generation, |
| 146 | }) |
| 147 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 148 | |
| 149 | const listOnObject = (bucketName: string, object: string) => |
| 150 | storage.listObjectAccessControls({ bucket: bucketName, object }).pipe( |
| 151 | Effect.map((page) => page.items ?? []), |
| 152 | Effect.catchTag("NotFound", () => Effect.succeed([] as storage.ObjectAccessControl[])), |
| 153 | ); |
| 154 | |
| 155 | const listObjectsOnBucket = (bucketName: string) => |
| 156 | storage.listObjects.items({ bucket: bucketName, maxResults: 1000 }).pipe( |
| 157 | Stream.runCollect, |
| 158 | Effect.map((chunk) => Array.from(chunk)), |
| 159 | Effect.catchTag("NotFound", () => Effect.succeed([] as storage.Storage_Object[])), |
| 160 | ); |
| 161 | |
| 162 | const waitUntilGone = (bucketName: string, object: string, entity: string, generation?: string) => |
| 163 | getByEntity(bucketName, object, entity, generation).pipe( |
| 164 | Effect.map((existing) => (existing === undefined ? ("gone" as const) : ("found" as const))), |
| 165 | Effect.repeat({ |
| 166 | schedule: Schedule.spaced("1 second"), |
| 167 | until: (status) => status === "gone", |
| 168 | times: 30, |
| 169 | }), |
| 170 | Effect.flatMap((status): Effect.Effect<void, DeleteNotConfirmed> => |
| 171 | status === "gone" |
| 172 | ? Effect.void |
| 173 | : Effect.fail( |
| 174 | new DeleteNotConfirmed({ |
| 175 | resource: `${bucketName}/o/${object}/acl/${entity}`, |
| 176 | }), |
| 177 | ), |
| 178 | ), |
| 179 | ); |
| 180 | |
| 181 | export const ObjectAccessControlProvider = () => |
| 182 | Provider.succeed(ObjectAccessControl, { |
| 183 | stables: ["bucketName", "object", "entity", "generation", "id", "selfLink"], |
| 184 | |
| 185 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 186 | if (!isResolved(news)) return undefined; |
| 187 | const previousBucket = olds?.bucketName ?? output?.bucketName; |
| 188 | const previousObject = olds?.object ?? output?.object; |
| 189 | const previousEntity = olds?.entity ?? output?.entity; |
| 190 | const previousGeneration = olds?.generation ?? output?.generation; |
| 191 | if (previousBucket !== undefined && news.bucketName !== previousBucket) { |
| 192 | return { action: "replace" as const, deleteFirst: false }; |
| 193 | } |
| 194 | if (previousObject !== undefined && news.object !== previousObject) { |
| 195 | return { action: "replace" as const, deleteFirst: false }; |
| 196 | } |
| 197 | if ( |
| 198 | previousEntity !== undefined && |
| 199 | normalizeEntity(news.entity) !== normalizeEntity(previousEntity) |
| 200 | ) { |
| 201 | return { action: "replace" as const, deleteFirst: false }; |
| 202 | } |
| 203 | if ( |
| 204 | news.generation !== undefined && |
| 205 | previousGeneration !== undefined && |
| 206 | news.generation !== previousGeneration |
| 207 | ) { |
| 208 | return { action: "replace" as const, deleteFirst: false }; |
| 209 | } |
| 210 | return undefined; |
| 211 | }), |
| 212 | |
| 213 | read: Effect.fn(function* ({ olds, output }) { |
| 214 | const bucketName = olds?.bucketName ?? output?.bucketName; |
| 215 | const object = olds?.object ?? output?.object; |
| 216 | const entity = olds?.entity ?? output?.entity; |
| 217 | if (!bucketName || !object || !entity) return undefined; |
| 218 | const generation = olds?.generation ?? output?.generation; |
| 219 | const existing = yield* getByEntity(bucketName, object, entity, generation); |
| 220 | if (existing === undefined) return undefined; |
| 221 | return toAttrs(existing, bucketName, object); |
| 222 | }), |
| 223 | |
| 224 | list: () => |
| 225 | Effect.gen(function* () { |
| 226 | const buckets = yield* listAlchemyBuckets(); |
| 227 | const pages = yield* Effect.forEach( |
| 228 | buckets, |
| 229 | (bucket) => { |
| 230 | const bucketName = bucket.name; |
| 231 | // Legacy ACL APIs reject uniform bucket-level access buckets. |
| 232 | if ( |
| 233 | !bucketName || |
| 234 | bucket.iamConfiguration?.uniformBucketLevelAccess?.enabled === true |
| 235 | ) { |
| 236 | return Effect.succeed([] as Array<ObjectAccessControl["Attributes"]>); |
| 237 | } |
| 238 | return listObjectsOnBucket(bucketName).pipe( |
| 239 | Effect.flatMap((objects) => |
| 240 | Effect.forEach( |
| 241 | objects, |
| 242 | (object) => { |
| 243 | const objectName = object.name; |
| 244 | if (!objectName) { |
| 245 | return Effect.succeed([] as Array<ObjectAccessControl["Attributes"]>); |
| 246 | } |
| 247 | return listOnObject(bucketName, objectName).pipe( |
| 248 | Effect.map((items) => |
| 249 | items |
| 250 | .filter((item) => isUserManagedAclEntity(item.entity)) |
| 251 | .map((item) => toAttrs(item, bucketName, objectName)), |
| 252 | ), |
| 253 | ); |
| 254 | }, |
| 255 | { concurrency: 8 }, |
| 256 | ), |
| 257 | ), |
| 258 | Effect.map((nested) => nested.flat()), |
| 259 | ); |
| 260 | }, |
| 261 | { concurrency: 4 }, |
| 262 | ); |
| 263 | return pages.flat(); |
| 264 | }), |
| 265 | |
| 266 | reconcile: Effect.fn(function* ({ news, output }) { |
| 267 | const bucketName = news.bucketName; |
| 268 | const object = news.object; |
| 269 | const entity = normalizeEntity(news.entity); |
| 270 | const role = normalizeRole(news.role); |
| 271 | const generation = news.generation; |
| 272 | |
| 273 | let current = |
| 274 | output?.entity !== undefined && output.object !== undefined |
| 275 | ? yield* getByEntity( |
| 276 | output.bucketName ?? bucketName, |
| 277 | output.object, |
| 278 | normalizeEntity(output.entity), |
| 279 | output.generation, |
| 280 | ) |
| 281 | : undefined; |
| 282 | if (current === undefined) { |
| 283 | current = yield* getByEntity(bucketName, object, entity, generation); |
| 284 | } |
| 285 | |
| 286 | if (current === undefined) { |
| 287 | const created = yield* storage |
| 288 | .insertObjectAccessControls({ |
| 289 | bucket: bucketName, |
| 290 | object, |
| 291 | generation, |
| 292 | body: { entity, role }, |
| 293 | }) |
| 294 | .pipe( |
| 295 | Effect.catchTag("Conflict", () => getByEntity(bucketName, object, entity, generation)), |
| 296 | ); |
| 297 | current = created ?? undefined; |
| 298 | } |
| 299 | |
| 300 | if (current === undefined || !current.entity) { |
| 301 | return yield* new ObjectAccessControlNotResolved({ |
| 302 | bucketName, |
| 303 | object, |
| 304 | entity, |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | if (normalizeRole(current.role ?? "") !== role) { |
| 309 | current = yield* storage.patchObjectAccessControls({ |
| 310 | bucket: bucketName, |
| 311 | object, |
| 312 | entity, |
| 313 | generation, |
| 314 | body: { entity, role }, |
| 315 | }); |
| 316 | } |
| 317 | |
| 318 | return toAttrs(current, bucketName, object); |
| 319 | }), |
| 320 | |
| 321 | delete: Effect.fn(function* ({ output }) { |
| 322 | const bucketName = output.bucketName; |
| 323 | const object = output.object; |
| 324 | const entity = output.entity; |
| 325 | if (!bucketName || !object || !entity) return; |
| 326 | const removed = yield* storage |
| 327 | .deleteObjectAccessControls({ |
| 328 | bucket: bucketName, |
| 329 | object, |
| 330 | entity, |
| 331 | generation: output.generation, |
| 332 | }) |
| 333 | .pipe( |
| 334 | Effect.as(true), |
| 335 | Effect.catchTags({ |
| 336 | NotFound: () => Effect.succeed(true), |
| 337 | // GCS refuses to drop the last OWNER ACL on an object. |
| 338 | ObjectOwnerAclRequired: () => Effect.succeed(false), |
| 339 | }), |
| 340 | ); |
| 341 | if (removed) { |
| 342 | yield* waitUntilGone(bucketName, object, entity, output.generation); |
| 343 | } |
| 344 | }), |
| 345 | }); |