mirror of
https://github.com/block/buzz.git
synced 2026-11-30 05:02:57 +00:00
feat(search): sign OpenSearch requests with AWS SigV4 (#9947)
## Summary The relay's OpenSearch client could only use HTTP basic auth. Amazon OpenSearch Serverless accepts only requests signed with AWS SigV4, and managed Amazon OpenSearch Service domains can require them, so the relay could not index into either. This PR adds optional SigV4 signing. It is off by default; basic auth and unauthenticated clusters work as before. | Variable | Meaning | | --- | --- | | `BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE` | Turns signing on and names the signing service: `aoss` for a Serverless collection, `es` for a managed domain. Unset means no signing. | | `BUZZ_OPENSEARCH_AWS_REGION` | Region to sign for. Defaults to `AWS_REGION`, then `AWS_DEFAULT_REGION`. | The relay refuses to start when signing is set without a region, together with basic auth, without `BUZZ_OPENSEARCH_URL`, or with a service other than `es` or `aoss`. - **Credentials** come from the AWS default chain: environment variables, the shared profile, a web identity token, then the container or instance role. No keys are added to the relay's own settings. - **Refresh** (`aws_credentials.rs`): the `opensearch` crate asks its credentials provider on every request, and the AWS default chain does not cache. Used directly, every search and bulk write would call the token service or the instance metadata endpoint. The relay keeps one set of credentials and reloads it five minutes before it expires (every 15 minutes for credentials without an expiry). Each load builds the AWS provider anew, because a kept one never rereads the shared profile; a key rotated there is picked up at the next load. Concurrent requests share one load, whether it succeeds or fails: the next load starts at least 30 s after the last one ended, and requests in between use the valid set or fail at once. A first load is bounded at 30 s. A reload while the current set is still valid is bounded at 5 s, and if it fails the current set stays in use. - **Logging:** a failed credential load logs only the kind of error, because the causes can carry what a `credential_process` helper printed. The AWS SDK logs the full cause itself under `aws_config` at warn level, which the relay's default log filter leaves out; the deployment doc says how to turn it on and what it can contain. Credentials are never logged, and the config's `Debug` output shows only the service and region. - **Dependencies:** enables the `opensearch` crate's `aws-auth` feature and adds `aws-config` for the credential chain. ### Worth knowing - The region is not read from the shared AWS profile file, only from the variables above. - Right after a Serverless collection became active, the first signed request got HTTP 401 once; the same run passed 45 s later. The indexer already retries such failures. - A Serverless collection does not support `_refresh`, which only the tests' `SearchIndex::refresh` uses. ### Related issue N/A. Closest existing work: #9838 (the OpenSearch index this client writes to) and #9862. No duplicate found. ### Testing - Unit tests: startup validation of the new settings; credentials are loaded once, survive a failed reload, a failed first load is not repeated per request, requests waiting on a stalled load share its one failure, and a key rotated in a shared profile file is picked up. - Local, unauthenticated OpenSearch 2.19.3: the `postgres_opensearch_search` suite, plus index create, bulk write, search, bulk delete and index delete through `SearchIndex`. - Verified against an Amazon OpenSearch Serverless collection through `SearchIndex` with `aoss` signing and credentials from a shared profile: alias check, index create, bulk write of documents with `community:event_id` ids, search (3 hits), bulk delete of one document, search (2 hits), index delete. On the same collection an unsigned client got HTTP 403 for search and bulk write, and a client signing for `es` got HTTP 403. - The relay binary refuses to start with signing and no region, and with signing plus basic auth. - Not run: a managed domain (`es` signing), and the web identity, container and instance role credential sources. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Salman Mohammed <smohammed@squareup.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -306,7 +306,8 @@ pub struct Config {
|
||||
pub storage_usage_delivery_url: Option<url::Url>,
|
||||
|
||||
/// OpenSearch cluster for message search (`BUZZ_OPENSEARCH_URL`, with
|
||||
/// optional `BUZZ_OPENSEARCH_USERNAME` and `BUZZ_OPENSEARCH_PASSWORD`).
|
||||
/// optional `BUZZ_OPENSEARCH_USERNAME` and `BUZZ_OPENSEARCH_PASSWORD`, or
|
||||
/// `BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE` and `BUZZ_OPENSEARCH_AWS_REGION`).
|
||||
/// When set, admitted messages are queued in the search outbox and the
|
||||
/// indexer keeps the index up to date; searches still use Postgres.
|
||||
pub opensearch: Option<buzz_search::opensearch::OpenSearchConfig>,
|
||||
@@ -1042,6 +1043,17 @@ impl Config {
|
||||
parse_bool("BUZZ_OPENSEARCH_PREVIEW", false)?,
|
||||
)
|
||||
.map_err(ConfigError::InvalidValue)?;
|
||||
let aws_region = match optional_utf8_env("AWS_REGION")? {
|
||||
Some(region) if !region.trim().is_empty() => Some(region),
|
||||
_ => optional_utf8_env("AWS_DEFAULT_REGION")?,
|
||||
};
|
||||
let opensearch = with_opensearch_aws_sigv4(
|
||||
opensearch,
|
||||
optional_utf8_env("BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE")?,
|
||||
optional_utf8_env("BUZZ_OPENSEARCH_AWS_REGION")?,
|
||||
aws_region,
|
||||
)
|
||||
.map_err(ConfigError::InvalidValue)?;
|
||||
let search_indexer_workers =
|
||||
parse_search_indexer_workers(optional_utf8_env("BUZZ_SEARCH_INDEXER_WORKERS")?)
|
||||
.map_err(ConfigError::InvalidValue)?;
|
||||
@@ -1734,10 +1746,55 @@ fn parse_opensearch_config(
|
||||
Ok(Some(buzz_search::opensearch::OpenSearchConfig {
|
||||
url: parsed.to_string(),
|
||||
basic_auth,
|
||||
aws_sigv4: None,
|
||||
index_prefix: buzz_search::opensearch::DEFAULT_INDEX_PREFIX.to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Add AWS SigV4 request signing to the OpenSearch settings.
|
||||
/// `BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE` turns it on and names the signing
|
||||
/// service: `es` for a managed domain, `aoss` for a Serverless collection.
|
||||
/// The region is `BUZZ_OPENSEARCH_AWS_REGION`, or else the AWS environment's
|
||||
/// own (`aws_region`). Empty values count as unset.
|
||||
///
|
||||
/// Signing replaces basic auth, so the two together are refused, as is
|
||||
/// signing without a region.
|
||||
fn with_opensearch_aws_sigv4(
|
||||
opensearch: Option<buzz_search::opensearch::OpenSearchConfig>,
|
||||
service: Option<String>,
|
||||
region: Option<String>,
|
||||
aws_region: Option<String>,
|
||||
) -> Result<Option<buzz_search::opensearch::OpenSearchConfig>, String> {
|
||||
let set = |value: Option<String>| {
|
||||
value
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
};
|
||||
let (service, region) = (set(service), set(region));
|
||||
let Some(service) = service else {
|
||||
if region.is_some() {
|
||||
return Err(
|
||||
"BUZZ_OPENSEARCH_AWS_REGION needs BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE".into(),
|
||||
);
|
||||
}
|
||||
return Ok(opensearch);
|
||||
};
|
||||
let Some(mut opensearch) = opensearch else {
|
||||
return Err("BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE needs BUZZ_OPENSEARCH_URL".into());
|
||||
};
|
||||
if !matches!(service.as_str(), "es" | "aoss") {
|
||||
return Err("BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE must be es (a managed domain) or aoss (a Serverless collection)".into());
|
||||
}
|
||||
if opensearch.basic_auth.is_some() {
|
||||
return Err("set BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE or BUZZ_OPENSEARCH_USERNAME and BUZZ_OPENSEARCH_PASSWORD, not both".into());
|
||||
}
|
||||
let Some(region) = region.or(set(aws_region)) else {
|
||||
return Err("BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE needs a region: set BUZZ_OPENSEARCH_AWS_REGION or AWS_REGION".into());
|
||||
};
|
||||
opensearch.aws_sigv4 = Some(buzz_search::opensearch::AwsSigV4 { service, region });
|
||||
Ok(Some(opensearch))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1811,6 +1868,73 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opensearch_aws_sigv4_is_off_by_default_and_validated() {
|
||||
let some = |value: &str| Some(value.to_string());
|
||||
let cluster = |username: Option<String>, password: Option<String>| {
|
||||
parse_opensearch_config(some("https://search.example.org"), username, password, true)
|
||||
.unwrap()
|
||||
};
|
||||
let signing = |config: Option<buzz_search::opensearch::OpenSearchConfig>| {
|
||||
config.and_then(|config| config.aws_sigv4)
|
||||
};
|
||||
|
||||
let unsigned =
|
||||
with_opensearch_aws_sigv4(cluster(None, None), None, some(" "), some("us-east-1"));
|
||||
assert_eq!(signing(unsigned.unwrap()), None);
|
||||
let basic = with_opensearch_aws_sigv4(
|
||||
cluster(some("relay"), some("secret")),
|
||||
None,
|
||||
None,
|
||||
some("us-east-1"),
|
||||
);
|
||||
assert!(basic.unwrap().unwrap().basic_auth.is_some());
|
||||
|
||||
for (service, region, aws_region, expected) in [
|
||||
("aoss", some("eu-west-1"), some("us-east-1"), "eu-west-1"),
|
||||
("es", None, some("us-east-1"), "us-east-1"),
|
||||
] {
|
||||
let signed =
|
||||
with_opensearch_aws_sigv4(cluster(None, None), some(service), region, aws_region);
|
||||
let signed = signing(signed.unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
(signed.service.as_str(), signed.region.as_str()),
|
||||
(service, expected)
|
||||
);
|
||||
}
|
||||
|
||||
for (opensearch, service, region, aws_region, why) in [
|
||||
(cluster(None, None), some("aoss"), None, None, "no region"),
|
||||
(
|
||||
cluster(None, None),
|
||||
some("s3"),
|
||||
some("us-east-1"),
|
||||
None,
|
||||
"unknown service",
|
||||
),
|
||||
(
|
||||
cluster(some("relay"), some("secret")),
|
||||
some("aoss"),
|
||||
some("us-east-1"),
|
||||
None,
|
||||
"with basic auth",
|
||||
),
|
||||
(None, some("aoss"), some("us-east-1"), None, "no URL"),
|
||||
(
|
||||
cluster(None, None),
|
||||
None,
|
||||
some("us-east-1"),
|
||||
None,
|
||||
"region without service",
|
||||
),
|
||||
] {
|
||||
assert!(
|
||||
with_opensearch_aws_sigv4(opensearch, service, region, aws_region).is_err(),
|
||||
"{why}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn klipy_config_debug_redacts_the_api_key() {
|
||||
let config = KlipyConfig {
|
||||
|
||||
Reference in New Issue
Block a user