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

Adds the ability to blacklist specific HTTP endpoints. #3252

Merged
merged 7 commits into from
Jul 10, 2017
Merged

Conversation

slackpad
Copy link
Contributor

No description provided.

agent/http.go Outdated
@@ -194,6 +201,12 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
fmt.Fprint(resp, errMsg)
}

if s.blacklist.IsDisallowed(req.URL.Path) {
err := fmt.Errorf("Permission denied, endpoint is disabled")
handleErr(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This relying on a string match to make it return an appropriate status code like 403 is weird and seems fragile. Would be nice to make some predefined Err500, Err404, Err403 etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is gross - I'll factor this out to make it more explicit. The other code is a little fragile but is baked in a few different places (and complicated by the fact the the errors go over the RPC interface, which launders their types into a generic error), so we can tackle that in another change.

@@ -757,6 +757,16 @@ Consul will not enable TLS for the HTTP API unless the `https` port has been ass
<br><br>
The following sub-keys are available:

* <a name="disable_endpoints"></a><a href="#disable_endpoints">`disable_endpoints`</a>
Copy link
Contributor

Choose a reason for hiding this comment

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

This would also disable any consul commands that use the underlying api, right? Is it worth calling out here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep - I'll add a note about that.

Copy link
Contributor

@magiconair magiconair left a comment

Choose a reason for hiding this comment

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

I would use Block instead of Disable because it is shorter and conveys the meaning better IMO but that's debatable. The blacklist test is a bit too convoluted and could be clearer. In general, the code looks ok.


// IsDisallowed will return true if the given path is included among any of the
// disallowed prefixes.
func (b *Blacklist) IsDisallowed(path 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.

s/IsDisallowed/Block/

// IsDisallowed will return true if the given path is included among any of the
// disallowed prefixes.
func (b *Blacklist) IsDisallowed(path string) bool {
_, _, disallowed := b.tree.LongestPrefix(path)
Copy link
Contributor

Choose a reason for hiding this comment

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

s/disallowed/blocked/

tests := []struct {
desc string
prefixes []string
paths []pathCase
Copy link
Contributor

Choose a reason for hiding this comment

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

just inline pathCase

agent/config.go Outdated
@@ -131,6 +131,10 @@ type DNSConfig struct {

// HTTPConfig is used to fine tune the Http sub-system.
type HTTPConfig struct {
// DisableEndpoints is a list of endpoint prefixes to disable in the
// HTTP API. Any requests to these will get a 403 response.
DisableEndpoints []string `mapstructure:"disable_endpoints"`
Copy link
Contributor

Choose a reason for hiding this comment

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

s/DisableEndpoints/BlockEndpoints/g

I'm inclined to just name this Blacklist but that may conflict with other blacklists in the future, e.g. network ranges, user agents, ...

agent/http.go Outdated
@@ -194,6 +201,12 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
fmt.Fprint(resp, errMsg)
}

if s.blacklist.IsDisallowed(req.URL.Path) {
err := fmt.Errorf("Permission denied, endpoint is disabled")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably type these errors instead of depending on magic words but that is probably a larger change.

}

// Try a banned endpoint, which should get a 403.
{
Copy link
Contributor

Choose a reason for hiding this comment

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

use t.Run subtests instead of block and comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

"/v1/agent/self",
},
[]pathCase{
{"/", false},
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use generic endpoint names like /a, /b to make it clear what you're testing. You're covering /a, /aa and /a/b for the /a blacklist but that isn't obvious. Also, with the two entries you didn't cover /v1/acl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call.

@slackpad slackpad merged commit 66edec5 into master Jul 10, 2017
@slackpad slackpad deleted the api-knock-out branch July 10, 2017 20:51
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