Skip to content

Commit f79e98f

Browse files
authored
fix: use_aws tool adds app name to the AWS CLI user agent string (#1417)
1 parent 9a391b1 commit f79e98f

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

crates/q_chat/src/tools/use_aws.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ use super::{
2626

2727
const READONLY_OPS: [&str; 6] = ["get", "describe", "list", "ls", "search", "batch_get"];
2828

29+
/// The environment variable name where we set additional metadata for the AWS CLI user agent.
30+
const USER_AGENT_ENV_VAR: &str = "AWS_EXECUTION_ENV";
31+
const USER_AGENT_APP_NAME: &str = "AmazonQ-For-CLI";
32+
const USER_AGENT_VERSION_KEY: &str = "Version";
33+
const USER_AGENT_VERSION_VALUE: &str = env!("CARGO_PKG_VERSION");
34+
2935
// TODO: we should perhaps composite this struct with an interface that we can use to mock the
3036
// actual cli with. That will allow us to more thoroughly test it.
3137
#[derive(Debug, Clone, Deserialize)]
@@ -45,7 +51,31 @@ impl UseAws {
4551

4652
pub async fn invoke(&self, _ctx: &Context, _updates: impl Write) -> Result<InvokeOutput> {
4753
let mut command = tokio::process::Command::new("aws");
48-
command.envs(std::env::vars()).arg("--region").arg(&self.region);
54+
55+
// Set up environment variables
56+
let mut env_vars: std::collections::HashMap<String, String> = std::env::vars().collect();
57+
58+
// Set up additional metadata for the AWS CLI user agent
59+
let user_agent_metadata_value = format!(
60+
"{} {}/{}",
61+
USER_AGENT_APP_NAME, USER_AGENT_VERSION_KEY, USER_AGENT_VERSION_VALUE
62+
);
63+
64+
// If the user agent metadata env var already exists, append to it, otherwise set it
65+
if let Some(existing_value) = env_vars.get(USER_AGENT_ENV_VAR) {
66+
if !existing_value.is_empty() {
67+
env_vars.insert(
68+
USER_AGENT_ENV_VAR.to_string(),
69+
format!("{} {}", existing_value, user_agent_metadata_value),
70+
);
71+
} else {
72+
env_vars.insert(USER_AGENT_ENV_VAR.to_string(), user_agent_metadata_value);
73+
}
74+
} else {
75+
env_vars.insert(USER_AGENT_ENV_VAR.to_string(), user_agent_metadata_value);
76+
}
77+
78+
command.envs(env_vars).arg("--region").arg(&self.region);
4979
if let Some(profile_name) = self.profile_name.as_deref() {
5080
command.arg("--profile").arg(profile_name);
5181
}

0 commit comments

Comments
 (0)