Skip to content

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

fix: address PR review comments (batch)

bernie-gbf8ec613b0ce
diff --git a/backend/src/ee/routes/v3/gateway-router.ts b/backend/src/ee/routes/v3/gateway-router.ts
index 28fb887843..994f24b5cf 100644
--- a/backend/src/ee/routes/v3/gateway-router.ts
+++ b/backend/src/ee/routes/v3/gateway-router.ts
@@ -48,9 +48,9 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {
...req.auditLogInfo,
orgId: req.permission.orgId,
event: {
- type: EventType.GATEWAY_ENROLLMENT_TOKEN_CREATE,
+ type: EventType.GATEWAY_CREATE,
metadata: {
- tokenId: gateway.id,
+ gatewayId: gateway.id,
name: req.body.name
}
}
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index fa50ab0393..0fa5eb605d 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -735,6 +735,7 @@ export enum EventType {
SECRET_VALIDATION_RULE_DELETE = "secret-validation-rule-delete",
// Gateway Enrollment Tokens
+ GATEWAY_CREATE = "gateway-create",
GATEWAY_ENROLLMENT_TOKEN_CREATE = "gateway-enrollment-token-create",
GATEWAY_ENROLLMENT_TOKEN_DELETE = "gateway-enrollment-token-delete",
GATEWAY_ENROLL = "gateway-enroll",
@@ -5782,6 +5783,14 @@ interface SecretValidationRuleDeleteEvent {
};
}
+interface GatewayCreateEvent {
+ type: EventType.GATEWAY_CREATE;
+ metadata: {
+ gatewayId: string;
+ name: string;
+ };
+}
+
interface GatewayEnrollmentTokenCreateEvent {
type: EventType.GATEWAY_ENROLLMENT_TOKEN_CREATE;
metadata: {
@@ -6336,6 +6345,7 @@ export type Event =
| SecretValidationRuleCreateEvent
| SecretValidationRuleUpdateEvent
| SecretValidationRuleDeleteEvent
+ | GatewayCreateEvent
| GatewayEnrollmentTokenCreateEvent
| GatewayEnrollmentTokenDeleteEvent
| GatewayEnrollEvent
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 019ac1ffb7..78a3e81fb5 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1261,6 +1261,10 @@ export const gatewayV2ServiceFactory = ({
throw new NotFoundError({ message: `Gateway ${gatewayId} not found` });
}
+ if (gateway.identityId) {
+ throw new BadRequestError({ message: "Cannot configure token auth for identity-based gateways" });
+ }
+
const gatewayToken = $generateEnrollmentToken();
const record = await gatewayEnrollmentTokenDAL.transaction(async (tx) => {
@@ -1360,36 +1364,35 @@ export const gatewayV2ServiceFactory = ({
if (!relay) throw new NotFoundError({ message: "No relay available for auto-selection" });
}
- // Atomic single-use consumption: only marks the token as used if usedAt is still null.
- // The WHERE usedAt IS NULL ensures concurrent requests can't both succeed.
- const consumed = await gatewayEnrollmentTokenDAL.transaction(async (tx) => {
+ if (!tokenRecord.gatewayId) {
+ throw new BadRequestError({ message: "Enrollment token is not linked to a gateway" });
+ }
+
+ // Fetch org CAs before the transaction to avoid holding a long DB lock during KMS calls.
+ const orgCAs = await $getOrgCAs(orgId);
+
+ // Consume the token and update the gateway in a single transaction.
+ // If the gateway update fails, the token consumption is rolled back.
+ const gateway = await gatewayEnrollmentTokenDAL.transaction(async (tx) => {
const rows = await tx(TableName.GatewayEnrollmentTokens)
.where({ id: tokenRecord.id })
.whereNull("usedAt")
.update({ usedAt: new Date() })
.returning("*");
- return rows.length > 0;
- });
- if (!consumed) {
- throw new BadRequestError({ message: "Enrollment token has already been used" });
- }
+ if (rows.length === 0) {
+ throw new BadRequestError({ message: "Enrollment token has already been used" });
+ }
- const orgCAs = await $getOrgCAs(orgId);
+ const existing = await gatewayV2DAL.findById(tokenRecord.gatewayId!, tx);
+ if (!existing) throw new NotFoundError({ message: `Gateway ${tokenRecord.gatewayId} not found` });
+ return gatewayV2DAL.updateById(
+ existing.id,
+ { $incr: { tokenVersion: 1 }, relayId: relay!.id, heartbeat: null, lastHealthCheckStatus: null },
+ tx
+ );
+ });
try {
- // The gateway record was created at token creation time.
- // Bump tokenVersion to invalidate any previous JWT and set the relay.
- if (!tokenRecord.gatewayId) {
- throw new BadRequestError({ message: "Enrollment token is not linked to a gateway" });
- }
- const existing = await gatewayV2DAL.findById(tokenRecord.gatewayId);
- if (!existing) throw new NotFoundError({ message: `Gateway ${tokenRecord.gatewayId} not found` });
- const gateway = await gatewayV2DAL.updateById(existing.id, {
- $incr: { tokenVersion: 1 },
- relayId: relay.id,
- heartbeat: null,
- lastHealthCheckStatus: null
- });
const certs = await $issueGatewayCerts({ orgId, orgCAs, relayName: relay.name, gateway });
diff --git a/backend/src/ee/services/pam-session/pam-session-dal.ts b/backend/src/ee/services/pam-session/pam-session-dal.ts
index 7da9305522..693f62a8c2 100644
--- a/backend/src/ee/services/pam-session/pam-session-dal.ts
+++ b/backend/src/ee/services/pam-session/pam-session-dal.ts
@@ -58,6 +58,7 @@ export const pamSessionDALFactory = (db: TDbClient) => {
.leftJoin(TableName.GatewayV2, `${TableName.PamResource}.gatewayId`, `${TableName.GatewayV2}.id`)
.select(selectAllTableCols(TableName.PamSession))
.select(db.ref("identityId").withSchema(TableName.GatewayV2).as("gatewayIdentityId"))
+ .select(db.ref("id").withSchema(TableName.GatewayV2).as("gatewayId"))
.where(`${TableName.PamSession}.projectId`, projectId);
return sessions;
diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.ts
index d9926a8c20..907767203e 100644
--- a/backend/src/server/plugins/auth/inject-identity.ts
+++ b/backend/src/server/plugins/auth/inject-identity.ts
@@ -188,7 +188,7 @@ export const injectIdentity = fp(
}
// Authentication is handled on a route-level (enrollment token in body)
- if (req.url === "/api/v2/gateways/enroll") {
+ if (req.url === "/api/v3/gateways/token-auth/enroll") {
return;
}
diff --git a/docs/cli/commands/gateway.mdx b/docs/cli/commands/gateway.mdx
index 0db35d4b41..4c0a37b49d 100644
--- a/docs/cli/commands/gateway.mdx
+++ b/docs/cli/commands/gateway.mdx
@@ -74,7 +74,7 @@ The Gateway supports multiple authentication methods. Below are the available au
<ParamField query="Flags">
<Expandable title="properties">
<ParamField query="enroll-method" type="string" required>
- Must be `static` when using an enrollment token.
+ Must be `token` when using an enrollment token.
</ParamField>
<ParamField query="token" type="string" required>
The one-time enrollment token from the Infisical UI.
diff --git a/frontend/src/layouts/OrganizationLayout/components/NetworkHealthBanner/NetworkHealthBanner.tsx b/frontend/src/layouts/OrganizationLayout/components/NetworkHealthBanner/NetworkHealthBanner.tsx
index 7b571beff9..75a0e52e7a 100644
--- a/frontend/src/layouts/OrganizationLayout/components/NetworkHealthBanner/NetworkHealthBanner.tsx
+++ b/frontend/src/layouts/OrganizationLayout/components/NetworkHealthBanner/NetworkHealthBanner.tsx
@@ -70,7 +70,11 @@ export const NetworkHealthBanner = () => {
) {
return true;
}
- return "heartbeat" in g && isHeartbeatStale(g.heartbeat);
+ if (!("heartbeat" in g)) return false;
+ // Skip gateways that have never connected (both fields null)
+ if (g.heartbeat === null && ("lastHealthCheckStatus" in g ? g.lastHealthCheckStatus : null) === null)
+ return false;
+ return isHeartbeatStale(g.heartbeat);
}).length ?? 0,
[gateways]
);