Introduce permission check in kubernetes frontend

Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com>
This commit is contained in:
Dominika Zemanovicova
2025-01-08 17:45:01 +01:00
parent 07efa3a8d8
commit 51c1a5f606
8 changed files with 114 additions and 1 deletions
@@ -28,13 +28,18 @@ import {
import {
DetectedError,
detectErrors,
kubernetesClustersPermission,
kubernetesResourcesPermission,
} from '@backstage/plugin-kubernetes-common';
import {
Content,
EmptyState,
Page,
Progress,
WarningPanel,
} from '@backstage/core-components';
import { useKubernetesClustersPermission } from './hooks/useKubernetesClustersPermission';
import { useKubernetesResourcesPermission } from './hooks/useKubernetesResourcesPermission';
type KubernetesContentProps = {
entity: Entity;
@@ -46,11 +51,27 @@ export const KubernetesContent = ({
entity,
refreshIntervalMs,
}: KubernetesContentProps) => {
const hasKubernetesClustersPermission = useKubernetesClustersPermission();
const hasKubernetesResourcesPermission = useKubernetesResourcesPermission();
const { kubernetesObjects, error } = useKubernetesObjects(
entity,
refreshIntervalMs,
);
if (!hasKubernetesClustersPermission || !hasKubernetesResourcesPermission) {
return (
<Page themeId="tool">
<Content>
<WarningPanel
title="Permission required"
message={`To view Kubernetes objects, contact your administrator to give you the
'${kubernetesClustersPermission.name}' and '${kubernetesResourcesPermission.name}' permission.`}
/>
</Content>
</Page>
);
}
const clusters = kubernetesObjects?.items.map(item => item.cluster) ?? [];
const clustersWithErrors =
@@ -0,0 +1,26 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { kubernetesClustersPermission } from '@backstage/plugin-kubernetes-common';
import { usePermission } from '@backstage/plugin-permission-react';
export const useKubernetesClustersPermission = () => {
const kubernetesClustersPermissionResult = usePermission({
permission: kubernetesClustersPermission,
});
return kubernetesClustersPermissionResult.allowed;
};
@@ -0,0 +1,26 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { kubernetesResourcesPermission } from '@backstage/plugin-kubernetes-common';
import { usePermission } from '@backstage/plugin-permission-react';
export const useKubernetesResourcesPermission = () => {
const kubernetesResourcesPermissionResult = usePermission({
permission: kubernetesResourcesPermission,
});
return kubernetesResourcesPermissionResult.allowed;
};