| import { getActiveBackend } from "../backend-registry/active-store"; |
| import type { Backend } from "../backend-registry/types"; |
| import type { CustomSecretWithoutValue } from "../secrets-service.types"; |
| import { withRetry } from "../with-retry"; |
| import { callCloudProxy } from "./proxy"; |
|
|
| interface CloudSecretsPage { |
| items: CustomSecretWithoutValue[]; |
| next_page_id: string | null; |
| } |
|
|
| const PAGE_LIMIT = 100; |
|
|
| function getActiveCloudBackend(): Backend { |
| const active = getActiveBackend().backend; |
| if (active.kind !== "cloud") { |
| throw new Error("Cloud secrets call requires a cloud backend."); |
| } |
| return active; |
| } |
|
|
| |
| |
| |
| |
| |
| export async function fetchCloudSecrets(): Promise<CustomSecretWithoutValue[]> { |
| const backend = getActiveCloudBackend(); |
|
|
| const secrets: CustomSecretWithoutValue[] = []; |
| let pageId: string | null = null; |
|
|
| do { |
| const query = new URLSearchParams({ limit: String(PAGE_LIMIT) }); |
| if (pageId) query.set("page_id", pageId); |
|
|
| const page = await callCloudProxy<CloudSecretsPage>({ |
| backend, |
| method: "GET", |
| path: `/api/v1/secrets/search?${query.toString()}`, |
| }); |
|
|
| secrets.push(...(page.items ?? [])); |
| pageId = page.next_page_id; |
| } while (pageId); |
|
|
| return secrets; |
| } |
|
|
| export interface SaveCloudSecretOptions { |
| |
| name: string; |
| |
| value?: string; |
| description?: string; |
| |
| previousName?: string; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function saveCloudSecret({ |
| name, |
| value, |
| description, |
| previousName, |
| }: SaveCloudSecretOptions): Promise<void> { |
| const backend = getActiveCloudBackend(); |
|
|
| if (previousName !== undefined) { |
| await withRetry(() => |
| callCloudProxy<unknown>({ |
| backend, |
| method: "PUT", |
| path: `/api/v1/secrets/${encodeURIComponent(previousName)}`, |
| body: { name, description }, |
| }), |
| ); |
| } |
|
|
| if (value !== undefined) { |
| await withRetry(() => |
| callCloudProxy<unknown>({ |
| backend, |
| method: "POST", |
| path: "/api/v1/secrets", |
| body: { name, value, description }, |
| }), |
| ); |
| } |
| } |
|
|
| export async function deleteCloudSecret(name: string): Promise<void> { |
| const backend = getActiveCloudBackend(); |
| await callCloudProxy<unknown>({ |
| backend, |
| method: "DELETE", |
| path: `/api/v1/secrets/${encodeURIComponent(name)}`, |
| }); |
| } |
|
|