Refactor EE secret versioning/snapshot access

This commit is contained in:
Tuan Dang
2022-12-25 20:04:27 -05:00
parent 8f765cba57
commit 9f724b5ede
7 changed files with 68 additions and 30 deletions
+19 -2
View File
@@ -3,7 +3,9 @@ import {
Secret
} from '../../models';
import {
SecretSnapshot
SecretSnapshot,
SecretVersion,
ISecretVersion
} from '../models';
/**
@@ -52,6 +54,21 @@ import {
}
}
const addSecretVersionsHelper = async ({
secretVersions
}: {
secretVersions: ISecretVersion[]
}) => {
try {
await SecretVersion.insertMany(secretVersions);
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
throw new Error('Failed to add secret versions');
}
}
export {
takeSecretSnapshotHelper
takeSecretSnapshotHelper,
addSecretVersionsHelper
}
+3 -1
View File
@@ -3,5 +3,7 @@ import SecretVersion, { ISecretVersion } from "./secretVersion";
export {
SecretSnapshot,
SecretVersion
ISecretSnapshot,
SecretVersion,
ISecretVersion
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { Schema, model, Types } from 'mongoose';
export interface ISecretVersion {
_id: Types.ObjectId;
_id?: Types.ObjectId;
secret: Types.ObjectId;
version: number;
isDeleted: boolean;
+12 -15
View File
@@ -1,22 +1,19 @@
import { LICENSE_KEY } from '../../config';
/**
* Class to handle Enterprise Edition license actions
*/
class EELicenseService {
/**
* Check if license key [licenseKey] corresponds to a
* valid Infisical Enterprise Edition license.
* @param {Object} obj
* @param {Object} obj.licenseKey
* @returns {Boolean}
*/
static async checkLicense({
licenseKey
}: {
licenseKey: string;
}) {
// TODO
return true;
private readonly _isLicenseValid: boolean;
constructor(licenseKey: string) {
this._isLicenseValid = true;
}
public get isLicenseValid(): boolean {
return this._isLicenseValid;
}
}
export default EELicenseService;
export default new EELicenseService(LICENSE_KEY);
+22 -2
View File
@@ -1,4 +1,8 @@
import { takeSecretSnapshotHelper } from '../helpers/secret';
import { ISecretVersion } from '../models';
import {
takeSecretSnapshotHelper,
addSecretVersionsHelper
} from '../helpers/secret';
import EELicenseService from './EELicenseService';
/**
@@ -21,9 +25,25 @@ class EESecretService {
licenseKey: string;
workspaceId: string;
}) {
EELicenseService.checkLicense({ licenseKey });
if (!EELicenseService.isLicenseValid) return;
await takeSecretSnapshotHelper({ workspaceId });
}
/**
* Adds secret versions [secretVersions] to the SecretVersion collection.
* @param {Object} obj
* @param {SecretVersion} obj.secretVersions
*/
static async addSecretVersions({
secretVersions
}: {
secretVersions: ISecretVersion[];
}) {
if (!EELicenseService.isLicenseValid) return;
await addSecretVersionsHelper({
secretVersions
});
}
}
export default EESecretService;