Skip to content

Commit 2a42a68

Browse files
committed
Gracefully report error in tag_format template
`render!()` panics when a strict-mode error occurs. Add a small helper fn to render template strings without the macro and gracefully return errors. Signed-off-by: Robert Detjens <[email protected]>
1 parent 62369ad commit 2a42a68

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/access_handlers/docker.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use bollard::{
66
};
77
use futures::{StreamExt, TryStreamExt};
88
use itertools::Itertools;
9-
use minijinja::render;
9+
use minijinja;
1010
use tokio;
1111
use tracing::{debug, error, info, trace, warn};
1212

13-
use crate::clients::docker;
13+
use crate::clients::{docker, render_strict};
1414
use crate::configparser::{get_config, get_profile_config};
1515

1616
/// container registry / daemon access checks
@@ -27,13 +27,16 @@ pub async fn check(profile_name: &str) -> Result<()> {
2727

2828
// build test image string
2929
// registry.example.com/somerepo/testimage:pleaseignore
30-
let test_image = render!(
30+
let test_image = render_strict(
3131
&registry_config.tag_format,
32+
minijinja::context! {
3233
domain => registry_config.domain,
3334
challenge => "accesscheck",
3435
container => "testimage",
3536
profile => profile_name
36-
);
37+
},
38+
)
39+
.context("could not render tag format template")?;
3740
debug!("will push test image to {}", test_image);
3841

3942
// push alpine image with build credentials

src/clients.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,19 @@ pub async fn wait_for_status(client: &kube::Client, object: &DynamicObject) -> R
345345

346346
Ok(())
347347
}
348+
349+
//
350+
// Minijinja strict rendering with error
351+
//
352+
353+
/// Similar to minijinja.render!(), but return Error if any undefined values.
354+
pub fn render_strict(template: &str, context: minijinja::Value) -> Result<String> {
355+
let mut strict_env = minijinja::Environment::new();
356+
// error on any undefined template variables
357+
strict_env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict);
358+
359+
let r = strict_env
360+
.render_str(template, context)
361+
.context(format!("could not render template {:?}", template))?;
362+
Ok(r)
363+
}

src/configparser/challenge.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use figment::Figment;
44
use fully_pub::fully_pub;
55
use glob::glob;
66
use itertools::Itertools;
7-
use minijinja::render;
87
use serde::{Deserialize, Serialize};
98
use serde_nested_with::serde_nested;
109
use std::collections::HashMap as Map;
@@ -13,6 +12,7 @@ use std::str::FromStr;
1312
use tracing::{debug, error, info, trace, warn};
1413
use void::Void;
1514

15+
use crate::clients::render_strict;
1616
use crate::configparser::config::Resource;
1717
use crate::configparser::field_coersion::string_or_struct;
1818
use crate::configparser::get_config;
@@ -154,13 +154,16 @@ impl ChallengeConfig {
154154
match &pod.image_source {
155155
ImageSource::Image(t) => Ok(t.to_string()),
156156
// render image tag template from config
157-
ImageSource::Build(b) => Ok(render!(
157+
ImageSource::Build(b) => render_strict(
158158
&get_config()?.registry.tag_format,
159-
domain => config.registry.domain,
160-
challenge => self.slugify(),
161-
container => pod.name,
162-
profile => profile_name
163-
)),
159+
minijinja::context! {
160+
domain => config.registry.domain,
161+
challenge => self.slugify(),
162+
container => pod.name,
163+
profile => profile_name
164+
},
165+
)
166+
.context("error rendering challenge image tag template"),
164167
}
165168
}
166169

0 commit comments

Comments
 (0)