import {z} from 'zod' export type SetupCloudAttestation = {type: 'aws_presigned_sts_get_caller_identity' | 'gcp_oidc_identity_token' | 'azure_entra_access_token', payload?: {[key: string]: unknown} | undefined}; export type SetupAuthorizationCreateRequest = {cloud_provider: 'aws' | 'gcp' | 'azure', requested_slug?: (string | null) | undefined, release_version?: (string | null) | undefined, cloud_attestation: SetupCloudAttestation}; export type CreateApiOnpremV1SetupAuthorizationsArgs = {body: SetupAuthorizationCreateRequest}; export type CreateApiOnpremV1SetupAuthorizationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const setupAuthorizationResponse = z.object({authorization_id: z.string(), setup_token: z.string(), expires_at: z.string(), cloud_provider: z.enum(["aws", "gcp", "azure"]), cloud_account: z.string(), cloud_principal: z.string().nullable().optional()}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseSetupAuthorizationResponse = z.object({success: z.literal(true), data: setupAuthorizationResponse, meta: responseMeta.optional()}); 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 }; /** * Create a short-lived broker setup token from customer cloud attestation * * POST /onprem/v1/setup-authorizations */ export const createApiOnpremV1SetupAuthorizations = async (args: CreateApiOnpremV1SetupAuthorizationsArgs, options: CreateApiOnpremV1SetupAuthorizationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/onprem/v1/setup-authorizations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(successResponseSetupAuthorizationResponse, await res.json()) };