mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 00:52:09 +00:00
Add notification on Service Token expiration
This commit is contained in:
@@ -28,5 +28,29 @@ export const serviceTokenDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
return { ...stOrm, findById };
|
||||
const findExpiredTokens = async (tx?: Knex) => {
|
||||
try {
|
||||
const docs: { name: string; projectName: string; createdByEmail: string; id: string; projectId: string }[] =
|
||||
await (tx || db.replicaNode())(TableName.ServiceToken)
|
||||
.leftJoin<TUsers>(
|
||||
TableName.Users,
|
||||
`${TableName.Users}.id`,
|
||||
db.raw(`${TableName.ServiceToken}."createdBy"::uuid`)
|
||||
)
|
||||
.join(TableName.Project, `${TableName.Project}.id`, `${TableName.ServiceToken}.projectId`)
|
||||
.whereRaw(
|
||||
`${TableName.ServiceToken}."expiresAt" < NOW() AND ${TableName.ServiceToken}."notificationSent" = false`
|
||||
)
|
||||
.select(`${TableName.ServiceToken}.name`)
|
||||
.select(`${TableName.ServiceToken}.id`)
|
||||
.select(`${TableName.Project}.name as projectName`)
|
||||
.select(`${TableName.ServiceToken}.projectId`)
|
||||
.select(`${TableName.Users}.email as createdByEmail`);
|
||||
|
||||
return docs;
|
||||
} catch (err) {
|
||||
throw new DatabaseError({ error: err, name: "FindById" });
|
||||
}
|
||||
};
|
||||
return { ...stOrm, findById, findExpiredTokens };
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@ import { TAccessTokenQueueServiceFactory } from "../access-token-queue/access-to
|
||||
import { ActorType } from "../auth/auth-type";
|
||||
import { TProjectDALFactory } from "../project/project-dal";
|
||||
import { TProjectEnvDALFactory } from "../project-env/project-env-dal";
|
||||
import { SmtpTemplates, TSmtpService } from "../smtp/smtp-service";
|
||||
import { TUserDALFactory } from "../user/user-dal";
|
||||
import { TServiceTokenDALFactory } from "./service-token-dal";
|
||||
import {
|
||||
@@ -33,6 +34,7 @@ type TServiceTokenServiceFactoryDep = {
|
||||
projectEnvDAL: Pick<TProjectEnvDALFactory, "findBySlugs">;
|
||||
projectDAL: Pick<TProjectDALFactory, "findById">;
|
||||
accessTokenQueue: Pick<TAccessTokenQueueServiceFactory, "updateServiceTokenStatus">;
|
||||
smtpService: Pick<TSmtpService, "sendMail">;
|
||||
};
|
||||
|
||||
export type TServiceTokenServiceFactory = ReturnType<typeof serviceTokenServiceFactory>;
|
||||
@@ -43,7 +45,8 @@ export const serviceTokenServiceFactory = ({
|
||||
permissionService,
|
||||
projectEnvDAL,
|
||||
projectDAL,
|
||||
accessTokenQueue
|
||||
accessTokenQueue,
|
||||
smtpService
|
||||
}: TServiceTokenServiceFactoryDep) => {
|
||||
const createServiceToken = async ({
|
||||
iv,
|
||||
@@ -185,11 +188,38 @@ export const serviceTokenServiceFactory = ({
|
||||
return { ...serviceToken, lastUsed: new Date(), orgId: project.orgId };
|
||||
};
|
||||
|
||||
const notifyExpiredTokens = async () => {
|
||||
const appCfg = getConfig();
|
||||
|
||||
const expiredTokens = await serviceTokenDAL.findExpiredTokens();
|
||||
if (expiredTokens.length === 0) return;
|
||||
|
||||
await Promise.all(
|
||||
expiredTokens.map(async (token) => {
|
||||
await smtpService
|
||||
.sendMail({
|
||||
recipients: [token.createdByEmail],
|
||||
subjectLine: "Service Token Expired",
|
||||
template: SmtpTemplates.ServiceTokenExpired,
|
||||
substitutions: {
|
||||
tokenName: token.name,
|
||||
projectName: token.projectName,
|
||||
url: `${appCfg.SITE_URL}/secret-manager/${token.projectId}/access-management?selectedTab=service-tokens`
|
||||
}
|
||||
})
|
||||
.then(async () => {
|
||||
await serviceTokenDAL.update({ id: token.id }, { notificationSent: true });
|
||||
});
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
return {
|
||||
createServiceToken,
|
||||
deleteServiceToken,
|
||||
getServiceToken,
|
||||
getProjectServiceTokens,
|
||||
fnValidateServiceToken
|
||||
fnValidateServiceToken,
|
||||
notifyExpiredTokens
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user