|
| 1 | +import SCRIPT_SECRETS_STORE_SECRET from "worker:secrets-store/secret"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { ServiceDesignator, Worker_Binding } from "../../runtime"; |
| 4 | +import { KV_PLUGIN, KVOptionsSchema } from "../kv"; |
| 5 | +import { PersistenceSchema, Plugin, ProxyNodeBinding } from "../shared"; |
| 6 | + |
| 7 | +const SecretsStoreSecretsSchema = z.record( |
| 8 | + z.object({ |
| 9 | + store_id: z.string(), |
| 10 | + secret_name: z.string(), |
| 11 | + }) |
| 12 | +); |
| 13 | + |
| 14 | +export const SecretsStoreSecretsOptionsSchema = z.object({ |
| 15 | + secretsStoreSecrets: SecretsStoreSecretsSchema.optional(), |
| 16 | +}); |
| 17 | + |
| 18 | +export const SecretsStoreSecretsSharedOptionsSchema = z.object({ |
| 19 | + secretsStorePersist: PersistenceSchema, |
| 20 | +}); |
| 21 | + |
| 22 | +export const SECRET_STORE_PLUGIN_NAME = "secrets-store"; |
| 23 | + |
| 24 | +function getkvNamespacesOptions( |
| 25 | + secretsStoreSecrets: z.input<typeof SecretsStoreSecretsSchema> |
| 26 | +): z.input<typeof KVOptionsSchema> { |
| 27 | + // Get unique store ids |
| 28 | + const storeIds = new Set( |
| 29 | + Object.values(secretsStoreSecrets).map((store) => store.store_id) |
| 30 | + ); |
| 31 | + // Setup a KV Namespace per store id with store id as the binding name |
| 32 | + const storeIdKvNamespaceEntries = Array.from(storeIds).map((storeId) => [ |
| 33 | + storeId, |
| 34 | + `${SECRET_STORE_PLUGIN_NAME}:${storeId}`, |
| 35 | + ]); |
| 36 | + |
| 37 | + return { |
| 38 | + kvNamespaces: Object.fromEntries(storeIdKvNamespaceEntries), |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function isKvBinding( |
| 43 | + binding: Worker_Binding |
| 44 | +): binding is Worker_Binding & { kvNamespace: ServiceDesignator } { |
| 45 | + return "kvNamespace" in binding; |
| 46 | +} |
| 47 | + |
| 48 | +export const SECRET_STORE_PLUGIN: Plugin< |
| 49 | + typeof SecretsStoreSecretsOptionsSchema, |
| 50 | + typeof SecretsStoreSecretsSharedOptionsSchema |
| 51 | +> = { |
| 52 | + options: SecretsStoreSecretsOptionsSchema, |
| 53 | + sharedOptions: SecretsStoreSecretsSharedOptionsSchema, |
| 54 | + async getBindings(options) { |
| 55 | + if (!options.secretsStoreSecrets) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + const bindings = Object.entries( |
| 60 | + options.secretsStoreSecrets |
| 61 | + ).map<Worker_Binding>(([name, config]) => { |
| 62 | + return { |
| 63 | + name, |
| 64 | + service: { |
| 65 | + name: `${SECRET_STORE_PLUGIN_NAME}:${config.store_id}:${config.secret_name}`, |
| 66 | + entrypoint: "SecretsStoreSecret", |
| 67 | + }, |
| 68 | + }; |
| 69 | + }); |
| 70 | + return bindings; |
| 71 | + }, |
| 72 | + getNodeBindings(options: z.infer<typeof SecretsStoreSecretsOptionsSchema>) { |
| 73 | + if (!options.secretsStoreSecrets) { |
| 74 | + return {}; |
| 75 | + } |
| 76 | + return Object.fromEntries( |
| 77 | + Object.keys(options.secretsStoreSecrets).map((name) => [ |
| 78 | + name, |
| 79 | + new ProxyNodeBinding(), |
| 80 | + ]) |
| 81 | + ); |
| 82 | + }, |
| 83 | + async getServices({ options, sharedOptions, ...restOptions }) { |
| 84 | + if (!options.secretsStoreSecrets) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + const kvServices = await KV_PLUGIN.getServices({ |
| 89 | + options: getkvNamespacesOptions(options.secretsStoreSecrets), |
| 90 | + sharedOptions: { |
| 91 | + kvPersist: sharedOptions.secretsStorePersist, |
| 92 | + }, |
| 93 | + ...restOptions, |
| 94 | + }); |
| 95 | + |
| 96 | + const kvBindings = await KV_PLUGIN.getBindings( |
| 97 | + getkvNamespacesOptions(options.secretsStoreSecrets), |
| 98 | + restOptions.workerIndex |
| 99 | + ); |
| 100 | + |
| 101 | + if (!kvBindings || !kvBindings.every(isKvBinding)) { |
| 102 | + throw new Error( |
| 103 | + "Expected KV plugin to return bindings with kvNamespace defined" |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + if (!Array.isArray(kvServices)) { |
| 108 | + throw new Error("Expected KV plugin to return an array of services"); |
| 109 | + } |
| 110 | + |
| 111 | + return [ |
| 112 | + ...kvServices, |
| 113 | + ...Object.entries(options.secretsStoreSecrets).map<Worker_Binding>( |
| 114 | + ([_, config]) => { |
| 115 | + return { |
| 116 | + name: `${SECRET_STORE_PLUGIN_NAME}:${config.store_id}:${config.secret_name}`, |
| 117 | + worker: { |
| 118 | + compatibilityDate: "2025-01-01", |
| 119 | + modules: [ |
| 120 | + { |
| 121 | + name: "secret.worker.js", |
| 122 | + esModule: SCRIPT_SECRETS_STORE_SECRET(), |
| 123 | + }, |
| 124 | + ], |
| 125 | + bindings: [ |
| 126 | + { |
| 127 | + name: "store", |
| 128 | + kvNamespace: kvBindings.find( |
| 129 | + // Look up the corresponding KV namespace for the store id |
| 130 | + (binding) => binding.name === config.store_id |
| 131 | + )?.kvNamespace, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "secret_name", |
| 135 | + json: JSON.stringify(config.secret_name), |
| 136 | + }, |
| 137 | + ], |
| 138 | + }, |
| 139 | + }; |
| 140 | + } |
| 141 | + ), |
| 142 | + ]; |
| 143 | + }, |
| 144 | +}; |
0 commit comments