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

Validate Slack field config and only allow the necessary input #1334

Merged
merged 1 commit into from
Apr 25, 2018

Conversation

taharah
Copy link
Contributor

@taharah taharah commented Apr 20, 2018

Resolves the comments on the PR #1002 to update the documentation for Slack fields. Currently, any arbitrary key/value pairs can be defined; however, per the Slack documentation for defining fields [1] only the properties title, value, and short can be defined with the first two being required for each field. This updates the Slack field configuration to only allow the aforementioned three properties as well as validates that both a title and value are defined for each property in the yaml.Unmarshaler interface. Moreover, this allows the value for short to be defined on a per-field basis.

return err
}
if c.Title == "" {
return fmt.Errorf("missing title in Slack field configuration, value=%v", c.Value)
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't log c.Value in the error.

return fmt.Errorf("missing title in Slack field configuration, value=%v", c.Value)
}
if c.Value == "" {
return fmt.Errorf("missing value in Slack field configuration, title=%v", c.Title)
Copy link
Member

Choose a reason for hiding this comment

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

Same for c.Title.

@@ -253,3 +253,43 @@ token: ''
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}

func TestSlackFieldConfigValidation(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test case with a valid YAML connfiguration?

notify/impl.go Outdated
for index, field := range n.conf.Fields {
// Check if short was defined for the field otherwise fallback to the global setting
var short bool
if field.Short {
Copy link
Member

Choose a reason for hiding this comment

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

This won't work as expected when field.Short is set to false on purpose. Short needs to be a *bool instead.

@taharah taharah force-pushed the fix-slack-config branch 3 times, most recently from 1e64169 to 7197159 Compare April 24, 2018 01:15
@taharah
Copy link
Contributor Author

taharah commented Apr 24, 2018

@simonpasquier I've resolved all of the comments you made. Thank you for reviewing!

Copy link
Member

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

2 small remarks, LGTM otherwise.

value: hello
short: true
- title: second
value: world
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to validate that the unmarshaled data is matching what is expected when there's no error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I'll get this added.

notify/impl.go Outdated
var fields = make([]config.SlackField, numFields)
for index, field := range n.conf.Fields {
// Check if short was defined for the field otherwise fallback to the global setting
var short *bool
Copy link
Member

Choose a reason for hiding this comment

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

nit but I would do:

                        var short bool
			if field.Short != nil {
				short = *field.Short
			} else {
				short = n.conf.ShortFields
			}

Copy link
Contributor Author

@taharah taharah Apr 24, 2018

Choose a reason for hiding this comment

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

The main reason it is being set as it is currently is that I reused SlackField from the config. package, which has Short defined as a *bool now. I initially had it like that, but changed it after the tests failed and caught the error. I can change the if statement to be as above, and then pass a pointer to the variable short when building the SlackField.

Signed-off-by: Trevor Wood <Trevor.G.Wood@gmail.com>
@taharah
Copy link
Contributor Author

taharah commented Apr 25, 2018

@simonpasquier I resolved your latest comments. If there's anything else you see in the latest batches of changes let me know and I'll get them pushed up. Thanks!

@simonpasquier
Copy link
Member

LGTM

@stuartnelson3 stuartnelson3 merged commit cecfe5b into prometheus:master Apr 25, 2018
hh pushed a commit to ii/alertmanager that referenced this pull request May 7, 2019
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
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.

3 participants