Skip to content

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

check account id from connection info

Matheus Nogueirad2d5a353436c
diff --git a/backend/src/services/app-connection/aws/aws-connection-fns.ts b/backend/src/services/app-connection/aws/aws-connection-fns.ts
index 85c730b6a3..695785c074 100644
--- a/backend/src/services/app-connection/aws/aws-connection-fns.ts
+++ b/backend/src/services/app-connection/aws/aws-connection-fns.ts
@@ -227,3 +227,64 @@ export const validateAwsConnectionCredentials = async (appConnection: TAwsConnec
return appConnection.credentials;
};
+
+const extractAccountIdFromArn = (roleArn: string): string | null => {
+ const match = roleArn.match(/^arn:aws[^:]*:iam::(\d{12}):/);
+ return match ? match[1] : null;
+};
+
+// eslint-disable-next-line no-bitwise
+const extractAccountIdFromAccessKey = (accessKeyId: string): string | null => {
+ if (accessKeyId.length < 16) return null;
+
+ const trimmed = accessKeyId.slice(4);
+ const base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+
+ const bits: number[] = [];
+ for (const char of trimmed) {
+ const val = base32Chars.indexOf(char.toUpperCase());
+ if (val === -1) return null;
+ for (let i = 4; i >= 0; i -= 1) {
+ // eslint-disable-next-line no-bitwise
+ bits.push((val >> i) & 1);
+ }
+ }
+
+ const bytes: number[] = [];
+ for (let i = 0; i < 6 * 8 && i < bits.length; i += 8) {
+ let byte = 0;
+ for (let j = 0; j < 8 && i + j < bits.length; j += 1) {
+ // eslint-disable-next-line no-bitwise
+ byte = (byte << 1) | bits[i + j];
+ }
+ bytes.push(byte);
+ }
+
+ if (bytes.length < 6) return null;
+
+ let num = 0n;
+ for (const byte of bytes) {
+ // eslint-disable-next-line no-bitwise
+ num = (num << 8n) | BigInt(byte);
+ }
+ const mask = 0x7fffffffff80n;
+ // eslint-disable-next-line no-bitwise
+ const accountId = ((num & mask) >> 7n).toString();
+
+ return accountId.padStart(12, "0");
+};
+
+export const extractAwsAccountId = (connection: {
+ method: string;
+ credentials: { roleArn?: string; accessKeyId?: string };
+}): string | null => {
+ if (connection.method === AwsConnectionMethod.AssumeRole && connection.credentials.roleArn) {
+ return extractAccountIdFromArn(connection.credentials.roleArn);
+ }
+
+ if (connection.method === AwsConnectionMethod.AccessKey && connection.credentials.accessKeyId) {
+ return extractAccountIdFromAccessKey(connection.credentials.accessKeyId);
+ }
+
+ return null;
+};
diff --git a/backend/src/services/secret-sync/secret-sync-maps.ts b/backend/src/services/secret-sync/secret-sync-maps.ts
index eeeff3828f..774fa01bc1 100644
--- a/backend/src/services/secret-sync/secret-sync-maps.ts
+++ b/backend/src/services/secret-sync/secret-sync-maps.ts
@@ -1,4 +1,5 @@
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
+import { extractAwsAccountId } from "@app/services/app-connection/aws/aws-connection-fns";
import { SecretSync, SecretSyncPlanType } from "@app/services/secret-sync/secret-sync-enums";
import { DestinationDuplicateCheckFn } from "@app/services/secret-sync/secret-sync-types";
@@ -211,15 +212,27 @@ export const SECRET_SYNC_SKIP_FIELDS_MAP: Record<SecretSync, string[]> = {
[SecretSync.Cloud66]: ["stackName"]
};
-const defaultDuplicateCheck: DestinationDuplicateCheckFn = () => true;
-const awsDuplicateCheck: DestinationDuplicateCheckFn = ({ existingConnectionId, newConnectionId }) => {
+const defaultDuplicateCheck: DestinationDuplicateCheckFn = async () => true;
+const awsDuplicateCheck: DestinationDuplicateCheckFn = async ({
+ existingConnectionId,
+ newConnectionId,
+ decryptConnection
+}) => {
if (!newConnectionId) return true;
+ if (existingConnectionId === newConnectionId) return true;
+ if (!existingConnectionId) return false;
- // TODO: we should also check the AWS account id here, otherwise
- // two connections to the same account would cause issues.
- // If assume role: get account id from ARN
- // If credentials, use AWS_ACCESS_ID to infer the account id
- return existingConnectionId === newConnectionId;
+ const [existingConn, newConn] = await Promise.all([
+ decryptConnection(existingConnectionId),
+ decryptConnection(newConnectionId)
+ ]);
+
+ const existingAccountId = extractAwsAccountId(existingConn);
+ const newAccountId = extractAwsAccountId(newConn);
+
+ if (!existingAccountId || !newAccountId) return false;
+
+ return existingAccountId === newAccountId;
};
export const DESTINATION_DUPLICATE_CHECK_MAP: Record<SecretSync, DestinationDuplicateCheckFn> = {
diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts
index c4e8bc2fe4..5645dfb2f6 100644
--- a/backend/src/services/secret-sync/secret-sync-service.ts
+++ b/backend/src/services/secret-sync/secret-sync-service.ts
@@ -16,7 +16,9 @@ import { deepEqualSkipFields } from "@app/lib/fn/object";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { OrgServiceActor } from "@app/lib/types";
+import { decryptAppConnectionCredentials } from "@app/services/app-connection/app-connection-fns";
import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-connection-service";
+import { TAppConnection } from "@app/services/app-connection/app-connection-types";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { TProjectBotServiceFactory } from "@app/services/project-bot/project-bot-service";
import { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
@@ -280,31 +282,48 @@ export const secretSyncServiceFactory = ({
try {
const existingSyncs = await secretSyncDAL.findByDestinationAndOrgId(destination, actor.orgId);
- const duplicates = existingSyncs.filter((sync) => {
- if (sync.id === excludeSyncId) {
- return false;
+ const decryptConnection = async (connId: string): Promise<TAppConnection> => {
+ const conn = await appConnectionDAL.findById(connId);
+ if (!conn || conn.orgId !== actor.orgId) {
+ throw new NotFoundError({ message: `App connection with ID "${connId}" not found` });
}
+ const credentials = await decryptAppConnectionCredentials({
+ orgId: conn.orgId,
+ encryptedCredentials: conn.encryptedCredentials,
+ kmsService,
+ projectId: conn.projectId
+ });
+ return { ...conn, credentials } as TAppConnection;
+ };
- try {
- const baseFieldsMatch = deepEqualSkipFields(sync.destinationConfig, destinationConfig, skipFields);
- if (baseFieldsMatch) {
- return DESTINATION_DUPLICATE_CHECK_MAP[destination]({
+ const candidates = existingSyncs.filter((sync) => {
+ if (sync.id === excludeSyncId) return false;
+ return deepEqualSkipFields(sync.destinationConfig, destinationConfig, skipFields);
+ });
+
+ const duplicateCheckResults = await Promise.all(
+ candidates.map(async (sync) => {
+ try {
+ const isDuplicate = await DESTINATION_DUPLICATE_CHECK_MAP[destination]({
existingConfig: sync.destinationConfig as Record<string, unknown>,
newConfig: destinationConfig,
existingConnectionId: sync.connectionId,
- newConnectionId: connectionId ?? null
+ newConnectionId: connectionId ?? null,
+ decryptConnection
});
+ return isDuplicate ? sync : null;
+ } catch {
+ return null;
}
- return false;
- } catch {
- return false;
- }
- });
+ })
+ );
+
+ const duplicates = duplicateCheckResults.filter(Boolean);
const hasDuplicate = duplicates.length > 0;
return {
hasDuplicate,
- duplicateProjectId: hasDuplicate ? duplicates[0].projectId : undefined
+ duplicateProjectId: hasDuplicate ? duplicates[0]!.projectId : undefined
};
} catch (error) {
return { hasDuplicate: false, duplicateProjectId: undefined };
diff --git a/backend/src/services/secret-sync/secret-sync-types.ts b/backend/src/services/secret-sync/secret-sync-types.ts
index f116aa79e8..2c913f07c1 100644
--- a/backend/src/services/secret-sync/secret-sync-types.ts
+++ b/backend/src/services/secret-sync/secret-sync-types.ts
@@ -49,6 +49,7 @@ import {
} from "@app/services/secret-sync/windmill";
import { TAppConnectionDALFactory } from "../app-connection/app-connection-dal";
+import { TAppConnection } from "../app-connection/app-connection-types";
import { TKmsServiceFactory } from "../kms/kms-service";
import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
import {
@@ -575,7 +576,8 @@ export type DestinationDuplicateCheckFn = (opts: {
newConfig: Record<string, unknown>;
existingConnectionId: string | null;
newConnectionId: string | null;
-}) => boolean;
+ decryptConnection: (connectionId: string) => Promise<TAppConnection>;
+}) => Promise<boolean>;
export type TPreSaveTransformDeps = {
secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "findOne">;
diff --git a/frontend/src/hooks/api/secretSyncs/queries.tsx b/frontend/src/hooks/api/secretSyncs/queries.tsx
index 1c176613b2..2019902a69 100644
--- a/frontend/src/hooks/api/secretSyncs/queries.tsx
+++ b/frontend/src/hooks/api/secretSyncs/queries.tsx
@@ -120,7 +120,12 @@ export const useCheckDuplicateDestination = (
>
) => {
return useQuery({
- queryKey: secretSyncKeys.duplicateCheck(destination, destinationConfig, connectionId, excludeSyncId),
+ queryKey: secretSyncKeys.duplicateCheck(
+ destination,
+ destinationConfig,
+ connectionId,
+ excludeSyncId
+ ),
queryFn: async () => {
const { data } = await apiRequest.post<{
hasDuplicate: boolean;
diff --git a/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts b/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
index 9cd1332d7d..3615fa1a1d 100644
--- a/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
+++ b/frontend/src/hooks/api/secretSyncs/useDuplicateDestinationCheck.ts
@@ -36,11 +36,18 @@ export const useDuplicateDestinationCheck = ({
isLoading,
error,
refetch
- } = useCheckDuplicateDestination(destination, destinationConfig, projectId, excludeSyncId, connectionId, {
- enabled: shouldCheck,
- staleTime: 0,
- gcTime: 0
- });
+ } = useCheckDuplicateDestination(
+ destination,
+ destinationConfig,
+ projectId,
+ excludeSyncId,
+ connectionId,
+ {
+ enabled: shouldCheck,
+ staleTime: 0,
+ gcTime: 0
+ }
+ );
return {
hasDuplicate: shouldCheck ? Boolean(duplicateData?.hasDuplicate) : false,