-
-
Notifications
You must be signed in to change notification settings - Fork 121
🐋Implement k8s authorization #618
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
Merged
amorey
merged 1 commit into
kubetail-org:cluster-agent-rewrite
from
gikaragia:add/k8s-authorization
Aug 24, 2025
Merged
Changes from all commits
Commits
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 hidden or 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,107 @@ | ||
| use k8s_openapi::api::authorization::v1::{ | ||
| ResourceAttributes, SelfSubjectAccessReview, SelfSubjectAccessReviewSpec, | ||
| }; | ||
| use kube::{Api, Client, Config, api::PostParams, config::AuthInfo}; | ||
| use tonic::{Status, metadata::MetadataMap}; | ||
|
|
||
| pub struct Authorizer { | ||
| k8s_config: Config, | ||
| } | ||
|
|
||
| /// Checks that the the k8s doing the request has proper rights to access the log files. | ||
| #[cfg(not(test))] | ||
| impl Authorizer { | ||
| /// Creates a new Authorizer, using the k8s authorization token to construct the proper | ||
| /// client set during authorization. | ||
| pub async fn new(request_metadata: &MetadataMap) -> Result<Self, Status> { | ||
| let token = request_metadata | ||
| .get("authorization") | ||
| .and_then(|token| token.to_str().ok()) | ||
| .ok_or_else(|| { | ||
| Status::new( | ||
| tonic::Code::Unauthenticated, | ||
| "authentication token not found", | ||
| ) | ||
| })? | ||
| .to_owned(); | ||
|
|
||
| let mut k8s_config = Config::infer().await.map_err(|error| { | ||
| Status::new( | ||
| tonic::Code::Unknown, | ||
| format!("unable to infer k8s config {error}"), | ||
| ) | ||
| })?; | ||
|
|
||
| k8s_config.auth_info = AuthInfo { | ||
| token: Some(token.into()), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| Ok(Self { k8s_config }) | ||
| } | ||
|
|
||
| /// Checks if the request is authorized by calling the k8s API. | ||
| pub async fn is_authorized( | ||
| &self, | ||
| mut namespaces: &Vec<String>, | ||
| verb: &str, | ||
| ) -> Result<(), Status> { | ||
| let client = Client::try_from(self.k8s_config.clone()) | ||
| .map_err(|error| Status::new(tonic::Code::Unauthenticated, error.to_string()))?; | ||
|
|
||
| // Default to all namespaces if no namespace is provided. | ||
| let empty_namespace = vec![String::new()]; | ||
| if namespaces.is_empty() { | ||
| namespaces = &empty_namespace; | ||
| } | ||
|
|
||
| let access_reviews: Api<SelfSubjectAccessReview> = Api::all(client); | ||
| for namespace in namespaces { | ||
| let access_review = SelfSubjectAccessReview { | ||
| spec: SelfSubjectAccessReviewSpec { | ||
| resource_attributes: Some(ResourceAttributes { | ||
| namespace: Some(namespace.to_owned()), | ||
| group: None, | ||
| verb: Some(verb.to_owned()), | ||
| resource: Some("pods/log".to_owned()), | ||
| ..ResourceAttributes::default() | ||
| }), | ||
| non_resource_attributes: None, | ||
| }, | ||
| ..SelfSubjectAccessReview::default() | ||
| }; | ||
|
|
||
| let response = access_reviews | ||
| .create(&PostParams::default(), &access_review) | ||
| .await | ||
| .map_err(|error| { | ||
| Status::new( | ||
| tonic::Code::Unknown, | ||
| format!("failed to authenticate {error}"), | ||
| ) | ||
| })?; | ||
|
|
||
| if response.status.is_none() || !response.status.unwrap().allowed { | ||
| return Err(Status::new( | ||
| tonic::Code::Unauthenticated, | ||
| format!("permission denied: `{verb} pods/log` in namespace `{namespace}`"), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl Authorizer { | ||
| pub async fn new(_request_metadata: &MetadataMap) -> Result<Self, Status> { | ||
| Ok(Self { | ||
| k8s_config: Config::infer().await.unwrap(), | ||
| }) | ||
| } | ||
|
|
||
| pub async fn is_authorized(self, _namespaces: &Vec<String>, _verb: &str) -> Result<(), Status> { | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
There are a couple of issues with this implementation.
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.
Makes sense. When we switch over to treating the Cluster API as a "Kubernetes API extension" then authentication will work with impersonation headers rather than tokens so I think doing things quick-and-simple like this for now sounds good. When we have a better idea of how auth will work long-term we can revisit it.