Skip to content

Commit

Permalink
Refactor EE secret versioning/snapshot access
Browse files Browse the repository at this point in the history
  • Loading branch information
dangtony98 committed Dec 26, 2022
1 parent 8f765cb commit 9f724b5
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
3 changes: 1 addition & 2 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { patchRouterParam } from './utils/patchAsyncRoutes';
import express from 'express';
import helmet from 'helmet';
Expand All @@ -7,7 +6,7 @@ import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';

dotenv.config();
import { PORT, NODE_ENV, SITE_URL } from './config';
import { PORT, NODE_ENV, SITE_URL, LICENSE_KEY } from './config';
import { apiLimiter } from './helpers/rateLimiter';

import {
Expand Down
21 changes: 19 additions & 2 deletions backend/src/ee/helpers/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
Secret
} from '../../models';
import {
SecretSnapshot
SecretSnapshot,
SecretVersion,
ISecretVersion
} from '../models';

/**
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion backend/src/ee/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import SecretVersion, { ISecretVersion } from "./secretVersion";

export {
SecretSnapshot,
SecretVersion
ISecretSnapshot,
SecretVersion,
ISecretVersion
}
2 changes: 1 addition & 1 deletion backend/src/ee/models/secretVersion.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
27 changes: 12 additions & 15 deletions backend/src/ee/services/EELicenseService.ts
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 22 additions & 2 deletions backend/src/ee/services/EESecretService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { takeSecretSnapshotHelper } from '../helpers/secret';
import { ISecretVersion } from '../models';
import {
takeSecretSnapshotHelper,
addSecretVersionsHelper
} from '../helpers/secret';
import EELicenseService from './EELicenseService';

/**
Expand All @@ -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;
17 changes: 10 additions & 7 deletions backend/src/helpers/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ const pushSecrets = async ({
};
});
await Secret.bulkWrite(operations as any);
await SecretVersion.insertMany(
toUpdate.map(({

// (EE) add secret versions for updated secrets
await EESecretService.addSecretVersions({
secretVersions: toUpdate.map(({
type,
ciphertextKey,
ivKey,
Expand All @@ -153,8 +155,8 @@ const pushSecrets = async ({
secretValueIV: ivValue,
secretValueTag: tagValue,
secretValueHash: hashValue
}))
);
}))
});

// handle adding new secrets
const toAdd = secrets.filter((s) => !(`${s.type}-${s.hashKey}` in oldSecretsObj));
Expand Down Expand Up @@ -185,8 +187,9 @@ const pushSecrets = async ({
})
);

await SecretVersion.insertMany(
newSecrets.map(({
// (EE) add secret versions for new secrets
EESecretService.addSecretVersions({
secretVersions: newSecrets.map(({
_id,
secretKeyCiphertext,
secretKeyIV,
Expand All @@ -209,7 +212,7 @@ const pushSecrets = async ({
secretValueTag,
secretValueHash
}))
);
});
}

// (EE) take a secret snapshot
Expand Down

0 comments on commit 9f724b5

Please sign in to comment.