-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: getAllowedResources for all namespaces using SelfSubjectRulesReview
Signed-off-by: Andreas Hippler <[email protected]>
- Loading branch information
Showing
5 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/common/cluster/authorization-namespace-review.injectable.ts
This file contains 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,56 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
|
||
import type { KubeConfig } from "@kubernetes/client-node"; | ||
import { AuthorizationV1Api } from "@kubernetes/client-node"; | ||
import logger from "../logger"; | ||
import { getInjectable } from "@ogre-tools/injectable"; | ||
|
||
export type NamespaceResources = (namespace: string) => Promise<string[]>; | ||
|
||
/** | ||
* @param proxyConfig This config's `currentContext` field must be set, and will be used as the target cluster | ||
*/ | ||
export function authorizationNamespaceReview(proxyConfig: KubeConfig): NamespaceResources { | ||
const api = proxyConfig.makeApiClient(AuthorizationV1Api); | ||
|
||
/** | ||
* Requests the permissions for actions on the kube cluster | ||
* @param namespace The namespace of the resources | ||
* @returns list of allowed resources | ||
*/ | ||
return async (namespace: string): Promise<string[]> => { | ||
try { | ||
const { body } = await api.createSelfSubjectRulesReview({ | ||
apiVersion: "authorization.k8s.io/v1", | ||
kind: "SelfSubjectRulesReview", | ||
spec: { namespace }, | ||
}); | ||
|
||
const resources = new Set<string>(); | ||
|
||
body.status?.resourceRules.forEach(resourceRule => { | ||
if (resourceRule.verbs.some(verb => ["*", "list"].includes(verb))) { | ||
resourceRule.resources?.forEach(resource => resources.add(resource)); | ||
} | ||
}); | ||
|
||
resources.delete("*"); | ||
|
||
return [...resources]; | ||
} catch (error) { | ||
logger.error(`[AUTHORIZATION-NAMESPACE-REVIEW]: failed to create subject rules review: ${error}`, { namespace }); | ||
|
||
return []; | ||
} | ||
}; | ||
} | ||
|
||
const authorizationNamespaceReviewInjectable = getInjectable({ | ||
id: "authorization-namespace-review", | ||
instantiate: () => authorizationNamespaceReview, | ||
}); | ||
|
||
export default authorizationNamespaceReviewInjectable; |
This file contains 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 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 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 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