Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: enhance license key handling and error management

Piyush Guptac2bdd12dd568
diff --git a/backend/src/ee/services/license/license-fns.ts b/backend/src/ee/services/license/license-fns.ts
index fca2455b0b..4231ba3c23 100644
--- a/backend/src/ee/services/license/license-fns.ts
+++ b/backend/src/ee/services/license/license-fns.ts
@@ -1,7 +1,7 @@
import axios, { AxiosError } from "axios";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
-import { getConfig } from "@app/lib/config/env";
+import { getConfig, TEnvConfig } from "@app/lib/config/env";
import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
@@ -9,17 +9,24 @@ import { UserAliasType } from "@app/services/user-alias/user-alias-types";
import { TFeatureSet, TLicenseKeyConfig, TOfflineLicenseContents } from "./license-types";
-const getOfflineLicenseContents = (licenseKey: string): TOfflineLicenseContents => {
- return JSON.parse(Buffer.from(licenseKey, "base64").toString("utf8")) as TOfflineLicenseContents;
-};
-
export const isOfflineLicenseKey = (licenseKey: string): boolean => {
- const contents = getOfflineLicenseContents(licenseKey);
- return "signature" in contents && "license" in contents;
+ try {
+ const contents = JSON.parse(Buffer.from(licenseKey, "base64").toString("utf8")) as TOfflineLicenseContents;
+
+ return "signature" in contents && "license" in contents;
+ } catch (error) {
+ return false;
+ }
};
-export const getLicenseKeyConfig = (): TLicenseKeyConfig => {
- const cfg = getConfig();
+export const getLicenseKeyConfig = (
+ config?: Pick<TEnvConfig, "LICENSE_KEY" | "LICENSE_KEY_OFFLINE">
+): TLicenseKeyConfig => {
+ const cfg = config || getConfig();
+
+ if (!cfg) {
+ return { isValid: false };
+ }
const licenseKey = cfg.LICENSE_KEY;
diff --git a/backend/src/ee/services/license/license-service.ts b/backend/src/ee/services/license/license-service.ts
index 04e77f2594..080556ca57 100644
--- a/backend/src/ee/services/license/license-service.ts
+++ b/backend/src/ee/services/license/license-service.ts
@@ -77,7 +77,7 @@ export const licenseServiceFactory = ({
let instanceType = InstanceType.OnPrem;
let onPremFeatures: TFeatureSet = getDefaultOnPremFeatures();
let selfHostedLicense: TOfflineLicense | null = null;
- const licenseKeyConfig = getLicenseKeyConfig();
+ const licenseKeyConfig = getLicenseKeyConfig(envConfig);
const licenseServerCloudApi = setupLicenseRequestWithStore(
envConfig.LICENSE_SERVER_URL || "",