Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS SSO profiles #208

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
153 changes: 149 additions & 4 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
43 changes: 42 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,12 @@
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 std::env;
use std::path::PathBuf;
use std::time::Duration;

#[derive(Clone, Debug)]
Expand All @@ -17,6 +20,17 @@ pub struct AwsCredentialsProvider {

impl AwsCredentialsProvider {
pub fn new() -> AwsCredentialsProvider {
// Set up profile provider using optionally supplied profile name //
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 //
let mut inherit_iam_role = false;
if let Ok(value) = env::var("MANTLE_AWS_INHERIT_IAM_ROLE") {
if value == "true" {
Expand All @@ -27,7 +41,7 @@ impl AwsCredentialsProvider {
AwsCredentialsProvider {
prefixed_environment_provider: EnvironmentProvider::with_prefix("MANTLE_AWS"),
environment_provider: EnvironmentProvider::default(),
profile_provider: ProfileProvider::new().ok(),
profile_provider,
container_provider: if inherit_iam_role {
let mut provider = ContainerProvider::new();
provider.set_timeout(Duration::from_secs(15));
Expand All @@ -46,6 +60,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 @@ -56,9 +77,29 @@ async fn chain_provider_credentials(
return Ok(creds);
}
if let Some(ref profile_provider) = provider.profile_provider {
// Check standard profile credentials first //
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);

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

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
Loading