mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-26 12:44:10 +00:00
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 <killme2008@gmail.com> * 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 <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
## HTTP API
|
||||
|
||||
When the server is started with a user provider, add `-u <username>:<password>` to the `curl` commands in this guide.
|
||||
|
||||
example:
|
||||
```bash
|
||||
curl --data "trace,flow=debug" 127.0.0.1:4000/debug/log_level
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# Profiling CPU
|
||||
|
||||
## HTTP API
|
||||
When the server is started with a user provider, add `-u <username>:<password>` 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
|
||||
|
||||
@@ -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 <username>:<password>` 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 = "<username>:<password>"
|
||||
```
|
||||
|
||||
Then run the script:
|
||||
|
||||
```bash
|
||||
GREPTIME_CURL_CONFIG=/path/to/curl.conf ./dump.sh <pid>
|
||||
```
|
||||
|
||||
### Memory profiling control
|
||||
|
||||
You can control heap profiling activation using the new HTTP APIs:
|
||||
|
||||
@@ -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 = "<username>:<password>"`.
|
||||
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: $?)."
|
||||
|
||||
+71
-2
@@ -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);
|
||||
|
||||
@@ -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<B>(req: &Request<B>) -> 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> {
|
||||
|
||||
Reference in New Issue
Block a user