Skip to content

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

add local secrets into the the dedupe map

adilson794376d8b4dc
diff --git a/backend/e2e-test/routes/v1/secret-replication.spec.ts b/backend/e2e-test/routes/v1/secret-replication.spec.ts
index a44e920bc4..4dbae2067a 100644
--- a/backend/e2e-test/routes/v1/secret-replication.spec.ts
+++ b/backend/e2e-test/routes/v1/secret-replication.spec.ts
@@ -386,3 +386,141 @@ describe.each([{ path: "/" }, { path: "/deep" }])(
},
{ timeout: 60_000 }
);
+
+describe(
+ "Secret replication of a source that also imports one of its own keys",
+ () => {
+ const sourcePath = "/replication-overlap";
+ const upstreamPath = "/replication-overlap-upstream";
+ const destinationPath = "/replication-overlap";
+
+ beforeAll(async () => {
+ const sourceFolder = await createFolder({
+ authToken: jwtAuthToken,
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ secretPath: "/",
+ name: "replication-overlap"
+ });
+
+ const upstreamFolder = await createFolder({
+ authToken: jwtAuthToken,
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ secretPath: "/",
+ name: "replication-overlap-upstream"
+ });
+
+ const destinationFolder = await createFolder({
+ authToken: jwtAuthToken,
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: seedData1.projectV3.id,
+ secretPath: "/",
+ name: "replication-overlap"
+ });
+
+ return async () => {
+ await deleteFolder({
+ authToken: jwtAuthToken,
+ secretPath: "/",
+ id: destinationFolder.id,
+ workspaceId: seedData1.projectV3.id,
+ environmentSlug: seedData1.environment.slug
+ });
+
+ await deleteFolder({
+ authToken: jwtAuthToken,
+ secretPath: "/",
+ id: sourceFolder.id,
+ workspaceId: seedData1.projectV3.id,
+ environmentSlug: "staging"
+ });
+
+ await deleteFolder({
+ authToken: jwtAuthToken,
+ secretPath: "/",
+ id: upstreamFolder.id,
+ workspaceId: seedData1.projectV3.id,
+ environmentSlug: "staging"
+ });
+ };
+ });
+
+ test("Replicates the source folder's own value of the key once", async () => {
+ await createTrackedSecret({
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ secretPath: upstreamPath,
+ authToken: jwtAuthToken,
+ key: "OVERLAP_KEY",
+ value: "upstream-value"
+ });
+
+ await createTrackedSecret({
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ secretPath: upstreamPath,
+ authToken: jwtAuthToken,
+ key: "UPSTREAM_ONLY_KEY",
+ value: "upstream-only-value"
+ });
+
+ await createTrackedSecret({
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ secretPath: sourcePath,
+ authToken: jwtAuthToken,
+ key: "OVERLAP_KEY",
+ value: "source-value"
+ });
+
+ await createSecretImport({
+ authToken: jwtAuthToken,
+ secretPath: sourcePath,
+ environmentSlug: "staging",
+ workspaceId: seedData1.projectV3.id,
+ importPath: upstreamPath,
+ importEnv: "staging"
+ });
+
+ await createSecretImport({
+ authToken: jwtAuthToken,
+ secretPath: destinationPath,
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: seedData1.projectV3.id,
+ importPath: sourcePath,
+ importEnv: "staging",
+ isReplication: true
+ });
+
+ await waitForReplicatedSecret({
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: seedData1.projectV3.id,
+ secretPath: destinationPath,
+ authToken: jwtAuthToken,
+ key: "UPSTREAM_ONLY_KEY",
+ value: "upstream-only-value"
+ });
+
+ const secret = await waitForReplicatedSecret({
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: seedData1.projectV3.id,
+ secretPath: destinationPath,
+ authToken: jwtAuthToken,
+ key: "OVERLAP_KEY",
+ value: "source-value"
+ });
+ expect(secret.secretValue).toBe("source-value");
+
+ const { imports } = await getSecretsV2({
+ environmentSlug: seedData1.environment.slug,
+ workspaceId: seedData1.projectV3.id,
+ secretPath: destinationPath,
+ authToken: jwtAuthToken
+ });
+ const replicatedKeys = imports.flatMap(({ secrets }) => secrets.map(({ secretKey }) => secretKey));
+ expect(replicatedKeys.filter((key) => key === "OVERLAP_KEY")).toHaveLength(1);
+ });
+ },
+ { timeout: 60_000 }
+);
diff --git a/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.test.ts b/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.test.ts
index 9670bfe6e0..840fb575a2 100644
--- a/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.test.ts
+++ b/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, test, vi } from "vitest";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { AUDIT_LOG_SENSITIVE_VALUE } from "@app/lib/config/const";
+import { BadRequestError } from "@app/lib/errors";
import { ActorType } from "@app/services/auth/auth-type";
import { SecretOperations } from "@app/services/secret/secret-types";
@@ -87,6 +88,21 @@ describe("detectSecretApprovalCommitConflicts", () => {
]);
expect(result.updates).toEqual([clean]);
});
+
+ test("refuses a request that creates the same key twice before reading the folder", async () => {
+ const { fns, secretV2BridgeDAL } = buildFns([]);
+ const commits = [
+ commit({ op: SecretOperations.Create, key: "A", id: "commit-A-1" }),
+ commit({ op: SecretOperations.Create, key: "A", id: "commit-A-2" }),
+ commit({ op: SecretOperations.Create, key: "B" })
+ ];
+
+ const result = fns.detectSecretApprovalCommitConflicts({ folderId: "folder-1", commits });
+
+ await expect(result).rejects.toBeInstanceOf(BadRequestError);
+ await expect(result).rejects.toThrow("it creates secret 'A' more than once");
+ expect(secretV2BridgeDAL.findBySecretKeys).not.toHaveBeenCalled();
+ });
});
describe("buildSecretMutationEvents", () => {
diff --git a/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.ts b/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.ts
index dcec091358..985142f667 100644
--- a/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.ts
+++ b/backend/src/ee/services/secret-approval-request/secret-approval-request-merge-fns.ts
@@ -5,7 +5,7 @@ import { SecretType } from "@app/db/schemas";
import { Actor, Event, EventType } from "@app/ee/services/audit-log/audit-log-types";
import { AUDIT_LOG_SENSITIVE_VALUE } from "@app/lib/config/const";
import { getConfig } from "@app/lib/config/env";
-import { NotFoundError } from "@app/lib/errors";
+import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { groupBy } from "@app/lib/fn";
import { logger } from "@app/lib/logger";
import { ActorType } from "@app/services/auth/auth-type";
@@ -271,6 +271,15 @@ export const secretApprovalRequestMergeFnsFactory = ({
const conflicts: TSecretApprovalCommitConflict[] = [];
let creates = commits.filter(({ op }) => op === SecretOperations.Create);
+ const keysCreatedTwice = Object.entries(groupBy(creates, (el) => el.key))
+ .filter(([, group]) => group.length > 1)
+ .map(([key]) => `'${key}'`);
+ if (keysCreatedTwice.length) {
+ throw new BadRequestError({
+ message: `Could not merge this change request because it creates ${keysCreatedTwice.length === 1 ? "secret" : "secrets"} ${keysCreatedTwice.join(", ")} more than once. Close the request, then submit the change again or resync the replicated import that filed it.`
+ });
+ }
+
if (creates.length) {
const secrets = await secretV2BridgeDAL.findBySecretKeys(
folderId,
diff --git a/backend/src/ee/services/secret-replication/secret-replication-service.ts b/backend/src/ee/services/secret-replication/secret-replication-service.ts
index e756d7dc4d..ba6d609382 100644
--- a/backend/src/ee/services/secret-replication/secret-replication-service.ts
+++ b/backend/src/ee/services/secret-replication/secret-replication-service.ts
@@ -184,7 +184,7 @@ export const secretReplicationServiceFactory = ({
})[];
}[]
) => {
- const deDupe = new Set<string>();
+ const deDupe = new Set<string>(localSecrets.map(({ key }) => key));
const secrets = [...localSecrets];
for (let i = importedSecrets.length - 1; i >= 0; i = -1) {