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

Parser cover more cases #5

Merged
merged 6 commits into from
Nov 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Adding nixosModule config:
name = "template.txt";
# to be notice that the source secret file may have trailing `\n`
content = "this is a template for testing ${config.vaultix.placeholder.example}";
# removing trailing and leading whitespace by default
trim = true;
# ...
};
}
};
Expand Down
4 changes: 4 additions & 0 deletions module/template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ let
Content of the template
'';
};
trim = (mkEnableOption { }) // {
default = true;
description = "remove trailing and leading whitespace of the secret content to insert";
};
name = mkOption {
type = types.str;
default = submod.config._module.args.name;
Expand Down
16 changes: 13 additions & 3 deletions src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,27 @@ impl Profile {
let mut template = t.content.clone();
let hashstrs_of_it = t.parse_hash_str_list().expect("parse template");

let trim_the_insertial = t.trim;

hashstr_ctx_map
.iter()
.filter(|(k, _)| hashstrs_of_it.contains(k))
.for_each(|(k, v)| {
// render
// render and insert
trace!("template before process: {}", template);

let raw_composed_insertial = String::from_utf8_lossy(v).to_string();

let insertial = if trim_the_insertial {
raw_composed_insertial.trim()
} else {
raw_composed_insertial.as_str()
};

template = template.replace(
format!("{{{{ {} }}}}", hex::encode(k.as_slice())).as_str(),
String::from_utf8_lossy(v).to_string().as_str(),
insertial,
);
trace!("processed template: {}", template);
});

deploy_to_fs(
Expand Down
66 changes: 52 additions & 14 deletions src/helper/template.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::profile::Template;
use eyre::Result;
use nom::{
bytes::complete::{is_not, tag, take_while_m_n},
bytes::complete::{tag, take_while_m_n},
combinator::verify,
error::Error,
sequence::delimited,
IResult,
Expand All @@ -15,15 +16,23 @@ fn parse_braced_hash(input: &str) -> IResult<&str, &str, Error<&str>> {
)(input)
}

fn pars<'a>(text: &'a str, res: &mut Vec<&'a str>) {
if let Ok((brace_start_then, _)) = is_not::<&str, &str, Error<&str>>("{")(text) {
if let Ok((remain, hashes)) = parse_braced_hash(brace_start_then) {
res.push(hashes);
if !remain.is_empty() {
pars(remain, res);
}
pub fn extract_all_hashes<'a>(input: &'a str, res: &mut Vec<&'a str>) {
if let Ok((o, b)) = verify(parse_braced_hash, |_: &str| true)(input) {
res.push(b);
extract_all_hashes(o, res)
} else if input.len() < 66 {
// less than expected `{{ hash }}` length
return;
} else {
let this = {
// handle multibytes
let res = input.char_indices().nth(1).map_or("", |(i, _)| &input[i..]);
// skip to next `{`
res.find('{').map_or("", |index| &res[index..])
};
};

extract_all_hashes(this, res)
}
}

impl Template {
Expand All @@ -32,17 +41,14 @@ impl Template {
let text = &self.content;

let mut res = vec![];
let text = format!(" {}", text); // hack
pars(text.as_str(), &mut res);
extract_all_hashes(text.as_str(), &mut res);
Ok(res
.into_iter()
.map(|s| decode(s).expect("hex decode"))
.collect())
}
}

// pub struct Template

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -52,7 +58,7 @@ mod tests {
#[test]
fn parse_template_single() {
let str =
"here has {{ dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93 }} whch should be replaced";
"here has {{ dcd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440aa93 }} which should be replaced";

let t = Template {
content: String::from(str),
Expand Down Expand Up @@ -96,6 +102,38 @@ mod tests {
)
}
#[test]
fn parse_template_normal() {
let str = "{{ cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93 }}";

let t = Template {
content: String::from(str),
..Template::default()
};
let l = t.parse_hash_str_list().unwrap();
assert_eq!(
hex!("cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93"),
l.first().unwrap().as_slice()
)
}
#[test]
fn parse_template_mix() {
let str = "{{ cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93 }} {{ {{ c4e0ae1067d1ee736e051d7927d783bb70b032bf116f618454bf47122956d5ce }}";

let t = Template {
content: String::from(str),
..Template::default()
};
let l = t.parse_hash_str_list().unwrap();
assert_eq!(
hex!("cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93"),
l.first().unwrap().as_slice()
);
assert_eq!(
hex!("c4e0ae1067d1ee736e051d7927d783bb70b032bf116f618454bf47122956d5ce"),
l.get(1).unwrap().as_slice()
);
}
#[test]
fn parse_template_with_heading_white() {
let str = " {{ cd789434d890685da841b8db8a02b0173b90eac3774109ba9bca1b81440a2a93 }}";

Expand Down
1 change: 1 addition & 0 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Secret {
pub struct Template {
pub name: String,
pub content: String,
pub trim: bool,
pub group: String,
pub mode: String,
pub owner: String,
Expand Down