Skip to content

feat: add @beesolve/dsql-helpers (DSQL construct with per-table roles, access reconciler, Drizzle factory) - #122

Merged
ivanbarlog merged 3 commits into
mainfrom
feat/dsql-helpers
Nov 16, 2026
Merged

ivanbarlog merged 3 commits into
mainfrom
feat/dsql-helpers

Conversation

@kiro-agent

@kiro-agent kiro-agent Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

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).

  1. DsqlCluster (CDK) declares roles and records which Lambda maps to which role.
  2. The desired access is written to SSM: /beesolve/dsql/<cluster id>/access-manifest.
  3. After migrations, the admin step runs 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.
const dsql = new DsqlCluster(this, "Dsql", {
  roles: { digest: { privileges: ["SELECT", "UPDATE"], tables: ["pending_notifications", "users"] } },
});
dsql.grantReadWrite(siteHandler);    // app_readwrite on every table
dsql.grantRead(statsHandler);        // app_read
dsql.grantWrite(importer);           // app_write (no SELECT)
dsql.grant(digestHandler, "digest"); // custom role; the role name is typed

Entry points

  • /cdk: DsqlCluster with grant, grantRead, grantWrite, grantReadWrite. A grant adds dsql:DbConnect only (never DbConnectAdmin) and sets DSQL_ENDPOINT, DSQL_REGION and DSQL_DB_USER. Granting a second, different role to the same principal fails at synth. new DsqlCluster(this, "Dsql") keeps the cluster logical ID Dsql, so the existing cluster is not replaced.
  • /admin: readAccessManifest, reconcileAccess (dryRun, prune), the pure planAccess, 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, generic Db, Tx, Transact and DbProps types, 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.md and docs/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 parameter DsqlAccessManifest… shares the Dsql prefix, so DSQL_ENDPOINT could 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 errors
  • type-check passes for helpers-dsql and aws-env-loader
  • helpers-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 fix
  • bunup build and bun pm pack --dry-run: the tarball has dist, DOCS.md, README.md and docs/how-to, and no ADRs

Not verified against a live cluster. On stg, start with reconcileAccess({ dryRun: true }). Two DSQL behaviours to confirm:

  • AWS IAM REVOKE works as documented;
  • one IAM role cannot map to more than one database role (the construct assumes it can't).

Before merge

  • Publish the 0.0.0 npm placeholder and register OIDC trust (publish-placeholder-package skill).
  • bun run add-package fails at "closing ]); not found" against the current bunup.config.ts. I added the build entry by hand and left the script unchanged.

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
Comment on lines +92 to +108
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>;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be inferred from valibot schemas

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@ivanbarlog
ivanbarlog merged commit c0deff5 into main Nov 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants