Skip to content

Commit

Permalink
Parse out provided sso profile info
Browse files Browse the repository at this point in the history
  • Loading branch information
TreehouseFalcon committed Dec 21, 2023
1 parent ba191d7 commit 5d6120c
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules

*.env
mantle.yml

*.DS_Store

Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.ignoreWords": ["confy"]
}
163 changes: 157 additions & 6 deletions mantle/Cargo.lock

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

3 changes: 3 additions & 0 deletions mantle/rbx_mantle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ clap = "2.33.0"
glob = "0.3.0"
sha2 = "0.9.8"
difference = "2.0.0"
dirs-next = "2.0.0"
rust-ini = "0.20.0"
rusoto_core = "0.47.0"
rusoto_sts = "0.48.0"
rusoto_s3 = "0.47.0"
tokio = { version = "1", features = ["full"] }
async-trait = "0.1.51"
Expand Down
45 changes: 44 additions & 1 deletion mantle/rbx_mantle/src/state/aws_credentials_provider.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use async_trait::async_trait;
use dirs_next::home_dir;
use ini::Ini;
use rusoto_core::credential::{
AwsCredentials, ContainerProvider, CredentialsError, EnvironmentProvider,
InstanceMetadataProvider, ProfileProvider, ProvideAwsCredentials,
};
use serde::{Deserialize, Serialize};
use std::env;
use std::path::PathBuf;
use std::time::Duration;

#[derive(Clone, Debug)]
Expand All @@ -15,14 +19,26 @@ pub struct AwsCredentialsProvider {
instance_metadata_provider: Option<InstanceMetadataProvider>,
}

#[derive(Default, Serialize, Deserialize)]
struct AwsConfig {
region: Option<String>,
output: Option<String>,
sso_start_url: Option<String>,
sso_region: Option<String>,
sso_account_id: Option<String>,
sso_role_name: Option<String>,
}

impl AwsCredentialsProvider {
pub fn new() -> AwsCredentialsProvider {
// Set up profile provider using optionally supplied profile name //
let mut profile_provider: Option<ProfileProvider> = None;
let profile_provider: Option<ProfileProvider>;
if let Ok(profile_name) = env::var("MANTLE_AWS_PROFILE") {
let mut provider = ProfileProvider::new().unwrap();
provider.set_profile(profile_name);
profile_provider = Some(provider);
} else {
profile_provider = ProfileProvider::new().ok();
}

// Inherit IAM role from instance metadata service or ECS agent role //
Expand Down Expand Up @@ -55,6 +71,13 @@ impl AwsCredentialsProvider {
}
}

fn get_config_path() -> PathBuf {
home_dir()
.expect("Expected a HOME directory")
.join(".aws")
.join("config")
}

async fn chain_provider_credentials(
provider: AwsCredentialsProvider,
) -> Result<AwsCredentials, CredentialsError> {
Expand All @@ -66,13 +89,33 @@ async fn chain_provider_credentials(
}
if let Some(ref profile_provider) = provider.profile_provider {
// Check standard profile credentials first //
println!("Checking profile provider (credentials)");
if let Ok(creds) = profile_provider.credentials().await {
return Ok(creds);
}

// Check SSO profile credentials as fallback //
println!("Checking profile provider (sso)");
let aws_config = Ini::load_from_file(get_config_path())
.expect(format!("Failed to load AWS config ({:?})", get_config_path()).as_str());
let profile_name = profile_provider.profile();
println!("profile name: {}", profile_name);
println!("config path: {:?}", get_config_path());

let target_section = aws_config
.iter()
.filter(|(section, _)| {
section.is_some() && section.unwrap() == format!("profile {}", profile_name)
})
.next();

if let Some((section, properties)) = target_section {
let section_name = section.unwrap();
println!("Section name: {}", section_name);
for (key, value) in properties.iter() {
println!("{}: {:?}", key, value);
}
}
}
if let Some(ref container_provider) = provider.container_provider {
if let Ok(creds) = container_provider.credentials().await {
Expand Down

0 comments on commit 5d6120c

Please sign in to comment.