Skip to content

Commit 1decd89

Browse files
committed
tweak: Don't emit information about secrets
1 parent 00d04e2 commit 1decd89

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

src/commands/apply.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ impl CommandRunnable for ApplyCommand {
3838
.map(|p| p.into())
3939
.ok_or(errors::user("No configuration directory provided.", "Provide the --config directory when running this command."))?;
4040

41-
let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
42-
4341
let mut output = crate::core::output::output();
4442

43+
let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
4544
for (key, val) in config.iter() {
4645
writeln!(output, " = config {}={}", key, val)?;
4746
}
47+
48+
let secrets = crate::core::config::load_all_config(&config_dir.join("secrets"))?;
49+
for (key, _val) in secrets.iter() {
50+
writeln!(output, " = secret {}=******", key)?;
51+
}
4852

4953
let packages = crate::core::package::get_all_packages(&config_dir.join("packages"))?;
5054

@@ -60,19 +64,25 @@ impl CommandRunnable for ApplyCommand {
6064
config.insert(key, val);
6165
}
6266

67+
let mut secrets = secrets.clone();
68+
for (key, val) in package.get_secrets()? {
69+
writeln!(output, " = secret {}=******", key)?;
70+
secrets.insert(key, val);
71+
}
72+
6373
let root_path = PathBuf::from("/");
6474
let files = package.get_files()?;
6575
for file in files {
6676
let target_path = package.files.get(&file.group).map(|f| f.as_path()).unwrap_or(&root_path);
6777
writeln!(output, " + {} '{}'", if file.is_template { "template" } else { "file" }, target_path.join(&file.relative_path).display())?;
6878

69-
file.apply(target_path, &config)?;
79+
file.apply(target_path, &config, &secrets)?;
7080
}
7181

7282
let tasks = package.get_tasks()?;
7383
for task in tasks {
7484
writeln!(output, " + task '{}'", &task.name)?;
75-
task.run(&config)?;
85+
task.run(&config, &secrets)?;
7686
}
7787
}
7888
Ok(0)
@@ -99,10 +109,10 @@ mod tests {
99109
let output = crate::core::output::mock();
100110

101111
let temp_path = temp.path().to_owned();
102-
crate::core::file::File::apply.mock_safe(move |f, target, config| {
112+
crate::core::file::File::apply.mock_safe(move |f, target, config, secrets| {
103113
let target = Box::leak(Box::new(temp_path.join(target.strip_prefix("/").unwrap())));
104114

105-
MockResult::Continue((f, target, config))
115+
MockResult::Continue((f, target, config, secrets))
106116
});
107117

108118
crate::core::config::load_script_config.mock_safe(|interpreter, _file| {

src/commands/plan.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ impl CommandRunnable for PlanCommand {
3838
.map(|p| p.into())
3939
.ok_or(errors::user("No configuration directory provided.", "Provide the --config directory when running this command."))?;
4040

41-
let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
42-
4341
let mut output = crate::core::output::output();
44-
42+
43+
let config = crate::core::config::load_all_config(&config_dir.join("config"))?;
4544
for (key, val) in config {
4645
writeln!(output, " = config {}={}", key, val)?;
4746
}
47+
48+
let secrets = crate::core::config::load_all_config(&config_dir.join("secrets"))?;
49+
for (key, _val) in secrets {
50+
writeln!(output, " = secret {}=******", key)?;
51+
}
4852

4953
let packages = crate::core::package::get_all_packages(&config_dir.join("packages"))?;
5054

@@ -58,6 +62,11 @@ impl CommandRunnable for PlanCommand {
5862
writeln!(output, " = config {}={}", key, val)?;
5963
}
6064

65+
let secrets = package.get_secrets()?;
66+
for (key, _val) in secrets {
67+
writeln!(output, " = secret {}=******", key)?;
68+
}
69+
6170
let root_path = PathBuf::from("/");
6271
let files = package.get_files()?;
6372
for file in files {
@@ -90,7 +99,7 @@ mod tests {
9099

91100
let output = crate::core::output::mock();
92101

93-
crate::core::file::File::apply.mock_safe(|_f, _target, _config| {
102+
crate::core::file::File::apply.mock_safe(|_f, _target, _config, _secrets| {
94103
panic!("The file should not have been written during the planning phase.");
95104
});
96105

src/core/file.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,26 @@ pub fn get_files(dir: &Path) -> Result<Vec<File>, errors::Error> {
9999

100100
#[cfg_attr(test, mockable)]
101101
impl File {
102-
#[instrument(level = "info", name = "file.apply", fields(file.path = %self.relative_path.display()), err, skip(self))]
102+
#[instrument(level = "info", name = "file.apply", fields(file.path = %self.relative_path.display()), err, skip(self, secrets))]
103103
pub fn apply(
104104
&self,
105105
target: &Path,
106106
config: &HashMap<String, String>,
107+
secrets: &HashMap<String, String>,
107108
) -> Result<(), errors::Error> {
108109
if self.is_template {
109-
self.template(target, config)
110+
self.template(target, config, secrets)
110111
} else {
111112
self.copy(target)
112113
}
113114
}
114115

115-
#[instrument(level = "debug", name = "file.template", fields(file.path = %self.relative_path.display()), err, skip(self))]
116+
#[instrument(level = "debug", name = "file.template", fields(file.path = %self.relative_path.display()), err, skip(self, secrets))]
116117
fn template(
117118
&self,
118119
target: &Path,
119120
config: &HashMap<String, String>,
121+
secrets: &HashMap<String, String>,
120122
) -> Result<(), errors::Error> {
121123
let output_path = target.join(&self.relative_path);
122124

@@ -132,6 +134,10 @@ impl File {
132134
context.insert(key.clone(), Value::String(val.clone()));
133135
}
134136

137+
for (key, val) in secrets {
138+
context.insert(key.clone(), Value::String(val.clone()));
139+
}
140+
135141
let context = Value::Object(context);
136142

137143
let rendered = template(&template_content, context)

src/core/package.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl Package {
4545
super::config::load_all_config(&self.path.join("config"))
4646
}
4747

48+
pub fn get_secrets(&self) -> Result<HashMap<String, String>, errors::Error> {
49+
super::config::load_all_config(&self.path.join("secrets"))
50+
}
51+
4852
pub fn get_files(&self) -> Result<Vec<File>, errors::Error> {
4953
super::file::get_all_files(&self.path.join("files"))
5054
}

src/core/script.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ pub fn get_all_scripts(dir: &Path) -> Result<Vec<Script>, errors::Error> {
4747

4848
#[cfg_attr(test, mockable)]
4949
impl Script {
50-
#[instrument(level = "info", name = "script.run", fields(task.name = %self.name, task.path = %self.path.display()), err, skip(self))]
51-
pub fn run(&self, config: &HashMap<String, String>) -> Result<(), errors::Error> {
50+
#[instrument(level = "info", name = "script.run", fields(task.name = %self.name, task.path = %self.path.display()), err, skip(self, secrets))]
51+
pub fn run(
52+
&self,
53+
config: &HashMap<String, String>,
54+
secrets: &HashMap<String, String>,
55+
) -> Result<(), errors::Error> {
5256
let extension = match self.path.extension() {
5357
Some(ext) => ext.to_str().ok_or(errors::user(
5458
&format!("Unable to parse the file extension used by the task file '{}'", self.path.display()),
@@ -58,12 +62,17 @@ impl Script {
5862
&format!("Could not determine how to run the task file '{}' because it did not have a file extension.", self.path.display()),
5963
"Use one of the supported file extensions to tell buckle how to execute this task file."))?
6064
};
65+
66+
let mut config = config.clone();
67+
for (key, val) in secrets {
68+
config.insert(key.clone(), val.into());
69+
}
6170

6271
match extension {
63-
"ps1" => run_script_task("pwsh", config, &self.path)?,
64-
"sh" => run_script_task("bash", config, &self.path)?,
65-
"bat" => run_script_task("cmd.exe", config, &self.path)?,
66-
"cmd" => run_script_task("cmd.exe", config, &self.path)?,
72+
"ps1" => run_script_task("pwsh", &config, &self.path)?,
73+
"sh" => run_script_task("bash", &config, &self.path)?,
74+
"bat" => run_script_task("cmd.exe", &config, &self.path)?,
75+
"cmd" => run_script_task("cmd.exe", &config, &self.path)?,
6776
_ => Err(errors::user(
6877
&format!(
6978
"The '{}' extension is not supported for task files.",
@@ -78,11 +87,15 @@ impl Script {
7887
}
7988

8089
#[cfg_attr(test, mockable)]
81-
#[instrument(name = "command.run", fields(stdout, stderr), skip(config), err)]
82-
pub fn run_script_task(interpreter: &str, config: &HashMap<String, String>, file: &Path) -> Result<(), errors::Error> {
90+
#[instrument(name = "command.run", fields(stdout, stderr), skip(env), err)]
91+
pub fn run_script_task(
92+
interpreter: &str,
93+
env: &HashMap<String, String>,
94+
file: &Path,
95+
) -> Result<(), errors::Error> {
8396
process::Command::new(interpreter)
8497
.arg(file)
85-
.envs(config)
98+
.envs(env)
8699
.output()
87100
.map_err(|err| errors::user_with_internal(
88101
&format!("Failed to execute the command '{} {}'.", interpreter, file.display()),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MAGIC=flash

src/test/data/secrets/magic.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MAGIC=<><

0 commit comments

Comments
 (0)