From 1ac56a91981bee4867dfb054bd2199c6111fe1eb Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 19 May 2022 13:54:38 +0200 Subject: Refactor K2V/S3 client creation --- src/login/mod.rs | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/login') diff --git a/src/login/mod.rs b/src/login/mod.rs index 5637e8a..0845371 100644 --- a/src/login/mod.rs +++ b/src/login/mod.rs @@ -3,9 +3,19 @@ pub mod static_provider; use anyhow::Result; use async_trait::async_trait; +use k2v_client::K2vClient; +use rusoto_core::HttpClient; +use rusoto_credential::{AwsCredentials, StaticProvider}; +use rusoto_s3::S3Client; +use rusoto_signature::Region; use crate::cryptoblob::Key as SymmetricKey; +#[async_trait] +pub trait LoginProvider { + async fn login(&self, username: &str, password: &str) -> Result; +} + #[derive(Clone, Debug)] pub struct Credentials { pub aws_access_key_id: String, @@ -14,7 +24,33 @@ pub struct Credentials { pub master_key: SymmetricKey, } -#[async_trait] -pub trait LoginProvider { - async fn login(&self, username: &str, password: &str) -> Result; +impl Credentials { + pub fn k2v_client(&self, k2v_region: &Region) -> Result { + let aws_creds = AwsCredentials::new( + self.aws_access_key_id.clone(), + self.aws_secret_access_key.clone(), + None, + None, + ); + + Ok(K2vClient::new( + k2v_region.clone(), + self.bucket.clone(), + aws_creds, + None, + )?) + } + + pub fn s3_client(&self, s3_region: &Region) -> Result { + let aws_creds_provider = StaticProvider::new_minimal( + self.aws_access_key_id.clone(), + self.aws_secret_access_key.clone(), + ); + + Ok(S3Client::new_with( + HttpClient::new()?, + aws_creds_provider, + s3_region.clone(), + )) + } } -- cgit v1.2.3