|
| 1 | +// SPDX-FileCopyrightText: © 2026 Phala Network <dstack@phala.network> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +//! KMS adapter for the shared API authenticator, guarding the admin listener. |
| 6 | +//! |
| 7 | +//! Unlike the KMS public RPC surface (which authenticates callers by RA-TLS |
| 8 | +//! attestation), the admin API is reached by operators/tooling, so it uses the |
| 9 | +//! same shared-token/htpasswd HTTP mechanism as the VMM and gateway. The token |
| 10 | +//! is configured via `core.admin.auth_token`, or the `DSTACK_KMS_ADMIN_TOKEN` / |
| 11 | +//! `ADMIN_API_TOKEN` environment variables. |
| 12 | +
|
| 13 | +use anyhow::{bail, Result}; |
| 14 | +use dstack_api_auth::{Authenticator, HttpAuthConfig, HttpAuthFairing}; |
| 15 | +use rocket::Route; |
| 16 | + |
| 17 | +use crate::config::AdminConfig; |
| 18 | + |
| 19 | +const ENV_ADMIN_TOKEN: &str = "DSTACK_KMS_ADMIN_TOKEN"; |
| 20 | +const ENV_ADMIN_TOKEN_COMPAT: &str = "ADMIN_API_TOKEN"; |
| 21 | + |
| 22 | +pub struct AdminAuthFairing(HttpAuthFairing); |
| 23 | + |
| 24 | +impl AdminAuthFairing { |
| 25 | + pub fn from_config(config: &AdminConfig) -> Result<Self> { |
| 26 | + if config.insecure_no_auth { |
| 27 | + return Ok(Self(HttpAuthFairing::new( |
| 28 | + Authenticator::disabled(), |
| 29 | + http_config(), |
| 30 | + ))); |
| 31 | + } |
| 32 | + let token = if !config.auth_token.is_empty() { |
| 33 | + config.auth_token.trim().to_owned() |
| 34 | + } else { |
| 35 | + std::env::var(ENV_ADMIN_TOKEN) |
| 36 | + .or_else(|_| std::env::var(ENV_ADMIN_TOKEN_COMPAT)) |
| 37 | + .unwrap_or_default() |
| 38 | + .trim() |
| 39 | + .to_owned() |
| 40 | + }; |
| 41 | + if token.is_empty() && config.htpasswd_file.as_os_str().is_empty() { |
| 42 | + bail!( |
| 43 | + "admin API is enabled but neither auth_token nor htpasswd_file is configured; \ |
| 44 | + set core.admin.auth_token, {ENV_ADMIN_TOKEN}, {ENV_ADMIN_TOKEN_COMPAT}, \ |
| 45 | + core.admin.htpasswd_file, or insecure_no_auth = true (testing only)" |
| 46 | + ); |
| 47 | + } |
| 48 | + let mut auth = Authenticator::from_tokens([token]); |
| 49 | + if !config.htpasswd_file.as_os_str().is_empty() { |
| 50 | + auth = auth.with_htpasswd_file(&config.htpasswd_file)?; |
| 51 | + } |
| 52 | + Ok(Self(HttpAuthFairing::new(auth, http_config()))) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn http_config() -> HttpAuthConfig { |
| 57 | + HttpAuthConfig { |
| 58 | + realm: "dstack-kms admin".into(), |
| 59 | + token_header: Some("X-Admin-Token".into()), |
| 60 | + allow_get_query_token: false, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[rocket::async_trait] |
| 65 | +impl rocket::fairing::Fairing for AdminAuthFairing { |
| 66 | + fn info(&self) -> rocket::fairing::Info { |
| 67 | + self.0.info() |
| 68 | + } |
| 69 | + async fn on_request(&self, req: &mut rocket::Request<'_>, data: &mut rocket::Data<'_>) { |
| 70 | + self.0.on_request(req, data).await |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub fn routes() -> Vec<Route> { |
| 75 | + dstack_api_auth::routes() |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod tests { |
| 80 | + use super::*; |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn enabled_without_credentials_fails_closed() { |
| 84 | + // the token can also come from the environment; make sure neither var is |
| 85 | + // set so this exercises the missing-credential path. |
| 86 | + std::env::remove_var(ENV_ADMIN_TOKEN); |
| 87 | + std::env::remove_var(ENV_ADMIN_TOKEN_COMPAT); |
| 88 | + let cfg = AdminConfig { |
| 89 | + enabled: true, |
| 90 | + ..Default::default() |
| 91 | + }; |
| 92 | + assert!(AdminAuthFairing::from_config(&cfg).is_err()); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn insecure_no_auth_is_allowed() { |
| 97 | + let cfg = AdminConfig { |
| 98 | + enabled: true, |
| 99 | + insecure_no_auth: true, |
| 100 | + ..Default::default() |
| 101 | + }; |
| 102 | + assert!(AdminAuthFairing::from_config(&cfg).is_ok()); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn configured_token_builds() { |
| 107 | + let cfg = AdminConfig { |
| 108 | + enabled: true, |
| 109 | + auth_token: "secret".into(), |
| 110 | + ..Default::default() |
| 111 | + }; |
| 112 | + assert!(AdminAuthFairing::from_config(&cfg).is_ok()); |
| 113 | + } |
| 114 | +} |
0 commit comments