Skip to content

Commit cfc0d55

Browse files
committed
fix(kms): set user-agent for auth api requests and improve error logging
Add dstack-kms/<version> user-agent header to auth webhook requests to avoid Cloudflare blocking. Log status code and response body on decode failure for easier debugging.
1 parent 1f1138b commit cfc0d55

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

kms/src/main_service/upgrade_authority.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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;
88
use serde::{Deserialize, Serialize};
99
use serde_human_bytes as hex_bytes;
@@ -60,6 +60,14 @@ pub(crate) struct GetInfoResponse {
6060
pub app_implementation: Option<String>,
6161
}
6262

63+
fn http_client() -> Result<reqwest::Client> {
64+
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")
69+
}
70+
6371
impl AuthApi {
6472
pub async fn is_app_allowed(&self, boot_info: &BootInfo, is_kms: bool) -> Result<BootResponse> {
6573
match self {
@@ -69,7 +77,7 @@ impl AuthApi {
6977
gateway_app_id: dev.gateway_app_id.clone(),
7078
}),
7179
AuthApi::Webhook { webhook } => {
72-
let client = reqwest::Client::new();
80+
let client = http_client()?;
7381
let path = if is_kms {
7482
"bootAuth/kms"
7583
} else {
@@ -78,7 +86,7 @@ impl AuthApi {
7886
let url = url_join(&webhook.url, path);
7987
let response = client.post(&url).json(&boot_info).send().await?;
8088
if !response.status().is_success() {
81-
bail!("Failed to check boot auth: {}", response.text().await?);
89+
bail!("failed to check boot auth: {}", response.text().await?);
8290
}
8391
Ok(response.json().await?)
8492
}
@@ -95,10 +103,12 @@ impl AuthApi {
95103
app_implementation: None,
96104
}),
97105
AuthApi::Webhook { webhook } => {
98-
let client = reqwest::Client::new();
106+
let client = http_client()?;
99107
let response = client.get(&webhook.url).send().await?;
100-
println!("url: {}", webhook.url);
101-
let info: AuthApiInfoResponse = response.json().await?;
108+
let status = response.status();
109+
let body = response.text().await?;
110+
let info: AuthApiInfoResponse = serde_json::from_str(&body)
111+
.with_context(|| format!("failed to decode auth api response from {}, status={status}, body={body}", webhook.url))?;
102112
Ok(GetInfoResponse {
103113
is_dev: false,
104114
kms_contract_address: Some(info.kms_contract_addr.clone()),

0 commit comments

Comments
 (0)