Conversation
c4a5835 to
c0b0e12
Compare
c0b0e12 to
79b8b8d
Compare
kvinwang
reviewed
Mar 4, 2026
Collaborator
kvinwang
left a comment
There was a problem hiding this comment.
Nice improvement on User-Agent and error logging! A few suggestions to take it further:
1. Extract generic http_get / http_post helpers
The current http_client() + manual response parsing can be unified into reusable helpers that handle User-Agent, status check, body truncation, and decode error context in one place:
async fn http_get<R: DeserializeOwned>(url: &str) -> Result<R> {
send_request(reqwest::Client::new().get(url), url).await
}
async fn http_post<R: DeserializeOwned>(url: &str, body: &impl Serialize) -> Result<R> {
send_request(reqwest::Client::new().post(url).json(body), url).await
}
async fn send_request<R: DeserializeOwned>(req: reqwest::RequestBuilder, url: &str) -> Result<R> {
static USER_AGENT: &str = concat!("dstack-kms/", env!("CARGO_PKG_VERSION"));
let response = req.header("User-Agent", USER_AGENT).send().await?;
let status = response.status();
let body = response.text().await?;
let short_body = &body[..body.len().min(512)];
if !status.is_success() {
bail!("auth api {url} returned {status}: {short_body}");
}
serde_json::from_str(&body).with_context(|| {
format!("failed to decode response from {url}, status={status}, body={short_body}")
})
}Then call sites become one-liners:
let resp: BootResponse = http_post(&url, &boot_info).await?;
let info: AuthApiInfoResponse = http_get(&webhook.url).await?;This gives you:
- Unified User-Agent across all requests
- Status check — non-2xx bails immediately with url + status + body
- Consistent decode error context — both
is_app_allowedandget_infoget the same treatment (currently onlyget_infohas the improved error logging) - Body truncation —
short_bodycaps at 512 bytes in error messages to avoid huge HTML pages blowing up logs - No separate
http_client()function needed
2. Remove stray println!
The println!("url: {}", webhook.url) on the removed line — good catch removing it 👍
3. Minor: body truncation for error messages
If the webhook returns a large HTML error page, the full body in the error message could be noisy. The &body[..body.len().min(512)] in the suggestion above handles this. Note short_body is only used for error messages — serde_json::from_str still parses the full body.
- Unify User-Agent, status check, body truncation, and decode error context into a single send_request function - Both is_app_allowed and get_info now share consistent error handling - Truncate response body to 512 bytes in error messages to avoid noisy HTML pages in logs - Remove separate http_client() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dstack-kms/<version>User-Agent header to all auth webhook requests, to avoid being blocked by WAF (e.g. Cloudflare)Test plan