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

Expose unknown fields and duplicate sections as diagnose warnings #11455

Merged
merged 6 commits into from
May 4, 2021

Conversation

sgmiller
Copy link
Collaborator

No description provided.

@vercel vercel bot temporarily deployed to Preview – vault-storybook April 26, 2021 21:52 Inactive
@vercel vercel bot temporarily deployed to Preview – vault April 26, 2021 21:52 Inactive
Copy link
Contributor

@swayne275 swayne275 left a comment

Choose a reason for hiding this comment

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

A lot of it looks good, just have a few comments

for _, ex := range expected {
// Only test the string, pos may change
if ex.Problem == er1.Problem {
continue outer
Copy link
Contributor

Choose a reason for hiding this comment

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

could these be refactored to just break and not have the tags?

Also, what are those tags called? I've only seen them with GOTO

Copy link
Contributor

Choose a reason for hiding this comment

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

And I ask about refactoring (at least for this loop) because i think it might be easier for another eng to understand down the road (i think it would have taken me less time to get it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They're block labels. The idea there was if you just break on the inner one you'll hit the t.Fatal, which isn't what we want. That said I can do it with a found bool instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

cool, i haven't seen that used before! closest i've seen is with goto. i'm gonna read about them!

do whatever you think is most readable, i'm not too concerned about this

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -13,6 +14,10 @@ import (

// SharedConfig contains some shared values
type SharedConfig struct {
FoundKeys []string `hcl:",decodedFields"`
UnusedKeys map[string][]token.Pos `hcl:",unusedKeyPositions"`
Copy link
Contributor

Choose a reason for hiding this comment

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

did you want this to be of type UnusedKeyMap as defined in config_util.go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah nice, I must have added that type later but didn't loop back.

func (ec *EntSharedConfig) ParseConfig(list *ast.ObjectList) error {
return nil
}

func ParseEntropy(result *SharedConfig, list *ast.ObjectList, blockName string) error {
return nil
}

// Creates the ConfigErrors for unused fields, which occur in various structs
func ValidateUnusedFields(unusedKeyPositions map[string][]token.Pos, source string) []ConfigError {
Copy link
Contributor

Choose a reason for hiding this comment

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

did you want the type to be UnusedKeyMap here too?

Copy link
Contributor

Choose a reason for hiding this comment

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

and in a few other places in this file


func UnusedFieldDifference(a, b map[string][]token.Pos, foundKeys []string) map[string][]token.Pos {
if a == nil {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

we have different behavior with a being nil vs b. could we document what a and b are supposed to be so that future users of this code know that order matters?

StatsiteAddr string `hcl:"statsite_address"`
StatsdAddr string `hcl:"statsd_address"`
FoundKeys []string `hcl:",decodedFields"`
UnusedKeys map[string][]token.Pos `hcl:",unusedKeyPositions"`
Copy link
Contributor

Choose a reason for hiding this comment

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

same about UnusedKeyMap

@@ -32,6 +32,16 @@ func StrListContains(haystack []string, needle string) bool {
return false
}

// StrListContainsCaseInsensitive looks for a string in a list of strings.
func StrListContainsCaseInsensitive(haystack []string, needle string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

love the variable names :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that's pretty common in other APIs. Suprised Go didn't use it for things like strings.Contains

Copy link
Contributor

@HridoyRoy HridoyRoy left a comment

Choose a reason for hiding this comment

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

Just a few comments. Was primarily wondering about the tests. Overall, looks awesome!

@@ -155,7 +155,11 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int {
c.UI.Output(err.Error())
return 1
}

errors := config.Validate("test")
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we change source for Validate to something like filename to make this more clear? Or sourceFilePath, or something of that nature which denotes it is a file path?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep

}
res := make(map[string][]token.Pos)
for k, v := range a {
if _, ok := b[k]; !ok && !strutil.StrListContainsCaseInsensitive(foundKeys, govalidator.UnderscoreToCamelCase(k)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does k need to be converted to govalidator.UnderscoreToCamelCase ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because HCL's AST refers to the struct member name, while the found keys uses the string from the .hcl file (in lower case with underscores)

expected := []configutil.ConfigError{
{
Problem: "unknown field bad_value found in configuration",
Position: token.Pos{},
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the Pos is being checked in the test. If possible, could we see this in action?

@@ -57,3 +57,7 @@ func TestConfigRaftRetryJoin(t *testing.T) {
func TestParseSeals(t *testing.T) {
testParseSeals(t)
}

func TestUnknownFieldValidation(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great! Can we also have a test that checks the filename for unusedKeys is correct, for configs between multiple files?

@vercel vercel bot temporarily deployed to Preview – vault April 30, 2021 19:24 Inactive
@vercel vercel bot temporarily deployed to Preview – vault-storybook April 30, 2021 19:24 Inactive
@sgmiller sgmiller requested a review from HridoyRoy May 4, 2021 16:30
HridoyRoy
HridoyRoy previously approved these changes May 4, 2021
Copy link
Contributor

@HridoyRoy HridoyRoy left a comment

Choose a reason for hiding this comment

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

Lgtm!

@HridoyRoy HridoyRoy dismissed their stale review May 4, 2021 16:38

I didn't look closely enough at the test failures, my apologies!

@vercel vercel bot temporarily deployed to Preview – vault-storybook May 4, 2021 18:36 Inactive
@vercel vercel bot temporarily deployed to Preview – vault May 4, 2021 18:36 Inactive
Copy link
Contributor

@HridoyRoy HridoyRoy left a comment

Choose a reason for hiding this comment

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

lgtm!

@sgmiller sgmiller merged commit 2feeb39 into master May 4, 2021
@sgmiller sgmiller deleted the diagnose-config2 branch May 4, 2021 19:48
AndreyZamyslov pushed a commit to yandex-cloud/vault that referenced this pull request Jun 10, 2021
…shicorp#11455)

* Expose unknown fields and duplicate sections as diagnose warnings

* section counts not needed, already handled

* Address PR feedback

* Prune more of the new fields before tests call deep.Equals

* Update go.mod
jartek pushed a commit to jartek/vault that referenced this pull request Sep 11, 2021
…shicorp#11455)

* Expose unknown fields and duplicate sections as diagnose warnings

* section counts not needed, already handled

* Address PR feedback

* Prune more of the new fields before tests call deep.Equals

* Update go.mod
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 this pull request may close these issues.

None yet

3 participants