-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
179 lines (157 loc) · 6.82 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Hive webserver
//!
//! The webserver is the main interface used for communication with the outside world. It offers various endpoints and APIs:
//! - static fileserver (Used to host the Hive backend UI)
//! - /auth endpoint (Used to authenticate Hive users)
//! - /graphql endpoint (Used to serve and receive data from the Hive backend UI)
//! - /test endpoint (Used to receive and handle test requests)
//!
//! The server automatically hands out CSRF tokens in a cookie so the user can protect forms against CSRF attacks. CSRF Tokens are handed out at every endpoint though only enforced by endpoints which actually receive data from the user.
//!
//! # TLS
//! The webserver is using rustls to encrypt every connection. The certificate needs to be stored at the following locations: [`PEM_CERT`] and [`PEM_KEY`]. For the webserver to work properly.
//!
//! # Rate limit
//! The webserver has a built in rate limiting webserver with load shedding. The currently chosen values have not been tested extensively and are very conservative.
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use axum::error_handling::HandleErrorLayer;
use axum::extract::Host;
use axum::handler::HandlerWithoutStateExt;
use axum::middleware::from_extractor;
use axum::response::Redirect;
use axum::routing::{self, post};
use axum::{middleware, BoxError, Extension, Router, Server};
use axum_server::tls_rustls::RustlsConfig;
use hyper::{StatusCode, Uri};
use tower::ServiceBuilder;
use tower_cookies::CookieManagerLayer;
use tower_http::services::{ServeDir, ServeFile};
use crate::database::MonitorDb;
use crate::tasks::TaskManager;
use crate::{Args, SHUTDOWN_SIGNAL};
mod api_token;
mod auth;
mod backend;
mod csrf;
mod handlers;
mod test;
pub use backend::get_schema_sdl;
const STATIC_FILES: &str = "data/webserver/static/";
const PEM_CERT: &str = "data/webserver/cert/cert.pem";
const PEM_KEY: &str = "data/webserver/cert/key.pem";
const GLOBAL_RATE_LIMIT_REQUEST_AMOUNT: u64 = 20;
const GLOBAL_RATE_LIMIT_DURATION_SECS: u64 = 1;
const GLOBAL_REQUEST_BUFFER_SIZE: usize = 40;
pub async fn web_server(db: Arc<MonitorDb>, task_manager: Arc<TaskManager>, cli_args: Arc<Args>) {
let app = app(db, task_manager);
let http_addr = SocketAddr::from(([0, 0, 0, 0], cli_args.http_port));
let https_addr = SocketAddr::from(([0, 0, 0, 0], cli_args.https_port));
let tls_config = RustlsConfig::from_pem_file(PEM_CERT, PEM_KEY).await.unwrap_or_else(|_| panic!("Failed to find the PEM certificate file. It should be stored in the application data folder: Cert: {} Key: {}", PEM_CERT, PEM_KEY));
let server = axum_server::bind_rustls(https_addr, tls_config).serve(app.into_make_service());
let http_server = redirect_http_to_https_server(cli_args, http_addr);
let mut shutdown_signal = SHUTDOWN_SIGNAL.subscribe();
tokio::select! {
result = server => {result.expect("Unhandled webserver error encountered")}
result = http_server => {result.expect("Unhandled http server error encountered")}
result = shutdown_signal.recv() => {result.expect("Failed to receive global shutdown signal")}
}
}
/// Builds the webserver with all endpoints
fn app(db: Arc<MonitorDb>, task_manager: Arc<TaskManager>) -> Router {
let graphql_routes = Router::new()
.route("/backend", post(handlers::graphql_backend))
.layer(
ServiceBuilder::new()
.layer(middleware::from_fn(csrf::require_csrf_token))
.layer(from_extractor::<auth::HiveAuth>())
.layer(Extension(db.clone()))
.layer(Extension(task_manager.clone()))
.layer(Extension(backend::build_schema())),
);
let auth_routes = Router::new()
.route("/backend", post(handlers::graphql_backend_auth))
.layer(
ServiceBuilder::new()
.layer(middleware::from_fn(csrf::require_csrf_token))
.layer(Extension(db.clone()))
.layer(Extension(backend::auth::build_schema())),
);
Router::new()
// Auth handlers
.nest("/auth", auth_routes)
// Graphql handlers
.nest("/graphql", graphql_routes)
// REST test request endpoint
.nest("/test", test::test_routes(db, task_manager))
// Static fileserver used to host the hive-backend-ui Vue app
.fallback(
routing::get_service(
ServeDir::new(STATIC_FILES)
.fallback(ServeFile::new(format!("{}index.html", STATIC_FILES))),
)
.handle_error(handle_serve_dir_error),
)
// Global layers
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(handle_loadshed_error))
.layer(
ServiceBuilder::new()
.load_shed()
.buffer(GLOBAL_REQUEST_BUFFER_SIZE)
.rate_limit(
GLOBAL_RATE_LIMIT_REQUEST_AMOUNT,
Duration::from_secs(GLOBAL_RATE_LIMIT_DURATION_SECS),
)
.into_inner(),
)
.layer(CookieManagerLayer::new())
.layer(middleware::from_fn(csrf::provide_csrf_token)),
)
}
async fn handle_serve_dir_error(error: std::io::Error) -> (StatusCode, String) {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to fetch static files, this is likely due to a bug in the software or wrong software setup: {}", error),
)
}
async fn handle_loadshed_error(err: BoxError) -> (StatusCode, String) {
(
StatusCode::SERVICE_UNAVAILABLE,
format!("Too many requests: {}", err),
)
}
/// Creates a http server which redirects all http traffic to https
async fn redirect_http_to_https_server(
cli_args: Arc<Args>,
addr: SocketAddr,
) -> Result<(), hyper::Error> {
let http_port: u16 = cli_args.http_port;
let https_port: u16 = cli_args.https_port;
fn make_https(
host: String,
uri: Uri,
http_port: u16,
https_port: u16,
) -> Result<Uri, BoxError> {
let mut parts = uri.into_parts();
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
if parts.path_and_query.is_none() {
parts.path_and_query = Some("/".parse().unwrap());
}
let https_host = host.replace(&http_port.to_string(), &https_port.to_string());
parts.authority = Some(https_host.parse()?);
Ok(Uri::from_parts(parts)?)
}
let redirect = move |Host(host): Host, uri: Uri| async move {
match make_https(host, uri, http_port, https_port) {
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
Err(err) => Err((StatusCode::BAD_REQUEST, err.to_string())),
}
};
Server::bind(&addr)
.serve(redirect.into_make_service())
.await
}