Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(secret-sync): address Daytona review feedback

InfisiCalvin657af331dab8
diff --git a/backend/src/lib/retry/index.ts b/backend/src/lib/retry/index.ts
index de71153995..2bd5af3443 100644
--- a/backend/src/lib/retry/index.ts
+++ b/backend/src/lib/retry/index.ts
@@ -1,4 +1,6 @@
/* eslint-disable no-await-in-loop */
+import { HttpStatusCode, isAxiosError } from "axios";
+
interface GitHubApiError extends Error {
status?: number;
response?: {
@@ -41,3 +43,25 @@ export const retryWithBackoff = async <T>(fn: () => Promise<T>, maxRetries = 3,
throw lastError!;
};
+
+/**
+ * Retries `fn` while the upstream answers 429, backing off exponentially.
+ *
+ * Deliberately narrower than `retryWithBackoff` above, which retries on any thrown error. Callers
+ * that turn 4xx responses into actionable messages (a rejected credential, a name collision) need
+ * those to surface on the first attempt rather than after several pointless round trips.
+ */
+export const retryOnRateLimit = async <T>(
+ fn: () => Promise<T>,
+ { maxRetries = 3, baseDelay = 500 }: { maxRetries?: number; baseDelay?: number } = {}
+): Promise<T> => {
+ for (let attempt = 0; ; attempt += 1) {
+ try {
+ return await fn();
+ } catch (error) {
+ const isRateLimited = isAxiosError(error) && error.response?.status === HttpStatusCode.TooManyRequests;
+ if (!isRateLimited || attempt >= maxRetries) throw error;
+ await delay(baseDelay * 2 ** attempt);
+ }
+ }
+};
diff --git a/backend/src/services/app-connection/daytona/daytona-connection-fns.ts b/backend/src/services/app-connection/daytona/daytona-connection-fns.ts
index 3f4f5f41f8..64edb66902 100644
--- a/backend/src/services/app-connection/daytona/daytona-connection-fns.ts
+++ b/backend/src/services/app-connection/daytona/daytona-connection-fns.ts
@@ -1,4 +1,4 @@
-import { AxiosError } from "axios";
+import { HttpStatusCode, isAxiosError } from "axios";
import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
@@ -38,14 +38,14 @@ export const validateDaytonaConnectionCredentials = async (config: TDaytonaConne
} catch (error: unknown) {
logger.error({ error }, "Failed to validate Daytona connection");
- if (error instanceof AxiosError) {
- if (error.response?.status === 401) {
+ if (isAxiosError(error)) {
+ if (error.response?.status === HttpStatusCode.Unauthorized) {
throw new BadRequestError({
message: "Unable to validate connection: the Daytona API key was rejected. Verify the key and try again."
});
}
- if (error.response?.status === 403) {
+ if (error.response?.status === HttpStatusCode.Forbidden) {
throw new BadRequestError({
message: `Unable to validate connection: the Daytona API key is missing the '${DAYTONA_MANAGE_SECRETS_PERMISSION}' permission. Grant it to the key in Daytona under Settings > API Keys.`
});
diff --git a/backend/src/services/secret-sync/daytona/daytona-sync-fns.ts b/backend/src/services/secret-sync/daytona/daytona-sync-fns.ts
index bcd5428025..1c4b316d49 100644
--- a/backend/src/services/secret-sync/daytona/daytona-sync-fns.ts
+++ b/backend/src/services/secret-sync/daytona/daytona-sync-fns.ts
@@ -1,8 +1,9 @@
/* eslint-disable no-await-in-loop */
-import { AxiosError } from "axios";
+import { HttpStatusCode, isAxiosError } from "axios";
import { request } from "@app/lib/config/request";
import { logger } from "@app/lib/logger";
+import { retryOnRateLimit } from "@app/lib/retry";
import { getDaytonaAuthHeaders } from "@app/services/app-connection/daytona";
import { IntegrationUrls } from "@app/services/integration-auth/integration-list";
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
@@ -16,27 +17,7 @@ const DAYTONA_SECRET_NAME_RULE =
"Daytona secret names must start with a letter or underscore and contain only letters, digits, hyphens and underscores.";
const DAYTONA_PAGE_SIZE = 100;
-const DAYTONA_MAX_PAGES = 100;
-
-const DAYTONA_MAX_RETRIES = 3;
-const DAYTONA_BASE_RETRY_DELAY_MS = 500;
-
-const sleep = (ms: number) =>
- new Promise<void>((resolve) => {
- setTimeout(resolve, ms);
- });
-
-const withDaytonaRetry = async <T>(fn: () => Promise<T>): Promise<T> => {
- for (let attempt = 0; ; attempt += 1) {
- try {
- return await fn();
- } catch (error) {
- const isRateLimited = error instanceof AxiosError && error.response?.status === 429;
- if (!isRateLimited || attempt >= DAYTONA_MAX_RETRIES) throw error;
- await sleep(DAYTONA_BASE_RETRY_DELAY_MS * 2 ** attempt);
- }
- }
-};
+const DAYTONA_MAX_PAGES = 10;
const listDaytonaSecrets = async (apiKey: string): Promise<TDaytonaSecret[]> => {
const secrets: TDaytonaSecret[] = [];
@@ -45,7 +26,7 @@ const listDaytonaSecrets = async (apiKey: string): Promise<TDaytonaSecret[]> =>
for (let page = 0; page < DAYTONA_MAX_PAGES; page += 1) {
const currentCursor = cursor;
- const { data } = await withDaytonaRetry(() =>
+ const { data } = await retryOnRateLimit(() =>
request.get<TDaytonaListSecretsResponse>(`${IntegrationUrls.DAYTONA_API_URL}/secret/paginated`, {
params: { limit: DAYTONA_PAGE_SIZE, ...(currentCursor ? { cursor: currentCursor } : {}) },
headers: getDaytonaAuthHeaders(apiKey)
@@ -66,7 +47,7 @@ const listDaytonaSecrets = async (apiKey: string): Promise<TDaytonaSecret[]> =>
};
const createDaytonaSecret = (apiKey: string, name: string, value: string) =>
- withDaytonaRetry(() =>
+ retryOnRateLimit(() =>
request.post(
`${IntegrationUrls.DAYTONA_API_URL}/secret`,
{ name, value },
@@ -75,7 +56,7 @@ const createDaytonaSecret = (apiKey: string, name: string, value: string) =>
);
const updateDaytonaSecretValue = (apiKey: string, secretId: string, value: string) =>
- withDaytonaRetry(() =>
+ retryOnRateLimit(() =>
request.patch(
`${IntegrationUrls.DAYTONA_API_URL}/secret/${encodeURIComponent(secretId)}`,
{ value },
@@ -84,7 +65,7 @@ const updateDaytonaSecretValue = (apiKey: string, secretId: string, value: strin
);
const deleteDaytonaSecret = (apiKey: string, secretId: string) =>
- withDaytonaRetry(() =>
+ retryOnRateLimit(() =>
request.delete(`${IntegrationUrls.DAYTONA_API_URL}/secret/${encodeURIComponent(secretId)}`, {
headers: getDaytonaAuthHeaders(apiKey)
})
@@ -127,7 +108,7 @@ export const DaytonaSyncFns = {
await createDaytonaSecret(apiKey, key, value);
}
} catch (error) {
- if (error instanceof AxiosError && error.response?.status === 409) {
+ if (isAxiosError(error) && error.response?.status === HttpStatusCode.Conflict) {
throw new SecretSyncError({
error,
secretKey: key,