-
Notifications
You must be signed in to change notification settings - Fork 0
feat: http utils read only org ims gate #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d9cafdb
5d11789
cda32d8
36251ff
66bac24
004d025
0724016
8ba90f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| export const FF_READ_ONLY_ADMIN = 'FT_LLMO-3008'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { Response } from '@adobe/fetch'; | ||
| import { isObject } from '@adobe/spacecat-shared-utils'; | ||
| import { LaunchDarklyClient } from '@adobe/spacecat-shared-launchdarkly-client'; | ||
|
|
||
| import { FF_READ_ONLY_ADMIN } from './constants.js'; | ||
| import { guardNonEmptyRouteCapabilities, resolveRouteCapability } from './route-utils.js'; | ||
|
|
||
| function forbidden(message) { | ||
| return new Response(JSON.stringify({ message }), { | ||
| status: 403, | ||
| headers: { 'Content-Type': 'application/json; charset=utf-8', 'x-error': message }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the read-only admin feature flag for the authenticated user's IMS org. | ||
| * Uses {@link AuthInfo#getTenantIds} to resolve the org and | ||
| * {@link LaunchDarklyClient#isFlagEnabledForIMSOrg} for evaluation. | ||
| * Fail-closed: returns false when the client/org is unavailable or evaluation errors. | ||
| * | ||
| * @param {Object} context - Universal context (lambda context) | ||
| * @param {Object} authInfo - The AuthInfo instance for the current user | ||
| * @returns {Promise<boolean>} | ||
| */ | ||
| async function evaluateFeatureFlag(context, authInfo) { | ||
| try { | ||
| const ldClient = LaunchDarklyClient.createFrom(context); | ||
| if (!ldClient) return false; | ||
|
|
||
| const tenantIds = authInfo.getTenantIds?.() || []; | ||
| const ident = tenantIds[0]; | ||
| if (!ident) return false; | ||
|
|
||
| const imsOrgId = `${ident}@AdobeOrg`; | ||
| return await ldClient.isFlagEnabledForIMSOrg(FF_READ_ONLY_ADMIN, imsOrgId); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read-only admin authorization wrapper for the helix-shared-wrap `.with()` chain. | ||
| * | ||
| * After successful authentication (authInfo already set on context by an earlier | ||
| * wrapper), this wrapper checks whether the authenticated user is a read-only admin. | ||
| * If so it: | ||
| * | ||
| * 1. Evaluates the `FT_LLMO-3008` LaunchDarkly feature flag (fail-closed). | ||
|
||
| * 2. Resolves the route's action from the routeCapabilities map and blocks | ||
| * write operations (or unmapped routes) for RO admins. | ||
| * 3. Emits a structured audit log entry for allowed RO admin requests. | ||
| * | ||
| * Non-RO-admin requests pass through untouched. | ||
| * | ||
| * @param {Function} fn - The handler to wrap. | ||
| * @param {{ routeCapabilities?: Object<string, string> }} opts - Map of route | ||
| * patterns (e.g. 'GET /sites/:siteId') to action strings ('read' | 'write'). | ||
| * @returns {Function} A wrapped handler. | ||
| */ | ||
| export function readOnlyAdminWrapper(fn, { routeCapabilities } = {}) { | ||
| guardNonEmptyRouteCapabilities('readOnlyAdminWrapper', routeCapabilities); | ||
|
|
||
| return async (request, context) => { | ||
| const { log } = context; | ||
| const authInfo = context.attributes?.authInfo; | ||
|
|
||
| if (authInfo?.isReadOnlyAdmin?.()) { | ||
| const ffEnabled = await evaluateFeatureFlag(context, authInfo); | ||
| if (!ffEnabled) { | ||
| log.warn('[ro-admin] Feature flag disabled, denying RO admin access'); | ||
| return forbidden('Read-only admin access is not enabled'); | ||
| } | ||
|
|
||
| if (isObject(routeCapabilities)) { | ||
ravverma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const capability = resolveRouteCapability(context, routeCapabilities); | ||
| const action = capability?.split(':').pop(); | ||
|
|
||
| if (action !== 'read') { | ||
| const route = `${context.pathInfo?.method} ${context.pathInfo?.suffix}`; | ||
| log.warn(`[ro-admin] Read-only admin blocked from route: ${route}`); | ||
| return forbidden('Read-only admin users cannot perform write operations'); | ||
| } | ||
| } | ||
|
|
||
| log.info(`[ro-admin-audit] RO admin accessed: ${context.pathInfo?.method} ${context.pathInfo?.suffix}`); | ||
| } | ||
|
|
||
| return fn(request, context); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { isObject } from '@adobe/spacecat-shared-utils'; | ||
|
|
||
| /** | ||
| * Matches pre-split request segments against a route pattern with :param segments. | ||
| * e.g. ['sites', 'abc-123', 'audits'] matches 'GET /sites/:siteId/audits' | ||
| */ | ||
| function matchRoute(method, requestSegments, routeKey) { | ||
| const spaceIdx = routeKey.indexOf(' '); | ||
| if (spaceIdx === -1) return false; | ||
|
|
||
| const routeMethod = routeKey.slice(0, spaceIdx); | ||
| if (routeMethod !== method) return false; | ||
|
|
||
| const routeSegments = routeKey.slice(spaceIdx + 1).split('/').filter(Boolean); | ||
| if (routeSegments.length !== requestSegments.length) return false; | ||
|
|
||
| return routeSegments.every( | ||
| (seg, i) => seg.charCodeAt(0) === 58 /* ':' */ || seg === requestSegments[i], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Looks up the value mapped to the current request from a route map | ||
| * using the method and path from context.pathInfo. Supports both exact | ||
| * matches and parameterized route patterns (e.g. 'GET /sites/:siteId'). | ||
| * | ||
| * @param {Object} context - Universal context with pathInfo | ||
| * @param {Object<string, string>} routeMap - Route pattern to value map | ||
| * @returns {string|null} The matched value or null | ||
| */ | ||
| export function resolveRouteCapability(context, routeMap) { | ||
| const method = context.pathInfo?.method?.toUpperCase(); | ||
| const path = context.pathInfo?.suffix; | ||
| if (!method || !path) return null; | ||
|
|
||
| const exactKey = `${method} ${path}`; | ||
| if (routeMap[exactKey]) return routeMap[exactKey]; | ||
|
|
||
| const requestSegments = path.split('/').filter(Boolean); | ||
| const matchedKey = Object.keys(routeMap) | ||
| .find((key) => matchRoute(method, requestSegments, key)); | ||
| return matchedKey ? routeMap[matchedKey] : null; | ||
| } | ||
|
|
||
| /** | ||
| * Throws at wrapper creation time if routeCapabilities is an empty object. | ||
| * An empty map would silently deny/block all requests, so this is a | ||
| * fail-fast guard against misconfiguration. | ||
| * | ||
| * @param {string} wrapperName - Name of the wrapper (for the error message) | ||
| * @param {Object} routeCapabilities - The route capabilities map | ||
| */ | ||
| export function guardNonEmptyRouteCapabilities(wrapperName, routeCapabilities) { | ||
| if (isObject(routeCapabilities) && Object.keys(routeCapabilities).length === 0) { | ||
| throw new Error(`${wrapperName}: routeCapabilities must not be an empty object`); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,19 @@ export declare function unauthorized(message?: string, headers?: object): Respon | |
|
|
||
| export declare function forbidden(message?: string, headers?: object): Response; | ||
|
|
||
| /** | ||
| * Read-only admin authorization wrapper for the helix-shared-wrap `.with()` chain. | ||
| * Blocks write operations for read-only admin users, gated by a LaunchDarkly feature flag. | ||
| * | ||
| * @param fn - The handler to wrap. | ||
| * @param opts - Options containing a routeCapabilities map of route patterns to actions. | ||
| * @returns A wrapped handler. | ||
| */ | ||
| export function readOnlyAdminWrapper( | ||
| fn: Function, | ||
| opts?: { routeCapabilities?: Record<string, string> }, | ||
|
||
| ): Function; | ||
|
|
||
| /** | ||
| * Utility functions | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.