From a7693d52b645e45b87a4f1df90ea9b101b5d426b Mon Sep 17 00:00:00 2001 From: dennis zhuang Date: Tue, 24 Nov 2026 06:32:07 +0000 Subject: [PATCH] fix(servers): require authentication for debug endpoints (#10592) * fix(servers): require authentication for debug endpoints The /debug routes are added after the HTTP middleware stack, so they did not go through the auth layer, and need_auth() only covered /v1/. Apply the auth middleware to the /debug routes and treat the /debug namespace as requiring authentication when a user provider is configured. Signed-off-by: Dennis Zhuang * docs: pass credentials to debug endpoints in profiling guides Let dump.sh read curl credentials from the config file in GREPTIME_CURL_CONFIG, and document authentication for the /debug endpoints in the profiling and log level guides. Signed-off-by: Dennis Zhuang --------- Signed-off-by: Dennis Zhuang --- .../how-to-change-log-level-on-the-fly.md | 2 + docs/how-to/how-to-profile-cpu.md | 2 + docs/how-to/how-to-profile-memory.md | 16 ++++ .../memory-profile-scripts/scripts/dump.sh | 13 +++- src/servers/src/http.rs | 73 ++++++++++++++++++- src/servers/src/http/authorize.rs | 4 +- 6 files changed, 105 insertions(+), 5 deletions(-) diff --git a/docs/how-to/how-to-change-log-level-on-the-fly.md b/docs/how-to/how-to-change-log-level-on-the-fly.md index c3bf2602a26..e8e19bafed4 100644 --- a/docs/how-to/how-to-change-log-level-on-the-fly.md +++ b/docs/how-to/how-to-change-log-level-on-the-fly.md @@ -2,6 +2,8 @@ ## HTTP API +When the server is started with a user provider, add `-u :` to the `curl` commands in this guide. + example: ```bash curl --data "trace,flow=debug" 127.0.0.1:4000/debug/log_level diff --git a/docs/how-to/how-to-profile-cpu.md b/docs/how-to/how-to-profile-cpu.md index ff50b0ea44c..65ce6910829 100644 --- a/docs/how-to/how-to-profile-cpu.md +++ b/docs/how-to/how-to-profile-cpu.md @@ -1,6 +1,8 @@ # Profiling CPU ## HTTP API +When the server is started with a user provider, add `-u :` to the `curl` commands below. + Sample at 99 Hertz, for 5 seconds, output report in [protobuf format](https://github.com/google/pprof/blob/master/proto/profile.proto). ```bash curl -X POST -s '0:4000/debug/prof/cpu' > /tmp/pprof.out diff --git a/docs/how-to/how-to-profile-memory.md b/docs/how-to/how-to-profile-memory.md index da4e3b6f0a7..ca9653c0ebf 100644 --- a/docs/how-to/how-to-profile-memory.md +++ b/docs/how-to/how-to-profile-memory.md @@ -58,6 +58,22 @@ enable_heap_profiling = true To disable memory profiling, set `enable_heap_profiling` to `false`. +### Authentication + +When the server is started with a user provider, the `/debug` endpoints require HTTP Basic authentication. Add `-u :` to the `curl` commands in this guide. + +`dump.sh` reads credentials from the curl config file set in `GREPTIME_CURL_CONFIG`. Create the file with mode `600` and the following content: + +``` +user = ":" +``` + +Then run the script: + +```bash +GREPTIME_CURL_CONFIG=/path/to/curl.conf ./dump.sh +``` + ### Memory profiling control You can control heap profiling activation using the new HTTP APIs: diff --git a/docs/how-to/memory-profile-scripts/scripts/dump.sh b/docs/how-to/memory-profile-scripts/scripts/dump.sh index d84bee75a56..6a7ba8aba96 100755 --- a/docs/how-to/memory-profile-scripts/scripts/dump.sh +++ b/docs/how-to/memory-profile-scripts/scripts/dump.sh @@ -11,6 +11,17 @@ threshold_kb=$((20 * 1024)) sleep_interval=$((10 * 60)) +# Servers with a user provider require credentials on /debug endpoints. Set +# GREPTIME_CURL_CONFIG to a curl config file containing `user = ":"`. +curl_args=(-sf -X POST) +if [ -n "${GREPTIME_CURL_CONFIG:-}" ]; then + if [ ! -r "$GREPTIME_CURL_CONFIG" ]; then + echo "$(date): Cannot read curl config file: '$GREPTIME_CURL_CONFIG'." + exit 1 + fi + curl_args+=(--config "$GREPTIME_CURL_CONFIG") +fi + # Variable to store the last measured memory usage in KB last_mem_kb=0 @@ -55,7 +66,7 @@ while true; do timestamp=$(date +%Y%m%d%H%M%S) profile_file="greptime-${timestamp}.gprof" # Execute curl and capture output to file - if curl -sf -X POST localhost:4000/debug/prof/mem > "$profile_file"; then + if curl "${curl_args[@]}" localhost:4000/debug/prof/mem > "$profile_file"; then echo "$(date): Memory profile saved to $profile_file" else echo "$(date): Failed to dump memory profile (curl exit code: $?)." diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index a8d70e8fb37..b0fe91898fe 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -1113,8 +1113,10 @@ impl HttpServer { ); // Debug handlers are part of the complete router; the API listener hides - // them through the outer namespace guard applied below. - let router = router.nest( + // them through the outer namespace guard applied below. They are added + // after the middleware stack above, so they need their own auth layer, + // applied outside `nest` so that it sees the full `/debug/...` path. + let debug_router = Router::new().nest( "/debug", Router::new() // handler for changing log level dynamically @@ -1157,6 +1159,14 @@ impl HttpServer { ), ), ); + // `merge` keeps the fallback of its argument, so merge the main router into + // the debug router to keep the middleware stack on unmatched paths. + let router = debug_router + .layer(middleware::from_fn_with_state( + AuthState::new(self.user_provider.clone()), + authorize::check_http_auth, + )) + .merge(router); // Seal the API listener last: this is the outermost layer, applied after // all middleware, debug routes and any caller/plugin routes, so that only @@ -1997,6 +2007,65 @@ mod test { assert!(!is_api_listener_path("/health")); } + #[tokio::test] + async fn debug_routes_require_auth_when_provider_is_configured() { + let server = HttpServerBuilder::new(HttpOptions::default()).build(); + let client = TestClient::new(server.build(server.make_app()).unwrap()).await; + assert_eq!( + client + .get("/debug/workload_scheduler") + .send() + .await + .status(), + StatusCode::OK + ); + let response = client + .get("/no_such_path") + .header("Origin", "https://example.com") + .send() + .await; + assert_eq!(response.status(), StatusCode::NOT_FOUND); + assert!( + response + .headers() + .contains_key(http::header::ACCESS_CONTROL_ALLOW_ORIGIN) + ); + + let provider = + auth::static_user_provider_from_option("static_user_provider:cmd:user=password") + .unwrap(); + let server = HttpServerBuilder::new(HttpOptions::default()) + .with_user_provider(Arc::new(provider)) + .build(); + let client = TestClient::new(server.build(server.make_app()).unwrap()).await; + assert_eq!( + client + .get("/debug/workload_scheduler") + .send() + .await + .status(), + StatusCode::UNAUTHORIZED + ); + assert_eq!( + client + .post("/debug/log_level") + .body("info") + .send() + .await + .status(), + StatusCode::UNAUTHORIZED + ); + assert_eq!( + client + .get("/debug/workload_scheduler") + .header("Authorization", "Basic dXNlcjpwYXNzd29yZA==") + .send() + .await + .status(), + StatusCode::OK + ); + } + #[tokio::test] async fn packed_capability_requires_auth_on_full_and_api_listeners() { let (tx, _rx) = mpsc::channel(1); diff --git a/src/servers/src/http/authorize.rs b/src/servers/src/http/authorize.rs index 325efd129db..570ffe7b5a7 100644 --- a/src/servers/src/http/authorize.rs +++ b/src/servers/src/http/authorize.rs @@ -41,7 +41,7 @@ use crate::error::{ use crate::http::header::{GREPTIME_TIMEZONE_HEADER_NAME, GreptimeDbName}; use crate::http::result::error_result::ErrorResponse; use crate::http::splunk::is_splunk_request; -use crate::http::{AUTHORIZATION_HEADER, HTTP_API_PREFIX, PUBLIC_API_PREFIX}; +use crate::http::{AUTHORIZATION_HEADER, HTTP_API_PREFIX, PUBLIC_API_PREFIX, is_namespace}; use crate::influxdb::{is_influxdb_request, is_influxdb_v2_request}; /// AuthState is a holder state for [`UserProviderRef`] @@ -446,7 +446,7 @@ fn need_auth(req: &Request) -> bool { } } - path.starts_with(HTTP_API_PREFIX) + path.starts_with(HTTP_API_PREFIX) || is_namespace(path, "/debug") } fn extract_param_from_query<'a>(query: &'a str, param: &'a str) -> Option<&'a str> {