use axum::{Json, extract::State};
use serde::Serialize;
use super::auth::HttpCaller;
use crate::ServerState;
#[derive(Debug, Serialize)]
pub(crate) struct WhoAmI {
subject: String,
auth_enabled: bool,
deploy_granted: bool,
all_namespaces: bool,
namespaces: Vec<String>,
}
pub(crate) async fn whoami(
State(state): State<ServerState>,
HttpCaller(caller): HttpCaller,
) -> Json<WhoAmI> {
Json(WhoAmI {
subject: caller.subject().to_owned(),
auth_enabled: state.runtime_config().auth.enabled,
deploy_granted: caller.deploy_granted(),
all_namespaces: caller.all_namespaces(),
namespaces: caller.namespaces(),
})
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use aion::EngineBuilder;
use aion_store::{EventStore, InMemoryStore};
use axum::{body, http::Request, http::StatusCode};
use serde_json::Value;
use tower::ServiceExt;
use super::super::router::workflow_router;
use super::super::test_support::{read_json, runtime_config, server_state};
use crate::{
NamespaceResolver, StaticScheduleNamespaces, StaticWorkflowNamespaces,
config::NamespaceMode,
};
async fn auth_off_router() -> Result<axum::Router, Box<dyn std::error::Error>> {
let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
let engine = Arc::new(
EngineBuilder::new()
.store_arc(store)
.in_memory_visibility()
.scheduler_threads(1)
.build()
.await?,
);
let resolver = NamespaceResolver::from_parts(
NamespaceMode::SharedEngine,
Some(engine),
Arc::new(StaticWorkflowNamespaces::default()),
Arc::new(StaticScheduleNamespaces::default()),
);
let mut config = runtime_config();
config.auth.enabled = false;
Ok(workflow_router(server_state(resolver, config).await?))
}
#[tokio::test]
async fn whoami_reports_operator_in_auth_off_mode() -> Result<(), Box<dyn std::error::Error>> {
let response = auth_off_router()
.await?
.oneshot(
Request::builder()
.uri("/whoami")
.body(body::Body::empty())?,
)
.await?;
assert_eq!(response.status(), StatusCode::OK);
let body: Value = read_json(response).await?;
assert_eq!(body["auth_enabled"], serde_json::json!(false));
assert_eq!(body["deploy_granted"], serde_json::json!(true));
assert_eq!(body["all_namespaces"], serde_json::json!(true));
assert_eq!(body["subject"], serde_json::json!("operator"));
assert_eq!(body["namespaces"], serde_json::json!([]));
Ok(())
}
}