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

Expected Exception during execution #1038

Closed
DaspawnW opened this issue Mar 28, 2023 · 0 comments · Fixed by #1093
Closed

Expected Exception during execution #1038

DaspawnW opened this issue Mar 28, 2023 · 0 comments · Fixed by #1093

Comments

@DaspawnW
Copy link

I have a custom helm chart post renderer https://github.com/DaspawnW/vault-crd-helm-renderer that receives a valid helm chart with some jinja2 templates and performs vault lookups to replace those lookups with real values out of HashiCorp Vault.

By accident we detected today that Jinjava doesn't enforce a strict mode by default. If you provide an expression that is invalid like this one:

String content = "{{ vault.lookupV2('kv-test/credentials', 'username) }}"

jinjava().renderForResult(content, context);

I would expect that Jinjava now fails as it doesn't contain the last "'", but it seems that the rendering continues and everything that happens after this invalid expression now fails to render properly.

Is there a way to enforce Jinjava to be in a strict mode?

Please see below a testcase where I would expect that it fails when the expression is wrongly written:

import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.TemplateError;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;

class TestClass {

    public String hello(String msg) {
        return "test" + msg;
    }

}

public class InvalidJinja {

    public Jinjava jinjava() {
        JinjavaConfig config = JinjavaConfig
                .newBuilder()
                .build();
        Jinjava jinjava = new Jinjava(config);
        return jinjava;
    }

    @Test
    void shouldFailToRender() {
        HashMap<String, Object> context = new HashMap<>();
        context.put("test", new TestClass());

        String content = "{{ test.hello('world) }}";
        RenderResult renderResult = jinjava().renderForResult(content, context);

        List<TemplateError> errors = renderResult.getErrors();

        System.out.println(content);
        Assertions.assertEquals(1, errors.size());
    }

}
jasmith-hs added a commit that referenced this issue Jul 24, 2023
jasmith-hs added a commit that referenced this issue Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant