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
This commit is contained in:
Anders Eurenius Runvald 2022-03-12 12:08:33 +01:00 committed by GitHub
parent 7cdc230100
commit ac8c2fe024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 12 deletions

View File

@ -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. | | `format` | `'on [$symbol($profile )(\($region\) )(\[$duration\])]($style)'` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. | | `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. |
| `region_aliases` | | Table of region aliases to display in addition to the AWS name. | | `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. | | `style` | `"bold yellow"` | The style for the module. |
| `expiration_symbol` | `X` | The symbol displayed when the temporary credentials have expired. | | `expiration_symbol` | `X` | The symbol displayed when the temporary credentials have expired. |
| `disabled` | `false` | Disables the `AWS` module. | | `disabled` | `false` | Disables the `AWS` module. |
@ -330,6 +331,8 @@ symbol = "🅰 "
[aws.region_aliases] [aws.region_aliases]
ap-southeast-2 = "au" ap-southeast-2 = "au"
us-east-1 = "va" us-east-1 = "va"
[aws.profile_aliases]
CompanyGroupFrobozzOnCallAccess = 'Frobozz'
``` ```
#### Display region #### Display region
@ -355,6 +358,8 @@ us-east-1 = "va"
format = "on [$symbol$profile]($style) " format = "on [$symbol$profile]($style) "
style = "bold blue" style = "bold blue"
symbol = "🅰 " symbol = "🅰 "
[aws.profile_aliases]
Enterprise_Naming_Scheme-voidstars = 'void**'
``` ```
## Azure ## Azure

View File

@ -10,6 +10,7 @@ pub struct AwsConfig<'a> {
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
pub region_aliases: HashMap<String, &'a str>, pub region_aliases: HashMap<String, &'a str>,
pub profile_aliases: HashMap<String, &'a str>,
pub expiration_symbol: &'a str, pub expiration_symbol: &'a str,
} }
@ -21,6 +22,7 @@ impl<'a> Default for AwsConfig<'a> {
style: "bold yellow", style: "bold yellow",
disabled: false, disabled: false,
region_aliases: HashMap::new(), region_aliases: HashMap::new(),
profile_aliases: HashMap::new(),
expiration_symbol: "X", expiration_symbol: "X",
} }
} }

View File

@ -111,11 +111,11 @@ fn get_credentials_duration(context: &Context, aws_profile: Option<&Profile>) ->
Some(expiration_date.timestamp() - chrono::Local::now().timestamp()) Some(expiration_date.timestamp() - chrono::Local::now().timestamp())
} }
fn alias_region(region: String, aliases: &HashMap<String, &str>) -> String { fn alias_name(name: Option<String>, aliases: &HashMap<String, &str>) -> Option<String> {
match aliases.get(&region) { name.as_ref()
None => region, .and_then(|n| aliases.get(n))
Some(alias) => (*alias).to_string(), .map(|&a| a.to_string())
} .or(name)
} }
fn get_credential_process(context: &Context, aws_profile: Option<&Profile>) -> Option<String> { fn get_credential_process(context: &Context, aws_profile: Option<&Profile>) -> Option<String> {
@ -187,12 +187,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
let mapped_region = if let Some(aws_region) = aws_region {
Some(alias_region(aws_region, &config.region_aliases))
} else {
None
};
let duration = { let duration = {
get_credentials_duration(context, aws_profile.as_ref()).map(|duration| { get_credentials_duration(context, aws_profile.as_ref()).map(|duration| {
if duration > 0 { if duration > 0 {
@ -203,6 +197,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}) })
}; };
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| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|variable, _| match variable { .map_meta(|variable, _| match variable {
@ -214,7 +212,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .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), "region" => mapped_region.as_ref().map(Ok),
"duration" => duration.as_ref().map(Ok), "duration" => duration.as_ref().map(Ok),
_ => None, _ => None,
@ -369,6 +367,46 @@ mod tests {
assert_eq!(expected, actual); 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] #[test]
fn credentials_file_is_ignored_when_is_directory() -> io::Result<()> { fn credentials_file_is_ignored_when_is_directory() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;