1mod ai;
2mod apps;
3mod auth;
4mod atproto;
5mod identity;
6mod cache;
7mod computer;
8mod core;
9mod deploys;
10mod files;
11mod guest;
12mod host;
13mod index;
14mod mcp;
15mod observability;
16mod oidc;
17mod shale;
18mod shale_page;
19mod shares;
20mod storage;
21mod telemetry;
22mod users;
23mod vms;
24mod youtube;
25
26use axum::{
27 Router,
28 body::Bytes,
29 extract::{Request, State},
30 http::{HeaderMap, Method, StatusCode},
31 response::{IntoResponse, Response, Sse, sse::Event},
32 routing::any,
33};
34use serde_json::{Value, json};
35use std::{
36 collections::HashMap,
37 path::PathBuf,
38 sync::{Arc, Mutex},
39 time::Duration,
40};
41use subtle::ConstantTimeEq;
42use tokio::sync::{Semaphore, watch};
43use tokio_stream::{StreamExt, wrappers::WatchStream};
44use tower_http::services::{ServeDir, ServeFile};
45
46type Result<T> = std::result::Result<T, Error>;
47
48#[derive(Clone, Debug)]
49struct Error {
50 status: u16,
51 message: String,
52}
53impl Error {
54 fn new(status: u16, message: impl Into<String>) -> Self {
55 Self {
56 status,
57 message: message.into(),
58 }
59 }
60}
61impl<E: std::error::Error> From<E> for Error {
62 fn from(error: E) -> Self {
63 Self::new(500, error.to_string())
64 }
65}
66impl IntoResponse for Error {
67 fn into_response(self) -> Response {
68 if self.status >= 500 {
69 eprintln!("{}: {}", self.status, self.message);
70 }
71 (
72 StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
73 self.message,
74 )
75 .into_response()
76 }
77}
78
79struct Document {
80 value: Value,
81 bytes: Bytes,
82}
83impl Document {
84 fn new(value: Value) -> Self {
85 let bytes = Bytes::from(serde_json::to_vec(&value).unwrap());
86 Self { value, bytes }
87 }
88 fn response(&self) -> Response {
89 ([("content-type", "application/json")], self.bytes.clone()).into_response()
90 }
91}
92
93struct App {
94 auth: auth::Store,
95 mcp: mcp::Store,
96 shale: shale::Backend,
97 http: reqwest::Client,
98 shale_http: reqwest::Client,
99 internal: Option<(url::Url, reqwest::Client)>,
100 cache: cache::Cache,
101 nomad_slots: Semaphore,
102 log_slots: Semaphore,
103 specs: Mutex<HashMap<String, Value>>,
104 repo: PathBuf,
105 data: PathBuf,
106 live: watch::Sender<Bytes>,
107 usage: Mutex<HashMap<String, Value>>,
108 vm_usage: Mutex<HashMap<String, Value>>,
109 seed_samples: Mutex<Vec<Value>>,
110 youtube: std::sync::OnceLock<youtube::Worker>,
111 heavy: Semaphore,
112 file_changes: tokio::sync::Mutex<()>,
113 index: Option<Arc<index::Index>>,
114}
115impl App {
116 fn request(&self, method: Method, address: &str) -> Result<reqwest::RequestBuilder> {
117 if env("SNOWGLOBE_PREVIEW", "") == "1" && !matches!(method, Method::GET | Method::HEAD) {
118 return Err(Error::new(403, "This preview cannot change other services. Use production for app controls."));
119 }
120 let url = url::Url::parse(address)?;
121 let client = match &self.internal {
122 Some((base, client)) if url.origin() == base.origin() => client,
123 _ => &self.http,
124 };
125 Ok(client.request(method, url))
126 }
127}
128
129fn env(name: &str, default: &str) -> String {
130 std::env::var(name).unwrap_or_else(|_| default.into())
131}
132fn array(value: &Value) -> &[Value] {
133 value.as_array().map(Vec::as_slice).unwrap_or_default()
134}
135fn string(value: &Value) -> &str {
136 value.as_str().unwrap_or_default()
137}
138fn number(value: &Value) -> f64 {
139 value
140 .as_f64()
141 .or_else(|| value.as_str().and_then(|s| s.parse().ok()))
142 .unwrap_or(0.0)
143}
144fn now() -> f64 {
145 std::time::SystemTime::now()
146 .duration_since(std::time::UNIX_EPOCH)
147 .unwrap()
148 .as_secs_f64()
149}
150fn encoded(value: &str) -> String {
151 url::form_urlencoded::byte_serialize(value.as_bytes()).collect()
152}
153fn params(items: &[(&str, String)]) -> String {
154 url::form_urlencoded::Serializer::new(String::new())
155 .extend_pairs(items.iter().map(|(k, v)| (*k, v)))
156 .finish()
157}
158
159fn user(headers: &HeaderMap) -> Result<Value> {
160 let name = headers.get("User-Name").and_then(|v| v.to_str().ok()).filter(|v| !v.is_empty()).ok_or_else(|| Error::new(401, "No signed-in user came with this request. Open the dashboard through its sign-in page."))?;
161 let groups: Vec<&str> = headers
162 .get("User-Groups")
163 .and_then(|v| v.to_str().ok())
164 .unwrap_or_default()
165 .split(|c: char| c == ',' || c.is_whitespace())
166 .filter(|s| !s.is_empty())
167 .map(|s| s.strip_prefix("role:").unwrap_or(s))
168 .collect();
169 let preview = headers
170 .get("cookie")
171 .and_then(|v| v.to_str().ok())
172 .unwrap_or_default()
173 .split(';')
174 .find_map(|s| s.trim().strip_prefix("view-as="));
175 let viewing = preview.is_some() && groups.contains(&"infra-admin");
176 let groups = if viewing {
177 preview
178 .unwrap()
179 .split(',')
180 .filter(|s| !s.is_empty() && *s != "infra-admin")
181 .collect()
182 } else {
183 groups
184 };
185 let sections: Vec<&str> = [
186 ("launcher", None),
187 ("admin", Some("infra-admin")),
188 ("metrics", Some("metrics")),
189 ("media", Some("media-manage")),
190 ("vms", Some("vm")),
191 ("ai", Some("ai")),
192 ]
193 .into_iter()
194 .filter(|(_, group)| can_open(&groups, *group))
195 .map(|(section, _)| section)
196 .collect();
197 let id = headers
198 .get("User-Id")
199 .and_then(|v| v.to_str().ok())
200 .unwrap_or(name);
201 Ok(json!({"id":id,"name":name,"groups":groups,"sections":sections,"viewing":viewing}))
202}
203fn can_open(groups: &[&str], access: Option<&str>) -> bool {
204 access.is_none() || groups.contains(&"infra-admin") || groups.contains(&access.unwrap())
205}
206fn need(user: &Value, section: &str) -> Result<()> {
207 if array(&user["sections"]).iter().any(|s| s == section) {
208 Ok(())
209 } else {
210 Err(Error::new(
211 403,
212 "Your account can't open this section. Ask an admin to add you to its group.",
213 ))
214 }
215}
216
217async fn command(program: &str, args: &[&str], input: Option<&[u8]>) -> Result<Vec<u8>> {
218 use std::process::Stdio;
219 use tokio::io::AsyncWriteExt;
220 let mut child = tokio::process::Command::new(program)
221 .args(args)
222 .stdin(if input.is_some() {
223 Stdio::piped()
224 } else {
225 Stdio::null()
226 })
227 .stdout(Stdio::piped())
228 .stderr(Stdio::piped())
229 .kill_on_drop(true)
230 .spawn()?;
231 if let Some(input) = input {
232 child.stdin.take().unwrap().write_all(input).await?;
233 }
234 let output = tokio::time::timeout(Duration::from_secs(120), child.wait_with_output())
235 .await
236 .map_err(|_| {
237 Error::new(
238 504,
239 "The operation is taking too long. Check its logs, then retry.",
240 )
241 })??;
242 if !output.status.success() {
243 return Err(Error::new(
244 502,
245 String::from_utf8_lossy(&output.stderr).trim(),
246 ));
247 }
248 Ok(output.stdout)
249}
250
251async fn api(State(app): State<Arc<App>>, request: Request) -> Result<Response> {
252 let path = request
253 .uri()
254 .path()
255 .trim_start_matches("/api/")
256 .trim_end_matches('/')
257 .to_owned();
258 let method = request.method().clone();
259 let query: HashMap<String, String> =
260 url::form_urlencoded::parse(request.uri().query().unwrap_or_default().as_bytes())
261 .into_owned()
262 .collect();
263 let parts: Vec<&str> = path.split('/').collect();
264 if parts.first() == Some(&"icons") && method == Method::GET {
265 return core::icon(&app, &parts, &query).await;
266 }
267 if parts.starts_with(&["account", "pictures"]) && method == Method::GET {
268 return users::picture(&app, &parts).await;
269 }
270 let me = user(request.headers())?;
271 if env("SNOWGLOBE_PREVIEW", "") == "1"
272 && matches!(parts[0], "seedbox" | "youtube" | "paper-clover")
273 {
274 return Err(Error::new(403, "This app uses production data. Open it in production to continue."));
275 }
276 let headers = request.headers().clone();
277 let section = match parts.first().copied().unwrap_or_default() {
278 "host" | "metrics" | "live" => Some("metrics"),
279 "services" if parts.len() == 1 => Some("metrics"),
280 "storage" if parts.len() == 1 => Some("metrics"),
281 "ai" | "mcp" => Some("ai"),
282 "services" | "vps" | "traces" | "storage" | "users" | "shares" | "deploys" | "paper-clover" => {
283 Some("admin")
284 }
285 "media" | "seedbox" | "youtube" => Some("media"),
286 "vms" => Some("vms"),
287 _ => None,
288 };
289 if let Some(section) = section {
290 need(&me, section)?;
291 }
292 if let ["vms", name, ..] = parts.as_slice() {
293 if !(parts.len() == 2
294 && ((*name == "history" && method == Method::GET)
295 || (*name == "iso" && method == Method::PUT)))
296 {
297 vms::authorize(&me, name).await?;
298 }
299 }
300 if let ["deploys", "runs", id] = parts.as_slice() {
301 return deploys::run_stream(app, id).await;
302 }
303 if parts.first() == Some(&"account") {
304 return users::account(app, request, &parts[1..], &me).await;
305 }
306 if path == "live" && method == Method::GET {
307 let stream = WatchStream::new(app.live.subscribe())
308 .filter(|data| !data.is_empty())
309 .map(|data| {
310 Ok::<_, std::convert::Infallible>(
311 Event::default().data(String::from_utf8_lossy(&data)),
312 )
313 });
314 return Ok(Sse::new(stream).into_response());
315 }
316 if let ["services", id, "logs", "stream"] = parts.as_slice()
317 && method == Method::GET
318 {
319 return telemetry::log_stream(app, request, id, &query).await;
320 }
321 if let ["vms", name, kind @ ("console" | "serial")] = parts.as_slice() {
322 if method == Method::GET {
323 return vms::console(request, name, *kind == "serial").await;
324 }
325 }
326 if parts == ["vms", "iso"] && method == Method::PUT {
327 return vms::upload(app, request, &query).await;
328 }
329 let body = axum::body::to_bytes(request.into_body(), 8 * 1024 * 1024).await?;
330 let value = if body.is_empty() {
331 Value::Null
332 } else {
333 serde_json::from_slice(&body)
334 .map_err(|_| Error::new(400, "The request didn't match what this route expects."))?
335 };
336 match parts[0] {
337 "ai" => ai::route(app, &method, &parts[1..], &me, &headers).await,
338 "mcp" => mcp::manage(app, &method, &parts[1..], &me, value, &headers).await,
339 "users" => users::route(app, &method, &parts[1..], &me, value).await,
340 "shares" => shares::route(app, &method, &parts[1..], &query, &me, value).await,
341 "vms" | "seedbox" | "paper-clover" => {
342 apps::route(app, &method, &parts, &query, &me, value).await
343 }
344 "youtube" => youtube::route(app, &method, &parts[1..], value).await,
345 "media" => files::route(app, true, &method, &parts[1..], &query, value).await,
346 "storage" if parts.get(1) == Some(&"files") => {
347 if parts.len() == 3 && ["map", "largest"].contains(&parts[2]) {
348 index::route(app, parts[2], &query).await
349 } else {
350 files::route(app, false, &method, &parts[2..], &query, value).await
351 }
352 }
353 "storage" => storage::route(app, &method, &parts[1..], &query, value).await,
354 "deploys" => deploys::route(app, &method, &parts[1..], &query).await,
355 _ => core::route(app, &method, &parts, &query, &me, value).await,
356 }
357}
358
359#[tokio::main(worker_threads = 4)]
360async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
361 let address: std::net::IpAddr = env("SNOWGLOBE_LISTEN_ADDRESS", "127.0.0.1").parse()?;
362 let proof = match std::env::var_os("SNOWGLOBE_PROXY_TOKEN_FILE").filter(|file| !file.is_empty()) {
363 Some(file) => {
364 let token = std::fs::read_to_string(file)?.trim().to_owned();
365 if token.len() != 64 || !token.bytes().all(|b| b.is_ascii_hexdigit()) {
366 return Err(std::io::Error::other(
367 "The dashboard proxy token must contain 64 hexadecimal characters.",
368 )
369 .into());
370 }
371 Some(Arc::<str>::from(token))
372 }
373 None if address.is_loopback() => None,
374 None => {
375 return Err(std::io::Error::other(
376 "Set SNOWGLOBE_PROXY_TOKEN_FILE before listening on a non-loopback address.",
377 )
378 .into());
379 }
380 };
381 let certificates = if let Ok(path) =
382 std::env::var("SNOWGLOBE_CA_BUNDLE").or_else(|_| std::env::var("NODE_EXTRA_CA_CERTS"))
383 {
384 reqwest::Certificate::from_pem_bundle(&std::fs::read(path)?)?
385 } else {
386 Vec::new()
387 };
388 let client = || {
389 let mut http = reqwest::Client::builder().timeout(Duration::from_secs(15));
390 for certificate in &certificates {
391 http = http.add_root_certificate(certificate.clone());
392 }
393 http
394 };
395 let internal = std::env::var("SNOWGLOBE_INTERNAL_URL")
396 .ok()
397 .map(
398 |address| -> std::result::Result<_, Box<dyn std::error::Error>> {
399 let base = url::Url::parse(&address)?;
400 if base.scheme() != "https"
401 || base.host_str().is_none()
402 || !base.username().is_empty()
403 || base.password().is_some()
404 || base.path() != "/"
405 || base.query().is_some()
406 || base.fragment().is_some()
407 {
408 return Err(std::io::Error::other(
409 "SNOWGLOBE_INTERNAL_URL must be an HTTPS origin.",
410 )
411 .into());
412 }
413 let token = proof.as_ref().ok_or_else(|| {
414 std::io::Error::other("SNOWGLOBE_INTERNAL_URL requires SNOWGLOBE_PROXY_TOKEN_FILE.")
415 })?;
416 let mut value = axum::http::HeaderValue::from_str(token)?;
417 value.set_sensitive(true);
418 let mut headers = HeaderMap::new();
419 headers.insert("Snowglobe-Proxy-Token", value);
420 Ok((
421 base,
422 client()
423 .default_headers(headers)
424 .redirect(reqwest::redirect::Policy::none())
425 .build()?,
426 ))
427 },
428 )
429 .transpose()?;
430 let mut shale_http = reqwest::Client::builder()
431 .connect_timeout(Duration::from_secs(5))
432 .read_timeout(Duration::from_secs(30))
433 .redirect(reqwest::redirect::Policy::none())
434 .retry(reqwest::retry::never());
435 for certificate in &certificates {
436 shale_http = shale_http.add_root_certificate(certificate.clone());
437 }
438 if internal.is_some() {
439 let mut token = axum::http::HeaderValue::from_str(proof.as_ref().unwrap())?;
440 token.set_sensitive(true);
441 let mut headers = HeaderMap::new();
442 headers.insert("Snowglobe-Proxy-Token", token);
443 shale_http = shale_http.default_headers(headers);
444 }
445 let shale_http = shale_http.build()?;
446 let (live, _) = watch::channel(Bytes::new());
447 let index = std::env::var("SNOWGLOBE_INDEX_POOL").ok().map(|pool| {
448 Arc::new(index::Index::new(
449 pool,
450 env("SNOWGLOBE_INDEX_DIR", ".cache/index").into(),
451 ))
452 });
453 let origin = env(
454 "SNOWGLOBE_PUBLIC_ORIGIN",
455 &format!("https://snowglobe.{}", env("SNOWGLOBE_DOMAIN", "snowglobe.test")),
456 );
457 let auth = auth::Store::new(
458 &PathBuf::from(env("SNOWGLOBE_DATA_DIR", "data")),
459 &origin,
460 &env(
461 "SNOWGLOBE_FILE_ORIGIN",
462 &format!("https://file.{}", env("SNOWGLOBE_DOMAIN", "snowglobe.test")),
463 ),
464 &env(
465 "SNOWGLOBE_AUTH_RP_ID",
466 &format!("auth.{}", env("SNOWGLOBE_DOMAIN", "snowglobe.test")),
467 ),
468 )
469 .map_err(|error| std::io::Error::other(error.message))?;
470 if let Some(path) = std::env::args().skip(1).next() {
471 if path == "--preview-account" {
472 if auth.ready() {
473 return Err(std::io::Error::other("Preview accounts require an empty store.").into());
474 }
475 let id = uuid::Uuid::new_v4().to_string();
476 let role = uuid::Uuid::new_v4().to_string();
477 let password = mcp::secret();
478 auth.import(json!({"rpId":env("SNOWGLOBE_AUTH_RP_ID", "auth.snowglobe.test"),
479 "roles":[{"id":role,"name":"infra-admin"}],
480 "users":[{"id":id,"username":"preview","enabled":true,
481 "firstName":"Preview","lastName":"Account","email":"preview@example.invalid",
482 "emailVerified":false,"requiredActions":[],"roles":[role],"credentials":[]}]}))
483 .map_err(|error| std::io::Error::other(error.message))?;
484 let hash = auth::password_hash(&password)
485 .map_err(|error| std::io::Error::other(error.message))?;
486 auth::set_password(&auth.db.lock().unwrap(), &id, &hash)
487 .map_err(|error| std::io::Error::other(error.message))?;
488 println!("{}", json!({"username":"preview","password":password}));
489 return Ok(());
490 }
491 if path == "--import-commit-identity" {
492 let input = serde_json::from_reader(std::io::stdin())?;
493 let output = identity::import(&auth, &input).map_err(|error| std::io::Error::other(error.message))?;
494 println!("{output}");
495 return Ok(());
496 }
497 if path == "--guest-provider" {
498 let input = serde_json::from_reader(std::io::stdin())?;
499 let output = guest::provision(&auth, input)
500 .map_err(|error| std::io::Error::other(error.message))?;
501 println!("{output}");
502 return Ok(());
503 }
504 if path == "--oidc-client" {
505 let input = serde_json::from_reader(std::io::stdin())?;
506 let output = oidc::provision(&auth, input)
507 .map_err(|error| std::io::Error::other(error.message))?;
508 println!("{output}");
509 return Ok(());
510 }
511 if path != "--import-accounts" {
512 return Err(std::io::Error::other("Unknown dashboard argument.").into());
513 }
514 let path = std::env::args()
515 .nth(2)
516 .ok_or_else(|| std::io::Error::other("Provide an account export path."))?;
517 let result = auth
518 .import(serde_json::from_slice(&std::fs::read(path)?)?)
519 .map_err(|error| std::io::Error::other(error.message))?;
520 println!("{result}");
521 return Ok(());
522 }
523 let import = PathBuf::from(env("SNOWGLOBE_DATA_DIR", "data")).join("accounts-import.json");
524 if import.exists() {
525 auth.import(serde_json::from_slice(&std::fs::read(&import)?)?)
526 .map_err(|error| std::io::Error::other(error.message))?;
527 std::fs::remove_file(&import)?;
528 }
529 if env("SNOWGLOBE_AUTH_REQUIRED", "0") == "1" && !auth.ready() {
530 return Err(std::io::Error::other(
531 "Import accounts before starting native authentication.",
532 )
533 .into());
534 }
535 let app = Arc::new(App {
536 auth,
537 mcp: mcp::Store::new(&PathBuf::from(env("SNOWGLOBE_DATA_DIR", "data")), &origin)
538 .map_err(|error| std::io::Error::other(error.message))?,
539 shale: shale::Backend::new(
540 &env(
541 "SNOWGLOBE_SHALE_URL",
542 &format!("https://shale.{}", env("SNOWGLOBE_DOMAIN", "snowglobe.test")),
543 ),
544 client(),
545 )
546 .map_err(|error| std::io::Error::other(error.message))?,
547 http: client().build()?,
548 shale_http,
549 internal,
550 cache: cache::Cache::default(),
551 nomad_slots: Semaphore::new(4),
552 log_slots: Semaphore::new(32),
553 specs: Mutex::new(HashMap::new()),
554 repo: env("SNOWGLOBE_REPO", "..").into(),
555 data: env("SNOWGLOBE_DATA_DIR", "data").into(),
556 live,
557 usage: Mutex::new(HashMap::new()),
558 vm_usage: Mutex::new(HashMap::new()),
559 seed_samples: Mutex::new(Vec::new()),
560 youtube: std::sync::OnceLock::new(),
561 heavy: Semaphore::new(4),
562 file_changes: tokio::sync::Mutex::new(()),
563 index,
564 });
565 if let Some(index) = app.index.clone() {
566 let state = app.clone();
567 tokio::spawn(async move {
568 index.start(state).await;
569 });
570 }
571 if std::env::var("SNOWGLOBE_YT_STATE").is_ok() {
572 for parts in [&[][..], &["library"][..]] {
573 let state = app.clone();
574 tokio::spawn(async move {
575 if let Err(error) = youtube::route(state, &Method::GET, parts, Value::Null).await {
576 eprintln!("youtube: {}", error.message);
577 }
578 });
579 }
580 }
581 core::start(app.clone());
582 telemetry::start(app.clone());
583 let dist = env("SNOWGLOBE_WEB_DIR", "dist");
584 let router = Router::new()
585 .route("/api/{*path}", any(api))
586 .route("/auth/{*path}", any(auth::route))
587 .route("/.well-known/openid-configuration", any(oidc::route))
588 .route("/.well-known/mcp-servers", axum::routing::get(mcp::discovery))
589 .route("/oauth/{*path}", any(mcp::oauth))
590 .route("/.well-known/{*path}", any(mcp::oauth))
591 .nest_service("/assets", ServeDir::new(format!("{dist}/assets")))
592 .with_state(app.clone())
593 .merge(observability::router(app.clone()))
594 .merge(computer::router(app.clone()))
595 .merge(shale::router(app.clone()))
596 .fallback_service(
597 ServeDir::new(&dist).fallback(ServeFile::new(format!("{dist}/index.html"))),
598 )
599 .layer(axum::middleware::from_fn(
600 move |mut request: Request, next: axum::middleware::Next| {
601 let proof = proof.clone();
602 let app = app.clone();
603 async move {
604 request.headers_mut().remove("User-Id");
605 if mcp::public(request.uri().path()) {
606 request.headers_mut().remove("Snowglobe-Proxy-Token");
607 request.headers_mut().remove("User-Name");
608 request.headers_mut().remove("User-Groups");
609 } else if let Some(proof) = proof {
610 let supplied = request
611 .headers()
612 .get("Snowglobe-Proxy-Token")
613 .map(|value| value.as_bytes())
614 .unwrap_or_default();
615 if supplied.ct_eq(proof.as_bytes()).unwrap_u8() == 0 {
616 return Error::new(403, "Open the dashboard through its sign-in page.")
617 .into_response();
618 }
619 request.headers_mut().remove("Snowglobe-Proxy-Token");
620 }
621 if request.uri().path() == "/auth/shale/page" {
622 return next.run(request).await;
623 }
624 let path = request.uri().path().to_owned();
625 let asset = path.starts_with("/api/account/pictures/")
626 || path.starts_with("/assets/")
627 || path.starts_with("/fonts/")
628 || path == "/snowflake.svg";
629 if app.auth.ready()
630 && !mcp::public(&path)
631 && !path.starts_with("/auth/")
632 && !asset
633 && path != "/sign-in"
634 {
635 request.headers_mut().remove("User-Name");
636 request.headers_mut().remove("User-Groups");
637 let account = match app.auth.session(request.headers(), "dashboard") {
638 Ok(account) => account,
639 Err(error) => return error.into_response(),
640 };
641 if account.is_null() {
642 return if path.starts_with("/api/") {
643 Error::new(401, "Sign in to Snow Globe.").into_response()
644 } else {
645 (
646 StatusCode::FOUND,
647 [(
648 "location",
649 format!(
650 "/sign-in?next={}",
651 encoded(&request.uri().to_string())
652 ),
653 )],
654 )
655 .into_response()
656 };
657 }
658 if guest::is_guest(&account) {
659 return if path.starts_with("/api/") {
660 Error::new(
661 403,
662 "Guest accounts can use Shale. Open Shale to continue.",
663 )
664 .into_response()
665 } else {
666 (
667 StatusCode::FOUND,
668 [(
669 "location",
670 format!(
671 "https://shale.{}/",
672 env("SNOWGLOBE_DOMAIN", "snowglobe.test")
673 ),
674 )],
675 )
676 .into_response()
677 };
678 }
679 if !matches!(
680 *request.method(),
681 Method::GET | Method::HEAD | Method::OPTIONS
682 ) && request
683 .headers()
684 .get("origin")
685 .and_then(|v| v.to_str().ok())
686 != Some(app.auth.origin.origin().ascii_serialization().as_str())
687 {
688 return Error::new(403, "Open Snow Globe and try again.")
689 .into_response();
690 }
691 if !array(&account["requiredActions"]).is_empty()
692 && !path.starts_with("/api/account")
693 && path.starts_with("/api/")
694 && path != "/api/me"
695 {
696 return Error::new(
697 403,
698 "Change your temporary password in your account first.",
699 )
700 .into_response();
701 }
702 let groups = array(&account["groups"])
703 .iter()
704 .map(|v| string(&v["name"]))
705 .collect::<Vec<_>>()
706 .join(",");
707 request
708 .headers_mut()
709 .insert("User-Name", string(&account["username"]).parse().unwrap());
710 request
711 .headers_mut()
712 .insert("User-Id", string(&account["id"]).parse().unwrap());
713 request
714 .headers_mut()
715 .insert("User-Groups", groups.parse().unwrap());
716 }
717 let document = !asset && !request.uri().path().starts_with("/api/");
718 if document {
719 request.headers_mut().remove("if-modified-since");
720 request.headers_mut().remove("if-none-match");
721 }
722 let mut response = next.run(request).await;
723 response
724 .headers_mut()
725 .insert("referrer-policy", "no-referrer".parse().unwrap());
726 response
727 .headers_mut()
728 .insert("x-content-type-options", "nosniff".parse().unwrap());
729 response
730 .headers_mut()
731 .insert("x-frame-options", "DENY".parse().unwrap());
732 if asset && response.status().is_success() {
733 response.headers_mut().insert(
734 "cache-control",
735 "public, max-age=31536000, immutable".parse().unwrap(),
736 );
737 } else if document {
738 response
739 .headers_mut()
740 .insert("cache-control", "no-store".parse().unwrap());
741 }
742 response
743 }
744 },
745 ));
746 let listener = tokio::net::TcpListener::bind(std::net::SocketAddr::new(
747 address,
748 env("PORT", "7070").parse()?,
749 ))
750 .await?;
751 println!("dashboard on {}", listener.local_addr()?);
752 let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
753 let (shutdown, waiting) = tokio::sync::oneshot::channel::<()>();
754 let mut server = Box::pin(std::future::IntoFuture::into_future(
755 axum::serve(listener, router).with_graceful_shutdown(async {
756 let _ = waiting.await;
757 }),
758 ));
759 tokio::select! {
760 result = &mut server => { result?; return Ok(()); },
761 _ = terminate.recv() => {},
762 _ = tokio::signal::ctrl_c() => {},
763 }
764 let _ = shutdown.send(());
765 if let Ok(result) = tokio::time::timeout(Duration::from_secs(5), server).await {
766 result?;
767 }
768 Ok(())
769}
770
771#[cfg(test)]
772mod tests {
773 use super::*;
774 #[test]
775 fn only_an_admin_can_preview_groups_and_preview_cannot_grant_admin() {
776 let mut headers = HeaderMap::new();
777 assert_eq!(user(&headers).unwrap_err().status, 401);
778 headers.insert("User-Name", "snow".parse().unwrap());
779 headers.insert("User-Groups", "role:metrics,media-manage".parse().unwrap());
780 headers.insert("cookie", "view-as=infra-admin".parse().unwrap());
781 let me = user(&headers).unwrap();
782 assert_eq!(me["viewing"], false);
783 assert!(need(&me, "admin").is_err());
784 assert!(need(&me, "metrics").is_ok());
785 headers.insert("User-Groups", "infra-admin".parse().unwrap());
786 let me = user(&headers).unwrap();
787 assert_eq!(me["viewing"], true);
788 assert!(need(&me, "admin").is_err());
789 headers.insert("cookie", "view-as=metrics".parse().unwrap());
790 let me = user(&headers).unwrap();
791 assert!(need(&me, "metrics").is_ok());
792 assert!(need(&me, "media").is_err());
793 headers.insert("cookie", "view-as=ai".parse().unwrap());
794 let me = user(&headers).unwrap();
795 assert!(need(&me, "ai").is_ok());
796 assert!(need(&me, "admin").is_err());
797 headers.insert("User-Groups", "ai".parse().unwrap());
798 let me = user(&headers).unwrap();
799 assert_eq!(me["viewing"], false);
800 assert!(need(&me, "ai").is_ok());
801 assert!(need(&me, "admin").is_err());
802 }
803}