Skip to content

Commit

Permalink
Add simple dashboard to ztunnel admin (istio#475)
Browse files Browse the repository at this point in the history
* add dashboard

* make gen

* include file with macro
  • Loading branch information
hanxiaop authored Mar 28, 2023
1 parent 70f7a33 commit b32cc7a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use drain::Watch;
use gperftools::heap_profiler::HEAP_PROFILER;
#[cfg(feature = "gperftools")]
use gperftools::profiler::PROFILER;
use hyper::{Body, Request, Response};
use hyper::{header::HeaderValue, header::CONTENT_TYPE, Body, Request, Response};
use pprof::protos::Message;
#[cfg(feature = "gperftools")]
use tokio::fs::File;
Expand Down Expand Up @@ -128,12 +128,55 @@ impl Service {
)
.await),
"/logging" => Ok(handle_logging(req).await),
"/" => Ok(handle_dashboard(req).await),
_ => Ok(empty_response(hyper::StatusCode::NOT_FOUND)),
}
})
}
}

async fn handle_dashboard(_req: Request<Body>) -> Response<Body> {
let apis = &[
(
"debug/pprof/profile",
"build profile using the pprof profiler (if supported)",
),
(
"debug/gprof/profile",
"build profile using the gperftools profiler (if supported)",
),
(
"debug/gprof/heap",
"collect heap profiling data (if supported)",
),
("quitquitquit", "shut down the server"),
("config_dump", "dump the current Ztunnel configuration"),
("logging", "query/changing logging levels"),
];

let mut api_rows = String::new();

for (index, (path, description)) in apis.iter().enumerate() {
api_rows.push_str(&format!(
"<tr class=\"{row_class}\"><td class=\"home-data\"><a href=\"{path}\">{path}</a></td><td class=\"home-data\">{description}</td></tr>\n",
row_class = if index % 2 == 1 { "gray" } else { "vert-space" },
path = path,
description = description
));
}

let html_str = include_str!("./assets/dashboard.html");
let html_str = html_str.replace("<!--API_ROWS_PLACEHOLDER-->", &api_rows);

let mut response = Response::new(Body::from(html_str));
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);

response
}

fn x509_to_pem(x509: &X509) -> String {
match x509.to_pem() {
Err(e) => format!("<pem construction error: {e}>"),
Expand Down
62 changes: 62 additions & 0 deletions src/assets/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ztunnel Admin</title>
<style>
.home-table {
font-family: sans-serif;
font-size: medium;
border-collapse: collapse;
border-spacing: 0;
}

.home-data {
text-align: left;
padding: 4px;
}

.home-form {
margin-bottom: 0;
}

.button-as-link {
background: none!important;
border: none;
padding: 0!important;
font-family: sans-serif;
font-size: medium;
color: #069;
text-decoration: underline;
cursor: pointer;
}

.gray {
background-color: #dddddd;
}

.vert-space {
height: 4px;
}

.option {
padding-bottom: 4px;
padding-top: 4px;
padding-right: 4px;
padding-left: 20px;
text-align: right;
}
</style>
</head>
<body cz-shortcut-listen="true">
<table class="home-table">
<tr><th class="home-data">Command</th><th class="home-data">Description</th></tr>
<tbody>
<tr class="vert-space">
<td></td>
<td></td>
</tr>
<!--API_ROWS_PLACEHOLDER-->
</tbody>
</table>
</body>
</html>

0 comments on commit b32cc7a

Please sign in to comment.