Skip to content

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

misc: addressed issue

Sheen Capadngan9d2188a6ea89
diff --git a/backend/src/services/license-client/usage/usage-counter-dal.ts b/backend/src/services/license-client/usage/usage-counter-dal.ts
index 1d9a865126..64a23caa24 100644
--- a/backend/src/services/license-client/usage/usage-counter-dal.ts
+++ b/backend/src/services/license-client/usage/usage-counter-dal.ts
@@ -13,9 +13,12 @@ const toCount = (row: unknown): number => Number((row as { count?: string | numb
// Live counts for the project-scoped metered features, summed across the whole org tree and excluding
// soft-deleted projects so they don't inflate a quota.
export const usageCounterDALFactory = (db: TDbClient) => {
- const countInternalCas = async (orgId: string): Promise<number> => {
+ // orgId omitted means instance-wide: self-hosted is one licence over the whole database and reports
+ // under an identity that is not an org id, so the org filter has to come off rather than be handed a
+ // non-uuid.
+ const countInternalCas = async (orgId?: string): Promise<number> => {
try {
- const row = await db
+ const qb = db
.replicaNode()(TableName.CertificateAuthority)
.join(
TableName.InternalCertificateAuthority,
@@ -23,10 +26,11 @@ export const usageCounterDALFactory = (db: TDbClient) => {
`${TableName.InternalCertificateAuthority}.caId`
)
.join(TableName.Project, `${TableName.CertificateAuthority}.projectId`, `${TableName.Project}.id`)
- .whereIn(`${TableName.Project}.orgId`, orgTreeIds(db.replicaNode(), orgId))
- .whereNull(`${TableName.Project}.deleteAfter`)
- .count(`${TableName.CertificateAuthority}.id as count`)
- .first();
+ .whereNull(`${TableName.Project}.deleteAfter`);
+
+ if (orgId) void qb.whereIn(`${TableName.Project}.orgId`, orgTreeIds(db.replicaNode(), orgId));
+
+ const row = await qb.count(`${TableName.CertificateAuthority}.id as count`).first();
return toCount(row);
} catch (error) {
throw new DatabaseError({ error, name: "Count internal CAs for usage" });
@@ -52,15 +56,17 @@ export const usageCounterDALFactory = (db: TDbClient) => {
});
// No `status = 'active'` filter: nothing writes EXPIRED or RENEWED, both are derived at read time.
- const $activeQuotaCertificates = (orgId: string) => {
+ // orgId omitted means instance-wide; see countInternalCas.
+ const $activeQuotaCertificates = (orgId?: string) => {
const qb = db
.replicaNode()(TableName.Certificate)
.join(TableName.Project, `${TableName.Certificate}.projectId`, `${TableName.Project}.id`)
- .whereIn(`${TableName.Project}.orgId`, orgTreeIds(db.replicaNode(), orgId))
.whereNull(`${TableName.Project}.deleteAfter`)
.where(`${TableName.Certificate}.notAfter`, ">", new Date())
.whereNot(`${TableName.Certificate}.status`, CertStatus.REVOKED);
+ if (orgId) void qb.whereIn(`${TableName.Project}.orgId`, orgTreeIds(db.replicaNode(), orgId));
+
return $excludeSignerCertificates(qb);
};
@@ -82,7 +88,7 @@ export const usageCounterDALFactory = (db: TDbClient) => {
// Joins on projectId rather than caId, which is nullable and would drop imported and discovered
// certificates. Both counts read hasWildcard from the covering index; filtering with LIKE over
// commonName/altNames instead forces a sequential scan (measured: ~2x at 600k rows).
- const countActiveCertificateQuotaKeysByOrg = async (orgId: string): Promise<{ total: number; wildcard: number }> => {
+ const countActiveCertificateQuotaKeysByOrg = async (orgId?: string): Promise<{ total: number; wildcard: number }> => {
try {
const row = (await $activeQuotaCertificates(orgId)
.countDistinct(`${TableName.Certificate}.quotaKey as total`)
diff --git a/backend/src/services/license-client/usage/usage-counters.ts b/backend/src/services/license-client/usage/usage-counters.ts
index 95adc6d066..4043041b34 100644
--- a/backend/src/services/license-client/usage/usage-counters.ts
+++ b/backend/src/services/license-client/usage/usage-counters.ts
@@ -48,22 +48,27 @@ export const buildMeteredFeatures = ({
isCloud
}: TBuildMeteredFeaturesDep): TMeteredFeature[] => [
{ feature: IdentitiesMeter, count: (orgId) => licenseDAL.countOrgUsersAndIdentities(isCloud ? orgId : null) },
- // The PKI meters count the whole org tree, so both report once at the root.
+ // The PKI meters count the whole org tree, so each reports once at the root. On self-hosted they
+ // count the whole instance instead: the report identity there is not an org id.
{
feature: InternalCas,
- count: (orgId) => usageCounterDAL.countInternalCas(orgId),
+ count: (orgId) => usageCounterDAL.countInternalCas(isCloud ? orgId : undefined),
resolveReportOrgId: (orgId) => usageCounterDAL.resolveRootOrgId(orgId)
},
{
feature: ActiveCerts,
- count: (orgId) => usageCounterDAL.countActiveCertificateQuotaKeysByOrg(orgId).then(({ total }) => total),
+ count: (orgId) =>
+ usageCounterDAL.countActiveCertificateQuotaKeysByOrg(isCloud ? orgId : undefined).then(({ total }) => total),
resolveReportOrgId: (orgId) => usageCounterDAL.resolveRootOrgId(orgId)
},
{
// The other half of the same query active_certs reads, so the two can never disagree about which
// certificates are live. Wildcards also count toward active_certs; this is a priced subset of it.
feature: WildcardCerts,
- count: (orgId) => usageCounterDAL.countActiveCertificateQuotaKeysByOrg(orgId).then(({ wildcard }) => wildcard),
+ count: (orgId) =>
+ usageCounterDAL
+ .countActiveCertificateQuotaKeysByOrg(isCloud ? orgId : undefined)
+ .then(({ wildcard }) => wildcard),
resolveReportOrgId: (orgId) => usageCounterDAL.resolveRootOrgId(orgId)
},
{
diff --git a/backend/src/services/license-client/usage/usage-metering.test.ts b/backend/src/services/license-client/usage/usage-metering.test.ts
index e64784367f..a7fb6629b8 100644
--- a/backend/src/services/license-client/usage/usage-metering.test.ts
+++ b/backend/src/services/license-client/usage/usage-metering.test.ts
@@ -276,6 +276,34 @@ describe("buildMeteredFeatures", () => {
expect(licenseDAL.countOfOrgMembers).toHaveBeenCalledWith(ORG_ID);
});
+ // The self-hosted report identity is the literal "self-hosted", not a uuid, so a PKI counter handed
+ // it would reach Postgres as an invalid uuid and throw.
+ test("self-hosted meters the PKI dimensions instance-wide, never passing the report identity to the DB", async () => {
+ const licenseDAL = {
+ countOrgUsersAndIdentities: vi.fn(async () => 0),
+ countOfOrgMembers: vi.fn(async () => 0)
+ };
+ const usageCounterDAL = {
+ countInternalCas: vi.fn(async () => 3),
+ resolveRootOrgId: vi.fn(async (id: string) => id),
+ countActiveCertificateQuotaKeysByOrg: vi.fn(async () => ({ total: 5, wildcard: 2 })),
+ isCertificateQuotaKeyActiveInOrg: vi.fn(async () => false),
+ countPamResources: vi.fn(async () => 0),
+ countSecretManagementIdentities: vi.fn(async () => 0),
+ countPamIdentities: vi.fn(async () => 0)
+ };
+ const metered = buildMeteredFeatures({ licenseDAL, usageCounterDAL, isCloud: false });
+ const byKey = Object.fromEntries(metered.map((m) => [m.feature.key, m.count]));
+
+ expect(await byKey[InternalCas.key]("self-hosted")).toBe(3);
+ expect(await byKey[ActiveCerts.key]("self-hosted")).toBe(5);
+ expect(await byKey[WildcardCerts.key]("self-hosted")).toBe(2);
+
+ expect(usageCounterDAL.countInternalCas).toHaveBeenCalledWith(undefined);
+ expect(usageCounterDAL.countActiveCertificateQuotaKeysByOrg).toHaveBeenCalledWith(undefined);
+ expect(usageCounterDAL.countActiveCertificateQuotaKeysByOrg).not.toHaveBeenCalledWith("self-hosted");
+ });
+
test("self-hosted meters secret identities across the whole instance (no org scope)", async () => {
const licenseDAL = {
countOrgUsersAndIdentities: vi.fn(async () => 0),