Skip to content

Add Content-Type header to Agent responses #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
394 changes: 198 additions & 196 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions aws_secretsmanager_agent/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@
///
/// # Returns
///
/// * `log_to_file` - `true` if writing logs to a file (default), `false` if writing logs to
/// stdout/stderr
/// * `log_to_file` - `true` if writing logs to a file (default), `false` if writing logs to stdout/stderr

Check warning on line 183 in aws_secretsmanager_agent/src/config.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/config.rs#L183

Added line #L183 was not covered by tests
pub fn log_to_file(&self) -> bool {
self.log_to_file
}
Expand Down
2 changes: 1 addition & 1 deletion aws_secretsmanager_agent/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub const EMPTY_ENV_LIST_MSG: &str =
pub const BAD_PREFIX_MSG: &str =
"The path prefix specified in the configuration file must begin with /.";

/// Other constants that are used across the code base.
// Other constants that are used across the code base.

// The application name.
pub const APPNAME: &str = "aws-secrets-manager-agent";
Expand Down
2 changes: 1 addition & 1 deletion aws_secretsmanager_agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ mod tests {
#[tokio::test]
async fn path_refresh_success() {
let req = "/v1/My/Test?versionStage=AWSPENDING&refreshNow=0";
let (status, body) = run_request(&req).await;
let (status, body) = run_request(req).await;
assert_eq!(status, StatusCode::OK);
validate_response_extra("My/Test", DEFAULT_VERSION, vec!["AWSPENDING"], body);
}
Expand Down
8 changes: 4 additions & 4 deletions aws_secretsmanager_agent/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {
assert_eq!(query.secret_id, secret_id);
assert_eq!(query.version_id, None);
assert_eq!(query.version_stage, None);
assert_eq!(query.refresh_now, false);
assert!(!query.refresh_now);
}

#[test]
Expand All @@ -113,7 +113,7 @@ mod tests {
assert_eq!(query.secret_id, secret_id);
assert_eq!(query.version_id, None);
assert_eq!(query.version_stage, None);
assert_eq!(query.refresh_now, true);
assert!(query.refresh_now);
}

#[test]
Expand All @@ -128,7 +128,7 @@ mod tests {
assert_eq!(query.secret_id, secret_id);
assert_eq!(query.version_id, None);
assert_eq!(query.version_stage, None);
assert_eq!(query.refresh_now, false);
assert!(!query.refresh_now);
}

#[test]
Expand Down Expand Up @@ -160,7 +160,7 @@ mod tests {
assert_eq!(query.secret_id, secret_id);
assert_eq!(query.version_id, None);
assert_eq!(query.version_stage, None);
assert_eq!(query.refresh_now, false);
assert!(!query.refresh_now);
}

#[test]
Expand Down
76 changes: 55 additions & 21 deletions aws_secretsmanager_agent/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
max_conn: usize,
}

/// HTTP response relevant fields
#[derive(Debug)]
struct ResponseContent {

Check warning on line 32 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
rsp_body: String,
content_type: ContentType,
}

/// Used to set Content-Type header

Check warning on line 37 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L35-L37

Added lines #L35 - L37 were not covered by tests
#[derive(Debug)]
enum ContentType {
Plain,
Json,
}

Check warning on line 42 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L40-L42

Added lines #L40 - L42 were not covered by tests

/// Handle incoming HTTP requests.
///
/// Implements the HTTP handler. Each incomming request is handled in its own
Expand Down Expand Up @@ -108,11 +122,22 @@

// Format the response.
match result {
Ok(rsp_body) => Ok(Response::builder()
Ok(ResponseContent {

Check warning on line 125 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L125

Added line #L125 was not covered by tests
rsp_body,
content_type,
}) => Ok(Response::builder()
.header(
"Content-Type",
match content_type {
ContentType::Plain => "text/plain",
ContentType::Json => "application/json",
},
)

Check warning on line 135 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L134-L135

Added lines #L134 - L135 were not covered by tests
.body(Full::new(Bytes::from(rsp_body)))
.unwrap()),
Err(e) => Ok(Response::builder()
.status(e.0)
.header("Content-Type", "text/plain")
.body(Full::new(Bytes::from(e.1)))
.unwrap()),
}
Expand All @@ -134,40 +159,49 @@
&self,
req: &Request<IncomingBody>,
count: usize,
) -> Result<String, HttpError> {
) -> Result<ResponseContent, HttpError> {
self.validate_max_conn(req, count)?; // Verify connection limits are not exceeded
self.validate_token(req)?; // Check for a valid SSRF token
self.validate_method(req)?; // Allow only GET requests

match req.uri().path() {
"/ping" => Ok("healthy".into()), // Standard health check
"/ping" => Ok(ResponseContent {
rsp_body: "healthy".into(),
content_type: ContentType::Plain,
}), // Standard health check

// Lambda extension style query
"/secretsmanager/get" => {
let qry = GSVQuery::try_from_query(&req.uri().to_string())?;
Ok(self
.cache_mgr
.fetch(
&qry.secret_id,
qry.version_id.as_deref(),
qry.version_stage.as_deref(),
qry.refresh_now,
)
.await?)
Ok(ResponseContent {

Check warning on line 176 in aws_secretsmanager_agent/src/server.rs

View check run for this annotation

Codecov / codecov/patch

aws_secretsmanager_agent/src/server.rs#L176

Added line #L176 was not covered by tests
rsp_body: self
.cache_mgr
.fetch(
&qry.secret_id,
qry.version_id.as_deref(),
qry.version_stage.as_deref(),
qry.refresh_now,
)
.await?,
content_type: ContentType::Json,
})
}

// Path style request
path if path.starts_with(self.path_prefix.as_str()) => {
let qry = GSVQuery::try_from_path_query(&req.uri().to_string(), &self.path_prefix)?;
Ok(self
.cache_mgr
.fetch(
&qry.secret_id,
qry.version_id.as_deref(),
qry.version_stage.as_deref(),
qry.refresh_now,
)
.await?)
Ok(ResponseContent {
rsp_body: self
.cache_mgr
.fetch(
&qry.secret_id,
qry.version_id.as_deref(),
qry.version_stage.as_deref(),
qry.refresh_now,
)
.await?,
content_type: ContentType::Json,
})
}
_ => Err(HttpError(404, "Not found".into())),
}
Expand Down
16 changes: 8 additions & 8 deletions aws_secretsmanager_caching/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl SecretsManagerCachingClient {
/// use aws_secretsmanager_caching::SecretsManagerCachingClient;
/// use std::num::NonZeroUsize;
/// use std::time::Duration;

///
/// let asm_client = SecretsManagerClient::from_conf(
/// Config::builder()
/// .behavior_version_latest()
Expand Down Expand Up @@ -141,15 +141,15 @@ impl SecretsManagerCachingClient {
/// use std::num::NonZeroUsize;
/// use std::time::Duration;
/// use aws_config::{BehaviorVersion, Region};

///
/// let config = aws_config::load_defaults(BehaviorVersion::latest())
/// .await
/// .into_builder()
/// .region(Region::from_static("us-west-2"))
/// .build();

///
/// let asm_builder = aws_sdk_secretsmanager::config::Builder::from(&config);

///
/// let client = SecretsManagerCachingClient::from_builder(
/// asm_builder,
/// NonZeroUsize::new(1000).unwrap(),
Expand Down Expand Up @@ -206,9 +206,9 @@ impl SecretsManagerCachingClient {
);
}

return Ok(self
return self
.refresh_secret_value(secret_id, version_id, version_stage, None)
.await?);
.await;
}

let read_lock = self.store.read().await;
Expand Down Expand Up @@ -424,12 +424,12 @@ impl SecretsManagerCachingClient {
}

#[cfg(debug_assertions)]
fn increment_counter(&self, counter: &AtomicU32) -> () {
fn increment_counter(&self, counter: &AtomicU32) {
counter.fetch_add(1, Ordering::SeqCst);
}

#[cfg(debug_assertions)]
fn reset_counter(&self, counter: &AtomicU32) -> () {
fn reset_counter(&self, counter: &AtomicU32) {
counter.store(0, Ordering::SeqCst);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ impl SecretStore for MemoryStore {
}

/// Write the secret value to the store

#[cfg(test)]
mod tests {

Expand Down
Loading