-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix: getAllowedResources for all namespaces using SelfSubjectRulesReview #6614
Merged
Nokel81
merged 4 commits into
lensapp:master
from
ahippler:feature/use-selfsubjectrulesreviews
Nov 22, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f0d9716
fix: getAllowedResources for all namespaces using SelfSubjectRulesReview
ahippler 896f896
fix: refresh accessibility every 15 min
ahippler 3b31afe
chore: remove unused clusterRefreshHandler
ahippler dc3893c
fix: resolve SelfSubjectRulesReview globs
ahippler 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 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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
/** | ||
* 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 { getInjectable } from "@ogre-tools/injectable"; | ||
import type { Logger } from "../logger"; | ||
import loggerInjectable from "../logger.injectable"; | ||
import type { KubeApiResource } from "../rbac"; | ||
|
||
/** | ||
* Requests the permissions for actions on the kube cluster | ||
* @param namespace The namespace of the resources | ||
* @param availableResources List of available resources in the cluster to resolve glob values fir api groups | ||
* @returns list of allowed resources names | ||
*/ | ||
export type RequestNamespaceResources = (namespace: string, availableResources: KubeApiResource[]) => Promise<string[]>; | ||
|
||
/** | ||
* @param proxyConfig This config's `currentContext` field must be set, and will be used as the target cluster | ||
*/ | ||
export type AuthorizationNamespaceReview = (proxyConfig: KubeConfig) => RequestNamespaceResources; | ||
|
||
interface Dependencies { | ||
logger: Logger; | ||
} | ||
|
||
const authorizationNamespaceReview = ({ logger }: Dependencies): AuthorizationNamespaceReview => { | ||
return (proxyConfig) => { | ||
|
||
const api = proxyConfig.makeApiClient(AuthorizationV1Api); | ||
|
||
return async (namespace, availableResources) => { | ||
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) { | ||
return; | ||
} | ||
|
||
const apiGroups = resourceRule.apiGroups; | ||
|
||
if (resourceRule.resources.length === 1 && resourceRule.resources[0] === "*" && apiGroups) { | ||
if (apiGroups[0] === "*") { | ||
availableResources.forEach(resource => resources.add(resource.apiName)); | ||
} else { | ||
availableResources.forEach((apiResource)=> { | ||
if (apiGroups.includes(apiResource.group || "")) { | ||
resources.add(apiResource.apiName); | ||
} | ||
}); | ||
} | ||
} else { | ||
resourceRule.resources.forEach(resource => resources.add(resource)); | ||
} | ||
|
||
}); | ||
|
||
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: (di) => { | ||
const logger = di.inject(loggerInjectable); | ||
|
||
return authorizationNamespaceReview({ logger }); | ||
}, | ||
}); | ||
|
||
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
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.
minikube does not have PodSecurityPolicy
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.
Then why did this test pass previously?
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.
looks like the api reports missing ressources as allowed: