From ac8c2fe02474bee6fa41abf826501ec663cb0bb0 Mon Sep 17 00:00:00 2001 From: Anders Eurenius Runvald Date: Sat, 12 Mar 2022 12:08:33 +0100 Subject: [PATCH] feat(aws): Add profile aliases (#3699) * Rename m.aws.alias_region to alias_name * Add aws profile aliases * Document aws.profile_aliases, with examples * Add tests for new aws.profile_aliases feature * Tidy alias_handling a bit --- docs/config/README.md | 5 ++++ src/configs/aws.rs | 2 ++ src/modules/aws.rs | 62 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 7ceb20c74..3110023ad 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -300,6 +300,7 @@ date is read from the `AWSUME_EXPIRATION` env var. | `format` | `'on [$symbol($profile )(\($region\) )(\[$duration\])]($style)'` | The format for the module. | | `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. | | `region_aliases` | | Table of region aliases to display in addition to the AWS name. | +| `profile_aliases` | | Table of profile aliases to display in addition to the AWS name. | | `style` | `"bold yellow"` | The style for the module. | | `expiration_symbol` | `X` | The symbol displayed when the temporary credentials have expired. | | `disabled` | `false` | Disables the `AWS` module. | @@ -330,6 +331,8 @@ symbol = "🅰 " [aws.region_aliases] ap-southeast-2 = "au" us-east-1 = "va" +[aws.profile_aliases] +CompanyGroupFrobozzOnCallAccess = 'Frobozz' ``` #### Display region @@ -355,6 +358,8 @@ us-east-1 = "va" format = "on [$symbol$profile]($style) " style = "bold blue" symbol = "🅰 " +[aws.profile_aliases] +Enterprise_Naming_Scheme-voidstars = 'void**' ``` ## Azure diff --git a/src/configs/aws.rs b/src/configs/aws.rs index 8cb2b2dd5..2bd6287b0 100644 --- a/src/configs/aws.rs +++ b/src/configs/aws.rs @@ -10,6 +10,7 @@ pub struct AwsConfig<'a> { pub style: &'a str, pub disabled: bool, pub region_aliases: HashMap, + pub profile_aliases: HashMap, pub expiration_symbol: &'a str, } @@ -21,6 +22,7 @@ impl<'a> Default for AwsConfig<'a> { style: "bold yellow", disabled: false, region_aliases: HashMap::new(), + profile_aliases: HashMap::new(), expiration_symbol: "X", } } diff --git a/src/modules/aws.rs b/src/modules/aws.rs index f59210657..9ff03a9ce 100644 --- a/src/modules/aws.rs +++ b/src/modules/aws.rs @@ -111,11 +111,11 @@ fn get_credentials_duration(context: &Context, aws_profile: Option<&Profile>) -> Some(expiration_date.timestamp() - chrono::Local::now().timestamp()) } -fn alias_region(region: String, aliases: &HashMap) -> String { - match aliases.get(®ion) { - None => region, - Some(alias) => (*alias).to_string(), - } +fn alias_name(name: Option, aliases: &HashMap) -> Option { + name.as_ref() + .and_then(|n| aliases.get(n)) + .map(|&a| a.to_string()) + .or(name) } fn get_credential_process(context: &Context, aws_profile: Option<&Profile>) -> Option { @@ -187,12 +187,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let mapped_region = if let Some(aws_region) = aws_region { - Some(alias_region(aws_region, &config.region_aliases)) - } else { - None - }; - let duration = { get_credentials_duration(context, aws_profile.as_ref()).map(|duration| { if duration > 0 { @@ -203,6 +197,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { }) }; + let mapped_region = alias_name(aws_region, &config.region_aliases); + + let mapped_profile = alias_name(aws_profile, &config.profile_aliases); + let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|variable, _| match variable { @@ -214,7 +212,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "profile" => aws_profile.as_ref().map(Ok), + "profile" => mapped_profile.as_ref().map(Ok), "region" => mapped_region.as_ref().map(Ok), "duration" => duration.as_ref().map(Ok), _ => None, @@ -369,6 +367,46 @@ mod tests { assert_eq!(expected, actual); } + #[test] + fn profile_set_with_alias() { + let actual = ModuleRenderer::new("aws") + .env("AWS_PROFILE", "CORPORATION-CORP_astronauts_ACCESS_GROUP") + .env("AWS_REGION", "ap-northeast-2") + .env("AWS_ACCESS_KEY_ID", "dummy") + .config(toml::toml! { + [aws.profile_aliases] + CORPORATION-CORP_astronauts_ACCESS_GROUP = "astro" + }) + .collect(); + let expected = Some(format!( + "on {}", + Color::Yellow.bold().paint("☁️ astro (ap-northeast-2) ") + )); + + assert_eq!(expected, actual); + } + + #[test] + fn region_and_profile_both_set_with_alias() { + let actual = ModuleRenderer::new("aws") + .env("AWS_PROFILE", "CORPORATION-CORP_astronauts_ACCESS_GROUP") + .env("AWS_REGION", "ap-southeast-2") + .env("AWS_ACCESS_KEY_ID", "dummy") + .config(toml::toml! { + [aws.profile_aliases] + CORPORATION-CORP_astronauts_ACCESS_GROUP = "astro" + [aws.region_aliases] + ap-southeast-2 = "au" + }) + .collect(); + let expected = Some(format!( + "on {}", + Color::Yellow.bold().paint("☁️ astro (au) ") + )); + + assert_eq!(expected, actual); + } + #[test] fn credentials_file_is_ignored_when_is_directory() -> io::Result<()> { let dir = tempfile::tempdir()?;