import {z} from 'zod' export type AwsCredentialsV1 = {accessKeyId: string, secretAccessKey: string}; export type AwsKmsConfigUpdate = {credentials: AwsCredentialsV1, type: 'AWS', version: number}; export type AzureCredentialsV1 = {clientId: string, clientSecret: string, tenantGuid: string}; export type AzureKmsConfigUpdate = {credentials: AzureCredentialsV1, type: 'AZURE', version: number}; export type GcpCredentialsV1 = {client_email: string, private_key: string}; export type GcpKmsConfigUpdate = {credentials: GcpCredentialsV1, type: 'GCP', version: number}; export type ThalesCredentialsV1 = {authDomain: string, connection: string, username: string, password: string}; export type ThalesKmsConfigUpdate = {credentials: ThalesCredentialsV1, type: 'THALES', version: number}; export type KmsConfigurationUpdate = AwsKmsConfigUpdate | AzureKmsConfigUpdate | GcpKmsConfigUpdate | ThalesKmsConfigUpdate; export type UpdateApi1KmsConfigsKmsConfigIdArgs = {'icl-requestor-id': string, body: {config?: KmsConfigurationUpdate | undefined, leasingAllowed?: boolean | undefined}}; export type UpdateApi1KmsConfigsKmsConfigIdOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const kmsConfigId = z.number().int().gte(1); export const base64 = z.string(); export const kmsType = z.enum(["AZURE", "AWS", "GCP"]); export const createdByType = z.enum(["Tsp", "Admin", "Va"]); export const createdBy = z.object({id: z.string(), name: z.string(), type: createdByType}); export const timestamp = z.string(); export const updateApi1KmsConfigsKmsConfigIdResponse = z.object({id: kmsConfigId, encryptedConfig: base64, kmsType: kmsType, version: z.number().int().gte(1), createdBy: createdBy, leasingAllowed: z.boolean(), assignmentCount: z.number().int().gte(0), secretCount: z.number().int().gte(0), created: timestamp, updated: timestamp}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * KMS Config Update * * PUT /1/kms/configs/{kmsConfigId} */ export const updateApi1KmsConfigsKmsConfigId = async (args: UpdateApi1KmsConfigsKmsConfigIdArgs, options?: UpdateApi1KmsConfigsKmsConfigIdOptions) => { const {baseUrl = "http://localhost:3000/api", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['icl-requestor-id'] != null) headers.set("icl-requestor-id", String(args['icl-requestor-id'])) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/1/kms/configs/${encodeURIComponent(String(args.kmsConfigId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApi1KmsConfigsKmsConfigIdResponse, await res.json()) };