Domain.GCP reference
FirestoreKeyStore
Section titled “FirestoreKeyStore”Source:
packages/domain/src/GCP/FirestoreKeyStore.tsKind: Layer · Provides:Domain.KeyStore
A Vault’s keys in a Firestore database, one document per subject. Leave point-in-time recovery and backup schedules off for it: a restored database brings shredded keys back.
FirestoreKeyStore: Keeping keys in Firestore
Section titled “FirestoreKeyStore: Keeping keys in Firestore”import * as Domain from "@alchemy.run/domain";import * as DomainGCP from "@alchemy.run/domain/GCP";import * as GCP from "@alchemy.run/gcp";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";
export const Keys = GCP.Firestore.Database("VaultKeys", { location: "us-central1", type: "FIRESTORE_NATIVE",});export const RootKey = Effect.gen(function* () { const keyRing = yield* GCP.KMS.KeyRing("Vault", { location: "us-central1" }); return yield* GCP.KMS.CryptoKey("VaultRootKey", { keyRing: keyRing.name });});
export const Sealing = Domain.VaultLive.pipe( Layer.provide(DomainGCP.FirestoreKeyStore(Keys)), Layer.provide(DomainGCP.KmsKeyWrap(RootKey)), Layer.provide( Layer.mergeAll( GCP.Firestore.GetDocumentHttp, GCP.Firestore.PatchDocumentHttp, GCP.Firestore.DeleteDocumentHttp, GCP.KMS.EncryptHttp, GCP.KMS.DecryptHttp, ), ),);KmsKeyWrap
Section titled “KmsKeyWrap”Source:
packages/domain/src/GCP/KmsKeyWrap.tsKind: Layer · Provides:Domain.KeyWrap
Wrap each subject’s key under a Cloud KMS key. KMS encrypts and decrypts the subject’s key with the subject as additional authenticated data, 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 Cloud KMS
Section titled “KmsKeyWrap: Wrapping keys under Cloud KMS”import * as Domain from "@alchemy.run/domain";import * as DomainGCP from "@alchemy.run/domain/GCP";import * as GCP from "@alchemy.run/gcp";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";
export const Keys = GCP.Firestore.Database("VaultKeys", { location: "us-central1", type: "FIRESTORE_NATIVE",});export const RootKey = Effect.gen(function* () { const keyRing = yield* GCP.KMS.KeyRing("Vault", { location: "us-central1" }); return yield* GCP.KMS.CryptoKey("VaultRootKey", { keyRing: keyRing.name });});
export const Sealing = Domain.VaultLive.pipe( Layer.provide(DomainGCP.FirestoreKeyStore(Keys)), Layer.provide(DomainGCP.KmsKeyWrap(RootKey)), Layer.provide( Layer.mergeAll( GCP.Firestore.GetDocumentHttp, GCP.Firestore.PatchDocumentHttp, GCP.Firestore.DeleteDocumentHttp, GCP.KMS.EncryptHttp, GCP.KMS.DecryptHttp, ), ),);KmsKeyWrap: Calling Cloud KMS less often
Section titled “KmsKeyWrap: Calling Cloud KMS less often”intermediateKey has each process keep one intermediate key in memory
and wrap subject keys under it locally, so Cloud KMS is called once per
intermediate key instead of once per seal and open. The intermediate key
is in the process’s memory, Cloud KMS no longer logs each subject’s access,
and a process cut off from Cloud KMS reads on for up to keep. Shredding is
unchanged.
DomainGCP.KmsKeyWrap(RootKey, { intermediateKey: { keep: "5 minutes" } });KmsMacReceiptKey
Section titled “KmsMacReceiptKey”Source:
packages/domain/src/GCP/KmsMacReceiptKey.tsKind: Layer · Provides:Domain.ReceiptKey
Sign receipts with a MAC key in Cloud KMS (macSign, checked with
macVerify). The key never leaves Cloud KMS: no process ever holds it,
and Cloud Audit Logs can record every signature and every check.
That costs one Cloud KMS operation per receipt: one macSign per command
(and per commit of a Decision), and one macVerify per signed receipt a
refused read passes back. At a million commands a month that is a million
operations, under your project’s Cloud KMS quota. Use
KmsReceiptKey to call Cloud KMS once per process every few minutes
instead.
A MAC key has no primary version: receipts are signed with the version you pass. Signing with a new version leaves receipts signed by the old one unchecked, so a caller holding one is refused at once rather than waiting.
KmsMacReceiptKey: Signing receipts inside Cloud KMS
Section titled “KmsMacReceiptKey: Signing receipts inside Cloud KMS”import * as DomainGCP from "@alchemy.run/domain/GCP";import * as GCP from "@alchemy.run/gcp";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";
export const ReceiptMacVersion = Effect.gen(function* () { const keyRing = yield* GCP.KMS.KeyRing("Receipts", { location: "us-central1" }); const key = yield* GCP.KMS.CryptoKey("ReceiptMacKey", { keyRing: keyRing.name, purpose: "MAC", versionTemplate: { algorithm: "HMAC_SHA256" }, skipInitialVersionCreation: true, }); return yield* GCP.KMS.CryptoKeyVersion("ReceiptMacVersion", { cryptoKey: key.name });});
export const Receipts = DomainGCP.KmsMacReceiptKey(ReceiptMacVersion).pipe( Layer.provide(Layer.mergeAll(GCP.KMS.MacSignHttp, GCP.KMS.MacVerifyHttp)),);KmsReceiptKey
Section titled “KmsReceiptKey”Source:
packages/domain/src/GCP/KmsReceiptKey.tsKind: Layer · Provides:Domain.ReceiptKey
Sign receipts with keys a Cloud KMS key wraps, calling Cloud KMS once per
signing key instead of once per receipt. Each process makes a signing key,
has Cloud KMS wrap it (encrypt), signs with it in memory for keep,
then makes another. A signature carries its key as Cloud KMS wrapped it,
so any process checks it with one decrypt the first time it sees that
key, and keeps the key for keep.
Cloud KMS operations grow with processes, not traffic: each process makes
one encrypt per keep, and at most one decrypt per key it is shown,
never more than one per refused read. Fifty processes with the default
keep of 15 minutes make about 144,000 encrypt operations a month,
however many commands they serve. What it trades: a signing key is in the
process’s memory, Cloud Audit Logs record one operation per key rather
than per receipt, and a process whose access to the key is revoked signs
and checks for up to keep more.
Use KmsMacReceiptKey when the key must never leave Cloud KMS, at
one operation per receipt. See
Cloud KMS pricing.
KmsReceiptKey: Signing receipts with keys Cloud KMS wraps
Section titled “KmsReceiptKey: Signing receipts with keys Cloud KMS wraps”import * as DomainGCP from "@alchemy.run/domain/GCP";import * as GCP from "@alchemy.run/gcp";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";
export const ReceiptRootKey = Effect.gen(function* () { const keyRing = yield* GCP.KMS.KeyRing("Receipts", { location: "us-central1" }); return yield* GCP.KMS.CryptoKey("ReceiptRootKey", { keyRing: keyRing.name });});
export const Receipts = DomainGCP.KmsReceiptKey(ReceiptRootKey).pipe( Layer.provide(Layer.mergeAll(GCP.KMS.EncryptHttp, GCP.KMS.DecryptHttp)),);KmsReceiptKey: Tuning how often Cloud KMS is called
Section titled “KmsReceiptKey: Tuning how often Cloud KMS is called”keep sets how long a process signs with one key and keeps each key it
unwrapped. Raise it to call Cloud KMS less; lower it to cut a revoked
process off sooner.
DomainGCP.KmsReceiptKey(ReceiptRootKey, { keep: "1 hour" });