1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix shouldShowResource when not a cluster admin (#6900)

* Fix shouldShowResource when not a cluster admin

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix only tests that rely on bad formatting of resources

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-11 06:02:10 -08:00 committed by GitHub
parent 841c6b5720
commit 0ec8cbd7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 45 additions and 41 deletions

View File

@ -13,7 +13,7 @@ const configMapsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "configmaps", apiName: "configmaps",
group: "v1", group: "",
}), }),
}), }),
injectionToken: frontEndRouteInjectionToken, injectionToken: frontEndRouteInjectionToken,

View File

@ -14,7 +14,7 @@ const limitRangesRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "limitranges", apiName: "limitranges",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const resourceQuotasRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "resourcequotas", apiName: "resourcequotas",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const secretsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "secrets", apiName: "secrets",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const eventsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "events", apiName: "events",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const namespacesRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "namespaces", apiName: "namespaces",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const endpointsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "endpoints", apiName: "endpoints",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const servicesRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "services", apiName: "services",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const nodesRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "nodes", apiName: "nodes",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const clusterOverviewRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "nodes", apiName: "nodes",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const persistentVolumeClaimsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "persistentvolumeclaims", apiName: "persistentvolumeclaims",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const persistentVolumesRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "persistentvolumes", apiName: "persistentvolumes",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const serviceAccountsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "serviceaccounts", apiName: "serviceaccounts",
group: "v1", group: "",
}), }),
}), }),

View File

@ -14,7 +14,7 @@ const podsRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "pods", apiName: "pods",
group: "v1", group: "",
}), }),
}), }),

View File

@ -13,17 +13,21 @@ export type KubeResource =
export interface KubeApiResource { export interface KubeApiResource {
kind: string; kind: string;
group: string; group: string; // api-group, if empty then "core"
apiName: string; apiName: string;
namespaced: boolean; namespaced: boolean;
} }
export interface KubeApiResourceDescriptor { export interface KubeApiResourceDescriptor {
apiName: string; apiName: string;
group: string; group: string; // api-group, if empty then "core"
} }
export const formatKubeApiResource = (res: KubeApiResourceDescriptor) => `${res.group}/${res.apiName}`; export const formatKubeApiResource = (desc: KubeApiResourceDescriptor) => (
desc.group
? `${desc.group}/${desc.apiName}`
: desc.apiName
);
export interface KubeApiResourceData { export interface KubeApiResourceData {
kind: string; // resource type (e.g. "Namespace") kind: string; // resource type (e.g. "Namespace")
@ -44,7 +48,7 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
configmaps: { configmaps: {
kind: "ConfigMap", kind: "ConfigMap",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
cronjobs: { cronjobs: {
@ -69,12 +73,12 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
endpoints: { endpoints: {
kind: "Endpoint", kind: "Endpoint",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
events: { events: {
kind: "Event", kind: "Event",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
horizontalpodautoscalers: { horizontalpodautoscalers: {
@ -99,17 +103,17 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
namespaces: { namespaces: {
kind: "Namespace", kind: "Namespace",
group: "v1", group: "",
namespaced: false, namespaced: false,
}, },
limitranges: { limitranges: {
kind: "LimitRange", kind: "LimitRange",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
leases: { leases: {
kind: "Lease", kind: "Lease",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
networkpolicies: { networkpolicies: {
@ -119,22 +123,22 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
nodes: { nodes: {
kind: "Node", kind: "Node",
group: "v1", group: "",
namespaced: false, namespaced: false,
}, },
persistentvolumes: { persistentvolumes: {
kind: "PersistentVolume", kind: "PersistentVolume",
group: "v1", group: "",
namespaced: false, namespaced: false,
}, },
persistentvolumeclaims: { persistentvolumeclaims: {
kind: "PersistentVolumeClaim", kind: "PersistentVolumeClaim",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
pods: { pods: {
kind: "Pod", kind: "Pod",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
poddisruptionbudgets: { poddisruptionbudgets: {
@ -159,7 +163,7 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
resourcequotas: { resourcequotas: {
kind: "ResourceQuota", kind: "ResourceQuota",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
replicasets: { replicasets: {
@ -179,17 +183,17 @@ export const apiResourceRecord: Record<KubeResource, KubeApiResourceData> = {
}, },
secrets: { secrets: {
kind: "Secret", kind: "Secret",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
serviceaccounts: { serviceaccounts: {
kind: "ServiceAccount", kind: "ServiceAccount",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
services: { services: {
kind: "Service", kind: "Service",
group: "v1", group: "",
namespaced: true, namespaced: true,
}, },
statefulsets: { statefulsets: {

View File

@ -66,7 +66,7 @@ describe("cluster/namespaces - edit namespace from new tab", () => {
builder.allowKubeResource({ builder.allowKubeResource({
apiName: "namespaces", apiName: "namespaces",
group: "v1", group: "",
}); });
}); });

View File

@ -37,7 +37,7 @@ describe("cluster/namespaces - edit namespaces from previously opened tab", () =
builder.allowKubeResource({ builder.allowKubeResource({
apiName: "namespaces", apiName: "namespaces",
group: "v1", group: "",
}); });
}); });

View File

@ -52,7 +52,7 @@ describe("cluster - visibility of sidebar items", () => {
beforeEach(() => { beforeEach(() => {
builder.allowKubeResource({ builder.allowKubeResource({
apiName: "namespaces", apiName: "namespaces",
group: "v1", group: "",
}); });
}); });
@ -77,7 +77,7 @@ const testRouteInjectable = getInjectable({
clusterFrame: true, clusterFrame: true,
isEnabled: di.inject(shouldShowResourceInjectionToken, { isEnabled: di.inject(shouldShowResourceInjectionToken, {
apiName: "namespaces", apiName: "namespaces",
group: "v1", group: "",
}), }),
}), }),

View File

@ -15,7 +15,7 @@ describe("workload overview", () => {
applicationBuilder = getApplicationBuilder().setEnvironmentToClusterFrame(); applicationBuilder = getApplicationBuilder().setEnvironmentToClusterFrame();
applicationBuilder.allowKubeResource({ applicationBuilder.allowKubeResource({
apiName: "pods", apiName: "pods",
group: "v1", group: "",
}); });
rendered = await applicationBuilder.render(); rendered = await applicationBuilder.render();
}); });

View File

@ -16,7 +16,7 @@ const requestCoreApiVersionsInjectable = getInjectable({
const { versions } = await k8sRequest(cluster, "/api") as V1APIVersions; const { versions } = await k8sRequest(cluster, "/api") as V1APIVersions;
return versions.map(version => ({ return versions.map(version => ({
group: version, group: "",
path: `/api/${version}`, path: `/api/${version}`,
})); }));
}; };

View File

@ -21,7 +21,7 @@ const podsWorkloadInjectable = getInjectable({
return { return {
resource: { resource: {
apiName: "pods", apiName: "pods",
group: "v1", group: "",
}, },
open: navigate, open: navigate,

View File

@ -69,7 +69,7 @@ describe("<ClusterFrame />", () => {
describe("given cluster with list nodes and namespaces permissions", () => { describe("given cluster with list nodes and namespaces permissions", () => {
beforeEach(() => { beforeEach(() => {
// TODO: replace with not using private info // TODO: replace with not using private info
(cluster as any).allowedResources.replace(["v1/nodes", "v1/namespaces"]); (cluster as unknown as { readonly allowedResources: Cluster["allowedResources"] }).allowedResources.replace(["nodes", "namespaces"]);
}); });
it("renders", () => { it("renders", () => {
@ -110,7 +110,7 @@ describe("<ClusterFrame />", () => {
describe("given cluster without list nodes, but with namespaces permissions", () => { describe("given cluster without list nodes, but with namespaces permissions", () => {
beforeEach(() => { beforeEach(() => {
(cluster as any).allowedResources.replace(["v1/namespaces"]); (cluster as unknown as { readonly allowedResources: Cluster["allowedResources"] }).allowedResources.replace(["namespaces"]);
}); });
it("renders", () => { it("renders", () => {

View File

@ -34,7 +34,7 @@ export const WorkloadEvents = withInjectables<Dependencies, WorkloadEventsProps>
getProps: (di, props) => ({ getProps: (di, props) => ({
workloadEventsAreAllowed: di.inject(shouldShowResourceInjectionToken, { workloadEventsAreAllowed: di.inject(shouldShowResourceInjectionToken, {
apiName: "events", apiName: "events",
group: "v1", group: "",
}), }),
...props, ...props,
}), }),