2019-12-20 17:30:47 +00:00
|
|
|
use std::collections::HashMap;
|
2019-10-14 15:05:03 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-05-13 00:43:46 +00:00
|
|
|
use chrono::DateTime;
|
2022-04-08 04:56:14 +00:00
|
|
|
use ini::Ini;
|
|
|
|
use once_cell::unsync::OnceCell;
|
2021-05-13 00:43:46 +00:00
|
|
|
|
2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-09-26 02:55:47 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::configs::aws::AwsConfig;
|
|
|
|
use crate::formatter::StringFormatter;
|
2022-04-08 04:56:14 +00:00
|
|
|
use crate::utils::render_time;
|
2019-10-04 12:42:33 +00:00
|
|
|
|
2019-11-02 11:08:54 +00:00
|
|
|
type Profile = String;
|
|
|
|
type Region = String;
|
2022-04-08 04:56:14 +00:00
|
|
|
type AwsConfigFile = OnceCell<Option<Ini>>;
|
|
|
|
type AwsCredsFile = OnceCell<Option<Ini>>;
|
2019-11-02 11:08:54 +00:00
|
|
|
|
2021-05-13 00:43:46 +00:00
|
|
|
fn get_credentials_file_path(context: &Context) -> Option<PathBuf> {
|
|
|
|
context
|
2022-08-11 05:48:22 +00:00
|
|
|
.get_env("AWS_SHARED_CREDENTIALS_FILE")
|
|
|
|
.or_else(|| context.get_env("AWS_CREDENTIALS_FILE"))
|
2021-05-13 00:43:46 +00:00
|
|
|
.and_then(|path| PathBuf::from_str(&path).ok())
|
|
|
|
.or_else(|| {
|
|
|
|
let mut home = context.get_home()?;
|
|
|
|
home.push(".aws/credentials");
|
|
|
|
Some(home)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_config_file_path(context: &Context) -> Option<PathBuf> {
|
|
|
|
context
|
2020-08-07 19:13:12 +00:00
|
|
|
.get_env("AWS_CONFIG_FILE")
|
2019-10-14 15:05:03 +00:00
|
|
|
.and_then(|path| PathBuf::from_str(&path).ok())
|
|
|
|
.or_else(|| {
|
2021-01-19 22:23:27 +00:00
|
|
|
let mut home = context.get_home()?;
|
2019-10-14 15:05:03 +00:00
|
|
|
home.push(".aws/config");
|
|
|
|
Some(home)
|
2021-05-13 00:43:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
// Initialize the AWS config file once
|
|
|
|
fn get_config<'a>(context: &Context, config: &'a OnceCell<Option<Ini>>) -> Option<&'a Ini> {
|
|
|
|
config
|
|
|
|
.get_or_init(|| {
|
|
|
|
let path = get_config_file_path(context)?;
|
|
|
|
Ini::load_from_file(path).ok()
|
|
|
|
})
|
|
|
|
.as_ref()
|
|
|
|
}
|
2019-10-14 15:05:03 +00:00
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
// Initialize the AWS credentials file once
|
|
|
|
fn get_creds<'a>(context: &Context, config: &'a OnceCell<Option<Ini>>) -> Option<&'a Ini> {
|
|
|
|
config
|
|
|
|
.get_or_init(|| {
|
|
|
|
let path = get_credentials_file_path(context)?;
|
|
|
|
Ini::load_from_file(path).ok()
|
|
|
|
})
|
|
|
|
.as_ref()
|
|
|
|
}
|
2019-10-14 15:05:03 +00:00
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
// Get the section for a given profile name in the config file.
|
|
|
|
fn get_profile_config<'a>(
|
|
|
|
config: &'a Ini,
|
|
|
|
profile: &Option<Profile>,
|
|
|
|
) -> Option<&'a ini::Properties> {
|
|
|
|
match profile {
|
2022-11-05 11:40:46 +00:00
|
|
|
Some(profile) => config.section(Some(format!("profile {profile}"))),
|
2022-04-08 04:56:14 +00:00
|
|
|
None => config.section(Some("default")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the section for a given profile name in the credentials file.
|
|
|
|
fn get_profile_creds<'a>(
|
|
|
|
config: &'a Ini,
|
|
|
|
profile: &Option<Profile>,
|
|
|
|
) -> Option<&'a ini::Properties> {
|
|
|
|
match profile {
|
|
|
|
None => config.section(Some("default")),
|
|
|
|
_ => config.section(profile.as_ref()),
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 15:05:03 +00:00
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
fn get_aws_region_from_config(
|
|
|
|
context: &Context,
|
|
|
|
aws_profile: &Option<Profile>,
|
|
|
|
aws_config: &AwsConfigFile,
|
|
|
|
) -> Option<Region> {
|
|
|
|
let config = get_config(context, aws_config)?;
|
|
|
|
let section = get_profile_config(config, aws_profile)?;
|
2019-10-14 15:05:03 +00:00
|
|
|
|
2022-05-23 10:58:27 +00:00
|
|
|
section.get("region").map(std::borrow::ToOwned::to_owned)
|
2019-10-14 15:05:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
fn get_aws_profile_and_region(
|
|
|
|
context: &Context,
|
|
|
|
aws_config: &AwsConfigFile,
|
|
|
|
) -> (Option<Profile>, Option<Region>) {
|
|
|
|
let profile_env_vars = ["AWSU_PROFILE", "AWS_VAULT", "AWSUME_PROFILE", "AWS_PROFILE"];
|
|
|
|
let region_env_vars = ["AWS_REGION", "AWS_DEFAULT_REGION"];
|
2021-03-13 08:35:50 +00:00
|
|
|
let profile = profile_env_vars
|
|
|
|
.iter()
|
|
|
|
.find_map(|env_var| context.get_env(env_var));
|
2022-03-14 21:49:23 +00:00
|
|
|
let region = region_env_vars
|
|
|
|
.iter()
|
|
|
|
.find_map(|env_var| context.get_env(env_var));
|
2021-03-13 08:35:50 +00:00
|
|
|
match (profile, region) {
|
2020-07-07 22:45:32 +00:00
|
|
|
(Some(p), Some(r)) => (Some(p), Some(r)),
|
|
|
|
(None, Some(r)) => (None, Some(r)),
|
2022-04-08 04:56:14 +00:00
|
|
|
(Some(p), None) => (
|
2021-07-29 18:27:46 +00:00
|
|
|
Some(p.clone()),
|
2022-04-08 04:56:14 +00:00
|
|
|
get_aws_region_from_config(context, &Some(p), aws_config),
|
2020-08-07 19:13:12 +00:00
|
|
|
),
|
2022-04-08 04:56:14 +00:00
|
|
|
(None, None) => (None, get_aws_region_from_config(context, &None, aws_config)),
|
2019-11-02 11:08:54 +00:00
|
|
|
}
|
2019-10-14 15:05:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
fn get_credentials_duration(
|
|
|
|
context: &Context,
|
|
|
|
aws_profile: &Option<String>,
|
|
|
|
aws_creds: &AwsCredsFile,
|
|
|
|
) -> Option<i64> {
|
|
|
|
let expiration_env_vars = ["AWS_SESSION_EXPIRATION", "AWSUME_EXPIRATION"];
|
2021-05-13 00:43:46 +00:00
|
|
|
let expiration_date = if let Some(expiration_date) = expiration_env_vars
|
|
|
|
.iter()
|
|
|
|
.find_map(|env_var| context.get_env(env_var))
|
|
|
|
{
|
|
|
|
chrono::DateTime::parse_from_rfc3339(&expiration_date).ok()
|
|
|
|
} else {
|
2022-04-08 04:56:14 +00:00
|
|
|
let creds = get_creds(context, aws_creds)?;
|
|
|
|
let section = get_profile_creds(creds, aws_profile)?;
|
|
|
|
|
2022-10-15 16:16:07 +00:00
|
|
|
let expiration_keys = ["expiration", "x_security_token_expires"];
|
|
|
|
expiration_keys
|
|
|
|
.iter()
|
|
|
|
.find_map(|expiration_key| section.get(expiration_key))
|
2022-04-08 04:56:14 +00:00
|
|
|
.and_then(|expiration| DateTime::parse_from_rfc3339(expiration).ok())
|
2021-05-13 00:43:46 +00:00
|
|
|
}?;
|
|
|
|
|
|
|
|
Some(expiration_date.timestamp() - chrono::Local::now().timestamp())
|
|
|
|
}
|
|
|
|
|
2022-03-12 11:08:33 +00:00
|
|
|
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)
|
2019-12-20 17:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
fn has_credential_process_or_sso(
|
|
|
|
context: &Context,
|
|
|
|
aws_profile: &Option<Profile>,
|
|
|
|
aws_config: &AwsConfigFile,
|
|
|
|
) -> Option<bool> {
|
|
|
|
let config = get_config(context, aws_config)?;
|
|
|
|
|
|
|
|
let section = get_profile_config(config, aws_profile)?;
|
|
|
|
Some(section.contains_key("credential_process") || section.contains_key("sso_start_url"))
|
2022-02-16 22:19:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
fn has_defined_credentials(
|
|
|
|
context: &Context,
|
|
|
|
aws_profile: &Option<Profile>,
|
|
|
|
aws_creds: &AwsCredsFile,
|
|
|
|
) -> Option<bool> {
|
|
|
|
let valid_env_vars = [
|
2022-02-16 22:19:13 +00:00
|
|
|
"AWS_ACCESS_KEY_ID",
|
|
|
|
"AWS_SECRET_ACCESS_KEY",
|
|
|
|
"AWS_SESSION_TOKEN",
|
|
|
|
];
|
|
|
|
|
|
|
|
// accept if set through environment variable
|
2022-04-08 04:56:14 +00:00
|
|
|
if valid_env_vars
|
2022-02-16 22:19:13 +00:00
|
|
|
.iter()
|
2022-04-08 04:56:14 +00:00
|
|
|
.any(|env_var| context.get_env(env_var).is_some())
|
2022-02-16 22:19:13 +00:00
|
|
|
{
|
2022-04-08 04:56:14 +00:00
|
|
|
return Some(true);
|
2022-02-16 22:19:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
let creds = get_creds(context, aws_creds)?;
|
|
|
|
let section = get_profile_creds(creds, aws_profile)?;
|
|
|
|
Some(section.contains_key("aws_access_key_id"))
|
2022-02-16 22:19:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:47 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let mut module = context.new_module("aws");
|
2019-10-04 12:42:33 +00:00
|
|
|
let config: AwsConfig = AwsConfig::try_load(module.config);
|
2019-09-26 02:55:47 +00:00
|
|
|
|
2022-04-08 04:56:14 +00:00
|
|
|
let aws_config = OnceCell::new();
|
|
|
|
let aws_creds = OnceCell::new();
|
|
|
|
|
|
|
|
let (aws_profile, aws_region) = get_aws_profile_and_region(context, &aws_config);
|
2020-07-07 22:45:32 +00:00
|
|
|
if aws_profile.is_none() && aws_region.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2019-11-02 11:08:54 +00:00
|
|
|
|
2022-02-16 22:19:13 +00:00
|
|
|
// only display if credential_process is defined or has valid credentials
|
2022-03-25 21:30:36 +00:00
|
|
|
if !config.force_display
|
2022-04-08 04:56:14 +00:00
|
|
|
&& !has_credential_process_or_sso(context, &aws_profile, &aws_config).unwrap_or(false)
|
|
|
|
&& !has_defined_credentials(context, &aws_profile, &aws_creds).unwrap_or(false)
|
2022-02-16 22:19:13 +00:00
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:43:46 +00:00
|
|
|
let duration = {
|
2022-04-08 04:56:14 +00:00
|
|
|
get_credentials_duration(context, &aws_profile, &aws_creds).map(|duration| {
|
2021-05-13 00:43:46 +00:00
|
|
|
if duration > 0 {
|
|
|
|
render_time((duration * 1000) as u128, false)
|
|
|
|
} else {
|
|
|
|
config.expiration_symbol.to_string()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2022-03-12 11:08:33 +00:00
|
|
|
let mapped_region = alias_name(aws_region, &config.region_aliases);
|
|
|
|
|
|
|
|
let mapped_profile = alias_name(aws_profile, &config.profile_aliases);
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2022-03-12 11:08:33 +00:00
|
|
|
"profile" => mapped_profile.as_ref().map(Ok),
|
2020-07-07 22:45:32 +00:00
|
|
|
"region" => mapped_region.as_ref().map(Ok),
|
2021-05-13 00:43:46 +00:00
|
|
|
"duration" => duration.as_ref().map(Ok),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
2020-09-28 20:38:50 +00:00
|
|
|
log::warn!("Error in module `aws`: \n{}", error);
|
2020-07-07 22:45:32 +00:00
|
|
|
return None;
|
2019-11-02 11:08:54 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-09-26 02:55:47 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2021-12-20 20:15:00 +00:00
|
|
|
use std::fs::{create_dir, File};
|
2020-08-07 19:13:12 +00:00
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
#[test]
|
2020-10-14 16:21:35 +00:00
|
|
|
#[ignore]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn no_region_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws").collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn region_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ (ap-northeast-2) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn region_set_with_alias() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_REGION", "ap-southeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.config(toml::toml! {
|
|
|
|
[aws.region_aliases]
|
|
|
|
ap-southeast-2 = "au"
|
|
|
|
})
|
|
|
|
.collect();
|
2021-03-06 19:47:06 +00:00
|
|
|
let expected = Some(format!("on {}", Color::Yellow.bold().paint("☁️ (au) ")));
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn default_region_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
|
|
|
.env("AWS_DEFAULT_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
2022-03-14 21:49:23 +00:00
|
|
|
Color::Yellow.bold().paint("☁️ (ap-northeast-2) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_set_from_aws_vault() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_VAULT", "astronauts-vault")
|
|
|
|
.env("AWS_PROFILE", "astronauts-profile")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts-vault ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:35:50 +00:00
|
|
|
#[test]
|
|
|
|
fn profile_set_from_awsu() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWSU_PROFILE", "astronauts-awsu")
|
|
|
|
.env("AWS_PROFILE", "astronauts-profile")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2021-03-13 08:35:50 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts-awsu ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:35:07 +00:00
|
|
|
#[test]
|
|
|
|
fn profile_set_from_awsume() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWSUME_PROFILE", "astronauts-awsume")
|
|
|
|
.env("AWS_PROFILE", "astronauts-profile")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2021-04-20 16:35:07 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts-awsume ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_and_region_set() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("☁️ astronauts (ap-northeast-2) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2022-03-12 11:08:33 +00:00
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:15:00 +00:00
|
|
|
#[test]
|
|
|
|
fn credentials_file_is_ignored_when_is_directory() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("credentials");
|
|
|
|
create_dir(&config_path)?;
|
|
|
|
|
|
|
|
assert!(ModuleRenderer::new("aws")
|
|
|
|
.env(
|
2022-08-11 05:48:22 +00:00
|
|
|
"AWS_SHARED_CREDENTIALS_FILE",
|
2021-12-20 20:15:00 +00:00
|
|
|
config_path.to_string_lossy().as_ref(),
|
|
|
|
)
|
|
|
|
.collect()
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_file_path_is_ignored_when_is_directory() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
create_dir(&config_path)?;
|
|
|
|
|
|
|
|
assert!(ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.collect()
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
#[test]
|
|
|
|
fn default_profile_set() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[default]
|
|
|
|
region = us-east-1
|
2022-02-16 22:19:13 +00:00
|
|
|
credential_process = /opt/bin/awscreds-retriever
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
[profile astronauts]
|
|
|
|
region = us-east-2
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ (us-east-1) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn profile_and_config_set() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[default]
|
|
|
|
region = us-east-1
|
|
|
|
|
|
|
|
[profile astronauts]
|
|
|
|
region = us-east-2
|
2022-02-16 22:19:13 +00:00
|
|
|
credential_process = /opt/bin/awscreds-retriever
|
2020-08-07 19:13:12 +00:00
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts (us-east-2) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_and_region_set_with_display_all() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("☁️ astronauts (ap-northeast-1) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_set_with_display_all() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn region_set_with_display_all() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
2021-03-06 19:47:06 +00:00
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ (ap-northeast-1) ")
|
2020-08-07 19:13:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_and_region_set_with_display_region() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_DEFAULT_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
format = "on [$symbol$region]($style) "
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {} ",
|
|
|
|
Color::Yellow.bold().paint("☁️ ap-northeast-1")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn profile_and_region_set_with_display_profile() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
format = "on [$symbol$profile]($style) "
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {} ",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn region_set_with_display_profile() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_REGION", "ap-northeast-1")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2020-08-07 19:13:12 +00:00
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
format = "on [$symbol$profile]($style) "
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("on {} ", Color::Yellow.bold().paint("☁️ ")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:43:46 +00:00
|
|
|
#[test]
|
|
|
|
fn expiration_date_set() {
|
|
|
|
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
|
|
|
|
|
|
|
let now_plus_half_hour: DateTime<Utc> = chrono::DateTime::from_utc(
|
|
|
|
NaiveDateTime::from_timestamp(chrono::Local::now().timestamp() + 1800, 0),
|
|
|
|
Utc,
|
|
|
|
);
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2021-05-13 00:43:46 +00:00
|
|
|
.env(
|
|
|
|
"AWS_SESSION_EXPIRATION",
|
|
|
|
now_plus_half_hour.to_rfc3339_opts(SecondsFormat::Secs, true),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
2022-04-21 18:58:06 +00:00
|
|
|
.paint("☁️ astronauts (ap-northeast-2) [30m] ")
|
2021-05-13 00:43:46 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expiration_date_set_from_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let credentials_path = dir.path().join("credentials");
|
|
|
|
let mut file = File::create(&credentials_path)?;
|
|
|
|
|
|
|
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
|
|
|
|
|
|
|
let now_plus_half_hour: DateTime<Utc> = chrono::DateTime::from_utc(
|
|
|
|
NaiveDateTime::from_timestamp(chrono::Local::now().timestamp() + 1800, 0),
|
|
|
|
Utc,
|
|
|
|
);
|
|
|
|
|
|
|
|
let expiration_date = now_plus_half_hour.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
|
|
|
|
|
2022-10-15 16:16:07 +00:00
|
|
|
let expiration_keys = ["expiration", "x_security_token_expires"];
|
|
|
|
expiration_keys.iter().for_each(|key| {
|
|
|
|
file.write_all(
|
|
|
|
format!(
|
|
|
|
"[astronauts]
|
2021-05-13 00:43:46 +00:00
|
|
|
aws_access_key_id=dummy
|
|
|
|
aws_secret_access_key=dummy
|
2022-10-15 16:16:07 +00:00
|
|
|
{}={}
|
2021-05-13 00:43:46 +00:00
|
|
|
",
|
2022-10-15 16:16:07 +00:00
|
|
|
key, expiration_date
|
|
|
|
)
|
|
|
|
.as_bytes(),
|
2021-05-13 00:43:46 +00:00
|
|
|
)
|
2022-10-15 16:16:07 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
|
|
|
.env(
|
|
|
|
"AWS_SHARED_CREDENTIALS_FILE",
|
|
|
|
credentials_path.to_string_lossy().as_ref(),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let actual_variant = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
|
|
|
.env(
|
|
|
|
"AWS_CREDENTIALS_FILE",
|
|
|
|
credentials_path.to_string_lossy().as_ref(),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual, actual_variant,
|
|
|
|
"both AWS_SHARED_CREDENTIALS_FILE and AWS_CREDENTIALS_FILE should work"
|
|
|
|
);
|
|
|
|
|
|
|
|
// In principle, "30m" should be correct. However, bad luck in scheduling
|
|
|
|
// on shared runners may delay it.
|
|
|
|
let possible_values = [
|
|
|
|
"30m2s", "30m1s", "30m", "29m59s", "29m58s", "29m57s", "29m56s", "29m55s",
|
|
|
|
];
|
|
|
|
let possible_values = possible_values.map(|duration| {
|
2022-11-05 11:40:46 +00:00
|
|
|
let segment_colored = format!("☁️ astronauts (ap-northeast-2) [{duration}] ");
|
2022-10-15 16:16:07 +00:00
|
|
|
Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint(segment_colored)
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
possible_values.contains(&actual),
|
|
|
|
"time is not in range: {actual:?}"
|
|
|
|
);
|
2022-01-22 11:54:04 +00:00
|
|
|
});
|
|
|
|
|
2021-05-13 00:43:46 +00:00
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn profile_and_region_set_show_duration() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2021-05-13 00:43:46 +00:00
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("☁️ astronauts (ap-northeast-2) ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expiration_date_set_expired() {
|
|
|
|
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
|
|
|
|
|
|
|
let now: DateTime<Utc> = chrono::DateTime::from_utc(
|
|
|
|
NaiveDateTime::from_timestamp(chrono::Local::now().timestamp() - 1800, 0),
|
|
|
|
Utc,
|
|
|
|
);
|
|
|
|
|
|
|
|
let symbol = "!!!";
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
expiration_symbol = symbol
|
|
|
|
})
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
2022-02-16 22:19:13 +00:00
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
2021-05-13 00:43:46 +00:00
|
|
|
.env(
|
|
|
|
"AWS_SESSION_EXPIRATION",
|
|
|
|
now.to_rfc3339_opts(SecondsFormat::Secs, true),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
2022-11-05 11:40:46 +00:00
|
|
|
.paint(format!("☁️ astronauts (ap-northeast-2) [{symbol}] "))
|
2021-05-13 00:43:46 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
#[test]
|
2020-10-14 16:21:35 +00:00
|
|
|
#[ignore]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn region_not_set_with_display_region() {
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
format = "on [$symbol$region]($style) "
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2022-02-16 22:19:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_any_credentials() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[default]
|
|
|
|
region = us-east-1
|
|
|
|
output = json
|
|
|
|
|
|
|
|
[profile astronauts]
|
|
|
|
region = us-east-2
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:30:36 +00:00
|
|
|
#[test]
|
|
|
|
fn missing_any_credentials_but_display_empty() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[profile astronauts]
|
|
|
|
region = us-east-2
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[aws]
|
|
|
|
force_display = true
|
|
|
|
})
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts (us-east-2) ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:19:13 +00:00
|
|
|
#[test]
|
|
|
|
fn access_key_credential_set() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let credentials_path = dir.path().join("credentials");
|
|
|
|
let mut file = File::create(&credentials_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[astronauts]
|
|
|
|
aws_access_key_id=dummy
|
|
|
|
aws_secret_access_key=dummy
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_REGION", "ap-northeast-2")
|
|
|
|
.env(
|
2022-08-11 05:48:22 +00:00
|
|
|
"AWS_SHARED_CREDENTIALS_FILE",
|
2022-02-16 22:19:13 +00:00
|
|
|
credentials_path.to_string_lossy().as_ref(),
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow
|
|
|
|
.bold()
|
|
|
|
.paint("☁️ astronauts (ap-northeast-2) ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn credential_process_set() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[default]
|
|
|
|
region = ap-northeast-2
|
|
|
|
credential_process = /opt/bin/awscreds-retriever
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ (ap-northeast-2) ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:14:52 +00:00
|
|
|
#[test]
|
|
|
|
fn sso_set() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let config_path = dir.path().join("config");
|
|
|
|
let mut file = File::create(&config_path)?;
|
|
|
|
|
|
|
|
file.write_all(
|
|
|
|
"[default]
|
|
|
|
region = ap-northeast-2
|
|
|
|
sso_start_url = https://starship.rs/sso
|
|
|
|
sso_region = <SSO-Default-Region>
|
|
|
|
sso_account_id = <AWS ACCOUNT ID>
|
|
|
|
sso_role_name = <AWS-ROLE-NAME>
|
|
|
|
"
|
|
|
|
.as_bytes(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
file.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ (ap-northeast-2) ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:19:13 +00:00
|
|
|
#[test]
|
|
|
|
fn access_key_env_var_set() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_ACCESS_KEY_ID", "dummy")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn secret_access_key_env_var_set() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_SECRET_ACCESS_KEY", "dummy")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn session_token_env_var_set() {
|
|
|
|
let actual = ModuleRenderer::new("aws")
|
|
|
|
.env("AWS_PROFILE", "astronauts")
|
|
|
|
.env("AWS_SESSION_TOKEN", "dummy")
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"on {}",
|
|
|
|
Color::Yellow.bold().paint("☁️ astronauts ")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
}
|