mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 16:28:11 +00:00
misc: soft delete shared secrets
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import { Knex } from "knex";
|
import { Knex } from "knex";
|
||||||
|
|
||||||
import { TDbClient } from "@app/db";
|
import { TDbClient } from "@app/db";
|
||||||
import { TableName } from "@app/db/schemas";
|
import { TableName, TSecretSharing } from "@app/db/schemas";
|
||||||
import { DatabaseError } from "@app/lib/errors";
|
import { DatabaseError } from "@app/lib/errors";
|
||||||
import { ormify } from "@app/lib/knex";
|
import { ormify, selectAllTableCols } from "@app/lib/knex";
|
||||||
|
|
||||||
export type TSecretSharingDALFactory = ReturnType<typeof secretSharingDALFactory>;
|
export type TSecretSharingDALFactory = ReturnType<typeof secretSharingDALFactory>;
|
||||||
|
|
||||||
@@ -13,15 +13,58 @@ export const secretSharingDALFactory = (db: TDbClient) => {
|
|||||||
const pruneExpiredSharedSecrets = async (tx?: Knex) => {
|
const pruneExpiredSharedSecrets = async (tx?: Knex) => {
|
||||||
try {
|
try {
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const docs = await (tx || db)(TableName.SecretSharing).where("expiresAt", "<", today).del();
|
const docs = await (tx || db)(TableName.SecretSharing)
|
||||||
|
.where("expiresAt", "<", today)
|
||||||
|
.andWhere("encryptedValue", "<>", "")
|
||||||
|
.update({
|
||||||
|
encryptedValue: "",
|
||||||
|
tag: "",
|
||||||
|
iv: "",
|
||||||
|
hashedHex: ""
|
||||||
|
});
|
||||||
return docs;
|
return docs;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new DatabaseError({ error, name: "pruneExpiredSharedSecrets" });
|
throw new DatabaseError({ error, name: "pruneExpiredSharedSecrets" });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const findActiveSharedSecrets = async (filters: Partial<TSecretSharing>, tx?: Knex) => {
|
||||||
|
try {
|
||||||
|
const now = new Date();
|
||||||
|
return await (tx || db)(TableName.SecretSharing)
|
||||||
|
.where(filters)
|
||||||
|
.andWhere("expiresAt", ">", now)
|
||||||
|
.andWhere("encryptedValue", "<>", "")
|
||||||
|
.select(selectAllTableCols(TableName.SecretSharing))
|
||||||
|
.orderBy("expiresAt", "asc");
|
||||||
|
} catch (error) {
|
||||||
|
throw new DatabaseError({
|
||||||
|
error,
|
||||||
|
name: "Find Active Shared Secrets"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const softDeleteById = async (id: string) => {
|
||||||
|
try {
|
||||||
|
await sharedSecretOrm.updateById(id, {
|
||||||
|
encryptedValue: "",
|
||||||
|
iv: "",
|
||||||
|
tag: "",
|
||||||
|
hashedHex: ""
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new DatabaseError({
|
||||||
|
error,
|
||||||
|
name: "Soft Delete Shared Secret"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...sharedSecretOrm,
|
...sharedSecretOrm,
|
||||||
pruneExpiredSharedSecrets
|
pruneExpiredSharedSecrets,
|
||||||
|
softDeleteById,
|
||||||
|
findActiveSharedSecrets
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ export const secretSharingServiceFactory = ({
|
|||||||
const { actor, actorId, orgId, actorAuthMethod, actorOrgId } = getSharedSecretsInput;
|
const { actor, actorId, orgId, actorAuthMethod, actorOrgId } = getSharedSecretsInput;
|
||||||
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId);
|
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId, actorAuthMethod, actorOrgId);
|
||||||
if (!permission) throw new UnauthorizedError({ name: "User not in org" });
|
if (!permission) throw new UnauthorizedError({ name: "User not in org" });
|
||||||
const userSharedSecrets = await secretSharingDAL.find({ userId: actorId, orgId }, { sort: [["expiresAt", "asc"]] });
|
const userSharedSecrets = await secretSharingDAL.findActiveSharedSecrets({ userId: actorId, orgId });
|
||||||
return userSharedSecrets;
|
return userSharedSecrets;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ export const secretSharingServiceFactory = ({
|
|||||||
}
|
}
|
||||||
if (sharedSecret.expiresAfterViews != null && sharedSecret.expiresAfterViews >= 0) {
|
if (sharedSecret.expiresAfterViews != null && sharedSecret.expiresAfterViews >= 0) {
|
||||||
if (sharedSecret.expiresAfterViews === 0) {
|
if (sharedSecret.expiresAfterViews === 0) {
|
||||||
await secretSharingDAL.deleteById(sharedSecretId);
|
await secretSharingDAL.softDeleteById(sharedSecretId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
|
await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
|
||||||
|
|||||||
Reference in New Issue
Block a user