Skip to content

Commit

Permalink
Enforce presence of Key and Value keys on field tag definition (googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
mlevesquedion authored Feb 1, 2021
1 parent 61f8356 commit 15d2c2d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ jobs:
staticcheck ./...
- name: Check YAML
uses: ibiqlik/action-yamllint@v3
run: |
yamllint .
test:
name: Test
Expand Down
2 changes: 1 addition & 1 deletion configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The tag `levee:"source"` is built-in. Additional tags may be identified via expl
```yaml
FieldTags:
- Key: levee
Val: source
Value: source
```
Sinks and sanitizers are identified by package, method, and (if applicable) receiver name.
Expand Down
23 changes: 16 additions & 7 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c Config) IsSourceFieldTag(tag string) bool {
for _, ft := range c.FieldTags {
val := st.Get(ft.Key)
for _, v := range strings.Split(val, ",") {
if v == ft.Val {
if v == ft.Value {
return true
}
}
Expand Down Expand Up @@ -141,18 +141,18 @@ func (vacuousMatcher) MatchString(string) bool {
}

type fieldTagMatcher struct {
Key string
Val string
Key string
Value string
}

// this type uses the default unmarshaller and mirrors configuration key-value pairs
type rawFieldTagMatcher struct {
Key string
Val string
Key string
Value string
}

func (ft *fieldTagMatcher) UnmarshalJSON(bytes []byte) error {
validFieldTagMatcherFields := []string{"key", "val", "value"}
validFieldTagMatcherFields := []string{"key", "value"}
if err := validateFieldNames(&bytes, "fieldTagMatcher", validFieldTagMatcherFields); err != nil {
return err
}
Expand All @@ -161,8 +161,17 @@ func (ft *fieldTagMatcher) UnmarshalJSON(bytes []byte) error {
if err := json.Unmarshal(bytes, &raw); err != nil {
return err
}

if raw.Key == "" {
return fmt.Errorf("invalid field tag matcher: please provide a non-empty Key")
}
if raw.Value == "" {
return fmt.Errorf("invalid field tag matcher: please provide a non-empty Value")
}

ft.Key = raw.Key
ft.Val = raw.Val
ft.Value = raw.Value

return nil
}

Expand Down
32 changes: 29 additions & 3 deletions internal/pkg/config/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,43 @@ func TestFieldTagMatcherUnmarshalling(t *testing.T) {
desc, yaml string
wantErr bool
}{
{
desc: "missing value",
yaml: "key: foo",
wantErr: true,
},
{
desc: "missing key",
yaml: "value: foo",
wantErr: true,
},
{
desc: "unknown field is not allowed",
yaml: `
Key: good
Value: good
UnknownField: bad`,
wantErr: true,
}, {
desc: "valid field tag config",
},
{
desc: "val should be value",
yaml: `
key: good
val: AlsoGood`,
val: "two letters short"`,
wantErr: true,
},
{
desc: "valid field tag config, lowercase",
yaml: `
key: foo
value: bar`,
wantErr: false,
},
{
desc: "valid field tag config, titlecase",
yaml: `
Key: foo
Value: bar`,
wantErr: false,
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/config/testdata/test-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sinks:
receiverRE: "^Sinker$"
fieldTags:
- key: example
val: sensitive
value: sensitive
exclude:
- packageRE: "^example.com/exclusion$"
methodRE: "^Foo$"
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/fieldtags/testdata/test-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
---
FieldTags:
- Key: example
Val: sensitive
Value: sensitive

0 comments on commit 15d2c2d

Please sign in to comment.