generated from RedHatInsights/frontend-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 54
feat(RHINENG-23947): Add Kessel perms #1521
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
Draft
mtclinton
wants to merge
1
commit into
RedHatInsights:master
Choose a base branch
from
mtclinton:RHINENG-23947
base: master
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.
Draft
Changes from all commits
Commits
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
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
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
15 changes: 8 additions & 7 deletions
15
src/PresentationalComponents/WithPermission/WithPermission.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 |
|---|---|---|
| @@ -1,20 +1,21 @@ | ||
| import React from 'react'; | ||
| import propTypes from 'prop-types'; | ||
| import { NotAuthorized } from '@redhat-cloud-services/frontend-components/NotAuthorized'; | ||
| import { usePermissionsWithContext } from '@redhat-cloud-services/frontend-components-utilities/RBACHook'; | ||
| import usePermissionCheck from '../../Utilities/hooks/usePermissionCheck'; | ||
|
|
||
| const WithPermission = ({ children, requiredPermissions = [] }) => { | ||
| const { hasAccess, isLoading } = usePermissionsWithContext(requiredPermissions); | ||
| if (!isLoading) { | ||
| return hasAccess ? children : <NotAuthorized serviceName='patch' />; | ||
| } else { | ||
| return ''; | ||
| const WithPermission = ({ children, requiredPermissions = [], hide = false }) => { | ||
| const { hasAccess, isLoading } = usePermissionCheck(requiredPermissions); | ||
|
|
||
| if (isLoading) { | ||
| return null; | ||
| } | ||
| return hasAccess ? children : !hide && <NotAuthorized serviceName='patch' />; | ||
| }; | ||
|
|
||
| WithPermission.propTypes = { | ||
| children: propTypes.node, | ||
| requiredPermissions: propTypes.array, | ||
| hide: propTypes.bool, | ||
| }; | ||
|
|
||
| export default WithPermission; |
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
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
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,36 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { RBAC_API_BASE_V2 } from '../constants'; | ||
|
|
||
| const STALE_TIME = 5 * 60 * 1000; | ||
|
|
||
| export const useKesselWorkspaces = (options = {}) => | ||
| useQuery({ | ||
| queryKey: ['workspaces', options.type, options.limit], | ||
| queryFn: async () => { | ||
| const response = await fetch( | ||
| `${RBAC_API_BASE_V2}/workspaces/?limit=${options.limit ?? 1000}&type=${options.type ?? 'all'}`, | ||
| ); | ||
| if (!response.ok) { | ||
| throw new Error('Failed to fetch workspaces'); | ||
| } | ||
| const data = await response.json(); | ||
| return data.data || []; | ||
| }, | ||
| enabled: options.enabled ?? true, | ||
| staleTime: options.staleTime, | ||
| }); | ||
|
|
||
| export const useFetchDefaultWorkspaceId = (enabled = true) => { | ||
| const { | ||
| data: workspaces, | ||
| isLoading, | ||
| error, | ||
| } = useKesselWorkspaces({ type: 'default', limit: 1, staleTime: STALE_TIME, enabled }); | ||
| const defaultWorkspace = workspaces?.[0]; | ||
|
|
||
| return { | ||
| workspaceId: defaultWorkspace?.id, | ||
| isLoading: enabled ? isLoading : false, | ||
| error, | ||
| }; | ||
| }; |
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,81 @@ | ||
| import { useMemo } from 'react'; | ||
| import { usePermissionsWithContext } from '@redhat-cloud-services/frontend-components-utilities/RBACHook'; | ||
| import { useSelfAccessCheck } from '@project-kessel/react-kessel-access-check'; | ||
| import { useFetchDefaultWorkspaceId } from './useKesselWorkspaces'; | ||
| import useFeatureFlag from './useFeatureFlag'; | ||
| import { featureFlags } from '../constants'; | ||
|
|
||
| export const PERMISSION_MAP = { | ||
| 'patch:*:read': 'patch_system_view', | ||
| 'patch:*:*': 'patch_system_edit', | ||
| 'patch:template:write': 'patch_template_edit', | ||
| }; | ||
|
|
||
| const getKesselAccessCheckParams = (permissionMap, requiredPermissions, workspaceId) => { | ||
| const resources = requiredPermissions | ||
| .map((perm) => { | ||
| const relation = permissionMap[perm]; | ||
| if (!relation) { | ||
| return null; | ||
| } | ||
| return { id: workspaceId, type: 'workspace', relation }; | ||
| }) | ||
| .filter(Boolean); | ||
|
|
||
| if (resources.length === 0) { | ||
| return { resources: [] }; | ||
| } | ||
|
|
||
| if (resources.length === 1) { | ||
| return { | ||
| resource: { id: resources[0].id, type: resources[0].type }, | ||
| relation: resources[0].relation, | ||
| }; | ||
| } | ||
|
|
||
| return { resources }; | ||
| }; | ||
|
|
||
| export const useRbacV1Permissions = (requiredPermissions) => { | ||
| const { hasAccess, isLoading } = usePermissionsWithContext(requiredPermissions); | ||
| return { hasAccess, isLoading }; | ||
| }; | ||
|
|
||
| export const useKesselPermissions = (requiredPermissions, enabled = true) => { | ||
| const { workspaceId, isLoading: workspaceLoading } = useFetchDefaultWorkspaceId(enabled); | ||
|
|
||
| const checkParams = useMemo( | ||
| () => getKesselAccessCheckParams(PERMISSION_MAP, requiredPermissions, workspaceId), | ||
| [workspaceId, requiredPermissions], | ||
| ); | ||
|
|
||
| const { data, loading, error } = useSelfAccessCheck(checkParams); | ||
|
|
||
| if (workspaceLoading) { | ||
| return { hasAccess: false, isLoading: true }; | ||
| } | ||
|
|
||
| if (!workspaceId || error) { | ||
| return { hasAccess: false, isLoading: false }; | ||
| } | ||
|
|
||
| const hasAccess = Array.isArray(data) | ||
| ? data.some((check) => check.allowed) | ||
| : (data?.allowed ?? false); | ||
|
|
||
| return { hasAccess, isLoading: loading }; | ||
| }; | ||
|
|
||
| const usePermissionCheck = (requiredPermissions) => { | ||
| const isKesselEnabled = useFeatureFlag(featureFlags.kessel_enabled); | ||
| const rbac = useRbacV1Permissions(requiredPermissions); | ||
| const kessel = useKesselPermissions(requiredPermissions, !!isKesselEnabled); | ||
|
|
||
| if (isKesselEnabled === undefined) { | ||
| return { hasAccess: false, isLoading: true }; | ||
| } | ||
|
|
||
| return isKesselEnabled ? kessel : rbac; | ||
| }; | ||
|
|
||
| export default usePermissionCheck; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just used the compliance frontend version, package lock is set to latest: 5.90.21