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

feat(aws): Add profile aliases #3699

Merged
merged 5 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -330,6 +331,8 @@ symbol = "🅰 "
[aws.region_aliases]
ap-southeast-2 = "au"
us-east-1 = "va"
[aws.profile_aliases]
CompanyGroupFrobozzOnCallAccess = 'Frobozz'
```

#### Display region
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/configs/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct AwsConfig<'a> {
pub style: &'a str,
pub disabled: bool,
pub region_aliases: HashMap<String, &'a str>,
pub profile_aliases: HashMap<String, &'a str>,
pub expiration_symbol: &'a str,
}

Expand All @@ -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",
}
}
Expand Down
62 changes: 50 additions & 12 deletions src/modules/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &str>) -> String {
match aliases.get(&region) {
None => region,
Some(alias) => (*alias).to_string(),
}
fn alias_name(name: Option<String>, aliases: &HashMap<String, &str>) -> Option<String> {
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<String> {
Expand Down Expand Up @@ -187,12 +187,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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 {
Expand All @@ -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| {
formatter
.map_meta(|variable, _| match variable {
Expand All @@ -214,7 +212,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => 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,
Expand Down Expand Up @@ -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()?;
Expand Down