Skip to content

Commit

Permalink
[blob-service] Standardize localstack config
Browse files Browse the repository at this point in the history
Summary:
We should have one standard way of configuring localstack endpoint for all services. This diff standardizes the localstack config for blob-service.
I consider Tunnelbroker as state-of-the-art solution for localstack endpoint configuration.

TL;DR

```
cargo run -- --sandbox --localstack-url "http://localhost:4566"
```
-->
```
cargo run
```

The `LOCALSTACK_ENDPOINT` env variable is automatically provided by Nix so no need to specify `--localstack-endpoint` explicitly.

Test Plan: Ran the above command and it worked. Blob service integration tests are working.

Reviewers: michal, jon

Reviewed By: jon

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8835
  • Loading branch information
barthap committed Aug 17, 2023
1 parent d89982a commit 10418a3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
28 changes: 10 additions & 18 deletions services/blob/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::{ensure, Result};
use clap::{builder::FalseyValueParser, Parser};
use clap::Parser;
use once_cell::sync::Lazy;
use tracing::info;

use crate::constants::{
DEFAULT_GRPC_PORT, DEFAULT_HTTP_PORT, DEFAULT_S3_BUCKET_NAME, LOCALSTACK_URL,
S3_BUCKET_ENV_VAR, SANDBOX_ENV_VAR,
DEFAULT_GRPC_PORT, DEFAULT_HTTP_PORT, DEFAULT_S3_BUCKET_NAME,
S3_BUCKET_ENV_VAR,
};

#[derive(Parser)]
Expand All @@ -17,15 +17,10 @@ pub struct AppConfig {
/// HTTP server listening port
#[arg(long, default_value_t = DEFAULT_HTTP_PORT)]
pub http_port: u16,
/// Run the service in sandbox
#[arg(long = "sandbox", default_value_t = false)]
// support the env var for compatibility reasons
#[arg(env = SANDBOX_ENV_VAR)]
#[arg(value_parser = FalseyValueParser::new())]
pub is_sandbox: bool,
/// AWS Localstack service URL, applicable in sandbox mode
#[arg(long, default_value_t = LOCALSTACK_URL.to_string())]
pub localstack_url: String,
/// AWS Localstack service URL
#[arg(env = "LOCALSTACK_ENDPOINT")]
#[arg(long)]
pub localstack_endpoint: Option<String>,
#[arg(env = S3_BUCKET_ENV_VAR)]
#[arg(long, default_value_t = DEFAULT_S3_BUCKET_NAME.to_string())]
pub s3_bucket_name: String,
Expand Down Expand Up @@ -58,12 +53,9 @@ pub(super) fn parse_cmdline_args() -> Result<()> {
pub async fn load_aws_config() -> aws_types::SdkConfig {
let mut config_builder = aws_config::from_env();

if CONFIG.is_sandbox {
info!(
"Running in sandbox environment. Localstack URL: {}",
&CONFIG.localstack_url
);
config_builder = config_builder.endpoint_url(&CONFIG.localstack_url);
if let Some(endpoint) = &CONFIG.localstack_endpoint {
info!("Using Localstack. AWS endpoint URL: {}", endpoint);
config_builder = config_builder.endpoint_url(endpoint);
}

config_builder.load().await
Expand Down
2 changes: 0 additions & 2 deletions services/blob/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pub const DEFAULT_GRPC_PORT: u16 = 50051;
pub const DEFAULT_HTTP_PORT: u16 = 51001;
pub const LOCALSTACK_URL: &str = "http://localstack:4566";
pub const MPSC_CHANNEL_BUFFER_CAPACITY: usize = 1;

/// 4MB limit
Expand Down Expand Up @@ -64,7 +63,6 @@ pub const BLOB_REVERSE_INDEX_TABLE_HASH_INDEX_NAME: &str = "blobHash-index";

// Environment variables

pub const SANDBOX_ENV_VAR: &str = "COMM_SERVICES_SANDBOX";
pub const LOG_LEVEL_ENV_VAR: &str =
tracing_subscriber::filter::EnvFilter::DEFAULT_ENV;

Expand Down
3 changes: 2 additions & 1 deletion services/blob/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ mod handlers {
}

fn cors_config() -> Cors {
if CONFIG.is_sandbox {
// For local development, use relaxed CORS config
if CONFIG.localstack_endpoint.is_some() {
// All origins, methods, request headers and exposed headers allowed.
// Credentials supported. Max age 1 hour. Does not send wildcard.
return Cors::permissive();
Expand Down
2 changes: 1 addition & 1 deletion services/blob/src/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl S3Client {
pub fn new(aws_config: &aws_types::SdkConfig) -> Self {
let s3_config = aws_sdk_s3::config::Builder::from(aws_config)
// localstack doesn't support virtual addressing
.force_path_style(crate::config::CONFIG.is_sandbox)
.force_path_style(crate::config::CONFIG.localstack_endpoint.is_some())
.build();
S3Client {
client: Arc::new(aws_sdk_s3::Client::from_conf(s3_config)),
Expand Down
14 changes: 1 addition & 13 deletions services/blob/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
use std::{env, error::Error as StdError};
use std::error::Error as StdError;
use tonic::codegen::futures_core::Stream;

use crate::constants;

fn is_env_flag_set(env_var_name: &str) -> bool {
let flag_value = env::var(env_var_name).unwrap_or_default().to_lowercase();
return ["1", "true"].contains(&flag_value.as_str());
}

/// Returns true if the `COMM_SERVICES_SANDBOX` environment variable is set
pub fn is_sandbox_env() -> bool {
return is_env_flag_set(constants::SANDBOX_ENV_VAR);
}

pub type BoxedError = Box<dyn StdError>;
// Trait type aliases aren't supported in Rust, but
// we can workaround this by creating an empty trait
Expand Down

0 comments on commit 10418a3

Please sign in to comment.