From fbf57e5c833ccf8b8e6c20111124fd76d9c43197 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sat, 14 Sep 2024 00:58:41 -0400 Subject: [PATCH] fix: Properly escape module json --- Cargo.lock | 2 ++ process/Cargo.toml | 3 --- recipe/src/module.rs | 12 ++---------- template/Cargo.toml | 2 +- template/src/lib.rs | 5 ++++- template/templates/modules/modules.j2 | 4 ++-- 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aaf85a92..6bec7bc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3385,6 +3385,8 @@ dependencies = [ "num-traits", "percent-encoding", "rinja_derive", + "serde", + "serde_json", ] [[package]] diff --git a/process/Cargo.toml b/process/Cargo.toml index 9b377161..58e1825f 100644 --- a/process/Cargo.toml +++ b/process/Cargo.toml @@ -52,6 +52,3 @@ workspace = true [features] sigstore = ["dep:tokio", "dep:sigstore"] - -[patch.crates-io] -sigstore = { git = "https://github.com/gmpinder/sigstore-rs.git", rev = "cd0d10429fc09ddb08a8d1c7244a1623772113af" } diff --git a/recipe/src/module.rs b/recipe/src/module.rs index a3bcc90c..b6166e0c 100644 --- a/recipe/src/module.rs +++ b/recipe/src/module.rs @@ -1,9 +1,9 @@ -use std::{borrow::Cow, path::PathBuf, process}; +use std::{borrow::Cow, path::PathBuf}; use blue_build_utils::syntax_highlighting::highlight_ser; use colored::Colorize; use indexmap::IndexMap; -use log::{error, trace, warn}; +use log::{trace, warn}; use miette::{bail, Result}; use serde::{Deserialize, Serialize}; use serde_yaml::Value; @@ -62,14 +62,6 @@ impl<'a> ModuleRequiredFields<'a> { self.get_module_type_list("containerfile", "snippets") } - #[must_use] - pub fn print_module_context(&'a self) -> String { - serde_json::to_string(self).unwrap_or_else(|e| { - error!("Failed to parse module!!!!!: {e}"); - process::exit(1); - }) - } - #[must_use] #[allow(clippy::missing_const_for_fn)] pub fn get_copy_args(&'a self) -> Option<(Option<&'a str>, &'a str, &'a str)> { diff --git a/template/Cargo.toml b/template/Cargo.toml index 1770d392..9cd32f75 100644 --- a/template/Cargo.toml +++ b/template/Cargo.toml @@ -9,7 +9,7 @@ license.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rinja = "0.3.0" +rinja = { version = "0.3.0", features = ["serde_json"] } blue-build-recipe = { version = "=0.8.17", path = "../recipe" } blue-build-utils = { version = "=0.8.17", path = "../utils" } diff --git a/template/src/lib.rs b/template/src/lib.rs index a2cb382e..6f506e19 100644 --- a/template/src/lib.rs +++ b/template/src/lib.rs @@ -152,7 +152,10 @@ fn should_color() -> bool { mod filters { #[allow(clippy::unnecessary_wraps)] - pub fn replace(input: T, from: char, to: &str) -> rinja::Result { + pub fn replace(input: T, from: char, to: &str) -> rinja::Result + where + T: std::fmt::Display, + { Ok(format!("{input}").replace(from, to)) } } diff --git a/template/templates/modules/modules.j2 b/template/templates/modules/modules.j2 index c2236652..ef21d3c0 100644 --- a/template/templates/modules/modules.j2 +++ b/template/templates/modules/modules.j2 @@ -27,7 +27,7 @@ RUN \ {%- endif %} --mount=type=bind,from=ghcr.io/blue-build/cli:{{ exports_tag }}-build-scripts,src=/scripts/,dst=/tmp/scripts/ \ --mount=type=cache,dst=/var/cache/rpm-ostree,id=rpm-ostree-cache-{{ recipe.name }}-{{ recipe.image_version }},sharing=locked \ - /tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module.print_module_context() }}' \ + /tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module|json|safe }}' \ && ostree container commit {%- endif %} {%- endif %} @@ -59,7 +59,7 @@ RUN \ --mount=type=bind,from=stage-modules,src=/modules,dst=/tmp/modules,rw \ {%- endif %} --mount=type=bind,from=ghcr.io/blue-build/cli:{{ exports_tag }}-build-scripts,src=/scripts/,dst=/tmp/scripts/ \ - /tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module.print_module_context() }}' + /tmp/scripts/run_module.sh '{{ module.module_type }}' '{{ module|json|safe }}' {%- endif %} {%- endif %} {%- endfor %}