Skip to content
This repository was archived by the owner on Apr 18, 2026. It is now read-only.

Fix: Enforce presence of Key and Value keys on field tag definition#266

Merged
mlevesquedion merged 7 commits into
google:masterfrom
mlevesquedion:fix-field-tag-value-bug
Feb 1, 2021
Merged

Fix: Enforce presence of Key and Value keys on field tag definition#266
mlevesquedion merged 7 commits into
google:masterfrom
mlevesquedion:fix-field-tag-value-bug

Conversation

@mlevesquedion
Copy link
Copy Markdown
Contributor

@mlevesquedion mlevesquedion commented Jan 28, 2021

I ran into this issue while writing the quickstart user guide (#265).

Originally when I was writing the config, I wrote:

FieldTags:
  - Key: datapolicy
    Value: password

Currently, we "allow" Value as a "valid" key, but in reality, if you use Value instead of Val, the Val will be empty and your matcher won't actually match anything. In fact, we don't check that the fields are present, only that if some fields are present, they are one of key, val, value.

So e.g. if you write:

FieldTags:
  - Foo: Bar

the code will complain about Foo being an invalid key, but if you write this:

FieldTags:
  - Key: Key

it won't complain about the missing Val field, but your matcher won't be able to match anything. (Unless your value really is empty, but I don't think we should support that use case.)

This PR fixes the issue by checking that the parsed Key and Val are non-empty strings, and producing an error if that isn't the case.

  • Running against a large codebase such as Kubernetes does not error out. (See DEVELOPING.md for instructions on how to do that.)
  • (N/A) [ ] Appropriate changes to README are included in PR

@@ -147,8 +147,9 @@ type fieldTagMatcher struct {

// this type uses the default unmarshaller and mirrors configuration key-value pairs
type rawFieldTagMatcher struct {
Copy link
Copy Markdown
Member

@vinayakankugoyal vinayakankugoyal Jan 28, 2021

Choose a reason for hiding this comment

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

Wont this allow to be parsed?
FieldTags:
- Key: datapolicy
Val: cert
Value: password

Copy link
Copy Markdown
Contributor Author

@mlevesquedion mlevesquedion Jan 28, 2021

Choose a reason for hiding this comment

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

That is correct. In that case we should probably do like we are doing with e.g. Package and PackageRE, and only allow one or the other to be set. Although, in my opinion there is little value in having both Val and Value, so I think we may actually want to keep only one of them. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah lets just keep Value and treat setting Val as an error. Being strict while parsing the config is totally fine IMO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@PurelyApplied, @benhxy, thoughts on this?

Copy link
Copy Markdown
Contributor

@PurelyApplied PurelyApplied Jan 29, 2021

Choose a reason for hiding this comment

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

Oh, I had a WIP branch that resolved some of this with RawMessage and switch statements. *dig dig dig*

#271 opened as draft. I hadn't added tests yet, but it hits some of the concerns you've voiced. There's also the issue where, if we want to be case insensitive, you could theoretically add all of key, Key, KEY, etc and have collisions that way while still being valid YAML. #271 pokes at the other matchers, too, since I wasn't a huge fan of that Valid Keys slice.

That is correct. In that case we should probably do like we are doing with e.g. Package and PackageRE, and only allow one or the other to be set. Although, in my opinion there is little value in having both Val and Value, so I think we may actually want to keep only one of them. WDYT?

I'd be +1 to only retaining Value. I think we'd kept Val because we didn't want to rewrite existing test configs. But we're v0.x. We can make a config-breaking change here.

Comment thread internal/pkg/config/matcher_test.go Outdated
wantErr: false,
},
{
desc: "valid field tag config using value",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we add a case where both Val and Value are set?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test wont be necessary if we only allow Value and not Val.

@mlevesquedion mlevesquedion changed the title Enforce presence of Key and Val keys on field tag definition Enforce presence of Key and Value keys on field tag definition Feb 1, 2021
Comment thread .github/workflows/ci.yaml
- name: Check YAML
uses: ibiqlik/action-yamllint@v3
run: |
yamllint .
Copy link
Copy Markdown
Contributor Author

@mlevesquedion mlevesquedion Feb 1, 2021

Choose a reason for hiding this comment

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

I had to change this because the action was reporting an error, while running yamllint locally did not. As it turns out, Github Actions runners have yamllint installed out of the box, so we don't need to use a third-party action.

Copy link
Copy Markdown
Contributor

@PurelyApplied PurelyApplied left a comment

Choose a reason for hiding this comment

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

LGTM. There's still plenty of cleanup that could be done, but that could wait for a future PR / until I clean up #271.

We still have a case insensitivity issue that could use a test, e.g.

FieldTags:
  - Key: levee
    key: also-levee
    Value: source
    value: ExtraSourcey

@mlevesquedion mlevesquedion merged commit 15d2c2d into google:master Feb 1, 2021
@mlevesquedion mlevesquedion deleted the fix-field-tag-value-bug branch February 1, 2021 20:56
@mlevesquedion mlevesquedion changed the title Enforce presence of Key and Value keys on field tag definition Fix: Enforce presence of Key and Value keys on field tag definition Feb 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants