Domain.AWS reference
DynamoKeyStore
Section titled “DynamoKeyStore”Source:
packages/domain/src/AWS/DynamoKeyStore.tsKind: 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.
DynamoKeyStore: Keeping keys in DynamoDB
Section titled “DynamoKeyStore: Keeping keys in DynamoDB”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, ), ),);KmsKeyWrap
Section titled “KmsKeyWrap”Source:
packages/domain/src/AWS/KmsKeyWrap.tsKind: 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.
KmsKeyWrap: Wrapping keys under KMS
Section titled “KmsKeyWrap: Wrapping keys under KMS”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, ), ),);KmsKeyWrap: Calling KMS less often
Section titled “KmsKeyWrap: Calling KMS less often”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" } });