|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# @wataruoguchi/emmett-crypto-shredding-kysely |
| 6 | + |
| 7 | +Kysely adapters for [Emmett Crypto Shredding](./emmett-crypto-shredding), providing PostgreSQL-backed implementations for key storage and policy resolution. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @wataruoguchi/emmett-crypto-shredding-kysely @wataruoguchi/emmett-crypto-shredding kysely pg |
| 13 | +``` |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +### 1. Run Database Migration |
| 18 | + |
| 19 | +Copy the migration file from `database/migrations/` to your migrations directory. |
| 20 | + |
| 21 | +### 2. Create Key Management and Policy Resolver |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { |
| 25 | + createKeyManagement, |
| 26 | + createPolicyResolver |
| 27 | +} from '@wataruoguchi/emmett-crypto-shredding-kysely'; |
| 28 | + |
| 29 | +const keyManagement = createKeyManagement(db); |
| 30 | +const policyResolver = createPolicyResolver(db, logger); |
| 31 | +``` |
| 32 | + |
| 33 | +### 3. Set Up Encryption Policies |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { createDefaultPolicies, createPolicies } from '@wataruoguchi/emmett-crypto-shredding-kysely'; |
| 37 | + |
| 38 | +// Option 1: Use default policies |
| 39 | +// Creates policies for common stream types: |
| 40 | +// - user-data: AES-GCM, 180 day rotation, stream scope |
| 41 | +// - audit-log: AES-GCM, 365 day rotation, stream scope |
| 42 | +await createDefaultPolicies(db, 'tenant-123'); |
| 43 | + |
| 44 | +// Option 2: Create custom policies |
| 45 | +await createPolicies(db, [ |
| 46 | + { |
| 47 | + policyId: 'tenant-123-user-data', |
| 48 | + partition: 'tenant-123', |
| 49 | + streamTypeClass: 'user-data', |
| 50 | + encryptionAlgorithm: 'AES-GCM', |
| 51 | + keyRotationIntervalDays: 180, |
| 52 | + keyScope: 'stream', |
| 53 | + }, |
| 54 | +]); |
| 55 | +``` |
| 56 | + |
| 57 | +### 4. Create Encrypted Event Store |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { createCryptoEventStore } from '@wataruoguchi/emmett-crypto-shredding'; |
| 61 | +import { getKyselyEventStore } from '@wataruoguchi/emmett-event-store-kysely'; |
| 62 | + |
| 63 | +const baseEventStore = getKyselyEventStore({ db, logger }); |
| 64 | + |
| 65 | +const cryptoEventStore = createCryptoEventStore({ |
| 66 | + baseEventStore, |
| 67 | + keyManagement, |
| 68 | + policyResolver, |
| 69 | + buildAAD: (ctx) => JSON.stringify({ |
| 70 | + partition: ctx.partition, |
| 71 | + streamId: ctx.streamId, |
| 72 | + streamType: ctx.streamType, |
| 73 | + eventType: ctx.eventType, |
| 74 | + }), |
| 75 | + logger, |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## API Reference |
| 80 | + |
| 81 | +### Key Management |
| 82 | + |
| 83 | +```typescript |
| 84 | +// Get or create active key |
| 85 | +const key = await keyManagement.getActiveKey({ |
| 86 | + partition: 'tenant-123', |
| 87 | + keyRef: 'user-data', |
| 88 | +}); |
| 89 | + |
| 90 | +// Rotate key |
| 91 | +await keyManagement.rotateKey({ |
| 92 | + partition: 'tenant-123', |
| 93 | + keyRef: 'user-data', |
| 94 | +}); |
| 95 | + |
| 96 | +// Destroy all keys for a partition (crypto shredding) |
| 97 | +await keyManagement.destroyPartitionKeys({ |
| 98 | + partition: 'tenant-123', |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +### Policy Management |
| 103 | + |
| 104 | +```typescript |
| 105 | +// Create policies |
| 106 | +await createPolicies(db, policies); |
| 107 | + |
| 108 | +// Update policy |
| 109 | +await updatePolicy(db, policyId, partition, { |
| 110 | + encryptionAlgorithm: 'AES-GCM', |
| 111 | + keyRotationIntervalDays: 365, |
| 112 | +}); |
| 113 | + |
| 114 | +// List policies |
| 115 | +const policies = await listPolicies(db, partition); |
| 116 | + |
| 117 | +// Delete policy |
| 118 | +await deletePolicy(db, policyId, partition); |
| 119 | +``` |
| 120 | + |
| 121 | +## See Also |
| 122 | + |
| 123 | +- [Crypto Shredding Core](./emmett-crypto-shredding) - Core encryption functionality |
| 124 | +- [Event Store Kysely](./emmett-event-store-kysely) - Base event store implementation |
| 125 | +- [Example Application](https://github.com/wataruoguchi/emmett-libs/tree/main/example) - Complete working example |
0 commit comments