-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make deployer only answer its own project
- Loading branch information
Showing
6 changed files
with
61 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::collections::HashMap; | ||
|
||
use async_trait::async_trait; | ||
use axum::extract::{FromRequest, Path, RequestParts}; | ||
use hyper::StatusCode; | ||
use shuttle_common::project::ProjectName; | ||
|
||
/// Gaurd to ensure request are for the project served by this deployer | ||
/// Note: this guard needs the `ProjectName` extension to be set | ||
pub struct ProjectNameGuard; | ||
|
||
#[async_trait] | ||
impl<B> FromRequest<B> for ProjectNameGuard | ||
where | ||
B: Send, | ||
{ | ||
type Rejection = StatusCode; | ||
|
||
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { | ||
// We expect some path parameters | ||
let Path(path): Path<HashMap<String, String>> = match req.extract().await { | ||
Ok(path) => path, | ||
Err(_) => return Err(StatusCode::NOT_FOUND), | ||
}; | ||
|
||
// All our routes have the `project_name` parameter | ||
let project_name = match path.get("project_name") { | ||
Some(project_name) => project_name, | ||
None => return Err(StatusCode::INTERNAL_SERVER_ERROR), | ||
}; | ||
|
||
// This extractor requires the ProjectName extension to be set | ||
let expected_project_name: &ProjectName = match req.extensions().get() { | ||
Some(expected) => expected, | ||
None => return Err(StatusCode::INTERNAL_SERVER_ERROR), | ||
}; | ||
|
||
if project_name == expected_project_name.as_str() { | ||
Ok(ProjectNameGuard) | ||
} else { | ||
Err(StatusCode::BAD_REQUEST) | ||
} | ||
} | ||
} |
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