Skip to content

Domain.AWS reference

Source: packages/domain/src/AWS/DynamoKeyStore.ts Kind: Layer · Provides: Domain.KeyStore

A Vault’s keys in a DynamoDB table, one item per subject. The table’s partition key is subject, a string. Leave point-in-time recovery and backups off for it: a restored table brings shredded keys back.

import * as AWS from "@alchemy.run/aws";
import * as Domain from "@alchemy.run/domain";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const Keys = AWS.DynamoDB.Table("VaultKeys", {
partitionKey: "subject",
attributes: { subject: "S" },
});
export const RootKey = AWS.KMS.Key("VaultRootKey");
export const Sealing = Domain.VaultLive.pipe(
Layer.provide(DomainAWS.DynamoKeyStore(Keys)),
Layer.provide(DomainAWS.KmsKeyWrap(RootKey)),
Layer.provide(
Layer.mergeAll(
AWS.DynamoDB.GetItemHttp,
AWS.DynamoDB.PutItemHttp,
AWS.DynamoDB.DeleteItemHttp,
AWS.KMS.GenerateDataKeyHttp,
AWS.KMS.DecryptHttp,
),
),
);

Source: packages/domain/src/AWS/KmsKeyWrap.ts Kind: Layer · Provides: Domain.KeyWrap

Wrap each subject’s key under an AWS KMS key. KMS makes the subject’s key (GenerateDataKey) and unwraps it (Decrypt), with the subject as the encryption context, so a stored key unwraps only for the subject it was made for. The root key never leaves KMS.

Every seal and open is one Decrypt call.

import * as AWS from "@alchemy.run/aws";
import * as Domain from "@alchemy.run/domain";
import * as DomainAWS from "@alchemy.run/domain/AWS";
import * as Layer from "effect/Layer";
export const Keys = AWS.DynamoDB.Table("VaultKeys", {
partitionKey: "subject",
attributes: { subject: "S" },
});
export const RootKey = AWS.KMS.Key("VaultRootKey");
export const Sealing = Domain.VaultLive.pipe(
Layer.provide(DomainAWS.DynamoKeyStore(Keys)),
Layer.provide(DomainAWS.KmsKeyWrap(RootKey)),
Layer.provide(
Layer.mergeAll(
AWS.DynamoDB.GetItemHttp,
AWS.DynamoDB.PutItemHttp,
AWS.DynamoDB.DeleteItemHttp,
AWS.KMS.GenerateDataKeyHttp,
AWS.KMS.DecryptHttp,
),
),
);

intermediateKey has each process keep one intermediate key in memory and wrap subject keys under it locally, so KMS is called once per intermediate key instead of once per seal and open. The intermediate key is in the process’s memory, KMS no longer logs each subject’s access, and a process cut off from KMS reads on for up to keep. Shredding is unchanged.

DomainAWS.KmsKeyWrap(RootKey, { intermediateKey: { keep: "5 minutes" } });