The best way to control Claude Code, Codex and more from any computer or phone

main
ts 84 lines 3 KB
Raw
1 import type * as kms from "@distilled.cloud/aws/kms";
2 import * as Binding from "alchemy/Binding";
3 import type * as Effect from "effect/Effect";
4 import type { AliasName } from "./Alias.ts";
5 import type { Key } from "./Key.ts";
6
7 export interface GenerateDataKeyRequest extends Omit<kms.GenerateDataKeyRequest, "KeyId"> {}
8
9 /**
10 * Runtime binding for `kms:GenerateDataKey`.
11 *
12 * Bind this operation to a KMS {@link Key} (or the `alias/...` name of a
13 * pre-existing key) inside a function runtime to get a callable that
14 * automatically injects the `KeyId`. Returns a fresh symmetric data key as
15 * both plaintext (for immediate envelope encryption outside KMS) and a
16 * ciphertext blob encrypted under the bound key (for storage alongside the
17 * data). Decrypt the stored blob later with the `Decrypt` binding.
18 *
19 * The `Plaintext` data key in the response is wrapped in `Redacted` so it
20 * never leaks into logs — unwrap with `Redacted.value(...)` at the point of
21 * use and discard it as soon as the envelope operation is done.
22 *
23 * ### Envelope Encryption
24 * **Example:** Generate a Data Key
25 * ```typescript
26 * import * as Redacted from "effect/Redacted";
27 *
28 * const generateDataKey = yield* AWS.KMS.GenerateDataKey(key);
29 *
30 * const response = yield* generateDataKey({ KeySpec: "AES_256" });
31 * const dataKey = Redacted.isRedacted(response.Plaintext)
32 * ? Redacted.value(response.Plaintext)
33 * : response.Plaintext; // 32-byte Uint8Array — use, then discard
34 * const stored = response.CiphertextBlob; // persist next to the data
35 * ```
36 *
37 * **Example:** Recover the Data Key Later
38 * ```typescript
39 * const decrypt = yield* AWS.KMS.Decrypt(key);
40 * const recovered = yield* decrypt({ CiphertextBlob: stored });
41 * ```
42 *
43 * ### Pre-Existing Keys
44 * **Example:** Bind by Alias Name
45 * ```typescript
46 * const generateDataKey = yield* AWS.KMS.GenerateDataKey("alias/app-key");
47 * ```
48 *
49 * ### Wiring
50 * **Example:** Provide the Implementation on a Lambda Function
51 * ```typescript
52 * // Envelope encryption pairs GenerateDataKey with Decrypt — provide
53 * // both HTTP layers on the Function's init Effect.
54 * export default EnvelopeFunction.make(
55 * { main: import.meta.url, functionUrl: true },
56 * Effect.gen(function* () {
57 * const key = yield* AWS.KMS.Key("DataKey");
58 * const generateDataKey = yield* AWS.KMS.GenerateDataKey(key);
59 * const decrypt = yield* AWS.KMS.Decrypt(key);
60 * // ... generate a data key, encrypt locally, store the CiphertextBlob
61 * return { fetch: handler };
62 * }).pipe(
63 * Effect.provide(
64 * Layer.mergeAll(AWS.KMS.GenerateDataKeyHttp, AWS.KMS.DecryptHttp),
65 * ),
66 * ),
67 * );
68 * ```
69 *
70 * @binding
71 */
72 export interface GenerateDataKey extends Binding.Service<
73 GenerateDataKey,
74 "AWS.KMS.GenerateDataKey",
75 (
76 key: Key | AliasName,
77 ) => Effect.Effect<
78 (
79 request: GenerateDataKeyRequest,
80 ) => Effect.Effect<kms.GenerateDataKeyResponse, kms.GenerateDataKeyError>
81 >
82 > {}
83
84 export const GenerateDataKey = Binding.Service<GenerateDataKey>("AWS.KMS.GenerateDataKey");