Skip to content

Commit

Permalink
fix: incorrect cluster fetching bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskari committed Jul 5, 2024
1 parent e879b0a commit a49e960
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
23 changes: 20 additions & 3 deletions src/state/navigation/extensionsAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getFetchFn } from '../utils/getFetchFn';
import { configurationAtom } from 'state/configuration/configurationAtom';
import { openapiPathIdListSelector } from 'state/openapi/openapiPathIdSelector';
import {
getPermissionResourceRules,
permissionSetsSelector,
PermissionSetState,
} from 'state/permissionSetsSelector';
Expand All @@ -24,6 +25,7 @@ import { RESOURCE_PATH } from 'hooks/useMessageList';
import pluralize from 'pluralize';
import { useGet } from 'shared/hooks/BackendAPI/useGet';
import { CustomResourceDefinition } from 'command-pallette/CommandPalletteUI/handlers/crHandler';
import { createPostFn } from 'shared/hooks/BackendAPI/usePost';

/*
the order of the overwrting extensions
Expand Down Expand Up @@ -92,15 +94,30 @@ async function getConfigMapsWithSelector(
const namespacedCMUrl = `/api/v1/namespaces/${currentNamespace ??
kubeconfigNamespace}/configmaps?labelSelector=${selector}`;

const hasAccessToClusterCMList = doesUserHavePermission(
const namespaceAccess = doesUserHavePermission(
['list'],
{ resourceGroupAndVersion: '', resourceKind: 'ConfigMap' },
permissionSet,
);
console.log(hasAccessToClusterCMList);

const postFn = createPostFn(fetchFn);
const clusterPermissionSet = await getPermissionResourceRules(
postFn,
'',
true,
);
const clusterAccess = doesUserHavePermission(
['list'],
{ resourceGroupAndVersion: '', resourceKind: 'ConfigMap' },
clusterPermissionSet,
);

// if user has no access to clusterwide namespace listing, fall back to namespaced listing
const url = hasAccessToClusterCMList ? clusterCMUrl : namespacedCMUrl;
const url = clusterAccess
? clusterCMUrl
: namespaceAccess
? namespacedCMUrl
: '';

if (!currentNamespace) {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/state/permissionSetsSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export function hasAnyRoleBound(permissionSet: PermissionSetState) {
export async function getPermissionResourceRules(
postFn: PostFn,
namespaceId?: string,
clusterWide?: boolean,
) {
const namespaceName = namespaceId ? namespaceId : '*';
const namespaceName = clusterWide || !namespaceId ? '*' : namespaceId;
const path = '/apis/authorization.k8s.io/v1/selfsubjectrulesreviews';
const ssrr = {
typeMeta: {
Expand Down
26 changes: 23 additions & 3 deletions src/state/utils/getConfigMaps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { FetchFn } from 'shared/hooks/BackendAPI/useFetch';
import { createPostFn } from 'shared/hooks/BackendAPI/usePost';
import { doesUserHavePermission } from 'state/navigation/filters/permissions';
import { PermissionSetState } from 'state/permissionSetsSelector';
import {
PermissionSetState,
getPermissionResourceRules,
} from 'state/permissionSetsSelector';
import { K8sResource } from 'types';

export type ConfigMapData = {
Expand Down Expand Up @@ -30,14 +34,30 @@ export async function getConfigMaps(
const namespacedCMUrl = `/api/v1/namespaces/${currentNamespace ??
kubeconfigNamespace}/configmaps?labelSelector=${labelSelector}`;

const hasAccessToClusterCMList = doesUserHavePermission(
const namespaceAccess = doesUserHavePermission(
['list'],
{ resourceGroupAndVersion: '', resourceKind: 'ConfigMap' },
permissionSet,
);

const postFn = createPostFn(fetchFn);
const clusterPermissionSet = await getPermissionResourceRules(
postFn,
'',
true,
);
const clusterAccess = doesUserHavePermission(
['list'],
{ resourceGroupAndVersion: '', resourceKind: 'ConfigMap' },
clusterPermissionSet,
);

// user has no access to clusterwide namespace listing, fall back to namespaced listing
const url = hasAccessToClusterCMList ? clusterCMUrl : namespacedCMUrl;
const url = clusterAccess
? clusterCMUrl
: namespaceAccess
? namespacedCMUrl
: '';

try {
const response = await fetchFn({ relativeUrl: url });
Expand Down

0 comments on commit a49e960

Please sign in to comment.