Skip to content

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

Invalidate license entitlements cache on billing-v2 mutations

Tre Dubrava439412eec5e1
diff --git a/backend/src/ee/services/license-v2/license-v2-service.ts b/backend/src/ee/services/license-v2/license-v2-service.ts
index ae62e77897..2a7f8971ec 100644
--- a/backend/src/ee/services/license-v2/license-v2-service.ts
+++ b/backend/src/ee/services/license-v2/license-v2-service.ts
@@ -45,6 +45,7 @@ type TLicenseV2ServiceFactoryDep = {
licenseClient: Pick<
TLicenseClientFactory,
| "getEntitlements"
+ | "invalidateEntitlements"
| "getCatalog"
| "getSubscription"
| "getCloudPlan"
@@ -546,6 +547,7 @@ export const licenseV2ServiceFactory = ({
await ensureManageBilling(orgId, actor);
const items = await resolveAddItems(productId, cadence);
const result = await licenseClient.addSubscriptionItems(orgId, { items });
+ await licenseClient.invalidateEntitlements(orgId);
return { subscriptionId: result.subscriptionId };
};
@@ -554,18 +556,21 @@ export const licenseV2ServiceFactory = ({
const removeProduct = async ({ orgId, actor, productId, prorationDate }: TRemoveBillingV2ProductDTO) => {
await ensureManageBilling(orgId, actor);
const result = await licenseClient.removeSubscriptionItem(orgId, productId, prorationDate);
+ await licenseClient.invalidateEntitlements(orgId);
return { subscriptionId: result.subscriptionId };
};
const cancelSubscription = async ({ orgId, actor }: TBillingV2SubscriptionLifecycleDTO) => {
await ensureManageBilling(orgId, actor);
const result = await licenseClient.cancelSubscription(orgId);
+ await licenseClient.invalidateEntitlements(orgId);
return { subscriptionId: result.subscriptionId };
};
const resumeSubscription = async ({ orgId, actor }: TBillingV2SubscriptionLifecycleDTO) => {
await ensureManageBilling(orgId, actor);
const result = await licenseClient.resumeSubscription(orgId);
+ await licenseClient.invalidateEntitlements(orgId);
return { subscriptionId: result.subscriptionId };
};
diff --git a/backend/src/services/license-client/license-client-cache.ts b/backend/src/services/license-client/license-client-cache.ts
index 3a61b9f42b..e457107e81 100644
--- a/backend/src/services/license-client/license-client-cache.ts
+++ b/backend/src/services/license-client/license-client-cache.ts
@@ -5,7 +5,7 @@ import { logger } from "@app/lib/logger";
import { TEntitlementOrg, TEntitlementsResponse, TLicenseClientBackend } from "./license-client-types";
type TEntitlementResolverDep = {
- keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry">;
+ keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "deleteItem">;
backend: Pick<TLicenseClientBackend, "fetchEntitlements">;
};
@@ -57,5 +57,16 @@ export const entitlementResolverFactory = ({ keyStore, backend }: TEntitlementRe
}
};
- return { getEntitlements };
+ // Drop the cached entitlements so the next read reflects a just-committed subscription change
+ // instead of waiting out the 30-minute TTL. The license server reconciles asynchronously via
+ // its Stripe webhook, so a stale read here is what otherwise makes a removed product linger.
+ const invalidateEntitlements = async (orgId: string): Promise<void> => {
+ try {
+ await keyStore.deleteItem(KeyStorePrefixes.LicenseEntitlements(orgId));
+ } catch (error) {
+ logger.error(error, `license-client: failed to invalidate entitlements [orgId=${orgId}]`);
+ }
+ };
+
+ return { getEntitlements, invalidateEntitlements };
};
diff --git a/backend/src/services/license-client/license-client.ts b/backend/src/services/license-client/license-client.ts
index 147751322c..f85fbbf3b5 100644
--- a/backend/src/services/license-client/license-client.ts
+++ b/backend/src/services/license-client/license-client.ts
@@ -19,7 +19,7 @@ type TLicenseClientFactoryDep = {
TEnvConfig,
"LICENSE_SERVER_V2_MODE" | "LICENSE_SERVER_V2_URL" | "LICENSE_SERVER_V2_SERVICE_KEY" | "INTERNAL_REGION"
>;
- keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry">;
+ keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "deleteItem">;
};
export type TLicenseClientFactory = ReturnType<typeof licenseClientFactory>;
@@ -68,6 +68,13 @@ export const licenseClientFactory = ({ envConfig, keyStore }: TLicenseClientFact
return resolver.getEntitlements(org);
};
+ const invalidateEntitlements = async (orgId: string) => {
+ if (!resolver) {
+ return;
+ }
+ await resolver.invalidateEntitlements(orgId);
+ };
+
const getCatalog = async () => {
if (!backend) {
return null;
@@ -148,6 +155,7 @@ export const licenseClientFactory = ({ envConfig, keyStore }: TLicenseClientFact
return {
...featureReaderFactory({ getEntitlements }),
getEntitlements,
+ invalidateEntitlements,
getCatalog,
getSubscription,
getCloudPlan,