88 type ProviderInstanceConfig ,
99 type ProviderInstanceMutation ,
1010 defaultInstanceIdForDriver ,
11- isKnownSecretProviderKind ,
1211 isProviderDriverKind ,
1312 ProviderDriverKind ,
1413 ProviderInstanceId ,
@@ -53,6 +52,7 @@ import {
5352 isModelSelectionProviderEnabled ,
5453} from "@t3tools/shared/serverSettings" ;
5554import * as ServerSecretStore from "./auth/ServerSecretStore.ts" ;
55+ import { secretProviderDefinitions } from "./secrets/secretProviderDefinitions.ts" ;
5656import {
5757 readSecretFieldValues ,
5858 sourceControlHostSecretName ,
@@ -144,7 +144,7 @@ function withoutUnknownSecretProviderConfigs(
144144) : ServerSettings [ "secretProviders" ] {
145145 return Object . fromEntries (
146146 Object . entries ( settings . secretProviders ) . map ( ( [ id , entry ] ) => {
147- if ( isKnownSecretProviderKind ( entry . kind ) ) return [ id , entry ] ;
147+ if ( secretProviderConfigGuards . has ( entry . kind ) ) return [ id , entry ] ;
148148 const { config : _config , ...envelope } = entry ;
149149 return [ id , envelope ] ;
150150 } ) ,
@@ -164,6 +164,40 @@ export function applyProviderInstanceMutation(
164164 return { ...settings , providerInstances } ;
165165}
166166
167+ const secretProviderConfigGuards = new Map (
168+ secretProviderDefinitions . definitions . map ( ( definition ) => [
169+ definition . kind as string ,
170+ Schema . is ( definition . configSchema ) ,
171+ ] ) ,
172+ ) ;
173+
174+ // Rejects a kind this server has not loaded rather than storing a config it cannot check.
175+ function ensureSecretProviderWritesAllowed (
176+ patch : ServerSettingsPatch ,
177+ settingsPath : string ,
178+ ) : Effect . Effect < void , ServerSettingsError > {
179+ for ( const [ id , entry ] of Object . entries ( patch . secretProviders ?? { } ) ) {
180+ if ( entry === null ) continue ;
181+ const isConfig = secretProviderConfigGuards . get ( entry . kind ) ;
182+ const problem =
183+ isConfig === undefined
184+ ? `Unsupported secret provider kind: ${ entry . kind } .`
185+ : isConfig ( entry . config )
186+ ? undefined
187+ : `Invalid ${ entry . kind } config for secret provider ${ id } .` ;
188+ if ( problem !== undefined ) {
189+ return Effect . fail (
190+ new ServerSettingsError ( {
191+ settingsPath,
192+ operation : "write-secret-provider" ,
193+ cause : new Error ( problem ) ,
194+ } ) ,
195+ ) ;
196+ }
197+ }
198+ return Effect . void ;
199+ }
200+
167201function ensureProviderInstanceMutationAllowed (
168202 settings : ServerSettings ,
169203 mutation : ProviderInstanceMutation ,
@@ -1185,11 +1219,14 @@ const make = Effect.gen(function* () {
11851219 getSettings : getMaterializedSettings ,
11861220 updateSettings : ( patch ) =>
11871221 updateAndPersistSettings ( ( current ) =>
1188- Effect . succeed ( applyServerSettingsPatch ( current , patch ) ) ,
1222+ ensureSecretProviderWritesAllowed ( patch , settingsPath ) . pipe (
1223+ Effect . as ( applyServerSettingsPatch ( current , patch ) ) ,
1224+ ) ,
11891225 ) ,
11901226 updateProviderInstance : ( mutation , patch = { } ) =>
11911227 updateAndPersistSettings ( ( current ) =>
11921228 Effect . gen ( function * ( ) {
1229+ yield * ensureSecretProviderWritesAllowed ( patch , settingsPath ) ;
11931230 yield * ensureProviderInstanceMutationAllowed ( current , mutation , settingsPath ) ;
11941231 const patched = applyServerSettingsPatch ( current , patch ) ;
11951232 return applyProviderInstanceMutation ( patched , mutation ) ;
0 commit comments