Skip to content

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

feat(secret-sync): detect duplicate Daytona syncs by organization

Calvin51ccae1c100b
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 69104e1aae..d0e6548b54 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -2882,7 +2882,9 @@ export const AppConnections = {
apiKey: "The Devin service-user API key used to authenticate against the Devin v3 API."
},
DAYTONA: {
- apiKey: "The Daytona API key used to authenticate with Daytona. It must carry the manage:secrets permission."
+ apiKey: "The Daytona API key used to authenticate with Daytona. It must carry the manage:secrets permission.",
+ organizationId:
+ "The Daytona organization the API key belongs to. Resolved from Daytona when the connection is created."
},
GITLAB: {
instanceUrl: "The GitLab instance URL to connect with.",
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 de18151f6b..8ba093c151 100644
--- a/backend/src/services/app-connection/daytona/daytona-connection-fns.ts
+++ b/backend/src/services/app-connection/daytona/daytona-connection-fns.ts
@@ -10,6 +10,10 @@ import { TDaytonaConnectionConfig } from "./daytona-connection-types";
const DAYTONA_MANAGE_SECRETS_PERMISSION = "manage:secrets";
+type TDaytonaCurrentApiKeyResponse = {
+ organizationId?: string;
+};
+
export const getDaytonaAuthHeaders = (apiKey: string) => ({
Authorization: `Bearer ${apiKey}`,
Accept: "application/json",
@@ -63,5 +67,30 @@ export const validateDaytonaConnectionCredentials = async (config: TDaytonaConne
});
}
- return config.credentials;
+ // A Daytona API key is bound to one organization, so this is the only way to learn which one a
+ // connection writes to. It is stored on the connection and used to tell whether two syncs share a
+ // destination, which nothing else about the key reveals.
+ let organizationId: string | undefined;
+ try {
+ const { data } = await safeRequest.get<TDaytonaCurrentApiKeyResponse>(
+ `${IntegrationUrls.DAYTONA_API_URL}/api-keys/current`,
+ { headers: getDaytonaAuthHeaders(apiKey) }
+ );
+ organizationId = data.organizationId;
+ } catch (error: unknown) {
+ throw new BadRequestError({
+ message: `Unable to validate connection: Daytona returned ${
+ isAxiosError(error) ? (error.response?.status ?? "no") : "no"
+ } status when reading the API key's organization. Verify the API key and try again.`
+ });
+ }
+
+ if (!organizationId) {
+ throw new BadRequestError({
+ message:
+ "Unable to validate connection: Daytona did not report an organization for this API key. Create the key under the organization whose secrets you want to sync to."
+ });
+ }
+
+ return { ...config.credentials, organizationId };
};
diff --git a/backend/src/services/app-connection/daytona/daytona-connection-schemas.ts b/backend/src/services/app-connection/daytona/daytona-connection-schemas.ts
index c7e05fd448..c9e68e9781 100644
--- a/backend/src/services/app-connection/daytona/daytona-connection-schemas.ts
+++ b/backend/src/services/app-connection/daytona/daytona-connection-schemas.ts
@@ -15,19 +15,26 @@ export const DaytonaConnectionApiKeyCredentialsSchema = z.object({
apiKey: z.string().trim().min(1, "API Key required").max(500).describe(AppConnections.CREDENTIALS.DAYTONA.apiKey)
});
+// A Daytona API key is bound to one organization, so the organization is resolved during validation
+// rather than supplied by the caller. It is stored on the connection and read back to tell whether
+// two syncs write to the same organization.
+export const DaytonaConnectionApiKeyStoredCredentialsSchema = DaytonaConnectionApiKeyCredentialsSchema.extend({
+ organizationId: z.string().trim().min(1).max(255).describe(AppConnections.CREDENTIALS.DAYTONA.organizationId)
+});
+
const BaseDaytonaConnectionSchema = BaseAppConnectionSchema.extend({
app: z.literal(AppConnection.Daytona)
});
export const DaytonaConnectionSchema = BaseDaytonaConnectionSchema.extend({
method: z.literal(DaytonaConnectionMethod.ApiKey),
- credentials: DaytonaConnectionApiKeyCredentialsSchema
+ credentials: DaytonaConnectionApiKeyStoredCredentialsSchema
});
export const SanitizedDaytonaConnectionSchema = z.discriminatedUnion("method", [
BaseDaytonaConnectionSchema.extend({
method: z.literal(DaytonaConnectionMethod.ApiKey),
- credentials: DaytonaConnectionApiKeyCredentialsSchema.pick({})
+ credentials: DaytonaConnectionApiKeyStoredCredentialsSchema.pick({ organizationId: true })
}).describe(JSON.stringify({ title: `${APP_CONNECTION_NAME_MAP[AppConnection.Daytona]} (API Key)` }))
]);
diff --git a/backend/src/services/secret-sync/secret-sync-maps.ts b/backend/src/services/secret-sync/secret-sync-maps.ts
index 2b7a645e3e..466113c511 100644
--- a/backend/src/services/secret-sync/secret-sync-maps.ts
+++ b/backend/src/services/secret-sync/secret-sync-maps.ts
@@ -1,6 +1,7 @@
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
import { buildAwsConnectionConfig, getAwsAccountId } from "@app/services/app-connection/aws/aws-connection-fns";
import { TAwsConnection } from "@app/services/app-connection/aws/aws-connection-types";
+import { TDaytonaConnection } from "@app/services/app-connection/daytona";
import { GcpSyncScope } from "@app/services/secret-sync/gcp/gcp-sync-enums";
import { SecretSync, SecretSyncPlanType } from "@app/services/secret-sync/secret-sync-enums";
import { DestinationDuplicateCheckFn } from "@app/services/secret-sync/secret-sync-types";
@@ -250,6 +251,29 @@ const awsDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, ne
return existingAccountId === newAccountId;
};
+// A Daytona API key is bound to one organization and nothing scopes below it, so two syncs collide
+// exactly when their connections resolve to the same organization, whether or not they share a
+// connection.
+const daytonaDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync, decryptConnection }) => {
+ // Without a connection on either side there is no organization to compare, and an empty Daytona
+ // config matches every other sync, so treating that as a collision would block them all.
+ if (!newSync.connectionId || !existingSync.connectionId) return false;
+
+ if (existingSync.connectionId === newSync.connectionId) return true;
+
+ const [existingConn, newConn] = await Promise.all([
+ decryptConnection(existingSync.connectionId),
+ decryptConnection(newSync.connectionId)
+ ]);
+
+ const existingOrgId = (existingConn as TDaytonaConnection).credentials.organizationId;
+ const newOrgId = (newConn as TDaytonaConnection).credentials.organizationId;
+
+ if (!existingOrgId || !newOrgId) return false;
+
+ return existingOrgId === newOrgId;
+};
+
const gcpDuplicateCheck: DestinationDuplicateCheckFn = async ({ existingSync, newSync }) => {
const existingConfig = existingSync.destinationConfig;
const newConfig = newSync.destinationConfig;
@@ -339,11 +363,7 @@ export const DESTINATION_DUPLICATE_CHECK_MAP: Record<SecretSync, DestinationDupl
[SecretSync.Qovery]: defaultDuplicateCheck,
[SecretSync.Cloud66]: defaultDuplicateCheck,
[SecretSync.Spacelift]: defaultDuplicateCheck,
- // Daytona has an empty destination config, so checkDuplicateDestination returns early and never
- // reaches this. Kept accurate rather than defaulted: a Daytona API key is bound to one organization
- // and nothing scopes below it, so two syncs sharing a connection do write the same secrets.
- [SecretSync.Daytona]: async ({ existingSync, newSync }) =>
- Boolean(newSync.connectionId) && existingSync.connectionId === newSync.connectionId
+ [SecretSync.Daytona]: daytonaDuplicateCheck
};
/**
diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts
index f1b50361ac..5f0dda96b4 100644
--- a/backend/src/services/secret-sync/secret-sync-service.ts
+++ b/backend/src/services/secret-sync/secret-sync-service.ts
@@ -276,7 +276,10 @@ export const secretSyncServiceFactory = ({
ProjectPermissionSub.SecretSyncs
);
- if (!destinationConfig || Object.keys(destinationConfig).length === 0) {
+ // An absent config means the caller has not filled the form in yet, so there is nothing to
+ // compare. An empty one is different: a destination whose scope comes entirely from its
+ // connection has no fields to send, and skipping those would leave them with no check at all.
+ if (!destinationConfig) {
return { hasDuplicate: false, duplicateProjectId: undefined };
}
diff --git a/docs/integrations/app-connections/daytona.mdx b/docs/integrations/app-connections/daytona.mdx
index e717d4cae4..73c379b943 100644
--- a/docs/integrations/app-connections/daytona.mdx
+++ b/docs/integrations/app-connections/daytona.mdx
@@ -54,7 +54,7 @@ A Daytona API key belongs to exactly one organization. The key you choose theref
![Daytona Connection Form](/images/app-connections/daytona/daytona-app-connection-form.png)
</Step>
<Step title="Connection created">
- Infisical validates the key against Daytona. Once it passes, the connection is ready to use in your project.
+ Infisical validates the key against Daytona and records the organization it belongs to. Once it passes, the connection is ready to use in your project.
</Step>
</Steps>
</Tab>