From a859c3b0bb501057b377a1a21171545909e5934e Mon Sep 17 00:00:00 2001 From: AJ Schmidt Date: Mon, 25 Mar 2024 13:08:00 -0400 Subject: [PATCH] Set User Agent Header for S3 Requests (#2137) * Set User Agent Header for S3 Requests Closes #2136. This PR configures the S3 backend to use a custom HTTP client which has a user agent header set. The user agent header enables `sccache` users to write AWS policies to accept or reject `sccache` requests to an S3 bucket based on their `sccache` version. --- Cargo.toml | 2 +- src/cache/s3.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 09f5e90d7..71c27a273 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -164,7 +164,7 @@ memcached = ["opendal/services-memcached"] native-zlib = [] oss = ["opendal/services-oss", "reqsign"] redis = ["url", "opendal/services-redis"] -s3 = ["opendal/services-s3", "reqsign"] +s3 = ["opendal/services-s3", "reqsign", "reqwest"] webdav = ["opendal/services-webdav"] # Enable features that will build a vendored version of openssl and # statically linked with it, instead of linking against the system-wide openssl diff --git a/src/cache/s3.rs b/src/cache/s3.rs index c80023c7b..000c3213c 100644 --- a/src/cache/s3.rs +++ b/src/cache/s3.rs @@ -11,8 +11,10 @@ // limitations under the License. use opendal::layers::LoggingLayer; +use opendal::raw::HttpClient; use opendal::services::S3; use opendal::Operator; +use reqwest::ClientBuilder; use crate::errors::*; @@ -29,6 +31,7 @@ impl S3Cache { server_side_encryption: Option, ) -> Result { let mut builder = S3::default(); + builder.http_client(set_user_agent()); builder.bucket(bucket); builder.root(key_prefix); @@ -63,6 +66,13 @@ impl S3Cache { } } +/// Set the user agent (helps with monitoring on the server side) +fn set_user_agent() -> HttpClient { + let user_agent = format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); + let client_builder = ClientBuilder::new().user_agent(user_agent); + HttpClient::build(client_builder).unwrap() +} + /// Resolve given endpoint along with use_ssl settings. fn endpoint_resolver(endpoint: &str, use_ssl: Option) -> Result { let endpoint_uri: http::Uri = endpoint