Skip to content

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

misc: address comments

Sheen Capadngan066dd9860e87
diff --git a/backend/src/db/migrations/20260206151238_add-uniqueness-constraints-for-pam.ts b/backend/src/db/migrations/20260206151238_add-uniqueness-constraints-for-pam.ts
index b2deb01d44..fd23435bda 100644
--- a/backend/src/db/migrations/20260206151238_add-uniqueness-constraints-for-pam.ts
+++ b/backend/src/db/migrations/20260206151238_add-uniqueness-constraints-for-pam.ts
@@ -16,7 +16,7 @@ export async function up(knex: Knex): Promise<void> {
// Rename any duplicate resources (keep oldest, append random suffix to newer ones)
await knex.raw(`
UPDATE ${TableName.PamResource} r1
- SET "name" = r1."name" || '-' || substr(md5(random()::text), 1, 3)
+ SET "name" = r1."name" || '-' || substr(md5(random()::text), 1, 4)
WHERE EXISTS (
SELECT 1 FROM ${TableName.PamResource} r2
WHERE r2."projectId" = r1."projectId"
@@ -38,10 +38,15 @@ export async function up(knex: Knex): Promise<void> {
const hasResourceId = await knex.schema.hasColumn(TableName.PamAccount, "resourceId");
if (hasName && hasResourceId) {
+ // Drop the old project/folder-level uniqueness constraints from the original PAM migration,
+ // since accounts should only be unique within a resource, not across the entire project.
+ await dropConstraintIfExists(TableName.PamAccount, "uidx_pam_account_children_name", knex);
+ await dropConstraintIfExists(TableName.PamAccount, "uidx_pam_account_root_name", knex);
+
// Rename any duplicate accounts (keep oldest, append random suffix to newer ones)
await knex.raw(`
UPDATE ${TableName.PamAccount} a1
- SET "name" = a1."name" || '-' || substr(md5(random()::text), 1, 3)
+ SET "name" = a1."name" || '-' || substr(md5(random()::text), 1, 4)
WHERE EXISTS (
SELECT 1 FROM ${TableName.PamAccount} a2
WHERE a2."resourceId" = a1."resourceId"
@@ -65,5 +70,28 @@ export async function down(knex: Knex): Promise<void> {
if (await knex.schema.hasTable(TableName.PamAccount)) {
await dropConstraintIfExists(TableName.PamAccount, ACCOUNT_CONSTRAINT_NAME, knex);
+
+ // Restore the original project/folder-level uniqueness constraints
+ const hasFolderId = await knex.schema.hasColumn(TableName.PamAccount, "folderId");
+ const hasProjectId = await knex.schema.hasColumn(TableName.PamAccount, "projectId");
+ const hasName = await knex.schema.hasColumn(TableName.PamAccount, "name");
+
+ if (hasName && hasProjectId) {
+ if (hasFolderId) {
+ await knex.schema.alterTable(TableName.PamAccount, (table) => {
+ table.unique(["projectId", "folderId", "name"], {
+ indexName: "uidx_pam_account_children_name",
+ predicate: knex.whereNotNull("folderId")
+ });
+ });
+ }
+
+ await knex.schema.alterTable(TableName.PamAccount, (table) => {
+ table.unique(["projectId", "name"], {
+ indexName: "uidx_pam_account_root_name",
+ predicate: knex.whereNull("folderId")
+ });
+ });
+ }
}
}
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 025b4e5e24..6af7b8da7c 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -224,7 +224,7 @@ export const pamAccountServiceFactory = ({
} catch (err) {
if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
throw new BadRequestError({
- message: `Account with name '${name}' already exists for this path`
+ message: `Account with name '${name}' already exists for this resource`
});
}
@@ -365,7 +365,7 @@ export const pamAccountServiceFactory = ({
} catch (err) {
if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
throw new BadRequestError({
- message: `Account with name '${name}' already exists for this path`
+ message: `Account with name '${name}' already exists for this resource`
});
}
diff --git a/backend/src/ee/services/pam-resource/pam-resource-service.ts b/backend/src/ee/services/pam-resource/pam-resource-service.ts
index 70734a17fb..3c903db50b 100644
--- a/backend/src/ee/services/pam-resource/pam-resource-service.ts
+++ b/backend/src/ee/services/pam-resource/pam-resource-service.ts
@@ -171,16 +171,25 @@ export const pamResourceServiceFactory = ({
});
}
- const resource = await pamResourceDAL.create({
- resourceType,
- encryptedConnectionDetails,
- gatewayId,
- name,
- projectId,
- encryptedRotationAccountCredentials
- });
+ try {
+ const resource = await pamResourceDAL.create({
+ resourceType,
+ encryptedConnectionDetails,
+ gatewayId,
+ name,
+ projectId,
+ encryptedRotationAccountCredentials
+ });
- return decryptResource(resource, projectId, kmsService);
+ return await decryptResource(resource, projectId, kmsService);
+ } catch (err) {
+ if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
+ throw new BadRequestError({
+ message: `Resource with name '${name}' already exists for this project`
+ });
+ }
+ throw err;
+ }
};
const updateById = async (
@@ -289,9 +298,18 @@ export const pamResourceServiceFactory = ({
return decryptResource(resource, resource.projectId, kmsService);
}
- const updatedResource = await pamResourceDAL.updateById(resourceId, updateDoc);
+ try {
+ const updatedResource = await pamResourceDAL.updateById(resourceId, updateDoc);
- return decryptResource(updatedResource, resource.projectId, kmsService);
+ return await decryptResource(updatedResource, resource.projectId, kmsService);
+ } catch (err) {
+ if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
+ throw new BadRequestError({
+ message: `Resource with name '${name}' already exists for this project`
+ });
+ }
+ throw err;
+ }
};
const deleteById = async (id: string, actor: OrgServiceActor) => {