Move secret versioning and snapshot functionality into ee and begin license scoping

This commit is contained in:
Tuan Dang
2022-12-25 17:08:21 -05:00
parent ff3370819d
commit 26fe1dd821
23 changed files with 290 additions and 178 deletions
+15 -52
View File
@@ -2,13 +2,19 @@ import * as Sentry from '@sentry/node';
import {
Secret,
ISecret,
SecretVersion,
ISecretVersion,
SecretSnapshot,
ISecretSnapshot
} from '../models';
import {
EESecretService
} from '../ee/services';
import {
SecretVersion
} from '../ee/models';
import {
takeSecretSnapshotHelper
} from '../ee/helpers/secret';
import { decryptSymmetric } from '../utils/crypto';
import { SECRET_SHARED, SECRET_PERSONAL } from '../variables';
import { LICENSE_KEY } from '../config';
interface PushSecret {
ciphertextKey: string;
@@ -206,9 +212,11 @@ const pushSecrets = async ({
);
}
await takeSecretSnapshotHelper({
// (EE) take a secret snapshot
await EESecretService.takeSecretSnapshot({
licenseKey: LICENSE_KEY,
workspaceId
});
})
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
@@ -362,56 +370,11 @@ const decryptSecrets = ({
return content;
};
/**
* Saves a copy of the current state of secrets in workspace with id
* [workspaceId] under a new snapshot with incremented version under the
* secretsnapshots collection.
* @param {Object} obj
* @param {String} obj.workspaceId
*/
const takeSecretSnapshotHelper = async ({
workspaceId
}: {
workspaceId: string;
}) => {
try {
const secrets = await Secret.find({
workspace: workspaceId
});
const latestSecretSnapshot = await SecretSnapshot.findOne({
workspace: workspaceId
}).sort({ version: -1 });
if (!latestSecretSnapshot) {
// case: no snapshots exist for workspace -> create first snapshot
await new SecretSnapshot({
workspace: workspaceId,
version: 1,
secrets
}).save();
return;
}
// case: snapshots exist for workspace
await new SecretSnapshot({
workspace: workspaceId,
version: latestSecretSnapshot.version + 1,
secrets
}).save();
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
throw new Error('Failed to take a secret snapshot');
}
}
export {
pushSecrets,
pullSecrets,
reformatPullSecrets,
decryptSecrets,
takeSecretSnapshotHelper
decryptSecrets
};