Repository navigation
feat: add @beesolve/dsql-helpers (DSQL construct with per-table roles, access reconciler, Drizzle factory) - #122
Conversation
a construct's children share its logical-id prefix (Dsql and DsqlAccessManifest1A2B3C4D), so the first prefix match depended on the order CloudFormation lists resources in
aurora dsql helpers: DsqlCluster cdk construct with per-function, per-table database roles (grantRead, grantWrite, grantReadWrite, grant), an access manifest in ssm, reconcileAccess to apply it in both directions, migration repair helpers, and createDsqlDatabase for lambda. docs, adrs and initial changeset
| export type AccessRole = { | ||
| readonly name: string; | ||
| readonly privileges: ReadonlyArray<DsqlPrivilege>; | ||
| /** Tables the privileges apply to. Absent means every table in the schema, including later ones. */ | ||
| readonly tables?: ReadonlyArray<string> | undefined; | ||
| /** IAM role or user ARNs mapped to this role with `AWS IAM GRANT`. */ | ||
| readonly principals: ReadonlyArray<string>; | ||
| }; | ||
|
|
||
| /** Desired database access, written by the CDK construct and applied by {@link reconcileAccess}. */ | ||
| export type AccessManifest = { | ||
| readonly version: 1; | ||
| readonly clusterId: string; | ||
| readonly account: string; | ||
| readonly schema: string; | ||
| readonly roles: ReadonlyArray<AccessRole>; | ||
| }; |
There was a problem hiding this comment.
these should be inferred from valibot schemas
There was a problem hiding this comment.
Done in 53c0a05: AccessRole and AccessManifest are now v.InferOutput<typeof …Schema> (with v.readonly() so they stay readonly). With inferred exported types the package switches to isolatedDeclarations: false and dts: { inferTypes: true, tsgo: true }, like aws-env-loader and dmarc-parser.
|
|
||
| - https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html | ||
| - https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html | ||
| - https://dev.to/damien_pace_2c3ec240cbd46/how-i-made-dsql-easier-4je5 |
There was a problem hiding this comment.
Link to anvil blog https://anvilcloud.dev/docs/blog/automating-dsql
There was a problem hiding this comment.
Done in 53c0a05: the reference now points to https://anvilcloud.dev/docs/blog/automating-dsql, and the mentions of Anvil in ADR-001 and ADR-002 link to it too. (Note: that URL returned 404 when I fetched it, so it may not be published yet.)
AccessRole and AccessManifest are now v.InferOutput of the schemas, so the validated shape and the type cannot drift. isolatedDeclarations is off and dts uses inferTypes + tsgo, as in the other packages that export inferred valibot types. link the anvil blog post in the adrs
Adds
@beesolve/dsql-helpers(packages/helpers-dsql): Aurora DSQL support extracted from expense-ease, with per-function, per-table database roles inspired by Anvil's DSQL component.How access works
IAM only decides who may connect (
dsql:DbConnect). Which tables a connection may touch is decided by the database role its IAM principal is mapped to (AWS IAM GRANT).DsqlCluster(CDK) declares roles and records which Lambda maps to which role./beesolve/dsql/<cluster id>/access-manifest.reconcileAccess. It creates roles, grants and revokes table privileges, and grants and revokes IAM mappings. Then it reads the state again and fails if anything is still different.Entry points
/cdk:DsqlClusterwithgrant,grantRead,grantWrite,grantReadWrite. A grant addsdsql:DbConnectonly (neverDbConnectAdmin) and setsDSQL_ENDPOINT,DSQL_REGIONandDSQL_DB_USER. Granting a second, different role to the same principal fails at synth.new DsqlCluster(this, "Dsql")keeps the cluster logical IDDsql, so the existing cluster is not replaced./admin:readAccessManifest,reconcileAccess(dryRun,prune), the pureplanAccess,readAccessState,repairFirstPendingMigration,readAsyncJobs. The manifest is validated before any SQL runs: identifier and privilege allowlists, an ARN pattern, and every principal must be in the cluster's account. A manifest that names a different cluster than the endpoint is refused..:createDsqlDatabase,toDsqlConfig,dsqlEnvEntries, genericDb,Tx,TransactandDbPropstypes, and SQLSTATE helpers.Peers are optional per entry point (
aws-cdk-lib/constructs,@aws/aurora-dsql-drizzle/drizzle-orm/pg).Design decisions:
docs/adr-001-motivation.mdanddocs/adr-002-manifest-and-admin-reconcile.md. The ADR-002 compares the admin step with a custom resource.Also in this PR
fix(aws-env-loader): the loader now prefers the resource whose logical ID is exactly the mapped prefix. The SSM parameterDsqlAccessManifest…shares theDsqlprefix, soDSQL_ENDPOINTcould have resolved to the parameter instead of the cluster. Has its own patch changeset and a regression test.Verification
bun run check(fmt + oxlint): 0 warnings, 0 errorstype-checkpasses forhelpers-dsqlandaws-env-loaderhelpers-dsql: 30 tests pass (CDK template assertions; the planner; the reconciler against an in-memory fake cluster)aws-env-loader: 118 tests pass; the new test fails without the fixbunupbuild andbun pm pack --dry-run: the tarball hasdist,DOCS.md,README.mdanddocs/how-to, and no ADRsNot verified against a live cluster. On stg, start with
reconcileAccess({ dryRun: true }). Two DSQL behaviours to confirm:AWS IAM REVOKEworks as documented;Before merge
0.0.0npm placeholder and register OIDC trust (publish-placeholder-packageskill).bun run add-packagefails at "closing ]); not found" against the currentbunup.config.ts. I added the build entry by hand and left the script unchanged.