Skip to content

Commit

Permalink
feat(backend): Add STSAdapter for validating AWS credentials
Browse files Browse the repository at this point in the history
This commit introduces the module `STSAdapter` that checks the
validity of AWS credentials by performing a `get_caller_identity`
operation via AWS's Secure Token Service (STS). A successful
operation indicates that the credentials are valid.

Refs: #14
  • Loading branch information
maikbasel committed Mar 4, 2024
1 parent cd7b867 commit 335b728
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 18 deletions.
132 changes: 115 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- '4566:4566' # LocalStack API Gateway
- '4592:4592' # AWS STS port
environment:
- SERVICES=ssm,secretsmanager,sts
- SERVICES=ssm,secretsmanager,iam,sts
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
- LAMBDA_EXECUTOR=docker-reuse
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ secstr = "0.5.1"
heck = "0.4.1"
rust-ini = "0.20.0"
directories = "5.0.1"
aws-sdk-sts = "1.15.0"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
Expand All @@ -46,6 +47,8 @@ test-context = "0.1.4"
tokio = { version = "1.15.0", features = ["full"] }
mockall = "0.11.4"
serial_test = "2.0.0"
testcontainers = "0.15.0"
testcontainers-modules = { version = "0.3.4", features = ["localstack"]}

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/common.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod aws;
pub mod report;
23 changes: 23 additions & 0 deletions src-tauri/src/common/aws.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use aws_config::BehaviorVersion;

pub fn localstack_endpoint() -> Option<String> {
match std::env::var("LOCALSTACK_ENDPOINT") {
Ok(value) => Some(value),
Err(_) => None,
}
}

pub fn shared_config_loader(profile_name: &str) -> aws_config::ConfigLoader {
aws_config::defaults(BehaviorVersion::latest()).credentials_provider(
aws_config::profile::ProfileFileCredentialsProvider::builder()
.profile_name(profile_name)
.build(),
)
}

pub fn sts_client(config: &aws_config::SdkConfig) -> aws_sdk_sts::Client {
// Copy config from aws_config::SdkConfig to aws_sdk_sts::Config
let sts_config_builder = aws_sdk_sts::config::Builder::from(config);

aws_sdk_sts::Client::from_conf(sts_config_builder.build())
}
3 changes: 3 additions & 0 deletions src-tauri/src/credentials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod application;
pub mod core;
pub mod infrastructure;
1 change: 1 addition & 0 deletions src-tauri/src/credentials/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions src-tauri/src/credentials/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod error;
pub mod spi;
22 changes: 22 additions & 0 deletions src-tauri/src/credentials/core/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::fmt::{Display, Formatter};

use error_stack::Context;

#[derive(Debug, Eq, PartialEq, Clone, serde::Serialize)]
pub enum CredentialsError {
InvalidCredentialsError,
UnexpectedError(String),
}

impl Display for CredentialsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
CredentialsError::InvalidCredentialsError => write!(f, "invalid credentials error"),
CredentialsError::UnexpectedError(skd_error) => {
write!(f, "unexpected error: {}", skd_error)
}
}
}
}

impl Context for CredentialsError {}
12 changes: 12 additions & 0 deletions src-tauri/src/credentials/core/spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use async_trait::async_trait;
use error_stack::Result;
#[cfg(test)]
use mockall::automock;

use crate::credentials::core::error::CredentialsError;

#[cfg_attr(test, automock)]
#[async_trait]
pub trait CredentialsDataSPI: Send + Sync {
async fn validate_credentials(&self, profile_name: &str) -> Result<(), CredentialsError>;
}
1 change: 1 addition & 0 deletions src-tauri/src/credentials/infrastructure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod aws;
1 change: 1 addition & 0 deletions src-tauri/src/credentials/infrastructure/aws.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod sts;
1 change: 1 addition & 0 deletions src-tauri/src/credentials/infrastructure/aws/sts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod sts_adapter;
Loading

0 comments on commit 335b728

Please sign in to comment.