import type { BackendConfig } from "~/schemas/volumes"; import { cryptoUtils } from "~/server/utils/crypto"; type SecretTransformer = (value: string) => Promise; const transformOptionalSecret = async ( value: string | undefined, transformSecret: SecretTransformer, ): Promise => { if (!value) { return value; } return await transformSecret(value); }; export const mapVolumeConfigSecrets = async ( config: BackendConfig, transformSecret: SecretTransformer, ): Promise => { switch (config.backend) { case "smb": return { ...config, password: await transformOptionalSecret(config.password, transformSecret), }; case "webdav": return { ...config, password: await transformOptionalSecret(config.password, transformSecret), }; case "sftp": return { ...config, password: await transformOptionalSecret(config.password, transformSecret), privateKey: await transformOptionalSecret(config.privateKey, transformSecret), }; case "nfs": case "directory": case "rclone": return config; } }; export const encryptVolumeConfig = async (config: BackendConfig): Promise => { return await mapVolumeConfigSecrets(config, cryptoUtils.sealSecret); };