Skip to content

Commit 63c6515

Browse files
authored
fix: set right domain and arn by region on secrets manager (#511)
* check whether the region is in China and use the appropriated domain * correct arn for lambda in chinese regions * fix: typo in china arn * fix: reuse function to detect right aws partition and support gov too * nest and rearrange imports * fix imports again
1 parent 30248ca commit 63c6515

7 files changed

Lines changed: 39 additions & 42 deletions

File tree

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use bottlecap::{
1313
base_url,
14-
config::{self, flush_strategy::FlushStrategy, AwsConfig, Config},
14+
config::{self, flush_strategy::FlushStrategy, get_aws_partition_by_region, AwsConfig, Config},
1515
event_bus::bus::EventBus,
1616
events::Event,
1717
lifecycle::{
@@ -43,16 +43,15 @@ use bottlecap::{
4343
};
4444
use datadog_trace_obfuscation::obfuscation_config;
4545
use decrypt::resolve_secrets;
46-
use dogstatsd::metric::{SortedTags, EMPTY_TAGS};
4746
use dogstatsd::{
4847
aggregator::Aggregator as MetricsAggregator,
4948
constants::CONTEXTS,
5049
dogstatsd::{DogStatsD, DogStatsDConfig},
5150
flusher::{build_fqdn_metrics, Flusher as MetricsFlusher},
51+
metric::{SortedTags, EMPTY_TAGS},
5252
};
5353
use reqwest::Client;
5454
use serde::Deserialize;
55-
use std::time::Duration;
5655
use std::{
5756
collections::{hash_map, HashMap},
5857
env,
@@ -61,11 +60,11 @@ use std::{
6160
path::Path,
6261
process::Command,
6362
sync::{Arc, Mutex},
63+
time::Duration,
6464
time::Instant,
6565
};
6666
use telemetry::listener::TelemetryListenerConfig;
67-
use tokio::sync::mpsc::Sender;
68-
use tokio::sync::Mutex as TokioMutex;
67+
use tokio::{sync::mpsc::Sender, sync::Mutex as TokioMutex};
6968
use tokio_util::sync::CancellationToken;
7069
use tracing::{debug, error};
7170
use tracing_subscriber::EnvFilter;
@@ -156,7 +155,8 @@ async fn register(client: &reqwest::Client) -> Result<RegisterResponse> {
156155
}
157156

158157
fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> String {
159-
format!("arn:aws:lambda:{region}:{account_id}:function:{function_name}")
158+
let aws_partition = get_aws_partition_by_region(region);
159+
format!("arn:{aws_partition}:lambda:{region}:{account_id}:function:{function_name}")
160160
}
161161

162162
#[tokio::main]

bottlecap/src/config/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ pub struct AwsConfig {
311311
pub sandbox_init_time: Instant,
312312
}
313313

314+
#[must_use]
315+
pub fn get_aws_partition_by_region(region: &str) -> String {
316+
match region {
317+
r if r.starts_with("us-gov-") => "aws-us-gov".to_string(),
318+
r if r.starts_with("cn-") => "aws-cn".to_string(),
319+
_ => "aws".to_string(),
320+
}
321+
}
322+
314323
#[cfg(test)]
315324
pub mod tests {
316325
use super::*;

bottlecap/src/lifecycle/invocation/triggers/api_gateway_http_event.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
use crate::config::get_aws_partition_by_region;
2+
use crate::lifecycle::invocation::{
3+
processor::MS_TO_NS,
4+
triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG},
5+
};
16
use datadog_trace_protobuf::pb::Span;
27
use serde::{Deserialize, Serialize};
38
use serde_json::Value;
49
use std::collections::HashMap;
510
use tracing::debug;
611

7-
use crate::lifecycle::invocation::{
8-
processor::MS_TO_NS,
9-
triggers::{
10-
get_aws_partition_by_region, lowercase_key, ServiceNameResolver, Trigger,
11-
FUNCTION_TRIGGER_EVENT_SOURCE_TAG,
12-
},
13-
};
14-
1512
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1613
pub struct APIGatewayHttpEvent {
1714
#[serde(rename = "routeKey")]

bottlecap/src/lifecycle/invocation/triggers/api_gateway_rest_event.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
use crate::config::get_aws_partition_by_region;
2+
use crate::lifecycle::invocation::{
3+
processor::MS_TO_NS,
4+
triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG},
5+
};
16
use datadog_trace_protobuf::pb::Span;
27
use serde::{Deserialize, Serialize};
38
use serde_json::Value;
49
use std::collections::HashMap;
510
use tracing::debug;
611

7-
use crate::lifecycle::invocation::{
8-
processor::MS_TO_NS,
9-
triggers::{
10-
get_aws_partition_by_region, lowercase_key, ServiceNameResolver, Trigger,
11-
FUNCTION_TRIGGER_EVENT_SOURCE_TAG,
12-
},
13-
};
14-
1512
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1613
pub struct APIGatewayRestEvent {
1714
#[serde(deserialize_with = "lowercase_key")]

bottlecap/src/lifecycle/invocation/triggers/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ pub trait Trigger: ServiceNameResolver {
5757
}
5858
}
5959

60-
#[must_use]
61-
pub fn get_aws_partition_by_region(region: &str) -> String {
62-
match region {
63-
r if r.starts_with("us-gov-") => "aws-us-gov".to_string(),
64-
r if r.starts_with("cn-") => "aws-cn".to_string(),
65-
_ => "aws".to_string(),
66-
}
67-
}
68-
6960
/// Serialize a `HashMap` with lowercase keys
7061
///
7162
pub fn lowercase_key<'de, D, V>(deserializer: D) -> Result<HashMap<String, V>, D::Error>

bottlecap/src/lifecycle/invocation/triggers/sqs_event.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
use datadog_trace_protobuf::pb::Span;
2-
use serde::{Deserialize, Serialize};
3-
use serde_json::Value;
4-
use std::collections::HashMap;
5-
use tracing::debug;
6-
1+
use crate::config::get_aws_partition_by_region;
72
use crate::lifecycle::invocation::{
83
processor::MS_TO_NS,
94
triggers::{
105
event_bridge_event::EventBridgeEvent,
11-
get_aws_partition_by_region,
126
sns_event::{SnsEntity, SnsRecord},
137
ServiceNameResolver, Trigger, DATADOG_CARRIER_KEY, FUNCTION_TRIGGER_EVENT_SOURCE_TAG,
148
},
159
};
1610
use crate::traces::context::{Sampling, SpanContext};
11+
use datadog_trace_protobuf::pb::Span;
12+
use serde::{Deserialize, Serialize};
13+
use serde_json::Value;
14+
use std::collections::HashMap;
15+
use tracing::debug;
1716

1817
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
1918
pub struct SqsEvent {

bottlecap/src/secrets/decrypt.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ fn build_get_secret_signed_headers(
170170
) -> Result<HeaderMap, Box<dyn std::error::Error>> {
171171
let amz_date = header_values.time.format("%Y%m%dT%H%M%SZ").to_string();
172172
let date_stamp = header_values.time.format("%Y%m%d").to_string();
173-
let host = format!(
174-
"{}.{}.amazonaws.com",
175-
header_values.service, aws_config.region
176-
);
173+
174+
let domain = if aws_config.region.starts_with("cn-") {
175+
"amazonaws.com.cn"
176+
} else {
177+
"amazonaws.com"
178+
};
179+
180+
let host = format!("{}.{}.{}", header_values.service, aws_config.region, domain);
177181

178182
let canonical_uri = "/";
179183
let canonical_querystring = "";

0 commit comments

Comments
 (0)