Skip to content

Commit 3b10c17

Browse files
authored
chore(config): create aws module (#659)
# What? Refactors methods related to AWS config into its own module # Motivation Just cleaning and removing stuff from main – [SVLS-6686](https://datadoghq.atlassian.net/browse/SVLS-6686) [SVLS-6686]: https://datadoghq.atlassian.net/browse/SVLS-6686?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 438b91b commit 3b10c17

File tree

11 files changed

+93
-70
lines changed

11 files changed

+93
-70
lines changed

bottlecap/src/bin/bottlecap/main.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
use bottlecap::lwa::proxy::start_lwa_proxy;
1313
use bottlecap::{
1414
base_url,
15-
config::{self, get_aws_partition_by_region, AwsConfig, Config},
15+
config::{
16+
self,
17+
aws::{build_lambda_function_arn, AwsConfig},
18+
Config,
19+
},
1620
event_bus::bus::EventBus,
1721
events::Event,
1822
lifecycle::{
@@ -179,11 +183,6 @@ async fn register(client: &Client) -> Result<RegisterResponse> {
179183
Ok(register_response)
180184
}
181185

182-
fn build_function_arn(account_id: &str, region: &str, function_name: &str) -> String {
183-
let aws_partition = get_aws_partition_by_region(region);
184-
format!("arn:{aws_partition}:lambda:{region}:{account_id}:function:{function_name}")
185-
}
186-
187186
#[tokio::main]
188187
async fn main() -> Result<()> {
189188
let start_time = Instant::now();
@@ -232,20 +231,10 @@ async fn main() -> Result<()> {
232231
}
233232

234233
fn load_configs(start_time: Instant) -> (AwsConfig, Arc<Config>) {
235-
// First load the configuration
236-
let aws_config = AwsConfig {
237-
region: env::var("AWS_DEFAULT_REGION").unwrap_or("us-east-1".to_string()),
238-
aws_access_key_id: env::var("AWS_ACCESS_KEY_ID").unwrap_or_default(),
239-
aws_secret_access_key: env::var("AWS_SECRET_ACCESS_KEY").unwrap_or_default(),
240-
aws_session_token: env::var("AWS_SESSION_TOKEN").unwrap_or_default(),
241-
aws_container_credentials_full_uri: env::var("AWS_CONTAINER_CREDENTIALS_FULL_URI")
242-
.unwrap_or_default(),
243-
aws_container_authorization_token: env::var("AWS_CONTAINER_AUTHORIZATION_TOKEN")
244-
.unwrap_or_default(),
245-
function_name: env::var("AWS_LAMBDA_FUNCTION_NAME").unwrap_or_default(),
246-
sandbox_init_time: start_time,
247-
};
248-
let lambda_directory = env::var("LAMBDA_TASK_ROOT").unwrap_or_else(|_| "/var/task".to_string());
234+
// First load the AWS configuration
235+
let aws_config = AwsConfig::from_env(start_time);
236+
let lambda_directory: String =
237+
env::var("LAMBDA_TASK_ROOT").unwrap_or_else(|_| "/var/task".to_string());
249238
let config = match config::get_config(Path::new(&lambda_directory), &aws_config.region) {
250239
Ok(config) => Arc::new(config),
251240
Err(_e) => {
@@ -630,7 +619,7 @@ fn setup_tag_provider(
630619
account_id: &str,
631620
) -> Arc<TagProvider> {
632621
let function_arn =
633-
build_function_arn(account_id, &aws_config.region, &aws_config.function_name);
622+
build_lambda_function_arn(account_id, &aws_config.region, &aws_config.function_name);
634623
let metadata_hash = hash_map::HashMap::from([(
635624
lambda::tags::FUNCTION_ARN_KEY.to_string(),
636625
function_arn.clone(),

bottlecap/src/config/aws.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::{env, time::Instant};
2+
3+
const AWS_DEFAULT_REGION: &str = "AWS_DEFAULT_REGION";
4+
const AWS_ACCESS_KEY_ID: &str = "AWS_ACCESS_KEY_ID";
5+
const AWS_SECRET_ACCESS_KEY: &str = "AWS_SECRET_ACCESS_KEY";
6+
const AWS_SESSION_TOKEN: &str = "AWS_SESSION_TOKEN";
7+
const AWS_CONTAINER_CREDENTIALS_FULL_URI: &str = "AWS_CONTAINER_CREDENTIALS_FULL_URI";
8+
const AWS_CONTAINER_AUTHORIZATION_TOKEN: &str = "AWS_CONTAINER_AUTHORIZATION_TOKEN";
9+
const AWS_LAMBDA_FUNCTION_NAME: &str = "AWS_LAMBDA_FUNCTION_NAME";
10+
11+
#[allow(clippy::module_name_repetitions)]
12+
#[derive(Debug, Clone)]
13+
pub struct AwsConfig {
14+
pub region: String,
15+
pub aws_access_key_id: String,
16+
pub aws_secret_access_key: String,
17+
pub aws_session_token: String,
18+
pub function_name: String,
19+
pub sandbox_init_time: Instant,
20+
pub aws_container_credentials_full_uri: String,
21+
pub aws_container_authorization_token: String,
22+
}
23+
24+
impl AwsConfig {
25+
#[must_use]
26+
pub fn from_env(start_time: Instant) -> Self {
27+
Self {
28+
region: env::var(AWS_DEFAULT_REGION).unwrap_or("us-east-1".to_string()),
29+
aws_access_key_id: env::var(AWS_ACCESS_KEY_ID).unwrap_or_default(),
30+
aws_secret_access_key: env::var(AWS_SECRET_ACCESS_KEY).unwrap_or_default(),
31+
aws_session_token: env::var(AWS_SESSION_TOKEN).unwrap_or_default(),
32+
aws_container_credentials_full_uri: env::var(AWS_CONTAINER_CREDENTIALS_FULL_URI)
33+
.unwrap_or_default(),
34+
aws_container_authorization_token: env::var(AWS_CONTAINER_AUTHORIZATION_TOKEN)
35+
.unwrap_or_default(),
36+
function_name: env::var(AWS_LAMBDA_FUNCTION_NAME).unwrap_or_default(),
37+
sandbox_init_time: start_time,
38+
}
39+
}
40+
}
41+
42+
#[must_use]
43+
pub fn get_aws_partition_by_region(region: &str) -> String {
44+
match region {
45+
r if r.starts_with("us-gov-") => "aws-us-gov".to_string(),
46+
r if r.starts_with("cn-") => "aws-cn".to_string(),
47+
_ => "aws".to_string(),
48+
}
49+
}
50+
51+
#[must_use]
52+
pub fn build_lambda_function_arn(account_id: &str, region: &str, function_name: &str) -> String {
53+
let aws_partition = get_aws_partition_by_region(region);
54+
format!("arn:{aws_partition}:lambda:{region}:{account_id}:function:{function_name}")
55+
}

bottlecap/src/config/mod.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod apm_replace_rule;
2+
pub mod aws;
23
pub mod env;
34
pub mod flush_strategy;
45
pub mod log_level;
@@ -9,7 +10,6 @@ pub mod yaml;
910

1011
use datadog_trace_utils::config_utils::{trace_intake_url, trace_intake_url_prefixed};
1112
use std::path::Path;
12-
use std::time::Instant;
1313

1414
use figment::providers::{Format, Yaml};
1515
use figment::{providers::Env, Figment};
@@ -271,28 +271,6 @@ fn build_fqdn_logs(site: String) -> String {
271271
format!("https://http-intake.logs.{site}")
272272
}
273273

274-
#[allow(clippy::module_name_repetitions)]
275-
#[derive(Debug, Clone)]
276-
pub struct AwsConfig {
277-
pub region: String,
278-
pub aws_access_key_id: String,
279-
pub aws_secret_access_key: String,
280-
pub aws_session_token: String,
281-
pub function_name: String,
282-
pub sandbox_init_time: Instant,
283-
pub aws_container_credentials_full_uri: String,
284-
pub aws_container_authorization_token: String,
285-
}
286-
287-
#[must_use]
288-
pub fn get_aws_partition_by_region(region: &str) -> String {
289-
match region {
290-
r if r.starts_with("us-gov-") => "aws-us-gov".to_string(),
291-
r if r.starts_with("cn-") => "aws-cn".to_string(),
292-
_ => "aws".to_string(),
293-
}
294-
}
295-
296274
#[cfg(test)]
297275
pub mod tests {
298276
use datadog_trace_obfuscation::replacer::parse_rules_from_string;

bottlecap/src/lifecycle/invocation/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::sync::{mpsc::Sender, watch};
1313
use tracing::{debug, warn};
1414

1515
use crate::{
16-
config::{self, AwsConfig},
16+
config::{self, aws::AwsConfig},
1717
lifecycle::invocation::{
1818
base64_to_string, context::Context, context::ContextBuffer, context::ReparentingInfo,
1919
create_empty_span, generate_span_id, get_metadata_from_value, span_inferrer::SpanInferrer,

bottlecap/src/lifecycle/invocation/span_inferrer.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@ use datadog_trace_protobuf::pb::Span;
44
use serde_json::Value;
55
use tracing::debug;
66

7-
use crate::config::AwsConfig;
8-
9-
use crate::lifecycle::invocation::{
10-
generate_span_id,
11-
triggers::{
12-
api_gateway_http_event::APIGatewayHttpEvent,
13-
api_gateway_rest_event::APIGatewayRestEvent,
14-
api_gateway_websocket_event::APIGatewayWebSocketEvent,
15-
dynamodb_event::DynamoDbRecord,
16-
event_bridge_event::EventBridgeEvent,
17-
kinesis_event::KinesisRecord,
18-
lambda_function_url_event::LambdaFunctionUrlEvent,
19-
s3_event::S3Record,
20-
sns_event::{SnsEntity, SnsRecord},
21-
sqs_event::{extract_trace_context_from_aws_trace_header, SqsRecord},
22-
step_function_event::StepFunctionEvent,
23-
Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_ARN_TAG,
24-
},
25-
};
267
use crate::traces::span_pointers::SpanPointer;
278
use crate::traces::{context::SpanContext, propagation::Propagator};
9+
use crate::{
10+
config::aws::AwsConfig,
11+
lifecycle::invocation::{
12+
generate_span_id,
13+
triggers::{
14+
api_gateway_http_event::APIGatewayHttpEvent,
15+
api_gateway_rest_event::APIGatewayRestEvent,
16+
api_gateway_websocket_event::APIGatewayWebSocketEvent,
17+
dynamodb_event::DynamoDbRecord,
18+
event_bridge_event::EventBridgeEvent,
19+
kinesis_event::KinesisRecord,
20+
lambda_function_url_event::LambdaFunctionUrlEvent,
21+
s3_event::S3Record,
22+
sns_event::{SnsEntity, SnsRecord},
23+
sqs_event::{extract_trace_context_from_aws_trace_header, SqsRecord},
24+
step_function_event::StepFunctionEvent,
25+
Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_ARN_TAG,
26+
},
27+
},
28+
};
2829

2930
#[derive(Default)]
3031
pub struct SpanInferrer {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::get_aws_partition_by_region;
1+
use crate::config::aws::get_aws_partition_by_region;
22
use crate::lifecycle::invocation::{
33
processor::MS_TO_NS,
44
triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::get_aws_partition_by_region;
1+
use crate::config::aws::get_aws_partition_by_region;
22
use crate::lifecycle::invocation::{
33
processor::MS_TO_NS,
44
triggers::{lowercase_key, ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
config::get_aws_partition_by_region,
2+
config::aws::get_aws_partition_by_region,
33
lifecycle::invocation::{
44
processor::MS_TO_NS,
55
triggers::{lowercase_key, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::get_aws_partition_by_region;
1+
use crate::config::aws::get_aws_partition_by_region;
22
use crate::lifecycle::invocation::{
33
processor::MS_TO_NS,
44
triggers::{

bottlecap/src/lwa/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fn build_forward_request(
397397
#[cfg(test)]
398398
#[allow(clippy::unwrap_used)]
399399
mod tests {
400-
use crate::config::{AwsConfig, Config};
400+
use crate::config::{aws::AwsConfig, Config};
401401
use crate::lifecycle::invocation::processor::Processor;
402402
use crate::lwa::proxy::start_lwa_proxy;
403403
use crate::tags::provider::Provider;

0 commit comments

Comments
 (0)