-
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
Open
ravverma
wants to merge
8
commits into
main
Choose a base branch
from
feat/http-utils-read-only-org-ims-gate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+997
−61
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d9cafdb
feat(http-utils): read-only org IMS gate, RO admin wrapper, and auth …
ravverma 5d11789
test(http-utils): cover JWT read-only admin and conflicting admin claims
ravverma cda32d8
fix: review commits
ravverma 36251ff
Merge branch 'main' into feat/http-utils-read-only-org-ims-gate
ravverma 66bac24
fix: review comments
ravverma 004d025
fix: rebase with main
ravverma 0724016
fix: update FF id
ravverma 8ba90f7
fix: rebase with main
ravverma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_ORG = 'FT_READ_ONLY_ORG'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/spacecat-shared-http-utils/src/auth/read-only-admin-wrapper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * 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_ORG } 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_ORG, 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_READ_ONLY_ORG` 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 - Required map of route | ||
| * patterns (e.g. 'GET /sites/:siteId') to action strings ('read' | 'write'). | ||
| * @returns {Function} A wrapped handler. | ||
| */ | ||
| export function readOnlyAdminWrapper(fn, { routeCapabilities } = {}) { | ||
| if (!routeCapabilities) { | ||
| throw new Error('readOnlyAdminWrapper: routeCapabilities is required'); | ||
| } | ||
| 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({ | ||
| tag: 'ro-admin', | ||
| email: authInfo.getProfile?.()?.email, | ||
| org: authInfo.getTenantIds?.()[0], | ||
| }, 'Feature flag disabled, denying RO admin access'); | ||
| return forbidden('Forbidden'); | ||
| } | ||
|
|
||
| if (isObject(routeCapabilities)) { | ||
ravverma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const capability = resolveRouteCapability(context, routeCapabilities); | ||
| // capability format is 'resource:action' (e.g. 'site:read', 'site:write'). | ||
| // split(':').pop() extracts the action; a missing or malformed value yields | ||
| // undefined, which is !== 'read' and correctly blocks the request. | ||
| const action = capability?.split(':').pop(); | ||
|
|
||
| if (action !== 'read') { | ||
| log.warn({ | ||
| tag: 'ro-admin', | ||
| email: authInfo.getProfile?.()?.email, | ||
| method: context.pathInfo?.method, | ||
| suffix: context.pathInfo?.suffix, | ||
| org: authInfo.getTenantIds?.()[0], | ||
| }, 'Read-only admin blocked from route'); | ||
| return forbidden('Forbidden'); | ||
| } | ||
| } | ||
|
|
||
| log.info({ | ||
| tag: 'ro-admin-audit', | ||
| email: authInfo.getProfile?.()?.email, | ||
| method: context.pathInfo?.method, | ||
| suffix: context.pathInfo?.suffix, | ||
| org: authInfo.getTenantIds?.()[0], | ||
| }, 'RO admin accessed route'); | ||
| } | ||
|
|
||
| return fn(request, context); | ||
| }; | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
packages/spacecat-shared-http-utils/src/auth/route-utils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.