Add support for pulling plan details from license server with LICENSE_KEY, LICENSE_SERVER_KEY

This commit is contained in:
Tuan Dang
2023-05-22 15:43:33 +03:00
parent 2f765600b1
commit 8341faddc5
24 changed files with 422 additions and 88 deletions
+2
View File
@@ -1,6 +1,7 @@
import * as stripeController from './stripeController';
import * as secretController from './secretController';
import * as secretSnapshotController from './secretSnapshotController';
import * as organizationsController from './organizationsController';
import * as workspaceController from './workspaceController';
import * as actionController from './actionController';
import * as membershipController from './membershipController';
@@ -9,6 +10,7 @@ export {
stripeController,
secretController,
secretSnapshotController,
organizationsController,
workspaceController,
actionController,
membershipController
@@ -0,0 +1,15 @@
import { Types } from 'mongoose';
import { Request, Response } from 'express';
import { getOrganizationPlanHelper } from '../../helpers/organizations';
export const getOrganizationPlan = async (req: Request, res: Response) => {
const { organizationId } = req.params;
const plan = await getOrganizationPlanHelper({
organizationId: new Types.ObjectId(organizationId)
});
return res.status(200).send({
plan
});
}
+39
View File
@@ -0,0 +1,39 @@
import { Types } from 'mongoose';
import * as Sentry from '@sentry/node';
import { Organization } from '../../models';
import { EELicenseService } from '../services';
import { getLicenseServerUrl } from '../../config';
import { licenseServerKeyRequest } from '../../config/request';
import { OrganizationNotFoundError } from '../../utils/errors';
export const getOrganizationPlanHelper = async ({
organizationId
}: {
organizationId: Types.ObjectId;
}) => {
try {
if (EELicenseService.instanceType === 'cloud') {
// instance of Infisical is a cloud instance
const organization = await Organization.findById(organizationId);
if (!organization) throw OrganizationNotFoundError();
const cachedPlan = EELicenseService.localFeatureSet.get(organizationId.toString());
if (cachedPlan) return cachedPlan;
const { data: { currentPlan } } = await licenseServerKeyRequest.get(
`${await getLicenseServerUrl()}/api/license-server/v1/customers/${organization.customerId}/cloud-plan`
);
// cache fetched plan for organization
EELicenseService.localFeatureSet.set(organizationId.toString(), currentPlan);
return currentPlan;
}
return EELicenseService.globalFeatureSet;
} catch (err) {
Sentry.setUser(null);
Sentry.captureException(err);
return EELicenseService.globalFeatureSet;
}
}
+2
View File
@@ -1,11 +1,13 @@
import secret from './secret';
import secretSnapshot from './secretSnapshot';
import organizations from './organizations';
import workspace from './workspace';
import action from './action';
export {
secret,
secretSnapshot,
organizations,
workspace,
action
}
+28
View File
@@ -0,0 +1,28 @@
import express from 'express';
const router = express.Router();
import {
requireAuth,
requireOrganizationAuth,
validateRequest
} from '../../../middleware';
import { param } from 'express-validator';
import { organizationsController } from '../../controllers/v1';
import {
OWNER, ADMIN, MEMBER, ACCEPTED
} from '../../../variables';
router.get(
'/:organizationId/plan',
requireAuth({
acceptedAuthModes: ['jwt', 'apiKey']
}),
requireOrganizationAuth({
acceptedRoles: [OWNER, ADMIN, MEMBER],
acceptedStatuses: [ACCEPTED]
}),
param('organizationId').exists().trim(),
validateRequest,
organizationsController.getOrganizationPlan
);
export default router;
+1 -1
View File
@@ -7,7 +7,7 @@ import {
requireAuth,
validateRequest
} from '../../../middleware';
import { param, body } from 'express-validator';
import { param } from 'express-validator';
import { ADMIN, MEMBER } from '../../../variables';
import { secretSnapshotController } from '../../controllers/v1';
+91 -4
View File
@@ -1,12 +1,99 @@
import NodeCache from 'node-cache';
import * as Sentry from '@sentry/node';
import {
getLicenseKey,
getLicenseServerKey,
getLicenseServerUrl
} from '../../config';
import {
licenseKeyRequest,
refreshLicenseServerKeyToken,
refreshLicenseKeyToken
} from '../../config/request';
interface FeatureSet {
_id: string | null;
slug: 'starter' | 'team' | 'pro' | 'enterprise' | null;
tier: number | null;
projectLimit: number | null;
memberLimit: number | null;
secretVersioning: boolean;
pitRecovery: boolean;
rbac: boolean;
customRateLimits: boolean;
customAlerts: boolean;
auditLogs: boolean;
}
/**
* Class to handle Enterprise Edition license actions
* Class to handle license/plan configurations:
* - Infisical Cloud: Fetch and cache customer plans in [localFeatureSet]
* - Self-hosted regular: Use default global feature set
* - Self-hosted enterprise: Fetch and update global feature set
*/
class EELicenseService {
private readonly _isLicenseValid: boolean;
private readonly _isLicenseValid: boolean; // TODO: deprecate
public instanceType: 'self-hosted' | 'enterprise-self-hosted' | 'cloud' = 'self-hosted';
public globalFeatureSet: FeatureSet = {
_id: null,
slug: null,
tier: null,
projectLimit: null,
memberLimit: null,
secretVersioning: true,
pitRecovery: true,
rbac: true,
customRateLimits: true,
customAlerts: true,
auditLogs: false
}
public localFeatureSet: NodeCache;
constructor(licenseKey: string) {
constructor() {
this._isLicenseValid = true;
this.localFeatureSet = new NodeCache({
stdTTL: 300
});
}
public async initGlobalFeatureSet() {
const licenseServerKey = await getLicenseServerKey();
const licenseKey = await getLicenseKey();
try {
if (licenseServerKey) {
// license server key is present -> validate it
const token = await refreshLicenseServerKeyToken()
if (token) {
this.instanceType = 'cloud';
}
return;
}
if (licenseKey) {
// license key is present -> validate it
const token = await refreshLicenseKeyToken();
if (token) {
const { data: { currentPlan } } = await licenseKeyRequest.get(
`${await getLicenseServerUrl()}/api/license/v1/plan`
);
this.globalFeatureSet = currentPlan;
this.instanceType = 'enterprise-self-hosted';
}
}
} catch (err) {
// case: self-hosted free
Sentry.setUser(null);
Sentry.captureException(err);
}
}
public get isLicenseValid(): boolean {
@@ -14,4 +101,4 @@ class EELicenseService {
}
}
export default new EELicenseService('N/A');
export default new EELicenseService();