Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/apis/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ pub struct SourceRepositoryList {
pub next: Option<String>,
}

fn source_api_headers() -> reqwest::header::HeaderMap {
const CORE_REQUEST_HEADERS: &[(&str, &str)] = &[
("Accept", "application/json"),
("Accept-Encoding", "gzip, deflate, br"),
("Accept-Language", "en-US,en;q=0.9"),
(
"User-Agent",
concat!("source-proxy/", env!("CARGO_PKG_VERSION")),
),
];
CORE_REQUEST_HEADERS
.iter()
.map(|(name, value)| {
(
reqwest::header::HeaderName::from_lowercase(name.as_bytes()).unwrap(),
reqwest::header::HeaderValue::from_str(value).unwrap(),
)
})
.collect()
}

#[async_trait]
impl Api for SourceApi {
/// Creates and returns a backend client for a specific repository.
Expand Down Expand Up @@ -235,7 +256,7 @@ impl Api for SourceApi {
) -> Result<Account, BackendError> {
let client = reqwest::Client::new();
// Create headers
let mut headers = reqwest::header::HeaderMap::new();
let mut headers = source_api_headers();
if user_identity.api_key.is_some() {
let api_key = user_identity.api_key.unwrap();
headers.insert(
Expand Down Expand Up @@ -323,7 +344,7 @@ impl SourceApi {
repository_id: &str,
) -> Result<SourceRepository, BackendError> {
// Try to get the cached value
let cache_key = format!("{}/{}", account_id, repository_id);
let cache_key = format!("{account_id}/{repository_id}");

if let Some(cached_repo) = self.repository_cache.get(&cache_key).await {
return Ok(cached_repo);
Expand Down Expand Up @@ -352,7 +373,7 @@ impl SourceApi {
) -> Result<DataConnection, BackendError> {
let source_key = env::var("SOURCE_KEY").unwrap();
let client = reqwest::Client::new();
let mut headers = reqwest::header::HeaderMap::new();
let mut headers = source_api_headers();
headers.insert(
reqwest::header::AUTHORIZATION,
reqwest::header::HeaderValue::from_str(&source_key).unwrap(),
Expand Down Expand Up @@ -421,15 +442,14 @@ impl SourceApi {
let source_api_url = env::var("SOURCE_API_URL").unwrap();

// Create headers
let mut headers = reqwest::header::HeaderMap::new();
let mut headers = source_api_headers();
headers.insert(
reqwest::header::AUTHORIZATION,
reqwest::header::HeaderValue::from_str(&source_key).unwrap(),
);
let response = client
.get(format!(
"{}/api/v1/api-keys/{}/auth",
source_api_url, access_key_id
"{source_api_url}/api/v1/api-keys/{access_key_id}/auth"
))
.headers(headers)
.send()
Expand All @@ -453,7 +473,7 @@ impl SourceApi {

// Try to get the cached value
let cache_key = if anon {
format!("{}/{}", account_id, repository_id)
format!("{account_id}/{repository_id}")
} else {
let api_key = user_identity.clone().api_key.unwrap();
format!("{}/{}/{}", account_id, repository_id, api_key.access_key_id)
Expand Down Expand Up @@ -502,7 +522,7 @@ impl SourceApi {
let source_api_url = env::var("SOURCE_API_URL").unwrap();

// Create headers
let mut headers = reqwest::header::HeaderMap::new();
let mut headers = source_api_headers();
if user_identity.api_key.is_some() {
let api_key = user_identity.api_key.unwrap();
headers.insert(
Expand All @@ -516,8 +536,7 @@ impl SourceApi {

let response = client
.get(format!(
"{}/api/v1/repositories/{}/{}/permissions",
source_api_url, account_id, repository_id
"{source_api_url}/api/v1/repositories/{account_id}/{repository_id}/permissions"
))
.headers(headers)
.send()
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn get_object(
range_start = s;
if end.is_empty() || end.parse::<u64>().is_ok() {
is_range_request = true;
Some(format!("bytes={}-{}", start, end))
Some(format!("bytes={start}-{end}"))
} else {
None
}
Expand Down Expand Up @@ -450,7 +450,7 @@ async fn list_objects(

#[get("/")]
async fn index() -> impl Responder {
HttpResponse::Ok().body(format!("Source Cooperative Data Proxy v{}", VERSION))
HttpResponse::Ok().body(format!("Source Cooperative Data Proxy v{VERSION}"))
}

// Main function to set up and run the HTTP server
Expand Down
91 changes: 0 additions & 91 deletions src/utils/apache_logger.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/utils/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
body,
content_hash.to_str().unwrap(),
);
let credential_scope = format!("{}/{}/{}/aws4_request", date, region, service);
let credential_scope = format!("{date}/{region}/{service}/aws4_request");

let Some(datetime) = headers.get("x-amz-date") else {
return Err("No x-amz-date header found".to_string());
Expand Down Expand Up @@ -214,7 +214,7 @@ fn uri_encode(input: &str, encode_forward_slash: bool) -> Cow<str> {
encoded.push(ch);
} else {
for byte in ch.to_string().as_bytes() {
encoded.push_str(&format!("%{:02X}", byte));
encoded.push_str(&format!("%{byte:02X}"));
}
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ fn calculate_signature(
service: &str,
string_to_sign: &str,
) -> String {
let k_date = hmac_sha256(format!("AWS4{}", key).as_bytes(), date.as_bytes());
let k_date = hmac_sha256(format!("AWS4{key}").as_bytes(), date.as_bytes());
let k_region = hmac_sha256(&k_date, region.as_bytes());
let k_service = hmac_sha256(&k_region, service.as_bytes());
let k_signing = hmac_sha256(&k_service, b"aws4_request");
Expand Down Expand Up @@ -326,7 +326,7 @@ fn get_canonical_query_string(query_string: &str) -> String {
let encoded_key = uri_encode(&key, true);
let encoded_value = uri_encode(&value, true);

encoded_params.push(format!("{}={}", encoded_key, encoded_value));
encoded_params.push(format!("{encoded_key}={encoded_value}"));
}

encoded_params.join("&")
Expand Down
18 changes: 9 additions & 9 deletions src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl error::ResponseError for BackendError {
let status_code = self.status_code();
let body = match status_code {
e if e.is_client_error() => self.to_string(),
_ => format!("Internal Server Error: {}", self),
_ => format!("Internal Server Error: {self}"),
};
if status_code.is_server_error() {
error!("Error: {}", self);
Expand Down Expand Up @@ -148,13 +148,13 @@ fn get_rusoto_error_message<T: std::error::Error>(
error: RusotoError<T>,
) -> String {
match error {
RusotoError::Service(e) => format!("{} Service Error: {}", operation, e),
RusotoError::HttpDispatch(e) => format!("{} HttpDispatch Error: {}", operation, e),
RusotoError::Credentials(e) => format!("{} Credentials Error: {}", operation, e),
RusotoError::Validation(e) => format!("{} Validation Error: {}", operation, e),
RusotoError::ParseError(e) => format!("{} Parse Error: {}", operation, e),
RusotoError::Service(e) => format!("{operation} Service Error: {e}"),
RusotoError::HttpDispatch(e) => format!("{operation} HttpDispatch Error: {e}"),
RusotoError::Credentials(e) => format!("{operation} Credentials Error: {e}"),
RusotoError::Validation(e) => format!("{operation} Validation Error: {e}"),
RusotoError::ParseError(e) => format!("{operation} Parse Error: {e}"),
RusotoError::Unknown(e) => format!("{} Unknown Error: status {}", operation, e.status),
RusotoError::Blocking => format!("{} Blocking Error", operation),
RusotoError::Blocking => format!("{operation} Blocking Error"),
}
}
macro_rules! impl_s3_errors {
Expand Down Expand Up @@ -202,12 +202,12 @@ impl From<RusotoError<ListObjectsV2Error>> for BackendError {

impl From<DeError> for BackendError {
fn from(error: DeError) -> BackendError {
BackendError::XmlParseError(format!("failed to parse xml: {}", error))
BackendError::XmlParseError(format!("failed to parse xml: {error}"))
}
}
impl From<serde_xml_rs::Error> for BackendError {
fn from(error: serde_xml_rs::Error) -> BackendError {
BackendError::XmlParseError(format!("failed to parse xml: {}", error))
BackendError::XmlParseError(format!("failed to parse xml: {error}"))
}
}

Expand Down