Skip to content

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

improvement(eng-4874): memoize org findByID

Tre Dubrava9f1571d7e9ea
diff --git a/backend/src/ee/services/group/group-service.ts b/backend/src/ee/services/group/group-service.ts
index fb86c23598..c2017f8cfe 100644
--- a/backend/src/ee/services/group/group-service.ts
+++ b/backend/src/ee/services/group/group-service.ts
@@ -6,6 +6,8 @@ import { AccessScope, OrganizationActionScope, OrgMembershipRole, TGroups, TRole
import { TOidcConfigDALFactory } from "@app/ee/services/oidc/oidc-config-dal";
import { BadRequestError, NotFoundError, PermissionBoundaryError, UnauthorizedError } from "@app/lib/errors";
import { alphaNumericNanoId } from "@app/lib/nanoid";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TGenericPermission } from "@app/lib/types";
import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
@@ -122,7 +124,9 @@ export const groupServiceFactory = ({
});
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
const isCustomRole = Boolean(rolePermissionDetails?.role);
if (role !== OrgMembershipRole.NoAccess) {
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -235,7 +239,9 @@ export const groupServiceFactory = ({
if (role) {
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
const isCustomRole = Boolean(rolePermissionDetails?.role);
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -880,7 +886,9 @@ export const groupServiceFactory = ({
const groupRoles = groupMembership.roles.map((el) => el.customRoleSlug || el.role);
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles(groupRoles, actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
// check if user has broader or equal to privileges than group
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -959,7 +967,9 @@ export const groupServiceFactory = ({
const groupRoles = groupMembership.roles.map((el) => el.customRoleSlug || el.role);
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles(groupRoles, actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
// check if user has broader or equal to privileges than group
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -1050,7 +1060,9 @@ export const groupServiceFactory = ({
const groupRoles = groupMembership.roles.map((el) => el.customRoleSlug || el.role);
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles(groupRoles, actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
// check if user has broader or equal to privileges than group
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -1127,7 +1139,9 @@ export const groupServiceFactory = ({
const groupRoles = groupMembership.roles.map((el) => el.customRoleSlug || el.role);
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles(groupRoles, actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
// check if user has broader or equal to privileges than group
const permissionBoundary = validatePrivilegeChangeOperation(
diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.ts b/backend/src/ee/services/ldap-config/ldap-config-service.ts
index 1d1e3f0eae..1e5aa96635 100644
--- a/backend/src/ee/services/ldap-config/ldap-config-service.ts
+++ b/backend/src/ee/services/ldap-config/ldap-config-service.ts
@@ -14,6 +14,8 @@ import { addUsersToGroupByUserIds, removeUsersFromGroupByUserIds } from "@app/ee
import { TUserGroupMembershipDALFactory } from "@app/ee/services/group/user-group-membership-dal";
import { throwOnPlanSeatLimitReached } from "@app/ee/services/license/license-fns";
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { sanitizeEmail, validateEmail } from "@app/lib/validator/validate-email";
import { TAuthLoginFactory } from "@app/services/auth/auth-login-service";
@@ -149,7 +151,7 @@ export const ldapConfigServiceFactory = ({
"Failed to create LDAP configuration due to plan restriction. Upgrade plan to create LDAP configuration."
});
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org) {
throw new NotFoundError({ message: `Could not find organization with ID "${orgId}"` });
@@ -277,7 +279,7 @@ export const ldapConfigServiceFactory = ({
"Failed to update LDAP configuration due to plan restriction. Upgrade plan to update LDAP configuration."
});
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org) {
throw new NotFoundError({ message: `Could not find organization with ID "${orgId}"` });
@@ -425,7 +427,7 @@ export const ldapConfigServiceFactory = ({
aliasType: UserAliasType.LDAP
});
- const organization = await orgDAL.findOrgById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!organization) throw new NotFoundError({ message: `Organization with ID '${orgId}' not found` });
if (userAlias) {
diff --git a/backend/src/ee/services/license/license-service.ts b/backend/src/ee/services/license/license-service.ts
index c8e311621f..97071d2383 100644
--- a/backend/src/ee/services/license/license-service.ts
+++ b/backend/src/ee/services/license/license-service.ts
@@ -15,6 +15,8 @@ import { TEnvConfig } from "@app/lib/config/env";
import { verifyOfflineLicense } from "@app/lib/crypto";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { TProjectDALFactory } from "@app/services/project/project-dal";
@@ -391,7 +393,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -430,7 +432,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: "Organization not found"
@@ -498,7 +500,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -581,7 +583,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -625,7 +627,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -660,7 +662,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -687,7 +689,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -724,7 +726,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -763,7 +765,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -799,7 +801,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -827,7 +829,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -858,7 +860,7 @@ export const licenseServiceFactory = ({
OrgPermissionSubjects.Billing
);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -882,7 +884,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
@@ -906,7 +908,7 @@ export const licenseServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionBillingActions.Read, OrgPermissionSubjects.Billing);
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!organization) {
throw new NotFoundError({
message: `Organization with ID '${orgId}' not found`
diff --git a/backend/src/ee/services/oidc/oidc-config-service.ts b/backend/src/ee/services/oidc/oidc-config-service.ts
index 78c6f85bc4..0a129f9274 100644
--- a/backend/src/ee/services/oidc/oidc-config-service.ts
+++ b/backend/src/ee/services/oidc/oidc-config-service.ts
@@ -15,7 +15,9 @@ import { OrgPermissionSsoActions, OrgPermissionSubjects } from "@app/ee/services
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { getConfig } from "@app/lib/config/env";
import { BadRequestError, ForbiddenRequestError, NotFoundError, OidcAuthError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { OrgServiceActor } from "@app/lib/types";
import {
@@ -207,7 +209,7 @@ export const oidcConfigServiceFactory = ({
aliasType: UserAliasType.OIDC
});
- const organization = await orgDAL.findOrgById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!organization) throw new NotFoundError({ message: `Organization with ID '${orgId}' not found` });
let user: TUsers;
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index b0c89be071..32784ab54c 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -818,7 +818,9 @@ export const pamAccountServiceFactory = ({
projectDAL.findById(account.projectId)
);
if (!project) throw new NotFoundError({ message: `Project with ID '${account.projectId}' not found` });
- const org = await orgDAL.findOrgById(project.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(project.orgId), () =>
+ orgDAL.findOrgById(project.orgId)
+ );
if (!org) throw new NotFoundError({ message: `Organization with ID '${project.orgId}' not found` });
// Determine which MFA method to use
@@ -1703,7 +1705,9 @@ export const pamAccountServiceFactory = ({
const actorUser = await userDAL.findById(actorId);
if (!actorUser) throw new NotFoundError({ message: `User with ID '${actorId}' not found` });
- const org = await orgDAL.findOrgById(actorOrgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(actorOrgId), () =>
+ orgDAL.findOrgById(actorOrgId)
+ );
if (!org) throw new NotFoundError({ message: `Organization with ID '${actorOrgId}' not found` });
const orgMfaMethod = org.enforceMfa ? (org.selectedMfaMethod as MfaMethod | null) : undefined;
diff --git a/backend/src/ee/services/pam-web-access/pam-web-access-service.ts b/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
index 489c26d0cc..f360297902 100644
--- a/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
+++ b/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
@@ -275,7 +275,9 @@ export const pamWebAccessServiceFactory = ({
const actorUser = await userDAL.findById(actor.id);
if (!actorUser) throw new NotFoundError({ message: `User with ID '${actor.id}' not found` });
- const org = await orgDAL.findOrgById(project.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(project.orgId), () =>
+ orgDAL.findOrgById(project.orgId)
+ );
if (!org) throw new NotFoundError({ message: `Organization with ID '${project.orgId}' not found` });
// Determine which MFA method to use
diff --git a/backend/src/ee/services/saml-config/saml-config-service.ts b/backend/src/ee/services/saml-config/saml-config-service.ts
index 63e6377fcd..5d221f4123 100644
--- a/backend/src/ee/services/saml-config/saml-config-service.ts
+++ b/backend/src/ee/services/saml-config/saml-config-service.ts
@@ -16,6 +16,8 @@ import {
} from "@app/db/schemas";
import { throwOnPlanSeatLimitReached } from "@app/ee/services/license/license-fns";
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { sanitizeEmail, validateEmail } from "@app/lib/validator/validate-email";
import { TAuthLoginFactory } from "@app/services/auth/auth-login-service";
import { AuthMethod } from "@app/services/auth/auth-type";
@@ -288,7 +290,7 @@ export const samlConfigServiceFactory = ({
"Failed to create SAML SSO configuration due to plan restriction. Upgrade plan to create SSO configuration."
});
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org) {
throw new NotFoundError({ message: `Could not find organization with ID "${orgId}"` });
@@ -360,7 +362,7 @@ export const samlConfigServiceFactory = ({
"Failed to update SAML SSO configuration due to plan restriction. Upgrade plan to update SSO configuration."
});
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org) {
throw new NotFoundError({ message: `Could not find organization with ID "${orgId}"` });
@@ -536,7 +538,7 @@ export const samlConfigServiceFactory = ({
const sanitizedEmail = sanitizeEmail(email);
validateEmail(sanitizedEmail);
- const organization = await orgDAL.findOrgById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!organization) throw new NotFoundError({ message: `Organization with ID '${orgId}' not found` });
const samlConfig = await samlConfigDAL.findOne({ orgId });
diff --git a/backend/src/ee/services/scim/scim-service.ts b/backend/src/ee/services/scim/scim-service.ts
index a6cb418885..d293b7e694 100644
--- a/backend/src/ee/services/scim/scim-service.ts
+++ b/backend/src/ee/services/scim/scim-service.ts
@@ -22,6 +22,8 @@ import { BadRequestError, NotFoundError, ScimRequestError, UnauthorizedError } f
import { logger } from "@app/lib/logger";
import { ms } from "@app/lib/ms";
import { alphaNumericNanoId } from "@app/lib/nanoid";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { sanitizeEmail, validateEmail } from "@app/lib/validator/validate-email";
import { TAdditionalPrivilegeDALFactory } from "@app/services/additional-privilege/additional-privilege-dal";
import { AuthTokenType } from "@app/services/auth/auth-type";
@@ -288,7 +290,7 @@ export const scimServiceFactory = ({
filter,
orgId
}) => {
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org.scimEnabled)
throw new ScimRequestError({
@@ -395,7 +397,7 @@ export const scimServiceFactory = ({
const sanitizedEmail = sanitizeEmail(email);
validateEmail(sanitizedEmail);
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org)
throw new ScimRequestError({
detail: "Organization not found",
@@ -573,7 +575,7 @@ export const scimServiceFactory = ({
// partial
const updateScimUser: TScimServiceFactory["updateScimUser"] = async ({ orgMembershipId, orgId, operations }) => {
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org.orgAuthMethod) {
throw new ScimRequestError({
detail: "Neither SAML or OIDC SSO is configured",
@@ -695,7 +697,7 @@ export const scimServiceFactory = ({
email: unsanitizedEmail,
externalId
}) => {
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org.orgAuthMethod) {
throw new ScimRequestError({
detail: "Neither SAML or OIDC SSO is configured",
@@ -867,7 +869,7 @@ export const scimServiceFactory = ({
message: "Failed to list SCIM groups due to plan restriction. Upgrade plan to list SCIM groups."
});
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new ScimRequestError({
detail: "Organization Not Found",
@@ -985,7 +987,7 @@ export const scimServiceFactory = ({
message: "Failed to create a SCIM group due to plan restriction. Upgrade plan to create a SCIM group."
});
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new ScimRequestError({
@@ -1283,7 +1285,7 @@ export const scimServiceFactory = ({
message: "Failed to update SCIM group due to plan restriction. Upgrade plan to update SCIM group."
});
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new ScimRequestError({
detail: "Organization Not Found",
@@ -1324,7 +1326,7 @@ export const scimServiceFactory = ({
message: "Failed to update SCIM group due to plan restriction. Upgrade plan to update SCIM group."
});
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new ScimRequestError({
@@ -1392,7 +1394,7 @@ export const scimServiceFactory = ({
message: "Failed to delete SCIM group due to plan restriction. Upgrade plan to delete SCIM group."
});
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new ScimRequestError({
detail: "Organization Not Found",
diff --git a/backend/src/lib/request-context/memo-keys.ts b/backend/src/lib/request-context/memo-keys.ts
index 8f6a130064..17e3680ab3 100644
--- a/backend/src/lib/request-context/memo-keys.ts
+++ b/backend/src/lib/request-context/memo-keys.ts
@@ -41,5 +41,9 @@ export const requestMemoKeys = {
userFindById: (userId: string) => `user:findById:${userId}`,
- identityFindById: (identityId: string) => `identity:findById:${identityId}`
+ identityFindById: (identityId: string) => `identity:findById:${identityId}`,
+
+ orgFindById: (orgId: string) => `org:findById:${orgId}`,
+
+ orgFindOrgById: (orgId: string) => `org:findOrgById:${orgId}`
};
diff --git a/backend/src/services/additional-privilege/project/project-additional-privilege-factory.ts b/backend/src/services/additional-privilege/project/project-additional-privilege-factory.ts
index 9fbc7f665f..6dedc7fddd 100644
--- a/backend/src/services/additional-privilege/project/project-additional-privilege-factory.ts
+++ b/backend/src/services/additional-privilege/project/project-additional-privilege-factory.ts
@@ -13,6 +13,8 @@ import {
ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { BadRequestError, NotFoundError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { OrgServiceActor } from "@app/lib/types";
import { unpackPermissions } from "@app/server/routes/sanitizedSchema/permission";
import { ActorType } from "@app/services/auth/auth-type";
@@ -413,7 +415,10 @@ export const newProjectAdditionalPrivilegesFactory = ({
: ([ProjectPermissionIdentityActions.Edit, ProjectPermissionSub.Identity] as const);
ForbiddenError.from(permission).throwUnlessCan(...permissionSet);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const { permission: targetUserPermission, memberships } = await $getPermission(
{ ...dto.permission, type: actorType, id: actorId },
scope.value
@@ -454,7 +459,10 @@ export const newProjectAdditionalPrivilegesFactory = ({
: ([ProjectPermissionIdentityActions.Edit, ProjectPermissionSub.Identity] as const);
ForbiddenError.from(permission).throwUnlessCan(...permissionSet);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const { permission: targetUserPermission, memberships } = await $getPermission(
{ ...dto.permission, type: actorType, id: actorId },
scope.value
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index 33a00c2bc8..2123720c73 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -25,6 +25,8 @@ import {
} from "@app/lib/errors";
import { getMinExpiresIn, removeTrailingSlash } from "@app/lib/fn";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { sanitizeEmail, validateEmail } from "@app/lib/validator";
import { getUserAgentType } from "@app/server/plugins/audit-log";
@@ -255,7 +257,9 @@ export const authLoginServiceFactory = ({
let refreshTokenExpiresIn: string | number = cfg.JWT_REFRESH_LIFETIME;
if (organizationId) {
- const org = await orgDAL.findById(organizationId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(organizationId), () =>
+ orgDAL.findById(organizationId)
+ );
if (org) {
await membershipUserDAL.update(
{ actorUserId: userId, scopeOrgId: org.id, scope: AccessScope.Organization },
@@ -927,7 +931,9 @@ export const authLoginServiceFactory = ({
);
if (authMethod === AuthMethod.GITHUB && serverCfg.defaultAuthOrgId && !appCfg.isCloud) {
- const defaultOrg = await orgDAL.findOrgById(serverCfg.defaultAuthOrgId);
+ const defaultOrg = await requestMemoize(requestMemoKeys.orgFindOrgById(serverCfg.defaultAuthOrgId), () =>
+ orgDAL.findOrgById(serverCfg.defaultAuthOrgId as string)
+ );
if (!defaultOrg) {
throw new BadRequestError({
message: `Failed to find default organization with ID ${serverCfg.defaultAuthOrgId}`
@@ -1158,7 +1164,9 @@ export const authLoginServiceFactory = ({
});
}
- const selectedOrg = await orgDAL.findById(organizationId);
+ const selectedOrg = await requestMemoize(requestMemoKeys.orgFindById(organizationId), () =>
+ orgDAL.findById(organizationId)
+ );
if (!selectedOrg) {
throw new NotFoundError({ message: `Organization with ID '${organizationId}' not found` });
}
@@ -1174,7 +1182,9 @@ export const authLoginServiceFactory = ({
});
}
- rootOrg = await orgDAL.findById(selectedOrg.rootOrgId);
+ rootOrg = await requestMemoize(requestMemoKeys.orgFindById(selectedOrg.rootOrgId), () =>
+ orgDAL.findById(selectedOrg.rootOrgId as string)
+ );
if (!rootOrg) {
throw new BadRequestError({
message: "Invalid sub-organization"
diff --git a/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts b/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts
index f06551a3a5..3d91a60f46 100644
--- a/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts
+++ b/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts
@@ -24,7 +24,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -83,7 +85,9 @@ export const identityAliCloudAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -537,7 +541,10 @@ export const identityAliCloudAuthServiceFactory = ({
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
index 16e22b6fb8..fabb3ec713 100644
--- a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts
@@ -25,7 +25,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -121,7 +123,9 @@ export const identityAwsAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -642,7 +646,10 @@ export const identityAwsAuthServiceFactory = ({
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts b/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts
index 03acd0f43a..90e4c36bad 100644
--- a/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts
+++ b/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts
@@ -20,7 +20,9 @@ import {
UnauthorizedError
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -77,7 +79,9 @@ export const identityAzureAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -543,7 +547,10 @@ export const identityAzureAuthServiceFactory = ({
actorAuthMethod,
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
index ea81657797..d30176cbd6 100644
--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
@@ -20,7 +20,9 @@ import {
UnauthorizedError
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -75,7 +77,9 @@ export const identityGcpAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -591,7 +595,10 @@ export const identityGcpAuthServiceFactory = ({
actorAuthMethod,
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts b/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts
index 7ac6363fee..f4935ee105 100644
--- a/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts
+++ b/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts
@@ -29,7 +29,9 @@ import {
UnauthorizedError
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { getValueByDot } from "@app/lib/template/dot-access";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
@@ -90,7 +92,9 @@ export const identityJwtAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -790,7 +794,10 @@ export const identityJwtAuthServiceFactory = ({
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
index b1541a4e14..23cd2f1d67 100644
--- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
+++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts
@@ -42,7 +42,9 @@ import { GatewayHttpProxyActions, GatewayProxyProtocol, withGatewayProxy } from
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -264,7 +266,9 @@ export const identityKubernetesAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -1281,7 +1285,10 @@ export const identityKubernetesAuthServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
index cb6c80350c..cedd2c137d 100644
--- a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
+++ b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
@@ -31,7 +31,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
@@ -171,7 +173,9 @@ export const identityLdapAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -831,7 +835,10 @@ export const identityLdapAuthServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts b/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts
index da5849b107..c957500fdc 100644
--- a/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts
+++ b/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts
@@ -25,7 +25,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -79,7 +81,9 @@ export const identityOciAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -555,7 +559,9 @@ export const identityOciAuthServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts b/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts
index b4bee1889a..4678d62344 100644
--- a/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts
+++ b/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts
@@ -31,7 +31,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { getValueByDot } from "@app/lib/template/dot-access";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
@@ -91,7 +93,9 @@ export const identityOidcAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -873,7 +877,10 @@ export const identityOidcAuthServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts b/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts
index 264d19bd1b..5b2cc81adb 100644
--- a/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts
+++ b/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts
@@ -29,7 +29,9 @@ import {
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
@@ -297,7 +299,9 @@ export const identitySpiffeAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
let subOrganizationId = isSubOrgIdentity ? org.id : null;
@@ -888,7 +892,10 @@ export const identitySpiffeAuthServiceFactory = ({
actorOrgId
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
index 6a2cbe8100..03ee186c0f 100644
--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
@@ -21,7 +21,9 @@ import {
UnauthorizedError
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -90,7 +92,9 @@ export const identityTlsCertAuthServiceFactory = ({
message: "Identity not found"
});
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
diff --git a/backend/src/services/identity-token-auth/identity-token-auth-service.ts b/backend/src/services/identity-token-auth/identity-token-auth-service.ts
index 5a5efb8c2d..49828299c3 100644
--- a/backend/src/services/identity-token-auth/identity-token-auth-service.ts
+++ b/backend/src/services/identity-token-auth/identity-token-auth-service.ts
@@ -25,6 +25,8 @@ import {
UnauthorizedError
} from "@app/lib/errors";
import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { ActorType, AuthTokenType } from "../auth/auth-type";
import { TIdentityDALFactory } from "../identity/identity-dal";
@@ -385,7 +387,10 @@ export const identityTokenAuthServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
@@ -479,7 +484,10 @@ export const identityTokenAuthServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.CreateToken,
@@ -504,7 +512,9 @@ export const identityTokenAuthServiceFactory = ({
const identity = await identityDAL.findById(identityTokenAuth.identityId);
if (!identity) throw new UnauthorizedError({ message: "Identity not found" });
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a organizationSlug is specified
@@ -776,7 +786,10 @@ export const identityTokenAuthServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.CreateToken,
diff --git a/backend/src/services/identity-ua/identity-ua-service.ts b/backend/src/services/identity-ua/identity-ua-service.ts
index e72be0e146..438f4e8904 100644
--- a/backend/src/services/identity-ua/identity-ua-service.ts
+++ b/backend/src/services/identity-ua/identity-ua-service.ts
@@ -23,7 +23,9 @@ import {
} from "@app/lib/errors";
import { checkIPAgainstBlocklist, extractIPDetails, isValidIpOrCidr, TIp } from "@app/lib/ip";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { AuthAttemptAuthMethod, AuthAttemptAuthResult, authAttemptCounter } from "@app/lib/telemetry/metrics";
import { ActorType, AuthTokenType } from "../auth/auth-type";
@@ -92,7 +94,9 @@ export const identityUaServiceFactory = ({
}
const identity = await identityDAL.findById(identityUa.identityId);
- const org = await orgDAL.findById(identity.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(identity.orgId), () =>
+ orgDAL.findById(identity.orgId)
+ );
const isSubOrgIdentity = Boolean(org.rootOrgId);
// If the identity is a sub-org identity, then the scope is always the org.id, and if it's a root org identity, then we need to resolve the scope if a subOrganizationName is specified
@@ -758,7 +762,10 @@ export const identityUaServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.RevokeAuth,
@@ -848,7 +855,10 @@ export const identityUaServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.CreateToken,
@@ -949,7 +959,10 @@ export const identityUaServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.GetToken,
@@ -1044,7 +1057,10 @@ export const identityUaServiceFactory = ({
actorOrgId,
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.GetToken,
@@ -1135,7 +1151,10 @@ export const identityUaServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(identityMembershipOrg.scopeOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(identityMembershipOrg.scopeOrgId),
+ () => orgDAL.findById(identityMembershipOrg.scopeOrgId)
+ );
const permissionBoundary = validatePrivilegeChangeOperation(
shouldUseNewPrivilegeSystem,
OrgPermissionIdentityActions.DeleteToken,
diff --git a/backend/src/services/identity/identity-service.ts b/backend/src/services/identity/identity-service.ts
index 1eab489108..fac9d47e86 100644
--- a/backend/src/services/identity/identity-service.ts
+++ b/backend/src/services/identity/identity-service.ts
@@ -11,6 +11,8 @@ import {
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError, NotFoundError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TIdentityProjectDALFactory } from "@app/services/identity-project/identity-project-dal";
import { getIdentityActiveLockoutAuthMethods } from "@app/services/identity-v2/identity-fns";
@@ -87,7 +89,9 @@ export const identityServiceFactory = ({
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], orgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
const isCustomRole = Boolean(rolePermissionDetails?.role);
if (isCustomRole) {
const plan = await licenseService.getPlan(orgId);
@@ -215,7 +219,9 @@ export const identityServiceFactory = ({
let customRole: TRoles | undefined;
if (role) {
const [rolePermissionDetails] = await permissionService.getOrgPermissionByRoles([role], actorOrgId);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(actorOrgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(requestMemoKeys.orgFindById(actorOrgId), () =>
+ orgDAL.findById(actorOrgId)
+ );
const isCustomRole = Boolean(rolePermissionDetails?.role);
if (isCustomRole) {
diff --git a/backend/src/services/membership-group/org/org-membership-group-factory.ts b/backend/src/services/membership-group/org/org-membership-group-factory.ts
index ff37c6cf3a..8eb7171944 100644
--- a/backend/src/services/membership-group/org/org-membership-group-factory.ts
+++ b/backend/src/services/membership-group/org/org-membership-group-factory.ts
@@ -13,6 +13,8 @@ import {
} from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { BadRequestError, InternalServerError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { isCustomOrgRole } from "@app/services/org/org-role-fns";
@@ -86,7 +88,10 @@ export const newOrgMembershipGroupFactory = ({
dto.data.roles.map((el) => el.role),
dto.permission.orgId
);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
for (const permissionRole of permissionRoles) {
if (permissionRole?.role?.name !== OrgMembershipRole.NoAccess) {
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -125,7 +130,10 @@ export const newOrgMembershipGroupFactory = ({
dto.permission.orgId
);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
for (const permissionRole of permissionRoles) {
if (permissionRole?.role?.name !== OrgMembershipRole.NoAccess) {
const permissionBoundary = validatePrivilegeChangeOperation(
diff --git a/backend/src/services/membership-group/project/project-membership-group-factory.ts b/backend/src/services/membership-group/project/project-membership-group-factory.ts
index 5561ebec22..94527e2193 100644
--- a/backend/src/services/membership-group/project/project-membership-group-factory.ts
+++ b/backend/src/services/membership-group/project/project-membership-group-factory.ts
@@ -13,6 +13,8 @@ import {
ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { BadRequestError, InternalServerError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { TMembershipGroupDALFactory } from "../membership-group-dal";
@@ -69,7 +71,10 @@ export const newProjectMembershipGroupFactory = ({
const groupDetails = await groupDAL.findById(dto.data.groupId);
if (!groupDetails) throw new BadRequestError({ message: "Group details not found" });
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.map((el) => el.role),
scope.value
@@ -122,7 +127,10 @@ export const newProjectMembershipGroupFactory = ({
const groupDetails = await groupDAL.findById(dto.selector.groupId);
if (!groupDetails) throw new BadRequestError({ message: "Group details not found" });
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.map((el) => el.role),
scope.value
diff --git a/backend/src/services/membership-identity/org/org-membership-identity-factory.ts b/backend/src/services/membership-identity/org/org-membership-identity-factory.ts
index 8a95a55f4e..d7a4651650 100644
--- a/backend/src/services/membership-identity/org/org-membership-identity-factory.ts
+++ b/backend/src/services/membership-identity/org/org-membership-identity-factory.ts
@@ -8,6 +8,8 @@ import {
} from "@app/ee/services/permission/permission-fns";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { BadRequestError, InternalServerError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { isCustomOrgRole } from "@app/services/org/org-role-fns";
@@ -69,7 +71,10 @@ export const newOrgMembershipIdentityFactory = ({
dto.permission.orgId
);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
for (const permissionRole of permissionRoles) {
if (permissionRole?.role?.name !== OrgMembershipRole.NoAccess) {
const permissionBoundary = validatePrivilegeChangeOperation(
@@ -111,7 +116,10 @@ export const newOrgMembershipIdentityFactory = ({
dto.permission.orgId
);
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
for (const permissionRole of permissionRoles) {
if (permissionRole?.role?.name !== OrgMembershipRole.NoAccess) {
const permissionBoundary = validatePrivilegeChangeOperation(
diff --git a/backend/src/services/membership-identity/project/project-membership-identity-factory.ts b/backend/src/services/membership-identity/project/project-membership-identity-factory.ts
index 17c7e0f16a..d67e60c67d 100644
--- a/backend/src/services/membership-identity/project/project-membership-identity-factory.ts
+++ b/backend/src/services/membership-identity/project/project-membership-identity-factory.ts
@@ -12,6 +12,8 @@ import {
ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { BadRequestError, InternalServerError, PermissionBoundaryError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
import { TOrgDALFactory } from "@app/services/org/org-dal";
@@ -78,7 +80,10 @@ export const newProjectMembershipIdentityFactory = ({
throw new BadRequestError({ message: "Failed to create project membership for a project scoped identity" });
}
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.map((el) => el.role),
scope.value
@@ -133,7 +138,10 @@ export const newProjectMembershipIdentityFactory = ({
throw new BadRequestError({ message: "Failed to update project membership for a project scoped identity" });
}
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.filter((el) => el.role !== ProjectMembershipRole.NoAccess).map((el) => el.role),
scope.value
diff --git a/backend/src/services/membership-user/membership-user-service.ts b/backend/src/services/membership-user/membership-user-service.ts
index e0a3503f6f..d3f02d0ccc 100644
--- a/backend/src/services/membership-user/membership-user-service.ts
+++ b/backend/src/services/membership-user/membership-user-service.ts
@@ -12,6 +12,8 @@ import { TPermissionServiceFactory } from "@app/ee/services/permission/permissio
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { groupBy } from "@app/lib/fn";
import { ms } from "@app/lib/ms";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { SearchResourceOperators } from "@app/lib/search-resource/search";
import { isDisposableEmail, sanitizeEmail, validateEmail } from "@app/lib/validator";
@@ -163,7 +165,9 @@ export const membershipUserServiceFactory = ({
const { scopeData, data } = dto;
const factory = scopeFactory[scopeData.scope];
- const orgDetails = await orgDAL.findById(dto.permission.orgId);
+ const orgDetails = await requestMemoize(requestMemoKeys.orgFindById(dto.permission.orgId), () =>
+ orgDAL.findById(dto.permission.orgId)
+ );
// If roles array is empty and scope is Organization, use org's default role
let rolesToUse = data.roles;
@@ -532,7 +536,9 @@ export const membershipUserServiceFactory = ({
await factory.onListMembershipUserGuard(dto);
- const organizationDetails = await orgDAL.findById(dto.scopeData.orgId);
+ const organizationDetails = await requestMemoize(requestMemoKeys.orgFindById(dto.scopeData.orgId), () =>
+ orgDAL.findById(dto.scopeData.orgId)
+ );
if (!organizationDetails.rootOrgId) return { users: [] };
const users = await membershipUserDAL.listAvailableUsers(organizationDetails.id, organizationDetails.rootOrgId);
diff --git a/backend/src/services/membership-user/org/org-membership-user-factory.ts b/backend/src/services/membership-user/org/org-membership-user-factory.ts
index 8e9b2aa2f2..a866a218cf 100644
--- a/backend/src/services/membership-user/org/org-membership-user-factory.ts
+++ b/backend/src/services/membership-user/org/org-membership-user-factory.ts
@@ -7,6 +7,8 @@ import { OrgPermissionActions, OrgPermissionSubjects } from "@app/ee/services/pe
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { getConfig } from "@app/lib/config/env";
import { BadRequestError, ForbiddenRequestError, InternalServerError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { ActorType } from "@app/services/auth/auth-type";
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
import { TokenType } from "@app/services/auth-token/auth-token-types";
@@ -79,7 +81,9 @@ export const newOrgMembershipUserFactory = ({
});
}
- const org = await orgDAL.findById(dto.permission.orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(dto.permission.orgId), () =>
+ orgDAL.findById(dto.permission.orgId)
+ );
if (org?.authEnforced) {
throw new ForbiddenRequestError({
name: "InviteUser",
@@ -122,7 +126,9 @@ export const newOrgMembershipUserFactory = ({
};
const signUpTokens: { email: string; link: string }[] = [];
- const orgDetails = await orgDAL.findById(dto.permission.orgId);
+ const orgDetails = await requestMemoize(requestMemoKeys.orgFindById(dto.permission.orgId), () =>
+ orgDAL.findById(dto.permission.orgId)
+ );
const serverCfg = await getServerCfg();
const isEmailLoginEnabled =
!serverCfg.enabledLoginMethods || serverCfg.enabledLoginMethods.includes(LoginMethod.EMAIL);
diff --git a/backend/src/services/membership-user/project/project-membership-user-factory.ts b/backend/src/services/membership-user/project/project-membership-user-factory.ts
index 0f74683b53..8d941cc3f4 100644
--- a/backend/src/services/membership-user/project/project-membership-user-factory.ts
+++ b/backend/src/services/membership-user/project/project-membership-user-factory.ts
@@ -86,7 +86,10 @@ export const newProjectMembershipUserFactory = ({
throw new BadRequestError({ message: `Users ${missingUsers.join(",")} not part of organization` });
}
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.filter((el) => el.role !== ProjectMembershipRole.NoAccess).map((el) => el.role),
scope.value
@@ -177,7 +180,10 @@ export const newProjectMembershipUserFactory = ({
throw new NotFoundError({ message: `User not found for project membership update` });
}
- const { shouldUseNewPrivilegeSystem } = await orgDAL.findById(dto.permission.orgId);
+ const { shouldUseNewPrivilegeSystem } = await requestMemoize(
+ requestMemoKeys.orgFindById(dto.permission.orgId),
+ () => orgDAL.findById(dto.permission.orgId)
+ );
const permissionRoles = await permissionService.getProjectPermissionByRoles(
dto.data.roles.filter((el) => el.role !== ProjectMembershipRole.NoAccess).map((el) => el.role),
scope.value
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index c4cbde0a53..77bccafc0c 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -33,6 +33,8 @@ import { delay as delayMs } from "@app/lib/delay";
import { BadRequestError, ForbiddenRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { alphaNumericNanoId } from "@app/lib/nanoid";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { QueueName } from "@app/queue";
import { getDefaultOrgMembershipRoleForUpdateOrg } from "@app/services/org/org-role-fns";
import { TOrgMembershipDALFactory } from "@app/services/org-membership/org-membership-dal";
@@ -177,7 +179,7 @@ export const orgServiceFactory = ({
const appCfg = getConfig();
const hasSubOrg = rootOrgId !== actorOrgId;
- const org = await orgDAL.findOrgById(rootOrgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(rootOrgId), () => orgDAL.findOrgById(rootOrgId));
if (!org) throw new NotFoundError({ message: `Organization with ID '${rootOrgId}' not found` });
let subOrg;
@@ -450,7 +452,9 @@ export const orgServiceFactory = ({
}
const plan = await licenseService.getPlan(orgId);
- const currentOrg = await orgDAL.findOrgById(actorOrgId);
+ const currentOrg = await requestMemoize(requestMemoKeys.orgFindOrgById(actorOrgId), () =>
+ orgDAL.findOrgById(actorOrgId)
+ );
if (secretShareBrandConfig !== undefined) {
if (!plan.secretShareExternalBranding) {
@@ -889,7 +893,7 @@ export const orgServiceFactory = ({
const invitingUser = await userDAL.findOne({ id: actorId });
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
const [inviteeOrgMembership] = await orgDAL.findMembership({
[`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
@@ -977,7 +981,7 @@ export const orgServiceFactory = ({
});
}
- const organization = await orgDAL.findById(orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
await tokenService.validateTokenForUser({
type: TokenType.TOKEN_EMAIL_ORG_INVITATION,
@@ -1236,7 +1240,9 @@ export const orgServiceFactory = ({
invitedUsers.map(async (invitedUser) => {
let org = orgCache[invitedUser.scopeOrgId];
if (!org) {
- org = await orgDAL.findById(invitedUser.scopeOrgId);
+ org = await requestMemoize(requestMemoKeys.orgFindById(invitedUser.scopeOrgId), () =>
+ orgDAL.findById(invitedUser.scopeOrgId)
+ );
orgCache[invitedUser.scopeOrgId] = org;
}
diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts
index 6d76f2b5e0..b6c2384b9f 100644
--- a/backend/src/services/secret-sharing/secret-sharing-service.ts
+++ b/backend/src/services/secret-sharing/secret-sharing-service.ts
@@ -11,6 +11,8 @@ import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, ForbiddenRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { ms } from "@app/lib/ms";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { OrgServiceActor, SecretSharingAccessType } from "@app/lib/types";
import { ActorType } from "../auth/auth-type";
@@ -726,7 +728,10 @@ export const secretSharingServiceFactory = ({
sharedSecret.orgId === orgId &&
sharedSecret.accessType === SecretSharingAccessType.Organization
) {
- organization = await orgDAL.findOrgById(sharedSecret.orgId);
+ const sharedOrgId = sharedSecret.orgId;
+ organization = await requestMemoize(requestMemoKeys.orgFindOrgById(sharedOrgId), () =>
+ orgDAL.findOrgById(sharedOrgId)
+ );
}
// decrement when we are sure the user will view secret.
@@ -827,7 +832,7 @@ export const secretSharingServiceFactory = ({
return null;
}
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
const assets = await orgAssetDAL.listAssetsByType(orgId, ["brand-logo", "brand-favicon"]);
const hasLogo = assets.some((a) => a.assetType === "brand-logo");
diff --git a/backend/src/services/secret-sync/secret-sync-service.ts b/backend/src/services/secret-sync/secret-sync-service.ts
index d51f07c676..e312f4ab1e 100644
--- a/backend/src/services/secret-sync/secret-sync-service.ts
+++ b/backend/src/services/secret-sync/secret-sync-service.ts
@@ -13,6 +13,8 @@ import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { deepEqualSkipFields } from "@app/lib/fn/object";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { OrgServiceActor } from "@app/lib/types";
import { TAppConnectionServiceFactory } from "@app/services/app-connection/app-connection-service";
import { TOrgDALFactory } from "@app/services/org/org-dal";
@@ -359,7 +361,9 @@ export const secretSyncServiceFactory = ({
// getProjectPermission above throws NotFoundError if the project doesn't exist and
// guarantees actor.orgId === project.orgId — no separate project lookup needed.
- const organization = await orgDAL.findById(actor.orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(actor.orgId), () =>
+ orgDAL.findById(actor.orgId)
+ );
if (organization?.blockDuplicateSecretSyncDestinations) {
const duplicateCheck = await checkDuplicateDestination(
{
@@ -490,7 +494,9 @@ export const secretSyncServiceFactory = ({
if (params.destinationConfig) {
// getProjectPermission above throws NotFoundError if the project doesn't exist and
// guarantees actor.orgId === project.orgId — no separate project lookup needed.
- const organization = await orgDAL.findById(actor.orgId);
+ const organization = await requestMemoize(requestMemoKeys.orgFindById(actor.orgId), () =>
+ orgDAL.findById(actor.orgId)
+ );
if (organization?.blockDuplicateSecretSyncDestinations) {
const duplicateCheck = await checkDuplicateDestination(
diff --git a/backend/src/services/service-token/service-token-service.ts b/backend/src/services/service-token/service-token-service.ts
index 9b8c431c41..d99bd0e835 100644
--- a/backend/src/services/service-token/service-token-service.ts
+++ b/backend/src/services/service-token/service-token-service.ts
@@ -191,7 +191,9 @@ export const serviceTokenServiceFactory = ({
if (!isMatch) throw new UnauthorizedError({ message: "Invalid service token" });
await accessTokenQueue.updateServiceTokenStatus(serviceToken.id);
- const serviceTokenOrgDetails = await orgDAL.findById(project.orgId);
+ const serviceTokenOrgDetails = await requestMemoize(requestMemoKeys.orgFindById(project.orgId), () =>
+ orgDAL.findById(project.orgId)
+ );
return {
...serviceToken,
diff --git a/backend/src/services/super-admin/super-admin-service.ts b/backend/src/services/super-admin/super-admin-service.ts
index 63f21e003a..c8f66d1e93 100644
--- a/backend/src/services/super-admin/super-admin-service.ts
+++ b/backend/src/services/super-admin/super-admin-service.ts
@@ -24,6 +24,8 @@ import {
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { OrgServiceActor } from "@app/lib/types";
import { isDisposableEmail, sanitizeEmail, validateEmail } from "@app/lib/validator";
import { TAuthTokenServiceFactory } from "@app/services/auth-token/auth-token-service";
@@ -977,7 +979,7 @@ export const superAdminServiceFactory = ({
throw new NotFoundError({ message: "Could not find server admin user" });
}
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
if (!org) {
throw new NotFoundError({ message: `Could not organization with ID "${orgId}"` });
@@ -1042,7 +1044,9 @@ export const superAdminServiceFactory = ({
throw new BadRequestError({ message: "No invite email associated with user." });
}
- const org = await orgDAL.findOrgById(orgMembership.scopeOrgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgMembership.scopeOrgId), () =>
+ orgDAL.findOrgById(orgMembership.scopeOrgId)
+ );
const appCfg = getConfig();
const serverAdmin = await userDAL.findById(actor.id);
@@ -1194,7 +1198,7 @@ export const superAdminServiceFactory = ({
};
const createEmailDomain = async ({ orgId, domain }: TAdminCreateEmailDomainDTO) => {
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (!org) throw new NotFoundError({ message: `Organization with ID '${orgId}' not found` });
const config = getConfig();
diff --git a/backend/src/services/telemetry/telemetry-service.ts b/backend/src/services/telemetry/telemetry-service.ts
index 73860e48a4..66f871eb46 100644
--- a/backend/src/services/telemetry/telemetry-service.ts
+++ b/backend/src/services/telemetry/telemetry-service.ts
@@ -8,7 +8,9 @@ import { getConfig } from "@app/lib/config/env";
import { request } from "@app/lib/config/request";
import { crypto } from "@app/lib/crypto/cryptography";
import { logger } from "@app/lib/logger";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { RequestContextKey } from "@app/lib/request-context/request-context-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { ActorType } from "@app/services/auth/auth-type";
import { TOrgDALFactory } from "@app/services/org/org-dal";
@@ -204,7 +206,7 @@ To opt into telemetry, you can set "TELEMETRY_ENABLED=true" within the environme
}
try {
- const org = await orgDAL.findOrgById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindOrgById(orgId), () => orgDAL.findOrgById(orgId));
if (org) {
if (!properties.name) {
properties.name = org.name;
diff --git a/backend/src/services/user-engagement/user-engagement-service.ts b/backend/src/services/user-engagement/user-engagement-service.ts
index b146729036..0e33d1e39e 100644
--- a/backend/src/services/user-engagement/user-engagement-service.ts
+++ b/backend/src/services/user-engagement/user-engagement-service.ts
@@ -2,6 +2,8 @@ import axios from "axios";
import { getConfig } from "@app/lib/config/env";
import { InternalServerError } from "@app/lib/errors";
+import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
+import { requestMemoize } from "@app/lib/request-context/request-memoizer";
import { TOrgDALFactory } from "../org/org-dal";
import { TUserDALFactory } from "../user/user-dal";
@@ -16,7 +18,7 @@ export type TUserEngagementServiceFactory = ReturnType<typeof userEngagementServ
export const userEngagementServiceFactory = ({ userDAL, orgDAL }: TUserEngagementServiceFactoryDep) => {
const createUserWish = async (userId: string, orgId: string, text: string) => {
const user = await userDAL.findById(userId);
- const org = await orgDAL.findById(orgId);
+ const org = await requestMemoize(requestMemoKeys.orgFindById(orgId), () => orgDAL.findById(orgId));
const appCfg = getConfig();
if (!appCfg.PYLON_API_KEY) {