-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authorize compute_ctl requests from the control plane (#10530)
The compute should only act if requests come from the control plane. Signed-off-by: Tristan Partin <[email protected]> Signed-off-by: Tristan Partin <[email protected]>
- Loading branch information
1 parent
4bbdb75
commit 7b7e4a9
Showing
14 changed files
with
417 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub(crate) mod json; | ||
pub(crate) mod path; | ||
pub(crate) mod query; | ||
pub(crate) mod request_id; | ||
|
||
pub(crate) use json::Json; | ||
pub(crate) use path::Path; | ||
pub(crate) use query::Query; | ||
pub(crate) use request_id::RequestId; |
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,86 @@ | ||
use std::{ | ||
fmt::Display, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use axum::{extract::FromRequestParts, response::IntoResponse}; | ||
use http::{StatusCode, request::Parts}; | ||
|
||
use crate::http::{JsonResponse, headers::X_REQUEST_ID}; | ||
|
||
/// Extract the request ID from the `X-Request-Id` header. | ||
#[derive(Debug, Clone, Default)] | ||
pub(crate) struct RequestId(pub String); | ||
|
||
#[derive(Debug)] | ||
/// Rejection used for [`RequestId`]. | ||
/// | ||
/// Contains one variant for each way the [`RequestId`] extractor can | ||
/// fail. | ||
pub(crate) enum RequestIdRejection { | ||
/// The request is missing the header. | ||
MissingRequestId, | ||
|
||
/// The value of the header is invalid UTF-8. | ||
InvalidUtf8, | ||
} | ||
|
||
impl RequestIdRejection { | ||
pub fn status(&self) -> StatusCode { | ||
match self { | ||
RequestIdRejection::MissingRequestId => StatusCode::INTERNAL_SERVER_ERROR, | ||
RequestIdRejection::InvalidUtf8 => StatusCode::BAD_REQUEST, | ||
} | ||
} | ||
|
||
pub fn message(&self) -> String { | ||
match self { | ||
RequestIdRejection::MissingRequestId => "request ID is missing", | ||
RequestIdRejection::InvalidUtf8 => "request ID is invalid UTF-8", | ||
} | ||
.to_string() | ||
} | ||
} | ||
|
||
impl IntoResponse for RequestIdRejection { | ||
fn into_response(self) -> axum::response::Response { | ||
JsonResponse::error(self.status(), self.message()) | ||
} | ||
} | ||
|
||
impl<S> FromRequestParts<S> for RequestId | ||
where | ||
S: Send + Sync, | ||
{ | ||
type Rejection = RequestIdRejection; | ||
|
||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { | ||
match parts.headers.get(X_REQUEST_ID) { | ||
Some(value) => match value.to_str() { | ||
Ok(request_id) => Ok(Self(request_id.to_string())), | ||
Err(_) => Err(RequestIdRejection::InvalidUtf8), | ||
}, | ||
None => Err(RequestIdRejection::MissingRequestId), | ||
} | ||
} | ||
} | ||
|
||
impl Deref for RequestId { | ||
type Target = String; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for RequestId { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl Display for RequestId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str(&self.0) | ||
} | ||
} |
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,2 @@ | ||
/// Constant for `X-Request-Id` header. | ||
pub const X_REQUEST_ID: &str = "x-request-id"; |
Oops, something went wrong.
7b7e4a9
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.
7940 tests run: 7552 passed, 0 failed, 388 skipped (full report)
Code coverage* (full report)
functions
:32.9% (8680 of 26394 functions)
lines
:48.8% (73903 of 151436 lines)
* collected from Rust tests only
7b7e4a9 at 2025-03-04T20:44:19.108Z :recycle: