Skip to content

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

fix(gateway): re-check relay and gateway CAs under their advisory locks

Claudec37279b85c34
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index e59a75036d..dcb8314164 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -139,6 +139,9 @@ export const gatewayV2ServiceFactory = ({
if (orgGatewayConfigV2) return orgGatewayConfigV2;
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.OrgGatewayV2Init(orgId)]);
+ // Re-read under the lock: a request that waited on it must not create a second one.
+ const lockedOrgGatewayConfigV2 = await orgGatewayConfigV2DAL.findOne({ orgId }, tx);
+ if (lockedOrgGatewayConfigV2) return lockedOrgGatewayConfigV2;
// generate root CA
const rootCaKeyAlgorithm = CertKeyAlgorithm.RSA_2048;
@@ -1200,6 +1203,12 @@ export const gatewayV2ServiceFactory = ({
}
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.GatewayPamSessionKey(gateway.id)]);
+ // Re-read under the lock: a request that waited on it must return the key the first one
+ // stored rather than replace it.
+ const lockedGateway = await gatewayV2DAL.findById(gateway.id, tx);
+ if (lockedGateway?.encryptedPamSessionKey) {
+ return decryptor({ cipherTextBlob: lockedGateway.encryptedPamSessionKey });
+ }
const newPamSessionKey = crypto.randomBytes(32);
const { cipherTextBlob: encryptedPamSessionKey } = encryptor({ plainText: newPamSessionKey });
diff --git a/backend/src/ee/services/relay/relay-service.ts b/backend/src/ee/services/relay/relay-service.ts
index 2853efaac8..81b2a45710 100644
--- a/backend/src/ee/services/relay/relay-service.ts
+++ b/backend/src/ee/services/relay/relay-service.ts
@@ -74,6 +74,9 @@ export const relayServiceFactory = ({
if (existingInstanceRelayConfig) return existingInstanceRelayConfig;
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.InstanceRelayConfigInit()]);
+ // Re-read under the lock: a request that waited on it must not create a second one.
+ const lockedInstanceRelayConfig = await instanceRelayConfigDAL.findById(INSTANCE_RELAY_CONFIG_UUID, tx);
+ if (lockedInstanceRelayConfig) return lockedInstanceRelayConfig;
const alg = keyAlgorithmToAlgCfg(CertKeyAlgorithm.RSA_2048);
const rootCaKeys = await crypto.nativeCrypto.subtle.generateKey(alg, true, ["sign", "verify"]);
@@ -428,6 +431,9 @@ export const relayServiceFactory = ({
}
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.OrgRelayConfigInit(orgId)]);
+ // Re-read under the lock: a request that waited on it must not create a second one.
+ const lockedOrgRelayConfig = await orgRelayConfigDAL.findOne({ orgId }, tx);
+ if (lockedOrgRelayConfig) return lockedOrgRelayConfig;
const alg = keyAlgorithmToAlgCfg(CertKeyAlgorithm.RSA_2048);
const orgRelayCaCert = new x509.X509Certificate(instanceCAs.orgRelayPkiCaCertificate);