Skip to content

Commit 10d90b3

Browse files
fix: broken state for telemetryClientId, user agent versioning (#1850)
1 parent c333c0f commit 10d90b3

File tree

3 files changed

+63
-52
lines changed

3 files changed

+63
-52
lines changed

crates/chat-cli/src/aws_common/user_agent_override_interceptor.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ use http::header::{
1818
InvalidHeaderValue,
1919
USER_AGENT,
2020
};
21-
use tracing::warn;
21+
use tracing::{
22+
trace,
23+
warn,
24+
};
2225

2326
/// The environment variable name of additional user agent metadata we include in the user agent
2427
/// string. This is used in AWS CloudShell where they want to track usage by version.
2528
const AWS_TOOLING_USER_AGENT: &str = "AWS_TOOLING_USER_AGENT";
2629

27-
const VERSION_HEADER: &str = "Version";
30+
const VERSION_HEADER: &str = "appVersion";
2831
const VERSION_VALUE: &str = env!("CARGO_PKG_VERSION");
2932

3033
#[derive(Debug)]
@@ -91,47 +94,56 @@ impl Intercept for UserAgentOverrideInterceptor {
9194
// Allow for overriding the user agent by an earlier interceptor (so, for example,
9295
// tests can use `AwsUserAgent::for_tests()`) by attempting to grab one out of the
9396
// config bag before creating one.
94-
let ua: Cow<'_, AwsUserAgent> = cfg.load::<AwsUserAgent>().map(Cow::Borrowed).map_or_else(
95-
|| {
97+
let ua: Cow<'_, AwsUserAgent> = match cfg.get_mut::<AwsUserAgent>() {
98+
Some(ua) => {
99+
apply_additional_metadata(&self.env, ua);
100+
Cow::Borrowed(ua)
101+
},
102+
None => {
96103
let api_metadata = cfg
97104
.load::<ApiMetadata>()
98105
.ok_or(UserAgentOverrideInterceptorError::MissingApiMetadata)?;
99106

100-
let aws_tooling_user_agent = env.get(AWS_TOOLING_USER_AGENT);
101-
let mut ua = AwsUserAgent::new_from_environment(env, api_metadata.clone());
102-
103-
let ver = format!("{VERSION_HEADER}/{VERSION_VALUE}");
104-
match AdditionalMetadata::new(clean_metadata(&ver)) {
105-
Ok(md) => {
106-
ua.add_additional_metadata(md);
107-
},
108-
Err(err) => panic!("Failed to parse version: {err}"),
109-
};
107+
let mut ua = AwsUserAgent::new_from_environment(self.env.clone(), api_metadata.clone());
110108

111109
let maybe_app_name = cfg.load::<AppName>();
112110
if let Some(app_name) = maybe_app_name {
113111
ua.set_app_name(app_name.clone());
114112
}
115-
if let Ok(val) = aws_tooling_user_agent {
116-
match AdditionalMetadata::new(clean_metadata(&val)) {
117-
Ok(md) => {
118-
ua.add_additional_metadata(md);
119-
},
120-
Err(err) => warn!(%err, %val, "Failed to parse {AWS_TOOLING_USER_AGENT}"),
121-
};
122-
}
123113

124-
Ok(Cow::Owned(ua))
114+
apply_additional_metadata(&env, &mut ua);
115+
116+
Cow::Owned(ua)
125117
},
126-
Result::<_, UserAgentOverrideInterceptorError>::Ok,
127-
)?;
118+
};
119+
120+
trace!(?ua, "setting user agent");
128121

129122
let headers = context.request_mut().headers_mut();
130123
headers.insert(USER_AGENT.as_str(), ua.aws_ua_header());
131124
Ok(())
132125
}
133126
}
134127

128+
fn apply_additional_metadata(env: &Env, ua: &mut AwsUserAgent) {
129+
let ver = format!("{VERSION_HEADER}/{VERSION_VALUE}");
130+
match AdditionalMetadata::new(clean_metadata(&ver)) {
131+
Ok(md) => {
132+
ua.add_additional_metadata(md);
133+
},
134+
Err(err) => panic!("Failed to parse version: {err}"),
135+
};
136+
137+
if let Ok(val) = env.get(AWS_TOOLING_USER_AGENT) {
138+
match AdditionalMetadata::new(clean_metadata(&val)) {
139+
Ok(md) => {
140+
ua.add_additional_metadata(md);
141+
},
142+
Err(err) => warn!(%err, %val, "Failed to parse {AWS_TOOLING_USER_AGENT}"),
143+
};
144+
}
145+
}
146+
135147
fn clean_metadata(s: &str) -> String {
136148
let valid_character = |c: char| -> bool {
137149
match c {

crates/chat-cli/src/database/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ impl Database {
266266
/// Get the client ID used for telemetry requests.
267267
pub fn get_client_id(&mut self) -> Result<Option<Uuid>, DatabaseError> {
268268
Ok(self
269-
.get_entry::<String>(Table::State, CLIENT_ID_KEY)?
269+
.get_json_entry::<String>(Table::State, CLIENT_ID_KEY)?
270270
.and_then(|s| Uuid::from_str(&s).ok()))
271271
}
272272

273273
/// Set the client ID used for telemetry requests.
274274
pub fn set_client_id(&mut self, client_id: Uuid) -> Result<usize, DatabaseError> {
275-
self.set_entry(Table::State, CLIENT_ID_KEY, client_id.to_string())
275+
self.set_json_entry(Table::State, CLIENT_ID_KEY, client_id.to_string())
276276
}
277277

278278
/// Get the start URL used for IdC login.
@@ -333,6 +333,19 @@ impl Database {
333333
self.set_json_entry(Table::Conversations, path, state)
334334
}
335335

336+
pub async fn get_secret(&self, key: &str) -> Result<Option<Secret>, DatabaseError> {
337+
Ok(self.get_entry::<String>(Table::Auth, key)?.map(Into::into))
338+
}
339+
340+
pub async fn set_secret(&self, key: &str, value: &str) -> Result<(), DatabaseError> {
341+
self.set_entry(Table::Auth, key, value)?;
342+
Ok(())
343+
}
344+
345+
pub async fn delete_secret(&self, key: &str) -> Result<(), DatabaseError> {
346+
self.delete_entry(Table::Auth, key)
347+
}
348+
336349
// Private functions. Do not expose.
337350

338351
fn migrate(self) -> Result<Self, DatabaseError> {
@@ -428,19 +441,6 @@ impl Database {
428441

429442
Ok(map)
430443
}
431-
432-
pub async fn get_secret(&self, key: &str) -> Result<Option<Secret>, DatabaseError> {
433-
Ok(self.get_entry::<String>(Table::Auth, key)?.map(Into::into))
434-
}
435-
436-
pub async fn set_secret(&self, key: &str, value: &str) -> Result<(), DatabaseError> {
437-
self.set_entry(Table::Auth, key, value)?;
438-
Ok(())
439-
}
440-
441-
pub async fn delete_secret(&self, key: &str) -> Result<(), DatabaseError> {
442-
self.delete_entry(Table::Auth, key)
443-
}
444444
}
445445

446446
fn max_migration<C: Deref<Target = Connection>>(conn: &C) -> Option<i64> {

crates/fig_desktop_api/src/requests/profile.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use fig_telemetry_core::{
88
QProfileSwitchIntent,
99
TelemetryResult,
1010
};
11+
use tracing::debug;
1112

1213
use super::{
1314
RequestResult,
@@ -59,17 +60,15 @@ pub async fn set_profile(request: SetProfileRequest) -> RequestResult {
5960
}
6061

6162
pub async fn list_available_profiles(_request: ListAvailableProfilesRequest) -> RequestResult {
62-
Ok(
63-
ServerOriginatedSubMessage::ListAvailableProfilesResponse(ListAvailableProfilesResponse {
64-
profiles: fig_api_client::profile::list_available_profiles()
65-
.await
66-
.iter()
67-
.map(|profile| fig_proto::fig::Profile {
68-
arn: profile.arn.clone(),
69-
profile_name: profile.profile_name.clone(),
70-
})
71-
.collect(),
63+
debug!("listing available profiles");
64+
let profiles = fig_api_client::profile::list_available_profiles()
65+
.await
66+
.iter()
67+
.map(|profile| fig_proto::fig::Profile {
68+
arn: profile.arn.clone(),
69+
profile_name: profile.profile_name.clone(),
7270
})
73-
.into(),
74-
)
71+
.collect();
72+
debug!(?profiles, "fetched profiles");
73+
Ok(ServerOriginatedSubMessage::ListAvailableProfilesResponse(ListAvailableProfilesResponse { profiles }).into())
7574
}

0 commit comments

Comments
 (0)