Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Use yaml.UnmarshalStrict() during strict validation #201

Merged
merged 1 commit into from Apr 11, 2018

Conversation

aeijdenberg
Copy link
Contributor

Earlier this week the go-yaml/yaml#307 landed which adds checking of duplicate keys when unmarshaling yaml, which would have saved me a few hours with a buggy pipeline that I wrote.

This PR changes to the behaviour of verify-pipeline to use yaml.UnmarshalStrict(), only when the existing --strict option is set. This also has the benefit of picking up any field names in a yaml file that don't match a struct (when unmarshalled into a map, anything is allowed) which can help detect typos.

Note that in order for these tests to pass, you will need to vendor in a new version of gopkg.in/yaml.v2. I'm not sure how they plan to update their branch - in my testing of below I updated to the tip of the devel branch at https://github.com/go-yaml/yaml, which I verified passes the full set of integration tests (testflight).

diff --git a/src/gopkg.in/yaml.v2 b/src/gopkg.in/yaml.v2
index e4d366fc..031c9222 160000
--- a/src/gopkg.in/yaml.v2
+++ b/src/gopkg.in/yaml.v2
@@ -1 +1 @@
-Subproject commit e4d366fc3c7938e2958e662b4258c7a89e1f0e3e
+Subproject commit 031c922227a592b2b562a1833438308381f9a8bf

(maybe you'll want to wait for a newer release from them?)

Anyway, submitting the PR now with some basic tests.

It might be interesting to consider using yaml.UnmarshalStrict() everywhere that yaml.Unmarshal() is used, but that's a bigger change to contemplate.

@aeijdenberg
Copy link
Contributor Author

Build failure is expected. Will need a newer version of gopkg.in/yaml.v2 to get UnmarshalStrict() at all, and then a newer version again (tip of the devel branch) in order to make all of the tests pass.

@vito
Copy link
Contributor

vito commented Jan 11, 2018

Thanks! Seems worth considering just always being strict. Are there any downsides/breakages from that, besides obviously breaking for people with invalid configurations which were silently being accepted before?

I'm gonna wait for go-yaml/yaml to have it in the v2 branch before merging.

@aeijdenberg
Copy link
Contributor Author

Hi @vito - I can't think of any reason not to use it everywhere, assuming that it does what it says on the box correctly, and if writing new code I definitely would. I'm not sure whether Concourse stores any yaml that it later reads out again, and whether stricter parsing might cause issues then?

When prepping this PR I did find that of the code in fly that calls yaml.Unmarshal() is actually in the bosh-cli package, so it might be a more involved change to try to push it everywhere.

@aeijdenberg
Copy link
Contributor Author

@vito - looks like a new version of gopkg.in/yaml.v2 has been released which has the changes we need, so this ought to be able to be merged now.

e.g. https://gopkg.in/yaml.v2 points to https://github.com/go-yaml/yaml/tree/v2.1.1 which includes go-yaml/yaml@77b384e

@marco-m
Copy link

marco-m commented Apr 7, 2018

Hello any news ?

@vito vito removed the blocked label Apr 11, 2018
@vito
Copy link
Contributor

vito commented Apr 11, 2018

Taking a look now! I'll merge this manually by bumping gopkg at the same time if the tests pass locally.

@vito vito merged commit bb31d4d into vmware-archive:master Apr 11, 2018
vito added a commit to concourse/concourse that referenced this pull request Apr 11, 2018
vmware-archive/fly#201

Submodule src/github.com/concourse/fly f9d92f5..2be951a:
  > Merge branch 'govau-unmarshalstrict'
  > Merge pull request #214 from alepee/patch-1
Submodule src/gopkg.in/yaml.v2 e4d366fc..5420a8b6:
  > Use underlying float precision when formatting floats (#353)
  > Fix typo in tab error message (#208).
  > Fix misspell of precede in ported code (#216)
  > Fix type on Marshal docs (#206).
  > Fixed typos in docstrings (#179).
  > Drop unnecessary explicit timestamp tags.
  > Fix broken test from last merge.
  > increment non-zero scanner error lines (#319)
  > Convert int to float when explicitly tagged.
  > Encode and decode arrays.
  > Fix edge case when decoding MinInt as -0b.
  > Remove mention of non-existent examples folder.
  > Fix curious assumption from the original C reader.
  > Ensure scanner has data before checking for blanks.
  > Drop invalid simple key assertion.
  > Improve map stabilization logic.
  > Fix unstable map key ordering (#195).
  > Merge pull request #336 from rogpeppe/025-go.mod
  > Merge pull request #335 from rogpeppe/024-merge-devel
  > Merge pull request #253 from heldtogether/patch-1
  > Merge pull request #308 from rogpeppe/016-revert-v2-PR273
  > Merge pull request #273 from rogpeppe/006-timestamps
  > Merge pull request #299 from rogpeppe/009-gofmt
  > Merge pull request #281 from houshengbo/fix-incorrect-line-number
  > Correct documentation for Marshal (#287)
  > Merge pull request #289 from rliebz/null-fix
  > Replace LICENSE text with actual license (#274)
  > Merge pull request #272 from rogpeppe/005-cleaner-tag-scan
  > Merge pull request #103 from andreychernih/bugfix/non-specific-tags
  > Merge pull request #271 from rogpeppe/004-embedded-example
  > Merge pull request #94 from mvo5/feature/embeded-structs-example
  > Merge pull request #264 from hiveminded/v2
  > Merge pull request #262 from wupeka/v2
  > Remove unreachable code to fix go vet (#249)
  > Fix dead URL for yaml specification (#240)
  > Tighten restrictions on float decoding (#171)
  > Fix decode test for Go 1.8 (#217)
  > Fix unmarshaler handling of empty strings.
  > new license in the README file (#189)
@vito
Copy link
Contributor

vito commented Apr 11, 2018

Thanks! All good.

@marco-m
Copy link

marco-m commented Apr 11, 2018

Man! beer++ if we meet one day!

@vito vito modified the milestone: v3.11.0 Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants