-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ability to blacklist specific HTTP endpoints. (#3252)
- Loading branch information
Showing
7 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package agent | ||
|
||
import ( | ||
"github.com/armon/go-radix" | ||
) | ||
|
||
// Blacklist implements an HTTP endpoint blacklist based on a list of endpoint | ||
// prefixes which should be blocked. | ||
type Blacklist struct { | ||
tree *radix.Tree | ||
} | ||
|
||
// NewBlacklist returns a blacklist for the given list of prefixes. | ||
func NewBlacklist(prefixes []string) *Blacklist { | ||
tree := radix.New() | ||
for _, prefix := range prefixes { | ||
tree.Insert(prefix, nil) | ||
} | ||
return &Blacklist{tree} | ||
} | ||
|
||
// Block will return true if the given path is included among any of the | ||
// blocked prefixes. | ||
func (b *Blacklist) Block(path string) bool { | ||
_, _, blocked := b.tree.LongestPrefix(path) | ||
return blocked | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package agent | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBlacklist(t *testing.T) { | ||
t.Parallel() | ||
|
||
complex := []string{ | ||
"/a", | ||
"/b/c", | ||
} | ||
|
||
tests := []struct { | ||
desc string | ||
prefixes []string | ||
path string | ||
block bool | ||
}{ | ||
{"nothing blocked root", nil, "/", false}, | ||
{"nothing blocked path", nil, "/a", false}, | ||
{"exact match 1", complex, "/a", true}, | ||
{"exact match 2", complex, "/b/c", true}, | ||
{"subpath", complex, "/a/b", true}, | ||
{"longer prefix", complex, "/apple", true}, | ||
{"longer subpath", complex, "/b/c/d", true}, | ||
{"partial prefix", complex, "/b/d", false}, | ||
{"no match", complex, "/c", false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
blacklist := NewBlacklist(tt.prefixes) | ||
if got, want := blacklist.Block(tt.path), tt.block; got != want { | ||
t.Fatalf("got %v want %v", got, want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters