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" } });