Skip to content

Commit 802e80a

Browse files
committed
refactor(kms): extract generic http_get/http_post helpers
- 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
1 parent 79b8b8d commit 802e80a

1 file changed

Lines changed: 22 additions & 21 deletions

File tree

kms/src/main_service/upgrade_authority.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::config::AuthApi;
66
use anyhow::{bail, Context, Result};
77
use ra_tls::attestation::AttestationMode;
8+
use serde::de::DeserializeOwned;
89
use serde::{Deserialize, Serialize};
910
use serde_human_bytes as hex_bytes;
1011

@@ -60,12 +61,26 @@ pub(crate) struct GetInfoResponse {
6061
pub app_implementation: Option<String>,
6162
}
6263

63-
fn http_client() -> Result<reqwest::Client> {
64+
async fn http_get<R: DeserializeOwned>(url: &str) -> Result<R> {
65+
send_request(reqwest::Client::new().get(url), url).await
66+
}
67+
68+
async fn http_post<R: DeserializeOwned>(url: &str, body: &impl Serialize) -> Result<R> {
69+
send_request(reqwest::Client::new().post(url).json(body), url).await
70+
}
71+
72+
async fn send_request<R: DeserializeOwned>(req: reqwest::RequestBuilder, url: &str) -> Result<R> {
6473
static USER_AGENT: &str = concat!("dstack-kms/", env!("CARGO_PKG_VERSION"));
65-
reqwest::Client::builder()
66-
.user_agent(USER_AGENT)
67-
.build()
68-
.context("failed to build http client")
74+
let response = req.header("User-Agent", USER_AGENT).send().await?;
75+
let status = response.status();
76+
let body = response.text().await?;
77+
let short_body = &body[..body.len().min(512)];
78+
if !status.is_success() {
79+
bail!("auth api {url} returned {status}: {short_body}");
80+
}
81+
serde_json::from_str(&body).with_context(|| {
82+
format!("failed to decode response from {url}, status={status}, body={short_body}")
83+
})
6984
}
7085

7186
impl AuthApi {
@@ -77,18 +92,13 @@ impl AuthApi {
7792
gateway_app_id: dev.gateway_app_id.clone(),
7893
}),
7994
AuthApi::Webhook { webhook } => {
80-
let client = http_client()?;
8195
let path = if is_kms {
8296
"bootAuth/kms"
8397
} else {
8498
"bootAuth/app"
8599
};
86100
let url = url_join(&webhook.url, path);
87-
let response = client.post(&url).json(&boot_info).send().await?;
88-
if !response.status().is_success() {
89-
bail!("Failed to check boot auth: {}", response.text().await?);
90-
}
91-
Ok(response.json().await?)
101+
http_post(&url, &boot_info).await
92102
}
93103
}
94104
}
@@ -103,16 +113,7 @@ impl AuthApi {
103113
app_implementation: None,
104114
}),
105115
AuthApi::Webhook { webhook } => {
106-
let client = http_client()?;
107-
let response = client.get(&webhook.url).send().await?;
108-
let status = response.status();
109-
let body = response.text().await?;
110-
let info: AuthApiInfoResponse = serde_json::from_str(&body).with_context(|| {
111-
format!(
112-
"failed to decode auth api response from {}, status={status}, body={body}",
113-
webhook.url
114-
)
115-
})?;
116+
let info: AuthApiInfoResponse = http_get(&webhook.url).await?;
116117
Ok(GetInfoResponse {
117118
is_dev: false,
118119
kms_contract_address: Some(info.kms_contract_addr.clone()),

0 commit comments

Comments
 (0)