From 3d937d801d91b8fca206d552a52004eabb83b67a Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:22:11 -0500 Subject: [PATCH 1/6] check whether the region is in China and use the appropriated domain --- bottlecap/src/secrets/decrypt.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bottlecap/src/secrets/decrypt.rs b/bottlecap/src/secrets/decrypt.rs index 25bf9292..36442f16 100644 --- a/bottlecap/src/secrets/decrypt.rs +++ b/bottlecap/src/secrets/decrypt.rs @@ -170,10 +170,14 @@ fn build_get_secret_signed_headers( ) -> Result> { let amz_date = header_values.time.format("%Y%m%dT%H%M%SZ").to_string(); let date_stamp = header_values.time.format("%Y%m%d").to_string(); - let host = format!( - "{}.{}.amazonaws.com", - header_values.service, aws_config.region - ); + + let domain = if aws_config.region.starts_with("cn-") { + "amazonaws.com.cn" + } else { + "amazonaws.com" + }; + + let host = format!("{}.{}.{}", header_values.service, aws_config.region, domain); let canonical_uri = "/"; let canonical_querystring = ""; From d044b415e238a25806674e8ff86f51e17ba28529 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:31:01 -0500 Subject: [PATCH 2/6] correct arn for lambda in chinese regions --- bottlecap/src/bin/bottlecap/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bottlecap/src/bin/bottlecap/main.rs b/bottlecap/src/bin/bottlecap/main.rs index 1123a943..95905a59 100644 --- a/bottlecap/src/bin/bottlecap/main.rs +++ b/bottlecap/src/bin/bottlecap/main.rs @@ -156,7 +156,13 @@ async fn register(client: &reqwest::Client) -> Result { } fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> String { - format!("arn:aws:lambda:{region}:{account_id}:function:{function_name}") + let arn_prefix = if region.starts_with("cn-") { + "aws-ch" + } else { + "aws" + }; + + format!("arn:{arn_prefix}:lambda:{region}:{account_id}:function:{function_name}") } #[tokio::main] From 8f51082a8231c76b44b464a3446ab5ca3fe6fb2a Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Tue, 14 Jan 2025 12:01:43 -0500 Subject: [PATCH 3/6] fix: typo in china arn --- bottlecap/src/bin/bottlecap/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bottlecap/src/bin/bottlecap/main.rs b/bottlecap/src/bin/bottlecap/main.rs index 95905a59..29e66f10 100644 --- a/bottlecap/src/bin/bottlecap/main.rs +++ b/bottlecap/src/bin/bottlecap/main.rs @@ -157,7 +157,7 @@ async fn register(client: &reqwest::Client) -> Result { fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> String { let arn_prefix = if region.starts_with("cn-") { - "aws-ch" + "aws-cn" } else { "aws" }; From 22ab29962e55ea582b6b03e0da9ca0a9ac3c9ca6 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:10:14 -0500 Subject: [PATCH 4/6] fix: reuse function to detect right aws partition and support gov too --- bottlecap/src/bin/bottlecap/main.rs | 10 +++------- bottlecap/src/config/mod.rs | 9 +++++++++ .../invocation/triggers/api_gateway_http_event.rs | 13 +++++-------- .../invocation/triggers/api_gateway_rest_event.rs | 13 +++++-------- bottlecap/src/lifecycle/invocation/triggers/mod.rs | 9 --------- .../src/lifecycle/invocation/triggers/sqs_event.rs | 13 ++++++------- 6 files changed, 28 insertions(+), 39 deletions(-) diff --git a/bottlecap/src/bin/bottlecap/main.rs b/bottlecap/src/bin/bottlecap/main.rs index 29e66f10..d4d00eff 100644 --- a/bottlecap/src/bin/bottlecap/main.rs +++ b/bottlecap/src/bin/bottlecap/main.rs @@ -9,6 +9,7 @@ #![deny(missing_copy_implementations)] #![deny(missing_debug_implementations)] +use bottlecap::config::get_aws_partition_by_region; use bottlecap::{ base_url, config::{self, flush_strategy::FlushStrategy, AwsConfig, Config}, @@ -156,13 +157,8 @@ async fn register(client: &reqwest::Client) -> Result { } fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> String { - let arn_prefix = if region.starts_with("cn-") { - "aws-cn" - } else { - "aws" - }; - - format!("arn:{arn_prefix}:lambda:{region}:{account_id}:function:{function_name}") + let aws_partition = get_aws_partition_by_region(region); + format!("arn:{aws_partition}:lambda:{region}:{account_id}:function:{function_name}") } #[tokio::main] diff --git a/bottlecap/src/config/mod.rs b/bottlecap/src/config/mod.rs index e7775e26..598ba1e2 100644 --- a/bottlecap/src/config/mod.rs +++ b/bottlecap/src/config/mod.rs @@ -311,6 +311,15 @@ pub struct AwsConfig { pub sandbox_init_time: Instant, } +#[must_use] +pub fn get_aws_partition_by_region(region: &str) -> String { + match region { + r if r.starts_with("us-gov-") => "aws-us-gov".to_string(), + r if r.starts_with("cn-") => "aws-cn".to_string(), + _ => "aws".to_string(), + } +} + #[cfg(test)] pub mod tests { use super::*; diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs index 69673cc2..349d8866 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs @@ -1,17 +1,14 @@ +use crate::config::get_aws_partition_by_region; +use crate::lifecycle::invocation::{ + processor::MS_TO_NS, + triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, +}; use datadog_trace_protobuf::pb::Span; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; use tracing::debug; -use crate::lifecycle::invocation::{ - processor::MS_TO_NS, - triggers::{ - get_aws_partition_by_region, lowercase_key, ServiceNameResolver, Trigger, - FUNCTION_TRIGGER_EVENT_SOURCE_TAG, - }, -}; - #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayHttpEvent { #[serde(rename = "routeKey")] diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs index 5e725163..191de03c 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs @@ -1,17 +1,14 @@ +use crate::config::get_aws_partition_by_region; +use crate::lifecycle::invocation::{ + processor::MS_TO_NS, + triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, +}; use datadog_trace_protobuf::pb::Span; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; use tracing::debug; -use crate::lifecycle::invocation::{ - processor::MS_TO_NS, - triggers::{ - get_aws_partition_by_region, lowercase_key, ServiceNameResolver, Trigger, - FUNCTION_TRIGGER_EVENT_SOURCE_TAG, - }, -}; - #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayRestEvent { #[serde(deserialize_with = "lowercase_key")] diff --git a/bottlecap/src/lifecycle/invocation/triggers/mod.rs b/bottlecap/src/lifecycle/invocation/triggers/mod.rs index c089aff7..afb87528 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/mod.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/mod.rs @@ -57,15 +57,6 @@ pub trait Trigger: ServiceNameResolver { } } -#[must_use] -pub fn get_aws_partition_by_region(region: &str) -> String { - match region { - r if r.starts_with("us-gov-") => "aws-us-gov".to_string(), - r if r.starts_with("cn-") => "aws-cn".to_string(), - _ => "aws".to_string(), - } -} - /// Serialize a `HashMap` with lowercase keys /// pub fn lowercase_key<'de, D, V>(deserializer: D) -> Result, D::Error> diff --git a/bottlecap/src/lifecycle/invocation/triggers/sqs_event.rs b/bottlecap/src/lifecycle/invocation/triggers/sqs_event.rs index a4bf0e44..aa0271cc 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/sqs_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/sqs_event.rs @@ -1,19 +1,18 @@ -use datadog_trace_protobuf::pb::Span; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; -use tracing::debug; - +use crate::config::get_aws_partition_by_region; use crate::lifecycle::invocation::{ processor::MS_TO_NS, triggers::{ event_bridge_event::EventBridgeEvent, - get_aws_partition_by_region, sns_event::{SnsEntity, SnsRecord}, ServiceNameResolver, Trigger, DATADOG_CARRIER_KEY, FUNCTION_TRIGGER_EVENT_SOURCE_TAG, }, }; use crate::traces::context::{Sampling, SpanContext}; +use datadog_trace_protobuf::pb::Span; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use tracing::debug; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct SqsEvent { From 4e6ac316d512c8eb622e10761c9a4181c4898828 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:21:53 -0500 Subject: [PATCH 5/6] nest and rearrange imports --- bottlecap/src/bin/bottlecap/main.rs | 23 ++++++++++++------- .../triggers/api_gateway_http_event.rs | 10 ++++---- .../triggers/api_gateway_rest_event.rs | 10 ++++---- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/bottlecap/src/bin/bottlecap/main.rs b/bottlecap/src/bin/bottlecap/main.rs index d4d00eff..9eb44fa0 100644 --- a/bottlecap/src/bin/bottlecap/main.rs +++ b/bottlecap/src/bin/bottlecap/main.rs @@ -9,10 +9,15 @@ #![deny(missing_copy_implementations)] #![deny(missing_debug_implementations)] -use bottlecap::config::get_aws_partition_by_region; use bottlecap::{ + config::{ + get_aws_partition_by_region, + self, + flush_strategy::FlushStrategy, + AwsConfig, + Config + }, base_url, - config::{self, flush_strategy::FlushStrategy, AwsConfig, Config}, event_bus::bus::EventBus, events::Event, lifecycle::{ @@ -44,17 +49,17 @@ use bottlecap::{ }; use datadog_trace_obfuscation::obfuscation_config; use decrypt::resolve_secrets; -use dogstatsd::metric::{SortedTags, EMPTY_TAGS}; use dogstatsd::{ + metric::{SortedTags, EMPTY_TAGS}, aggregator::Aggregator as MetricsAggregator, constants::CONTEXTS, dogstatsd::{DogStatsD, DogStatsDConfig}, - flusher::{build_fqdn_metrics, Flusher as MetricsFlusher}, + flusher::{build_fqdn_metrics, Flusher as MetricsFlusher} }; use reqwest::Client; use serde::Deserialize; -use std::time::Duration; use std::{ + time::Duration, collections::{hash_map, HashMap}, env, io::{Error, Result}, @@ -62,11 +67,13 @@ use std::{ path::Path, process::Command, sync::{Arc, Mutex}, - time::Instant, + time::Instant }; use telemetry::listener::TelemetryListenerConfig; -use tokio::sync::mpsc::Sender; -use tokio::sync::Mutex as TokioMutex; +use tokio::{ + sync::mpsc::Sender, + sync::Mutex as TokioMutex +}; use tokio_util::sync::CancellationToken; use tracing::{debug, error}; use tracing_subscriber::EnvFilter; diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs index 349d8866..423906d4 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs @@ -1,13 +1,13 @@ -use crate::config::get_aws_partition_by_region; -use crate::lifecycle::invocation::{ - processor::MS_TO_NS, - triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, -}; use datadog_trace_protobuf::pb::Span; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; use tracing::debug; +use crate::config::get_aws_partition_by_region; +use crate::lifecycle::invocation::{ + processor::MS_TO_NS, + triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, +}; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayHttpEvent { diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs index 191de03c..68c21d3f 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs @@ -1,13 +1,13 @@ -use crate::config::get_aws_partition_by_region; -use crate::lifecycle::invocation::{ - processor::MS_TO_NS, - triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, -}; use datadog_trace_protobuf::pb::Span; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; use tracing::debug; +use crate::config::get_aws_partition_by_region; +use crate::lifecycle::invocation::{ + processor::MS_TO_NS, + triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, +}; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayRestEvent { From c10d3f321fd0df5a0412a64fbe97c841117382f8 Mon Sep 17 00:00:00 2001 From: alexgallotta <5581237+alexgallotta@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:26:21 -0500 Subject: [PATCH 6/6] fix imports again --- bottlecap/src/bin/bottlecap/main.rs | 21 ++++++------------- .../triggers/api_gateway_http_event.rs | 10 ++++----- .../triggers/api_gateway_rest_event.rs | 10 ++++----- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/bottlecap/src/bin/bottlecap/main.rs b/bottlecap/src/bin/bottlecap/main.rs index 9eb44fa0..91d399a6 100644 --- a/bottlecap/src/bin/bottlecap/main.rs +++ b/bottlecap/src/bin/bottlecap/main.rs @@ -10,14 +10,8 @@ #![deny(missing_debug_implementations)] use bottlecap::{ - config::{ - get_aws_partition_by_region, - self, - flush_strategy::FlushStrategy, - AwsConfig, - Config - }, base_url, + config::{self, flush_strategy::FlushStrategy, get_aws_partition_by_region, AwsConfig, Config}, event_bus::bus::EventBus, events::Event, lifecycle::{ @@ -50,16 +44,15 @@ use bottlecap::{ use datadog_trace_obfuscation::obfuscation_config; use decrypt::resolve_secrets; use dogstatsd::{ - metric::{SortedTags, EMPTY_TAGS}, aggregator::Aggregator as MetricsAggregator, constants::CONTEXTS, dogstatsd::{DogStatsD, DogStatsDConfig}, - flusher::{build_fqdn_metrics, Flusher as MetricsFlusher} + flusher::{build_fqdn_metrics, Flusher as MetricsFlusher}, + metric::{SortedTags, EMPTY_TAGS}, }; use reqwest::Client; use serde::Deserialize; use std::{ - time::Duration, collections::{hash_map, HashMap}, env, io::{Error, Result}, @@ -67,13 +60,11 @@ use std::{ path::Path, process::Command, sync::{Arc, Mutex}, - time::Instant + time::Duration, + time::Instant, }; use telemetry::listener::TelemetryListenerConfig; -use tokio::{ - sync::mpsc::Sender, - sync::Mutex as TokioMutex -}; +use tokio::{sync::mpsc::Sender, sync::Mutex as TokioMutex}; use tokio_util::sync::CancellationToken; use tracing::{debug, error}; use tracing_subscriber::EnvFilter; diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs index 423906d4..349d8866 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs @@ -1,13 +1,13 @@ -use datadog_trace_protobuf::pb::Span; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; -use tracing::debug; use crate::config::get_aws_partition_by_region; use crate::lifecycle::invocation::{ processor::MS_TO_NS, triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, }; +use datadog_trace_protobuf::pb::Span; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use tracing::debug; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayHttpEvent { diff --git a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs index 68c21d3f..191de03c 100644 --- a/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs +++ b/bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs @@ -1,13 +1,13 @@ -use datadog_trace_protobuf::pb::Span; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; -use tracing::debug; use crate::config::get_aws_partition_by_region; use crate::lifecycle::invocation::{ processor::MS_TO_NS, triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG}, }; +use datadog_trace_protobuf::pb::Span; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use tracing::debug; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct APIGatewayRestEvent {