Skip to content

Commit 7da5d06

Browse files
authored
Merge pull request #525 from Dstack-TEE/feat-set-header-for-kms-auth-api-request
fix(kms): set user-agent for auth api requests and improve error logging
2 parents d8fb3d6 + 802e80a commit 7da5d06

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

kms/src/main_service/upgrade_authority.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
use crate::config::AuthApi;
6-
use anyhow::{bail, Result};
6+
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,6 +61,28 @@ pub(crate) struct GetInfoResponse {
6061
pub app_implementation: Option<String>,
6162
}
6263

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> {
73+
static USER_AGENT: &str = concat!("dstack-kms/", env!("CARGO_PKG_VERSION"));
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+
})
84+
}
85+
6386
impl AuthApi {
6487
pub async fn is_app_allowed(&self, boot_info: &BootInfo, is_kms: bool) -> Result<BootResponse> {
6588
match self {
@@ -69,18 +92,13 @@ impl AuthApi {
6992
gateway_app_id: dev.gateway_app_id.clone(),
7093
}),
7194
AuthApi::Webhook { webhook } => {
72-
let client = reqwest::Client::new();
7395
let path = if is_kms {
7496
"bootAuth/kms"
7597
} else {
7698
"bootAuth/app"
7799
};
78100
let url = url_join(&webhook.url, path);
79-
let response = client.post(&url).json(&boot_info).send().await?;
80-
if !response.status().is_success() {
81-
bail!("Failed to check boot auth: {}", response.text().await?);
82-
}
83-
Ok(response.json().await?)
101+
http_post(&url, &boot_info).await
84102
}
85103
}
86104
}
@@ -95,10 +113,7 @@ impl AuthApi {
95113
app_implementation: None,
96114
}),
97115
AuthApi::Webhook { webhook } => {
98-
let client = reqwest::Client::new();
99-
let response = client.get(&webhook.url).send().await?;
100-
println!("url: {}", webhook.url);
101-
let info: AuthApiInfoResponse = response.json().await?;
116+
let info: AuthApiInfoResponse = http_get(&webhook.url).await?;
102117
Ok(GetInfoResponse {
103118
is_dev: false,
104119
kms_contract_address: Some(info.kms_contract_addr.clone()),

0 commit comments

Comments
 (0)