#![allow(clippy::unwrap_used)]
use std::{io::Read, path::PathBuf};
use pegase::{
generators::{
Secret, SecretGenerator,
hash::{HashKind, Pbkdf2Params},
pass::{PassCharset, PassSize},
ssh::{SshKeyKind, SshKeySize},
},
recipe::{
Recipe,
file_recipe::{FileFormat, FileRecipe},
generator_tree::{GeneratorTree, GeneratorTreeWithRef, SecretTree},
},
};
pub mod helpers;
use helpers::{pass, ssh};
use tempfile::NamedTempFile;
use zeroize::Zeroizing;
use crate::helpers::{assert_hash, assert_pass, assert_same_struct, assert_ssh, hash};
fn gen_recipe(path: PathBuf, file_format: FileFormat) -> Recipe {
Recipe {
files: vec![FileRecipe {
path,
format: Some(file_format),
content: GeneratorTreeWithRef::Map(
[
pass("pass1", None, None),
(
"node".to_string(),
GeneratorTreeWithRef::Map(
[
(
"subnode".to_string(),
GeneratorTreeWithRef::Map(
[
pass(
"pass2",
Some(PassSize::Length(10_000)),
Some(PassCharset::Hex),
),
hash("hash1", None, None, None),
]
.into(),
),
),
ssh("ssh1", None, None),
hash(
"hash2",
Some(HashKind::Pbkdf2 {
params: Some(Pbkdf2Params { rounds: 1_000 }),
}),
Some(PassSize::AtLeastBits(36_000)),
Some(PassCharset::Num),
),
(
"list".to_string(),
GeneratorTreeWithRef::List(vec![GeneratorTreeWithRef::Map(
[ssh(
"ssh2",
Some(SshKeyKind::Rsa { bits: None }),
Some("my_rsa_key".to_string()),
)]
.into(),
)]),
),
]
.into(),
),
),
]
.into(),
),
sops: Default::default(),
}],
shared: Default::default(),
}
}
fn gen_expected_output() -> SecretTree {
let sym = SecretTree::Secret(Secret::String(Zeroizing::new(String::new())));
let asym = SecretTree::Secret(Secret::Asym {
private: Zeroizing::new(String::new()),
public: String::new(),
});
let hash = SecretTree::Secret(Secret::Hash {
plain: Zeroizing::new(String::new()),
hash: String::new(),
});
SecretTree::Map(
[
("pass1".to_string(), sym.clone()),
(
"node".to_string(),
SecretTree::Map(
[
(
"subnode".to_string(),
SecretTree::Map(
[
("pass2".to_string(), sym.clone()),
("hash1".to_string(), hash.clone()),
]
.into(),
),
),
("ssh1".to_string(), asym.clone()),
("hash2".to_string(), hash.clone()),
(
"list".to_string(),
SecretTree::List(
[SecretTree::Map([("ssh2".to_string(), asym.clone())].into())]
.into(),
),
),
]
.into(),
),
),
]
.into(),
)
}
fn assert_output(output: SecretTree) {
let pass1 = output
.map()
.unwrap()
.get("pass1")
.unwrap()
.secret()
.unwrap();
let pass2 = output
.map()
.unwrap()
.get("node")
.unwrap()
.map()
.unwrap()
.get("subnode")
.unwrap()
.map()
.unwrap()
.get("pass2")
.unwrap()
.secret()
.unwrap();
let ssh1 = output
.map()
.unwrap()
.get("node")
.unwrap()
.map()
.unwrap()
.get("ssh1")
.unwrap()
.secret()
.unwrap();
let ssh2 = output
.map()
.unwrap()
.get("node")
.unwrap()
.map()
.unwrap()
.get("list")
.unwrap()
.list()
.unwrap()
.first()
.unwrap()
.map()
.unwrap()
.get("ssh2")
.unwrap()
.secret()
.unwrap();
let hash1 = output
.map()
.unwrap()
.get("node")
.unwrap()
.map()
.unwrap()
.get("subnode")
.unwrap()
.map()
.unwrap()
.get("hash1")
.unwrap()
.secret()
.unwrap();
let hash2 = output
.map()
.unwrap()
.get("node")
.unwrap()
.map()
.unwrap()
.get("hash2")
.unwrap()
.secret()
.unwrap();
assert_pass(pass1, 40, None);
assert_pass(pass2, 10_000, Some(PassCharset::Hex));
assert_ssh(ssh1, SshKeyKind::Ed25519, "");
assert_ssh(
ssh2,
SshKeyKind::Rsa {
bits: Some(SshKeySize(4096)),
},
"my_rsa_key",
);
assert_hash(hash1, HashKind::Argon2 { params: None }, 40, None);
assert_hash(
hash2,
HashKind::Pbkdf2 {
params: Some(Pbkdf2Params { rounds: 1_000 }),
},
10_838,
Some(PassCharset::Num),
);
}
#[test]
fn check_gen_tree_env() {
let sym = SecretTree::Secret(Secret::String(Zeroizing::new(String::new())));
let tmp_file = NamedTempFile::new().unwrap();
let recipe = Recipe {
files: vec![FileRecipe {
path: tmp_file.path().to_path_buf(),
format: Some(FileFormat::Env),
content: GeneratorTreeWithRef::Map(
[
pass("pass1", None, None),
(
"node".to_string(),
GeneratorTreeWithRef::Map(
[
(
"subnode".to_string(),
GeneratorTreeWithRef::Map(
[
pass(
"pass2",
Some(PassSize::Length(10_000)),
Some(PassCharset::Hex),
),
hash("hash1", None, None, None),
]
.into(),
),
),
ssh("ssh1", None, None),
hash(
"hash2",
Some(HashKind::Pbkdf2 {
params: Some(Pbkdf2Params { rounds: 1_000 }),
}),
Some(PassSize::AtLeastBits(36_000)),
Some(PassCharset::Num),
),
ssh(
"ssh2",
Some(SshKeyKind::Rsa { bits: None }),
Some("my_rsa_key".to_string()),
),
]
.into(),
),
),
]
.into(),
),
sops: Default::default(),
}],
shared: Default::default(),
};
let expected_output = SecretTree::Map(
[
("pass1".to_string(), sym.clone()),
("node_subnode_pass2".to_string(), sym.clone()),
("node_ssh1_private".to_string(), sym.clone()),
("node_ssh1_public".to_string(), sym.clone()),
("node_ssh2_private".to_string(), sym.clone()),
("node_ssh2_public".to_string(), sym.clone()),
("node_subnode_hash1_plain".to_string(), sym.clone()),
("node_subnode_hash1_hash".to_string(), sym.clone()),
("node_hash2_plain".to_string(), sym.clone()),
("node_hash2_hash".to_string(), sym.clone()),
]
.into(),
);
recipe.generate_files().unwrap();
let output = serde_envfile::from_reader(tmp_file).unwrap();
println!("output: {output:?}");
println!("expected_output: {expected_output:?}");
assert_same_struct(&output, &expected_output);
let pass1 = output
.map()
.unwrap()
.get("pass1")
.unwrap()
.secret()
.unwrap();
let pass2 = output
.map()
.unwrap()
.get("node_subnode_pass2")
.unwrap()
.secret()
.unwrap();
let ssh1_priv = output
.map()
.unwrap()
.get("node_ssh1_private")
.unwrap()
.secret()
.unwrap();
let ssh1_pub = output
.map()
.unwrap()
.get("node_ssh1_public")
.unwrap()
.secret()
.unwrap();
let ssh2_priv = output
.map()
.unwrap()
.get("node_ssh2_private")
.unwrap()
.secret()
.unwrap();
let ssh2_pub = output
.map()
.unwrap()
.get("node_ssh2_public")
.unwrap()
.secret()
.unwrap();
let hash1_plain = output
.map()
.unwrap()
.get("node_subnode_hash1_plain")
.unwrap()
.secret()
.unwrap();
let hash1_hash = output
.map()
.unwrap()
.get("node_subnode_hash1_hash")
.unwrap()
.secret()
.unwrap();
let hash2_plain = output
.map()
.unwrap()
.get("node_hash2_plain")
.unwrap()
.secret()
.unwrap();
let hash2_hash = output
.map()
.unwrap()
.get("node_hash2_hash")
.unwrap()
.secret()
.unwrap();
let (
Secret::String(ssh1_priv_secret),
Secret::String(ssh1_pub_secret),
Secret::String(ssh2_priv_secret),
Secret::String(ssh2_pub_secret),
Secret::String(hash1_plain_secret),
Secret::String(hash1_hash_secret),
Secret::String(hash2_plain_secret),
Secret::String(hash2_hash_secret),
) = (
ssh1_priv,
ssh1_pub,
ssh2_priv,
ssh2_pub,
hash1_plain,
hash1_hash,
hash2_plain,
hash2_hash,
)
else {
panic!()
};
assert_pass(pass1, 40, None);
assert_pass(pass2, 10_000, Some(PassCharset::Hex));
assert_ssh(
&Secret::Asym {
private: ssh1_priv_secret.clone(),
public: ssh1_pub_secret.to_string(),
},
SshKeyKind::Ed25519,
"",
);
assert_ssh(
&Secret::Asym {
private: ssh2_priv_secret.clone(),
public: ssh2_pub_secret.to_string(),
},
SshKeyKind::Rsa {
bits: Some(SshKeySize(4096)),
},
"my_rsa_key",
);
assert_hash(
&Secret::Hash {
plain: hash1_plain_secret.clone(),
hash: hash1_hash_secret.to_string(),
},
HashKind::Argon2 { params: None },
40,
None,
);
assert_hash(
&Secret::Hash {
plain: hash2_plain_secret.clone(),
hash: hash2_hash_secret.to_string(),
},
HashKind::Pbkdf2 {
params: Some(Pbkdf2Params { rounds: 1_000 }),
},
10_838,
Some(PassCharset::Num),
);
}
#[test]
fn check_gen_tree_json() {
let tmp_file = NamedTempFile::new().unwrap();
let recipe = gen_recipe(tmp_file.path().to_path_buf(), FileFormat::Json);
let expected_output = gen_expected_output();
recipe.generate_files().unwrap();
let output = serde_json::from_reader(tmp_file).unwrap();
println!("output: {output:?}");
println!("expected_output: {expected_output:?}");
assert_same_struct(&output, &expected_output);
assert_output(output);
}
#[test]
fn check_gen_tree_toml() {
let mut tmp_file = NamedTempFile::new().unwrap();
let recipe = gen_recipe(tmp_file.path().to_path_buf(), FileFormat::Toml);
let expected_output = gen_expected_output();
recipe.generate_files().unwrap();
let mut output_string = String::new();
let _ = tmp_file.read_to_string(&mut output_string);
let output = toml::from_str(&output_string).unwrap();
println!("output: {output:?}");
println!("expected_output: {expected_output:?}");
assert_same_struct(&output, &expected_output);
assert_output(output);
}
#[test]
fn check_gen_tree_yaml() {
let tmp_file = NamedTempFile::new().unwrap();
let recipe = gen_recipe(tmp_file.path().to_path_buf(), FileFormat::Yaml);
let expected_output = gen_expected_output();
recipe.generate_files().unwrap();
let output = serde_saphyr::from_reader(tmp_file).unwrap();
println!("output: {output:?}");
println!("expected_output: {expected_output:?}");
assert_same_struct(&output, &expected_output);
assert_output(output);
}
#[test]
fn check_ref() {
let sym = SecretTree::Secret(Secret::String(Zeroizing::new(String::new())));
let asym = SecretTree::Secret(Secret::Asym {
private: Zeroizing::new(String::new()),
public: String::new(),
});
let hash = SecretTree::Secret(Secret::Hash {
plain: Zeroizing::new(String::new()),
hash: String::new(),
});
let tmp_file_public = NamedTempFile::new().unwrap();
let tmp_file_private1 = NamedTempFile::new().unwrap();
let tmp_file_private2 = NamedTempFile::new().unwrap();
let recipe = Recipe {
files: vec![
FileRecipe {
path: tmp_file_public.path().to_path_buf(),
format: Some(FileFormat::Yaml),
content: GeneratorTreeWithRef::Map(
[(
"my_key".to_string(),
GeneratorTreeWithRef::Ref("my_key.public".to_string()),
)]
.into(),
),
sops: Default::default(),
},
FileRecipe {
path: tmp_file_private1.path().to_path_buf(),
format: Some(FileFormat::Yaml),
content: GeneratorTreeWithRef::Map(
[
(
"my_key".to_string(),
GeneratorTreeWithRef::Ref("my_key.private".to_string()),
),
(
"my_secrets".to_string(),
GeneratorTreeWithRef::Ref("a_group_of_secrets".to_string()),
),
]
.into(),
),
sops: Default::default(),
},
FileRecipe {
path: tmp_file_private2.path().to_path_buf(),
format: Some(FileFormat::Yaml),
content: GeneratorTreeWithRef::Map(
[(
"my_secrets".to_string(),
GeneratorTreeWithRef::Ref("a_group_of_secrets".to_string()),
)]
.into(),
),
sops: Default::default(),
},
],
shared: GeneratorTree::Map(
[
(
"my_key".to_string(),
GeneratorTree::Generator(SecretGenerator::Ssh(Default::default())),
),
(
"a_group_of_secrets".to_string(),
GeneratorTree::Map(
[
(
"secret1".to_string(),
GeneratorTree::Generator(SecretGenerator::Pass(Default::default())),
),
(
"secret2".to_string(),
GeneratorTree::Generator(SecretGenerator::Hash(Default::default())),
),
(
"secret3".to_string(),
GeneratorTree::Generator(SecretGenerator::Ssh(Default::default())),
),
]
.into(),
),
),
]
.into(),
),
};
let expected_output_public = SecretTree::Map([("my_key".to_string(), sym.clone())].into());
let expected_output_private1 = SecretTree::Map(
[
("my_key".to_string(), sym.clone()),
(
"my_secrets".to_string(),
SecretTree::Map(
[
("secret1".to_string(), sym.clone()),
("secret2".to_string(), hash.clone()),
("secret3".to_string(), asym.clone()),
]
.into(),
),
),
]
.into(),
);
let expected_output_private2 = SecretTree::Map(
[(
"my_secrets".to_string(),
SecretTree::Map(
[
("secret1".to_string(), sym.clone()),
("secret2".to_string(), hash.clone()),
("secret3".to_string(), asym.clone()),
]
.into(),
),
)]
.into(),
);
recipe.generate_files().unwrap();
let output_public = serde_saphyr::from_reader(tmp_file_public).unwrap();
let output_private1 = serde_saphyr::from_reader(tmp_file_private1).unwrap();
let output_private2 = serde_saphyr::from_reader(tmp_file_private2).unwrap();
assert_same_struct(&output_public, &expected_output_public);
assert_same_struct(&output_private1, &expected_output_private1);
assert_same_struct(&output_private2, &expected_output_private2);
let public_my_key = output_public
.map()
.unwrap()
.get("my_key")
.unwrap()
.secret()
.unwrap();
let private1_my_key = output_private1
.map()
.unwrap()
.get("my_key")
.unwrap()
.secret()
.unwrap();
let private1_secret1 = output_private1
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret1")
.unwrap()
.secret()
.unwrap();
let private1_secret2 = output_private1
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret2")
.unwrap()
.secret()
.unwrap();
let private1_secret3 = output_private1
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret3")
.unwrap()
.secret()
.unwrap();
let private2_secret1 = output_private2
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret1")
.unwrap()
.secret()
.unwrap();
let private2_secret2 = output_private2
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret2")
.unwrap()
.secret()
.unwrap();
let private2_secret3 = output_private2
.map()
.unwrap()
.get("my_secrets")
.unwrap()
.map()
.unwrap()
.get("secret3")
.unwrap()
.secret()
.unwrap();
let (Secret::String(my_key_public), Secret::String(my_key_private)) =
(public_my_key, private1_my_key)
else {
panic!()
};
assert_pass(private1_secret1, 40, None);
assert_hash(
private1_secret2,
HashKind::Argon2 { params: None },
40,
None,
);
assert_ssh(private1_secret3, SshKeyKind::Ed25519, "");
assert_ssh(
&Secret::Asym {
private: my_key_private.clone(),
public: my_key_public.to_string(),
},
SshKeyKind::Ed25519,
"",
);
assert_eq!(private1_secret1, private2_secret1);
assert_eq!(private1_secret2, private2_secret2);
assert_eq!(private1_secret3, private2_secret3);
}