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

config: validate URLs at config load time #1468

Merged
merged 5 commits into from
Jul 26, 2018

Conversation

simonpasquier
Copy link
Member

Closes #1460

if err != nil {
t.Fatal(err)
}
require.Equal(t, "<secret>\n", string(c), "SecretURL not properly elidedin YAML.")
Copy link
Contributor

Choose a reason for hiding this comment

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

missing space

notify/impl.go Outdated
alias = hashKey(key)
alerts = types.Alerts(as...)
msg interface{}
apiURL, _ = url.Parse(n.conf.APIURL.String())
Copy link

Choose a reason for hiding this comment

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

For consistency and clarity for future readers we could use same comment here about checking at conf load time

notify/impl.go Outdated
@@ -759,8 +763,10 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
data = n.tmpl.Data(receiverName(ctx, n.logger), groupLabels(ctx, n.logger), as...)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
url = fmt.Sprintf("%sv2/room/%s/notification?auth_token=%s", n.conf.APIURL, n.conf.RoomID, n.conf.AuthToken)
// n.conf.APIURL is already checked at configuration load time.
url, _ = url.Parse(n.conf.APIURL.String())
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit confused here, why not just n.conf.APIURL is this not already a URL?

Copy link
Member Author

Choose a reason for hiding this comment

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

n.conf.APIURL is a pointer so I didn't want to modify it.

Copy link
Contributor

Choose a reason for hiding this comment

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

you could also do

u := *c.conf.APIURL
u.Path += fmt.Sprintf("v2/room/%s/notification?auth_token=%s", n.conf.RoomID, n.conf.AuthToken)
// ...
resp, err := ctxhttp.Post(ctx, c, u.String(), contentTypeJSON, &buf)

Copy link
Contributor

Choose a reason for hiding this comment

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

since we've assigned something to variable name url, we've shadowed the reference to the package net/url in this scope. Having url's named u is annoying, but at some point someone will want to use net/url later in this package..

@mxinden
Copy link
Member

mxinden commented Jul 13, 2018

@simonpasquier There are a couple more URL strings inside notifier.go. Would it make sense to replace them as well?

@simonpasquier
Copy link
Member Author

@mxinden absolutely! Not sure how I managed to miss them...

@brian-brazil
Copy link
Contributor

The pushover url is a template, not a url.

@simonpasquier
Copy link
Member Author

@brian-brazil yep, I replied too fast. This isn't the Pushover API URL...

Copy link
Contributor

@stuartnelson3 stuartnelson3 left a comment

Choose a reason for hiding this comment

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

couple relatively unimportant comments

notify/impl.go Outdated
@@ -759,8 +763,10 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
data = n.tmpl.Data(receiverName(ctx, n.logger), groupLabels(ctx, n.logger), as...)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
url = fmt.Sprintf("%sv2/room/%s/notification?auth_token=%s", n.conf.APIURL, n.conf.RoomID, n.conf.AuthToken)
// n.conf.APIURL is already checked at configuration load time.
url, _ = url.Parse(n.conf.APIURL.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

you could also do

u := *c.conf.APIURL
u.Path += fmt.Sprintf("v2/room/%s/notification?auth_token=%s", n.conf.RoomID, n.conf.AuthToken)
// ...
resp, err := ctxhttp.Post(ctx, c, u.String(), contentTypeJSON, &buf)

notify/impl.go Outdated
@@ -759,8 +763,10 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
data = n.tmpl.Data(receiverName(ctx, n.logger), groupLabels(ctx, n.logger), as...)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
url = fmt.Sprintf("%sv2/room/%s/notification?auth_token=%s", n.conf.APIURL, n.conf.RoomID, n.conf.AuthToken)
// n.conf.APIURL is already checked at configuration load time.
url, _ = url.Parse(n.conf.APIURL.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

since we've assigned something to variable name url, we've shadowed the reference to the package net/url in this scope. Having url's named u is annoying, but at some point someone will want to use net/url later in this package..

Signed-off-by: Simon Pasquier <[email protected]>
notify/impl.go Outdated
@@ -802,8 +802,7 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
roomid = tmplText(n.conf.RoomID)
// n.conf.APIURL is already checked at configuration load time.
url, _ = url.Parse(n.conf.APIURL.String())
url = *n.conf.APIURL
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't a deep copy, needs to be fixed.

notify/impl.go Outdated
@@ -798,8 +802,9 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
tmplText = tmplText(n.tmpl, data, &err)
tmplHTML = tmplHTML(n.tmpl, data, &err)
roomid = tmplText(n.conf.RoomID)
url = fmt.Sprintf("%sv2/room/%s/notification?auth_token=%s", n.conf.APIURL, roomid, n.conf.AuthToken)
apiURL = *n.conf.APIURL
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't a deep copy, needs to be fixed.

Signed-off-by: Simon Pasquier <[email protected]>
@stuartnelson3 stuartnelson3 merged commit 0ccc7c9 into prometheus:master Jul 26, 2018
gabreal pushed a commit to paritytech/alertmanager that referenced this pull request Jul 31, 2018
* config: validate URLs at config load time

Signed-off-by: Simon Pasquier <[email protected]>

* Address Brian and Lucas comments

Signed-off-by: Simon Pasquier <[email protected]>

* Shallow copy of URL instead of reparsing it

Signed-off-by: Simon Pasquier <[email protected]>

* Unshadow net/url package

Signed-off-by: Simon Pasquier <[email protected]>

* Make a deep-copy of URL struct

Signed-off-by: Simon Pasquier <[email protected]>
mxinden pushed a commit to mxinden/alertmanager that referenced this pull request Aug 14, 2018
* config: validate URLs at config load time

Signed-off-by: Simon Pasquier <[email protected]>

* Address Brian and Lucas comments

Signed-off-by: Simon Pasquier <[email protected]>

* Shallow copy of URL instead of reparsing it

Signed-off-by: Simon Pasquier <[email protected]>

* Unshadow net/url package

Signed-off-by: Simon Pasquier <[email protected]>

* Make a deep-copy of URL struct

Signed-off-by: Simon Pasquier <[email protected]>
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.

5 participants