mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 12:38:36 +00:00
notifyExpiredTokens fixes
This commit is contained in:
@@ -28,51 +28,36 @@ export const serviceTokenDALFactory = (db: TDbClient) => {
|
||||
}
|
||||
};
|
||||
|
||||
const findExpiredTokens = async (tx?: Knex, batchSize = 1000) => {
|
||||
const findExpiringTokens = async (tx?: Knex, batchSize = 500, offset = 0) => {
|
||||
try {
|
||||
const allDocs: { name: string; projectName: string; createdByEmail: string; id: string; projectId: string }[] =
|
||||
[];
|
||||
let offset = 0;
|
||||
let hasMoreRecords = true;
|
||||
const batch: { 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() + INTERVAL '1 day' AND ${TableName.ServiceToken}."expiryNotificationSent" = false`
|
||||
)
|
||||
.whereNotNull(`${TableName.Users}.email`)
|
||||
.select(
|
||||
db.ref("id").withSchema(TableName.ServiceToken),
|
||||
db.ref("name").withSchema(TableName.ServiceToken),
|
||||
db.ref("projectId").withSchema(TableName.ServiceToken),
|
||||
db.ref("createdBy").withSchema(TableName.ServiceToken),
|
||||
db.ref("email").withSchema(TableName.Users).as("createdByEmail"),
|
||||
db.ref("name").withSchema(TableName.Project).as("projectName")
|
||||
)
|
||||
.limit(batchSize)
|
||||
.offset(offset);
|
||||
|
||||
while (hasMoreRecords) {
|
||||
// eslint-disable-next-line
|
||||
const batch: { name: string; projectName: string; createdByEmail: string; id: string; projectId: string }[] =
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
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() + INTERVAL '1 day' AND ${TableName.ServiceToken}."expiryNotificationSent" = false`
|
||||
)
|
||||
.whereNotNull(`${TableName.Users}.email`)
|
||||
.select(
|
||||
db.ref("id").withSchema(TableName.ServiceToken),
|
||||
db.ref("name").withSchema(TableName.ServiceToken),
|
||||
db.ref("projectId").withSchema(TableName.ServiceToken),
|
||||
db.ref("createdBy").withSchema(TableName.ServiceToken),
|
||||
db.ref("email").withSchema(TableName.Users).as("createdByEmail"),
|
||||
db.ref("name").withSchema(TableName.Project).as("projectName")
|
||||
)
|
||||
.limit(batchSize)
|
||||
.offset(offset);
|
||||
|
||||
if (batch.length === 0) {
|
||||
hasMoreRecords = false;
|
||||
} else {
|
||||
allDocs.push(...batch);
|
||||
offset += batchSize;
|
||||
}
|
||||
}
|
||||
|
||||
return allDocs;
|
||||
return batch;
|
||||
} catch (err) {
|
||||
throw new DatabaseError({ error: err, name: "FindExpiredTokens" });
|
||||
}
|
||||
};
|
||||
return { ...stOrm, findById, findExpiredTokens };
|
||||
|
||||
return { ...stOrm, findById, findExpiringTokens };
|
||||
};
|
||||
|
||||
@@ -189,31 +189,48 @@ export const serviceTokenServiceFactory = ({
|
||||
return { ...serviceToken, lastUsed: new Date(), orgId: project.orgId };
|
||||
};
|
||||
|
||||
const notifyExpiredTokens = async () => {
|
||||
const notifyExpiringTokens = async () => {
|
||||
const appCfg = getConfig();
|
||||
let processedCount = 0;
|
||||
let hasMoreRecords = true;
|
||||
let offset = 0;
|
||||
const batchSize = 500;
|
||||
|
||||
const expiredTokens = await serviceTokenDAL.findExpiredTokens();
|
||||
if (expiredTokens.length === 0) return;
|
||||
while (hasMoreRecords) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const expiringTokens = await serviceTokenDAL.findExpiringTokens(undefined, batchSize, offset);
|
||||
|
||||
await Promise.all(
|
||||
expiredTokens.map(async (token) => {
|
||||
try {
|
||||
await smtpService.sendMail({
|
||||
recipients: [token.createdByEmail],
|
||||
subjectLine: "Your Service Token is about to expire",
|
||||
template: SmtpTemplates.ServiceTokenExpired,
|
||||
substitutions: {
|
||||
tokenName: token.name,
|
||||
projectName: token.projectName,
|
||||
url: `${appCfg.SITE_URL}/secret-manager/${token.projectId}/access-management?selectedTab=service-tokens`
|
||||
}
|
||||
});
|
||||
await serviceTokenDAL.update({ id: token.id }, { expiryNotificationSent: true });
|
||||
} catch (error) {
|
||||
logger.error(error, `Failed to send expiration notification for token ${token.id}:`);
|
||||
}
|
||||
})
|
||||
);
|
||||
if (expiringTokens.length === 0) {
|
||||
hasMoreRecords = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await Promise.all(
|
||||
expiringTokens.map(async (token) => {
|
||||
try {
|
||||
await smtpService.sendMail({
|
||||
recipients: [token.createdByEmail],
|
||||
subjectLine: "Service Token Expiry Notice",
|
||||
template: SmtpTemplates.ServiceTokenExpired,
|
||||
substitutions: {
|
||||
tokenName: token.name,
|
||||
projectName: token.projectName,
|
||||
url: `${appCfg.SITE_URL}/secret-manager/${token.projectId}/access-management?selectedTab=service-tokens`
|
||||
}
|
||||
});
|
||||
await serviceTokenDAL.update({ id: token.id }, { expiryNotificationSent: true });
|
||||
} catch (error) {
|
||||
logger.error(error, `Failed to send expiration notification for token ${token.id}:`);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
processedCount += expiringTokens.length;
|
||||
offset += batchSize;
|
||||
}
|
||||
|
||||
return processedCount;
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -222,6 +239,6 @@ export const serviceTokenServiceFactory = ({
|
||||
getServiceToken,
|
||||
getProjectServiceTokens,
|
||||
fnValidateServiceToken,
|
||||
notifyExpiredTokens
|
||||
notifyExpiringTokens
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user