Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Add case for MI token renewal
Tuan Dangec5cf97f18df
diff --git a/backend/src/ee/controllers/v1/machineIdentitiesController.ts b/backend/src/ee/controllers/v1/machineIdentitiesController.tsindex eccc4246c6..c45c9ea792 100644--- a/backend/src/ee/controllers/v1/machineIdentitiesController.ts+++ b/backend/src/ee/controllers/v1/machineIdentitiesController.ts@@ -481,29 +481,47 @@ export const renewAccessToken = async (req: Request, res: Response) => {message: "Failed to renew non-renewable access token"});+ // ttl checkif (accessTokenTTL > 0) {const currentDate = new Date();if (accessTokenLastRenewedAt) {// access token has been renewedconst accessTokenRenewed = new Date(accessTokenLastRenewedAt);const ttlInMilliseconds = accessTokenTTL * 1000;- const expirationTime = new Date(accessTokenRenewed.getTime() + ttlInMilliseconds);+ const expirationDate = new Date(accessTokenRenewed.getTime() + ttlInMilliseconds);- if (currentDate > expirationTime) throw UnauthorizedRequestError({+ if (currentDate > expirationDate) throw UnauthorizedRequestError({message: "Failed to renew MI access token due to TTL expiration"});} else {// access token has never been renewedconst accessTokenCreated = new Date(accessTokenCreatedAt);const ttlInMilliseconds = accessTokenTTL * 1000;- const expirationTime = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);+ const expirationDate = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);- if (currentDate > expirationTime) throw UnauthorizedRequestError({+ if (currentDate > expirationDate) throw UnauthorizedRequestError({message: "Failed to renew MI access token due to TTL expiration"});}}+ // max ttl checks+ if (accessTokenMaxTTL > 0) {+ const accessTokenCreated = new Date(accessTokenCreatedAt);+ const ttlInMilliseconds = accessTokenMaxTTL * 1000;+ const currentDate = new Date();+ const expirationDate = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);++ if (currentDate > expirationDate) throw UnauthorizedRequestError({+ message: "Failed to renew MI access token due to Max TTL expiration"+ });++ const extendToDate = new Date(currentDate.getTime() + accessTokenTTL);+ if (extendToDate > expirationDate) throw UnauthorizedRequestError({+ message: "Failed to renew MI access token past its Max TTL expiration"+ });+ }+await IdentityAccessToken.findByIdAndUpdate(machineIdentityAccessToken._id,{diff --git a/backend/src/utils/authn/authModeValidators/machineIdentity.ts b/backend/src/utils/authn/authModeValidators/machineIdentity.tsindex c99f4d3b1b..00c6fbe635 100644--- a/backend/src/utils/authn/authModeValidators/machineIdentity.ts+++ b/backend/src/utils/authn/authModeValidators/machineIdentity.ts@@ -45,18 +45,18 @@ export const validateMachineIdentity = async ({// access token has been renewedconst accessTokenRenewed = new Date(accessTokenLastRenewedAt);const ttlInMilliseconds = accessTokenTTL * 1000;- const expirationTime = new Date(accessTokenRenewed.getTime() + ttlInMilliseconds);+ const expirationDate = new Date(accessTokenRenewed.getTime() + ttlInMilliseconds);- if (currentDate > expirationTime) throw UnauthorizedRequestError({+ if (currentDate > expirationDate) throw UnauthorizedRequestError({message: "Failed to authenticate MI access token due to TTL expiration"});} else {// access token has never been renewedconst accessTokenCreated = new Date(accessTokenCreatedAt);const ttlInMilliseconds = accessTokenTTL * 1000;- const expirationTime = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);+ const expirationDate = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);- if (currentDate > expirationTime) throw UnauthorizedRequestError({+ if (currentDate > expirationDate) throw UnauthorizedRequestError({message: "Failed to authenticate MI access token due to TTL expiration"});}@@ -67,9 +67,9 @@ export const validateMachineIdentity = async ({const accessTokenCreated = new Date(accessTokenCreatedAt);const ttlInMilliseconds = accessTokenMaxTTL * 1000;const currentDate = new Date();- const expirationTime = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);+ const expirationDate = new Date(accessTokenCreated.getTime() + ttlInMilliseconds);- if (currentDate > expirationTime) throw UnauthorizedRequestError({+ if (currentDate > expirationDate) throw UnauthorizedRequestError({message: "Failed to authenticate MI access token due to Max TTL expiration"});}