Generate typed cluster service clients and Axum routes from one contract
This commit is contained in:
@@ -6,6 +6,10 @@ rust-version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
axum.workspace = true
|
||||
bedrock-rpc = { path = "../bedrock-rpc" }
|
||||
clusters.workspace = true
|
||||
serde.workspace = true
|
||||
subtle.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["net"] }
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod rpc;
|
||||
|
||||
use std::{io, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
@@ -9,7 +11,7 @@ use axum::{
|
||||
routing::get,
|
||||
};
|
||||
use clusters::{DOCUMENTS, ResolvedDeployment, SqlDriver};
|
||||
use serde::Serialize;
|
||||
use rpc::DeploymentStatus;
|
||||
use subtle::ConstantTimeEq;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -20,29 +22,27 @@ struct AdminState {
|
||||
read_only: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DeploymentStatus<'a> {
|
||||
deployment: &'a str,
|
||||
config_version: Option<&'a str>,
|
||||
storage_driver: &'static str,
|
||||
read_only: bool,
|
||||
}
|
||||
|
||||
impl AdminState {
|
||||
fn status(&self) -> DeploymentStatus<'_> {
|
||||
fn status(&self) -> DeploymentStatus {
|
||||
let config = &self.config;
|
||||
DeploymentStatus {
|
||||
deployment: &config.deployment,
|
||||
deployment: config.deployment.clone(),
|
||||
config_version: self
|
||||
.config_version
|
||||
.as_deref()
|
||||
.or(Some(&config.config_version)),
|
||||
storage_driver: self.storage_driver,
|
||||
.clone()
|
||||
.or_else(|| Some(config.config_version.clone())),
|
||||
storage_driver: self.storage_driver.to_owned(),
|
||||
read_only: self.read_only,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl rpc::Service for AdminState {
|
||||
async fn status(&self, _: rpc::StatusRequest) -> Result<DeploymentStatus, StatusCode> {
|
||||
Ok(self.status())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn router(
|
||||
config: Arc<ResolvedDeployment>,
|
||||
config_version: Option<String>,
|
||||
@@ -71,8 +71,16 @@ pub fn router(
|
||||
};
|
||||
let read_only = settings.read_only;
|
||||
|
||||
let state = AdminState {
|
||||
config,
|
||||
config_version,
|
||||
storage_driver,
|
||||
read_only,
|
||||
};
|
||||
let admin = Router::new()
|
||||
.route("/status", get(deployment_status))
|
||||
.route("/admin/v1/status", get(deployment_status))
|
||||
.with_state(state.clone())
|
||||
.merge(rpc::router(state))
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
Arc::<str>::from(token),
|
||||
require_admin,
|
||||
@@ -80,13 +88,7 @@ pub fn router(
|
||||
Ok(Router::new()
|
||||
.route("/healthz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.route("/readyz", get(|| async { StatusCode::NO_CONTENT }))
|
||||
.nest("/admin/v1", admin)
|
||||
.with_state(AdminState {
|
||||
config,
|
||||
config_version,
|
||||
storage_driver,
|
||||
read_only,
|
||||
}))
|
||||
.merge(admin))
|
||||
}
|
||||
|
||||
async fn deployment_status(State(state): State<AdminState>) -> Response {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
//! Shared wire contract for querying the Bedrock instance serving the request.
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct StatusRequest {}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct DeploymentStatus {
|
||||
pub deployment: String,
|
||||
pub config_version: Option<String>,
|
||||
pub storage_driver: String,
|
||||
pub read_only: bool,
|
||||
}
|
||||
|
||||
bedrock_rpc::service! {
|
||||
"/admin/v1/deployment" {
|
||||
fn status(StatusRequest) -> DeploymentStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use admin_backend::rpc::{Client, StatusRequest};
|
||||
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
|
||||
use bedrock_rpc::{Error, HttpTransport, Transport};
|
||||
use clusters::DeploymentConfig;
|
||||
|
||||
#[tokio::test]
|
||||
async fn status_rpc_uses_the_admin_listener_and_its_authentication() {
|
||||
let config = DeploymentConfig::default().resolve().unwrap();
|
||||
let app =
|
||||
admin_backend::router(Arc::new(config), Some("release-7".into()), "test".into()).unwrap();
|
||||
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
|
||||
.await
|
||||
.unwrap();
|
||||
let origin = format!("http://{}", listener.local_addr().unwrap());
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
|
||||
let peer = |headers| HttpTransport::new(&origin, Duration::from_secs(3), headers).unwrap();
|
||||
let anonymous = Client(peer(HeaderMap::new()));
|
||||
assert!(matches!(
|
||||
anonymous.status(&StatusRequest {}).await,
|
||||
Err(Error::Status(StatusCode::UNAUTHORIZED))
|
||||
));
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::AUTHORIZATION,
|
||||
HeaderValue::from_static("Bearer test"),
|
||||
);
|
||||
let client = Client(peer(headers));
|
||||
let status = client.status(&StatusRequest {}).await.unwrap();
|
||||
assert_eq!(status.config_version.as_deref(), Some("release-7"));
|
||||
assert_eq!(status.deployment, "default");
|
||||
|
||||
let malformed = client
|
||||
.0
|
||||
.post("/admin/v1/deployment/status", b"{".to_vec())
|
||||
.await;
|
||||
assert!(matches!(
|
||||
malformed,
|
||||
Err(Error::Status(StatusCode::BAD_REQUEST))
|
||||
));
|
||||
server.abort();
|
||||
}
|
||||
Reference in New Issue
Block a user