Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions crates/cluster_agent/src/authorizer.rs
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))]
Copy link
Copy Markdown
Collaborator Author

@gikaragia gikaragia Aug 23, 2025

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.

  • First, the ideal way to implement such functionality is with an interceptor (middleware). The problem is that tonic does not support async interceptors out of the box, and I would need to use a custom crate for this functionality. I chose not to use an interceptor.
  • Secondly, the ideal way to stub the authorizer in the tests would be to use dependency injection. However, this made the types very complicated, so I think that using #[cfg] directives is preferable

Copy link
Copy Markdown
Member

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.

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(())
}
}
7 changes: 7 additions & 0 deletions crates/cluster_agent/src/log_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use types::cluster_agent::{
LogMetadataWatchEvent, LogMetadataWatchRequest,
};

use crate::authorizer::Authorizer;
use crate::log_metadata::log_metadata_watcher::LogMetadataWatcher;

mod log_metadata_watcher;
Expand Down Expand Up @@ -99,6 +100,7 @@ impl LogMetadataService for LogMetadataImpl {
&self,
request: Request<LogMetadataListRequest>,
) -> Result<Response<LogMetadataList>, Status> {
let authorizer = Authorizer::new(request.metadata()).await?;
let request = request.into_inner();

if !self.logs_dir.is_dir() {
Expand All @@ -117,6 +119,8 @@ impl LogMetadataService for LogMetadataImpl {
.filter(|namespace| !namespace.is_empty())
.collect();

authorizer.is_authorized(&namespaces, "list").await?;

let mut files = ReadDirStream::new(read_dir(&self.logs_dir).await?);

let mut metadata_items = Vec::new();
Expand Down Expand Up @@ -173,6 +177,7 @@ impl LogMetadataService for LogMetadataImpl {
&self,
request: Request<LogMetadataWatchRequest>,
) -> Result<Response<Self::WatchStream>, Status> {
let authorizer = Authorizer::new(request.metadata()).await?;
let request = request.into_inner();
let term_tx = self.term_tx.clone();

Expand All @@ -182,6 +187,8 @@ impl LogMetadataService for LogMetadataImpl {
.filter(|namespace| !namespace.is_empty())
.collect();

authorizer.is_authorized(&namespaces, "watch").await?;

let (log_metadata_watcher, log_metadata_rx) = LogMetadataWatcher::new(
Path::new(&self.logs_dir).to_path_buf(),
namespaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,13 @@ mod test {
if let Some(file_size) = file_size {
assert_eq!(event_file_info.as_ref().unwrap().size, file_size as i64);
} else {
assert_eq!(event_file_info, None);
assert_eq!(
event_file_info,
Some(LogMetadataFileInfo {
size: 0,
last_modified_at: None,
})
);
}
}
}
10 changes: 10 additions & 0 deletions crates/cluster_agent/src/log_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use rgkl::{stream_backward, stream_forward};

use tonic::{Request, Response, Status};

use crate::authorizer::Authorizer;

#[derive(Debug)]
pub struct LogRecordsImpl {
logs_dir: PathBuf,
Expand Down Expand Up @@ -62,11 +64,15 @@ impl LogRecordsService for LogRecordsImpl {
&self,
request: Request<LogRecordsStreamRequest>,
) -> Result<Response<Self::StreamBackwardStream>, Status> {
let authorizer = Authorizer::new(request.metadata()).await?;
let request = request.into_inner();
let file_path = self.get_log_filename(&request).map_err(|status| *status)?;
let (tx, rx) = mpsc::channel(100);
let term_tx = self.term_tx.clone();

let namespaces = vec![request.namespace.clone()];
authorizer.is_authorized(&namespaces, "list").await?;

self.task_tracker.spawn(async move {
stream_backward::stream_backward(
&file_path,
Expand All @@ -91,9 +97,13 @@ impl LogRecordsService for LogRecordsImpl {
&self,
request: Request<LogRecordsStreamRequest>,
) -> Result<Response<Self::StreamForwardStream>, Status> {
let authorizer = Authorizer::new(request.metadata()).await?;
let request = request.into_inner();
let file_path = self.get_log_filename(&request).map_err(|status| *status)?;

let namespaces = vec![request.namespace.clone()];
authorizer.is_authorized(&namespaces, "list").await?;

let (tx, rx) = mpsc::channel(100);
let term_tx = self.term_tx.clone();

Expand Down
1 change: 1 addition & 0 deletions crates/cluster_agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use types::cluster_agent::FILE_DESCRIPTOR_SET;
use types::cluster_agent::log_metadata_service_server::LogMetadataServiceServer;
use types::cluster_agent::log_records_service_server::LogRecordsServiceServer;

mod authorizer;
mod config;
mod log_metadata;
mod log_records;
Expand Down
Loading