Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
chore(vendor): update @lightbasenl/backend
Browse files Browse the repository at this point in the history
_This PR is created by sync and will be force-pushed daily. Overwriting any manual changes done to this PR._

- feat(backend): support `oneOfRequiredPermissions` (lightbasenl/platform-components@f78adcc)
- feat(feature-flag,multitenant): report cache events as Sentry metric (lightbasenl/platform-components@22ebc13)- Failed to execute `npx compas lint`. Sync is not able to correct this, so human checks and fixes are necessary for this PR.
  • Loading branch information
github-actions[bot] committed Jun 18, 2024
1 parent 07e7822 commit 573a780
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 10 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"scripts": {},
"dependencies": {
"@lightbase/pull-through-cache": "0.1.2",
"@lightbase/pull-through-cache": "0.2.0",
"@xmldom/xmldom": "0.8.10",
"bcrypt": "5.1.1",
"rate-limiter-flexible": "5.0.3",
Expand All @@ -30,5 +30,5 @@
"url": "https://github.com/lightbasenl/platform-components.git",
"directory": "packages/backend"
},
"gitHead": "b335a6c8aa6f5e14489b582b02b6fa45beda00b6"
"gitHead": "f78adcc36cb69030ffdd5d59d77ba3dc5a0ed272"
}
48 changes: 47 additions & 1 deletion vendor/backend/src/auth/user.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ const authQueries = {
* @property {boolean|undefined} [requireDigidBased]
* @property {boolean|undefined} [requireKeycloakBased]
* @property {boolean|undefined} [requirePasswordBased]
* @property {AuthPermissionIdentifier[]|undefined} [requiredPermissions]
* @property {AuthPermissionIdentifier[]|undefined} [requiredPermissions] Require all
* provided permissions
* @property {AuthPermissionIdentifier[]|undefined} [oneOfRequiredPermissions] Require
* one of the provided permissions
*/

/**
Expand Down Expand Up @@ -632,6 +635,49 @@ export async function authRequireUser(
});
}

if (
Array.isArray(options.requiredPermissions) ||
Array.isArray(options.oneOfRequiredPermissions)
) {
const permissionSet = new Set();
// @ts-expect-error
for (const role of user.roles) {
// @ts-expect-error
for (const rolePermission of role.role.permissions) {
permissionSet.add(rolePermission.permission.identifier);
}
}

if (options.requiredPermissions) {
const missingPermissions = [];
for (const requiredPermission of options.requiredPermissions) {
if (!permissionSet.has(requiredPermission)) {
missingPermissions.push(requiredPermission);
}
}

if (missingPermissions.length > 0) {
throw AppError.validationError(`${eventKey}.missingPermissions`, {
missingPermissions,
});
}
} else if (options.oneOfRequiredPermissions) {
let hasOnePermission = false;
for (const requiredPermission of options.oneOfRequiredPermissions) {
if (permissionSet.has(requiredPermission)) {
hasOnePermission = true;
break;
}
}

if (!hasOnePermission) {
throw AppError.validationError(`${eventKey}.missingPermissions`, {
missingPermissions: options.oneOfRequiredPermissions,
});
}
}
}

if (
Array.isArray(options.requiredPermissions) &&
options.requiredPermissions.length > 0
Expand Down
4 changes: 4 additions & 0 deletions vendor/backend/src/feature-flag/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AppError } from "@compas/stdlib";
import { PullThroughCache } from "@lightbase/pull-through-cache";
import { queryFeatureFlag, sql } from "../services.js";
import { cacheEventToSentryMetric } from "../util.js";

/**
* Short TTL feature flag cache. Keeps all flags for 5 seconds in memory,
Expand All @@ -16,6 +17,9 @@ export const featureFlagCache = new PullThroughCache()
})
.withFetcher({
fetcher: featureFlagFetcher,
})
.withEventCallback({
callback: cacheEventToSentryMetric("featureFlag"),
});

/**
Expand Down
3 changes: 1 addition & 2 deletions vendor/backend/src/feature-flag/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ export async function featureFlagSetDynamic(

if (featureFlagCache.isEnabled()) {
// Clear the cache if enabled. This method function is often only used in test code, which most likely disables the cache anyways.
featureFlagCache.disable();
featureFlagCache.enable();
featureFlagCache.clearAll();
}

eventStop(event);
Expand Down
4 changes: 4 additions & 0 deletions vendor/backend/src/multitenant/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isNil, uuid } from "@compas/stdlib";
import { PullThroughCache } from "@lightbase/pull-through-cache";
import { queryTenant, sql, tenantBuilder } from "../services.js";
import { cacheEventToSentryMetric } from "../util.js";

/**
* Frequently sampled tenant cache.
Expand All @@ -19,6 +20,9 @@ export const tenantCache = new PullThroughCache()
})
.withFetcher({
fetcher: tenantFetcher,
})
.withEventCallback({
callback: cacheEventToSentryMetric("tenant"),
});

/**
Expand Down
21 changes: 20 additions & 1 deletion vendor/backend/src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { existsSync } from "node:fs";
import { AppError, isNil, pathJoin } from "@compas/stdlib";
import { _compasSentryExport, AppError, isNil, pathJoin } from "@compas/stdlib";

/**
* Count the different PullThroughCache events as their own metric
*
* @param {string} cacheName
* @returns {(function(string): void)}
*/
export function cacheEventToSentryMetric(cacheName) {
return (event) => {
if (_compasSentryExport?.metrics?.increment) {
_compasSentryExport.metrics.increment(`cache.${event}`, 1, {
tags: {
cacheName,
},
unit: "none",
});
}
};
}

/**
* Takes an AppError and normalizes it to a 401, to simplify frontend error handling on
Expand Down

0 comments on commit 573a780

Please sign in to comment.