-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor deployer to run locally without auth (#810)
* feat: refactor deployer to run locally without auth * docs: update contributing deployer guide * refactor: workspace dep * fix: bump regex to 1.8.1 to fix 1.8.0 bug * fix: provisioner port for local deployer * refactor: renaming * feat: refactor to request token from auth * refactor: remove claims feature from deployer common * refactor: use key/cookie from original request * refactor: implement scopebuilder * fix: clippy * refactor: address review * refactor: auth container command * refactor: cleanup builder in start fn * docs: local arg docs
- Loading branch information
Showing
10 changed files
with
320 additions
and
153 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
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
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::net::Ipv4Addr; | ||
|
||
use axum::{ | ||
headers::{authorization::Bearer, Authorization, Cookie, Header, HeaderMapExt}, | ||
http::Request, | ||
middleware::Next, | ||
response::Response, | ||
Extension, | ||
}; | ||
use hyper::{ | ||
client::{connect::dns::GaiResolver, HttpConnector}, | ||
Body, Client, StatusCode, Uri, | ||
}; | ||
use hyper_reverse_proxy::ReverseProxy; | ||
use once_cell::sync::Lazy; | ||
use serde_json::Value; | ||
use tracing::error; | ||
|
||
static PROXY_CLIENT: Lazy<ReverseProxy<HttpConnector<GaiResolver>>> = | ||
Lazy::new(|| ReverseProxy::new(Client::new())); | ||
|
||
/// This middleware proxies a request to the auth service to get a JWT, which we need to access | ||
/// the deployer endpoints, and we'll also need it in the claim layer of the provisioner and runtime | ||
/// clients. | ||
/// | ||
/// Follow the steps in https://github.com/shuttle-hq/shuttle/blob/main/CONTRIBUTING.md#testing-deployer-only | ||
/// to learn how to insert an admin user in the auth state. | ||
/// | ||
/// WARNING: do not set this layer in production. | ||
pub async fn set_jwt_bearer<B>( | ||
Extension(auth_uri): Extension<Uri>, | ||
mut request: Request<B>, | ||
next: Next<B>, | ||
) -> Result<Response, StatusCode> { | ||
let mut auth_details = None; | ||
|
||
if let Some(bearer) = request.headers().typed_get::<Authorization<Bearer>>() { | ||
auth_details = Some(make_token_request("/auth/key", bearer)); | ||
} | ||
|
||
if let Some(cookie) = request.headers().typed_get::<Cookie>() { | ||
auth_details = Some(make_token_request("/auth/session", cookie)); | ||
} | ||
|
||
if let Some(token_request) = auth_details { | ||
let response = PROXY_CLIENT | ||
.call( | ||
Ipv4Addr::LOCALHOST.into(), | ||
&auth_uri.to_string(), | ||
token_request, | ||
) | ||
.await | ||
.expect("failed to proxy request to auth service"); | ||
|
||
let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); | ||
let convert: Value = serde_json::from_slice(&body) | ||
.expect("failed to deserialize body as JSON, did you login?"); | ||
|
||
let token = convert["token"] | ||
.as_str() | ||
.expect("response body should have a token"); | ||
|
||
request | ||
.headers_mut() | ||
.typed_insert(Authorization::bearer(token).expect("to set JWT token")); | ||
|
||
let response = next.run(request).await; | ||
|
||
Ok(response) | ||
} else { | ||
error!("No api-key bearer token or cookie found, make sure you are logged in."); | ||
Err(StatusCode::UNAUTHORIZED) | ||
} | ||
} | ||
|
||
fn make_token_request(uri: &str, header: impl Header) -> Request<Body> { | ||
let mut token_request = Request::builder().uri(uri); | ||
token_request | ||
.headers_mut() | ||
.expect("manual request to be valid") | ||
.typed_insert(header); | ||
|
||
token_request | ||
.body(Body::empty()) | ||
.expect("manual request to be valid") | ||
} |
Oops, something went wrong.