mod access;
mod agent_id;
mod api_error;
mod capability;
mod catalog;
mod client_addr;
mod config;
mod connect;
mod connections;
mod device;
#[cfg(test)]
mod identity_catalog_tests;
mod oauth;
mod pacing;
mod providers;
mod proxy;
mod security;
mod signature;
mod templates;
#[cfg(test)]
mod test_support;
mod webhooks;
use std::sync::Arc;
use axum::{
extract::{FromRef, State},
response::Html,
routing::{delete, get, post},
Router,
};
use axum_extra::extract::cookie::Key;
use sha2::{Digest, Sha512};
use tower_http::trace::TraceLayer;
use tracing_subscriber::EnvFilter;
pub use access::{Access, AccessPolicy, AllowAll, EnvAccessPolicy};
pub use agent_id::{parse as parse_agent_id, AgentId};
pub use config::{
Config, TrustForwardedFor, WebhookConfig, DEFAULT_CATALOG_PATH,
DEFAULT_KEY_CHECK_LIMIT_PER_HOUR, DEFAULT_OPERATOR_NAME, DEFAULT_WEBHOOK_INBOX_MAX_BYTES,
};
pub use device::DeviceConfig;
#[derive(Clone)]
struct AppState {
http_client: reqwest::Client,
key: Key,
base_url: String,
public_origin: String,
catalog: catalog::Catalog,
device: DeviceConfig,
security: Option<security::Security>,
access: Arc<dyn AccessPolicy>,
operator: templates::Operator,
key_check_limit: u32,
key_check_window: std::time::Duration,
trust_forwarded_for: config::TrustForwardedFor,
webhooks: Option<Arc<webhooks::Webhooks>>,
pacer: Arc<pacing::Pacer>,
#[cfg(test)]
test_upstream: Option<String>,
}
const KEY_CHECK_WINDOW: std::time::Duration = std::time::Duration::from_secs(3600);
impl FromRef<AppState> for Key {
fn from_ref(state: &AppState) -> Self {
state.key.clone()
}
}
pub(crate) fn now_secs() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock before epoch")
.as_secs()
}
pub(crate) fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock before epoch")
.as_millis() as u64
}
pub(crate) async fn check_access(
state: &AppState,
owner: &AgentId,
) -> Result<(), api_error::ApiError> {
match state.access.check(owner).await {
Access::Allowed => Ok(()),
Access::Denied(reason) => Err(api_error::ApiError::AccessDenied(reason)),
}
}
fn build_http_client() -> reqwest::Client {
reqwest::Client::builder()
.user_agent("LocalThought-integration-proxy")
.redirect(reqwest::redirect::Policy::none())
.connect_timeout(std::time::Duration::from_secs(10))
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("failed to build HTTP client")
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Config(String),
Catalog(String),
Security(String),
Bind {
addr: String,
source: std::io::Error,
},
Serve(std::io::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Config(err) => write!(f, "configuration error: {err}"),
Error::Catalog(err) => write!(f, "catalog configuration error: {err}"),
Error::Security(err) => write!(f, "security configuration error: {err}"),
Error::Bind { addr, source } => write!(f, "failed to bind {addr}: {source}"),
Error::Serve(err) => write!(f, "server error: {err}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Bind { source, .. } | Error::Serve(source) => Some(source),
_ => None,
}
}
}
pub async fn build_app(config: &Config) -> Result<Router, Error> {
let access = EnvAccessPolicy::new(
config.allowed_agents.clone(),
config.revoked_subjects.clone(),
);
build_app_with_access(config, Arc::new(access)).await
}
pub async fn build_app_with_access(
config: &Config,
access: Arc<dyn AccessPolicy>,
) -> Result<Router, Error> {
let base_url = config::validate_base_url(&config.base_url).map_err(Error::Config)?;
config.device.validate().map_err(Error::Config)?;
let key = match &config.session_secret {
Some(secret) => Key::from(&Sha512::digest(secret.as_bytes())),
None => {
tracing::warn!(
"SESSION_SECRET is not set; using a random key. A consent screen open during a restart must be started again."
);
Key::generate()
}
};
let http_client = build_http_client();
let catalog = catalog::Catalog::load(&config.catalog_path, &http_client)
.await
.map_err(Error::Catalog)?;
let security = security::Security::connect(&config.database_url, &config.encryption_key)
.await
.map_err(Error::Security)?;
let webhooks = webhooks::start(&config.webhooks, &config.database_url, &security, &catalog)
.await
.map_err(Error::Security)?;
if config.key_check_limit_per_hour > 0
&& config.trust_forwarded_for == config::TrustForwardedFor::None
&& std::env::var_os("DYNO").is_some()
{
tracing::warn!(
"running on Heroku without TRUST_FORWARDED_FOR=heroku: the key-check limit counts the router's address, so all clients share it"
);
}
let state = AppState {
http_client,
key,
base_url,
public_origin: config.public_origin(),
catalog,
device: config.device.clone(),
security: Some(security),
access,
operator: templates::Operator::from_config(config),
key_check_limit: config.key_check_limit_per_hour,
key_check_window: KEY_CHECK_WINDOW,
trust_forwarded_for: config.trust_forwarded_for,
webhooks,
pacer: Arc::default(),
#[cfg(test)]
test_upstream: None,
};
if let Some(webhooks) = &state.webhooks {
webhooks::sweeper::spawn(
webhooks.store.clone(),
webhooks::provider::ProviderHookDeleter {
state: state.clone(),
},
);
}
Ok(router(state))
}
pub async fn serve(config: Config) -> Result<(), Error> {
let app = build_app(&config).await?;
let addr = format!("0.0.0.0:{}", config.port);
tracing::info!("integration-proxy listening on http://{addr}");
let listener = tokio::net::TcpListener::bind(&addr)
.await
.map_err(|source| Error::Bind {
addr: addr.clone(),
source,
})?;
axum::serve(
listener,
app.into_make_service_with_connect_info::<std::net::SocketAddr>(),
)
.await
.map_err(Error::Serve)
}
pub async fn run() -> std::process::ExitCode {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()))
.try_init();
let result = match Config::from_env() {
Ok(config) => serve(config).await,
Err(err) => Err(Error::Config(err)),
};
match result {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(err) => {
eprintln!("{err}");
std::process::ExitCode::FAILURE
}
}
}
fn browser_cors() -> tower_http::cors::CorsLayer {
use axum::http::{header, HeaderName, Method};
use tower_http::cors::{Any, CorsLayer};
CorsLayer::new()
.allow_origin(Any)
.allow_methods([
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::OPTIONS,
])
.allow_headers(
[
header::AUTHORIZATION,
HeaderName::from_static(proxy::IDEMPOTENCY_KEY),
HeaderName::from_static(signature::AGENT_HEADER),
HeaderName::from_static(signature::PUBLIC_KEY_HEADER),
HeaderName::from_static(signature::TIMESTAMP_HEADER),
HeaderName::from_static(signature::SIGNATURE_HEADER),
HeaderName::from_static(signature::VERSION_HEADER),
]
.into_iter()
.chain(proxy::CALLER_HEADERS)
.collect::<Vec<_>>(),
)
.expose_headers(proxy::forwarded_response_headers().collect::<Vec<_>>())
}
fn router(state: AppState) -> Router {
let webhook_routes = match &state.webhooks {
Some(_) => webhooks::routes::router(),
None => Router::new(),
};
Router::new()
.merge(webhook_routes)
.merge(device::router())
.route("/", get(home))
.route("/healthz", get(healthz))
.route("/logo.png", get(logo))
.route("/connect", get(connect::page))
.route("/connect/authorize", post(connect::authorize))
.route("/connect/redeem", post(connect::redeem))
.route("/connections", get(connections::list))
.route("/connections/:connection_id", delete(connections::delete))
.route(
"/connections/:connection_id/agents",
post(connections::add_agent),
)
.route(
"/connections/:connection_id/agents/:agent",
delete(connections::remove_agent),
)
.route("/runtimes", post(connections::add_runtime))
.route("/runtimes/:agent", delete(connections::remove_runtime))
.route(
"/proxy/:connection_id/:platform/*path",
axum::routing::any(proxy::forward),
)
.route("/oauth/:provider/callback", get(oauth::callback))
.route("/catalog", get(catalog::list))
.route("/catalog/:file", get(catalog::document))
.layer(browser_cors())
.layer(TraceLayer::new_for_http().make_span_with(|request: &axum::http::Request<_>| {
tracing::info_span!("http_request", method = %request.method(), path = request.uri().path())
}))
.with_state(state)
}
async fn healthz(State(state): State<AppState>) -> impl axum::response::IntoResponse {
match &state.security {
Some(security) if security.is_ready().await => (axum::http::StatusCode::OK, "ok"),
_ => (
axum::http::StatusCode::SERVICE_UNAVAILABLE,
"database unavailable",
),
}
}
async fn logo() -> impl axum::response::IntoResponse {
(
[
(axum::http::header::CONTENT_TYPE, "image/png"),
(axum::http::header::CACHE_CONTROL, "public, max-age=3600"),
],
include_bytes!("../static/logo.png").as_slice(),
)
}
async fn home(State(state): State<AppState>) -> Html<String> {
Html(templates::render_home(&state.operator))
}
#[cfg(test)]
mod browser_tests {
use super::*;
use axum::{
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;
#[tokio::test]
async fn a_null_origin_frame_may_preflight_signed_proxy_requests() {
let app = test_support::router_without_database();
let response = app
.clone()
.oneshot(
Request::builder()
.method("OPTIONS")
.uri("/proxy/conn/pets/pets")
.header("origin", "null")
.header("access-control-request-method", "PATCH")
.header(
"access-control-request-headers",
"authorization,content-type,if-match,if-none-match,if-modified-since,idempotency-key,x-atomic-agent,x-atomic-public-key,x-atomic-timestamp,x-atomic-signature,x-atomic-signature-version",
)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.headers()["access-control-allow-origin"], "*");
let allowed = response.headers()["access-control-allow-headers"]
.to_str()
.unwrap()
.to_owned();
for name in [
"authorization",
"content-type",
"if-match",
"if-none-match",
"if-modified-since",
"idempotency-key",
"x-atomic-agent",
"x-atomic-public-key",
"x-atomic-timestamp",
"x-atomic-signature",
"x-atomic-signature-version",
] {
assert!(allowed.contains(name), "{name} not in {allowed}");
}
assert!(!response
.headers()
.contains_key("access-control-allow-credentials"));
let response = app
.oneshot(
Request::builder()
.uri("/catalog")
.header("origin", "null")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let exposed = response.headers()["access-control-expose-headers"]
.to_str()
.unwrap();
let exposed: Vec<&str> = exposed.split(',').map(str::trim).collect();
for name in [
"link",
"retry-after",
"etag",
"content-type",
"last-modified",
"x-total-count",
"x-next-page",
]
.into_iter()
.chain(proxy::RATE_LIMIT_HEADERS)
{
assert!(exposed.contains(&name), "{name} not in {exposed:?}");
}
assert!(!exposed.contains(&"x-connection-code"));
assert!(!exposed.contains(&"set-cookie"));
}
}