Skip to content

fix(controller): JSON-unmarshal marshaled expression template before evaluating#6285

Merged
alexec merged 27 commits into
argoproj:masterfrom
crenshaw-dev:fix-escaped-double-quotes
Aug 5, 2021
Merged

fix(controller): JSON-unmarshal marshaled expression template before evaluating#6285
alexec merged 27 commits into
argoproj:masterfrom
crenshaw-dev:fix-escaped-double-quotes

Conversation

@crenshaw-dev
Copy link
Copy Markdown
Member

@crenshaw-dev crenshaw-dev commented Jul 5, 2021

Signed-off-by: Michael Crenshaw michael@crenshaw.dev

Fixes #6260 and #6325

The problem

FastTemplate plucks the {{=whatever}} expression template from the stringified JSON representation of the (Argo script/container) template and replaces it with whatever the expression evaluates to.

So for example, you might start with:

"env": [
  {
    "name": "A",
    "value": "{{=toJson('a')}}"
  }
]

That becomes

"env": [
  {
    "name": "A",
    "value": ""a""
  }
]

When it should be

"env": [
  {
    "name": "A",
    "value": "\"a\""
  }
]

Testing

I tested with this workflow, which would have previously failed:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: expression-template-double-quotes
spec:
  entrypoint: main
  templates:
    - name: main
      script:
        env:
          - name: A
            value: '{{="test"}}'
        image: debian:9.4
        command: [bash]
        source: |
          echo "$A"

And this workflow, which would have previously failed:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: expression-template-double-quotes
spec:
  entrypoint: main
  templates:
    - name: main
      script:
        env:
          - name: A
            value: '{{=toJson('a')}}'
        image: debian:9.4
        command: [bash]
        source: |
          echo "$A"

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@codecov

This comment was marked as resolved.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev force-pushed the fix-escaped-double-quotes branch from fb9f530 to 75c3155 Compare July 5, 2021 20:37
Comment thread docs/running-locally.md
Comment thread hack/recurl.sh
@crenshaw-dev crenshaw-dev marked this pull request as ready for review July 5, 2021 23:02
Copy link
Copy Markdown
Member

@simster7 simster7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to separate your changes to docs/running-locally.md and hack/recurl.sh from the rest into a separate PR, I can merge those immediately.

Comment thread util/template/template_test.go
@crenshaw-dev
Copy link
Copy Markdown
Member Author

@simster7 I haven't really thought through the behavior of expr w.r.t. escaped backslashes... I might add another replace line for \\ -> \.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev force-pushed the fix-escaped-double-quotes branch from 5f34114 to 7011bcc Compare July 8, 2021 18:15
@crenshaw-dev
Copy link
Copy Markdown
Member Author

@simster7 I changed from a naive string replace solution to what I think is a more robust JSON-unmarshal approach.

Apologies if there are test failures, I'm on a machine with an incomplete dev env right now.

I'll open a PR with just the docs/hack changes.

@crenshaw-dev
Copy link
Copy Markdown
Member Author

Note to self: add a test for templates that contain newlines. Seems reasonable to break a long expression template across a couple lines, and I think the JSON-encoding would break the expression evaluation.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev changed the title fix(controller): allow double quotes in expression templates fix(controller): JSON-unmarshal marshaled expression template before evaluating Jul 9, 2021
@crenshaw-dev
Copy link
Copy Markdown
Member Author

I don't know how to interpret this error when running make pre-commit -B locally:

?       github.com/argoproj/argo-workflows/v3/workflow/verify   [no test files]
FAIL
make: *** [test] Error 1

Must be a local config issue, because I don't see it in the CI tests.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev requested a review from simster7 July 9, 2021 15:57
@crenshaw-dev
Copy link
Copy Markdown
Member Author

Just realized the output needs to be marshaled as a JSON string to avoid errors when unmarshaling the template again.

The lack of that step to marshal as a string causes this to fail in 3.1.1:

{{=toJson(map(0..4, {sprig.randNumeric(5)}))}}

@crenshaw-dev crenshaw-dev marked this pull request as draft July 12, 2021 01:44
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev
Copy link
Copy Markdown
Member Author

Methinks the tests are flaky.

@alexec
Copy link
Copy Markdown
Contributor

alexec commented Aug 4, 2021

@markterm what help do you need from us to get this over the line?

@crenshaw-dev
Copy link
Copy Markdown
Member Author

@alexec I think you might have auto-completed the wrong name, but happy for more help if @markterm has time. ;-)

The only thing I want to do is to run a workflow or two with templating in the metrics fields (that's where I made the most invasive changes).

Unfortunately, I can't bill this work to my employer (big yuck), so I'm having to push it forward on weekends. If an Argo dev could verify that metrics templating works fine, I'll be happy with this PR. Otherwise, I'll do my best to carve out a few minutes some evening to give it a test.

@crenshaw-dev
Copy link
Copy Markdown
Member Author

I've been reading the unit test output incorrectly. I see there's a metrics-related failure. Maybe strconv.Quote and JSON marshaling have a bad interaction. Will look at it.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev
Copy link
Copy Markdown
Member Author

@alexec now that I've figured out how to read the test logs, I've knocked out a couple of other bugs.

This one has me stumped:

--- FAIL: TestGetPodByNode (0.10s)
    operator_test.go:3534: 
        	Error Trace:	operator_test.go:3534
        	Error:      	Expected value not to be nil.
        	Test:       	TestGetPodByNode

@crenshaw-dev
Copy link
Copy Markdown
Member Author

It passes locally in GoLand, so... flakiness?

@alexec
Copy link
Copy Markdown
Contributor

alexec commented Aug 4, 2021

That test is flakey. See #6458

@crenshaw-dev
Copy link
Copy Markdown
Member Author

Sweet. Looks like I missed a codegen thingy again. Will run it tomorrow.

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev marked this pull request as ready for review August 5, 2021 13:32
@crenshaw-dev
Copy link
Copy Markdown
Member Author

@alexec looks like this is gonna pass all the checks. I still don't love the code, but it'll fix the issue until we can go to your per-field evaluation approach.

Comment thread examples/expression-reusing-verbose-snippets.yaml Outdated
Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
@crenshaw-dev crenshaw-dev force-pushed the fix-escaped-double-quotes branch from dc7bb59 to f64b644 Compare August 5, 2021 13:46
@alexec alexec enabled auto-merge (squash) August 5, 2021 15:24
@alexec alexec merged commit 2a2ecc9 into argoproj:master Aug 5, 2021
@alexec alexec linked an issue Aug 9, 2021 that may be closed by this pull request
@SVilgelm
Copy link
Copy Markdown

Could you please bump new version with this fix?

@sarabala1979 sarabala1979 mentioned this pull request Aug 11, 2021
28 tasks
sarabala1979 pushed a commit that referenced this pull request Aug 12, 2021
…evaluating (#6285)

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
sarabala1979 pushed a commit that referenced this pull request Aug 12, 2021
…evaluating (#6285)

Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/templating Templating with `{{...}}` type/security Security related

Projects

None yet

5 participants