import type { PublicMethodsOf } from '@kbn/utility-types' ; import type { ISavedObjectsRepository , ISavedObjectTypeRegistry , SavedObject , SavedObjectsBaseOptions , SavedObjectsServiceSetup , StartServicesAccessor , } from 'src/core/server' ; import type { SecurityPluginSetup } from '../../../security/server' ; import type { EncryptedSavedObjectsService } from '../crypto' ; import { EncryptedSavedObjectsClientWrapper } from './encrypted_saved_objects_client_wrapper' ; import { getDescriptorNamespace , normalizeNamespace } from './get_descriptor_namespace' ; export { normalizeNamespace } ; interface SetupSavedObjectsParams { service : PublicMethodsOf < EncryptedSavedObjectsService > ; savedObjects : SavedObjectsServiceSetup ; security ? : SecurityPluginSetup ; getStartServices : StartServicesAccessor ; } export type ClientInstanciator = ( options ? : EncryptedSavedObjectsClientOptions ) => EncryptedSavedObjectsClient ; export interface EncryptedSavedObjectsClientOptions { includedHiddenTypes ? : string [ ] ; } export interface EncryptedSavedObjectsClient { getDecryptedAsInternalUser : < T = unknown > ( type : string , id : string , options ? : SavedObjectsBaseOptions ) => Promise < SavedObject < T > > ; } export function setupSavedObjects ( { service , savedObjects , security , getStartServices , } ) { savedObjects . addClientWrapper ( Number . MAX_SAFE_INTEGER , 'encryptedSavedObjects' , ( { client : baseClient , typeRegistry : baseTypeRegistry , request } ) => new EncryptedSavedObjectsClientWrapper ( { baseClient , baseTypeRegistry , service , getCurrentUser : ( ) => security ?. authc . getCurrentUser ( request ) ?? undefined , } ) ) ; return ( clientOpts ) => { const internalRepositoryAndTypeRegistryPromise = getStartServices ( ) . then ( ( [ core ] ) => [ core . savedObjects . createInternalRepository ( clientOpts ?. includedHiddenTypes ) , core . savedObjects . getTypeRegistry ( ) , ] as [ ISavedObjectsRepository , ISavedObjectTypeRegistry ] ) ; return { getDecryptedAsInternalUser : async < T = unknown > ( type , id , options ? ) : Promise < SavedObject < T > > => { const [ internalRepository , typeRegistry ] = await internalRepositoryAndTypeRegistryPromise ; const savedObject = await internalRepository . get ( type , id , options ) ; return { ... savedObject , attributes : ( await service . decryptAttributes ( { type , id , namespace : getDescriptorNamespace ( typeRegistry , type , options ?. namespace ) , } , savedObject . attributes as Record < string , unknown > ) ) as T , } ; } , } ; } ; }