-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: allow config files set address validation (#73)
Add DialedAddressValidator support for JSON and Protobuf config files. Signed-off-by: Gaukas Wang <[email protected]>
- Loading branch information
Showing
6 changed files
with
431 additions
and
68 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,56 @@ | ||
package water | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrAddressValidatorNotInitialized = errors.New("address validator not initialized properly") | ||
ErrAddressValidationDenied = errors.New("address validation denied") | ||
) | ||
|
||
type addressValidator struct { | ||
catchAll bool | ||
allowlist map[string][]string // map[address]networks | ||
denylist map[string][]string // map[address]networks | ||
} | ||
|
||
func (a *addressValidator) validate(network, address string) error { | ||
if a.catchAll { | ||
// only check denylist, otherwise allow | ||
if a.denylist == nil { | ||
return ErrAddressValidatorNotInitialized | ||
} | ||
|
||
if deniedNetworks, ok := a.denylist[address]; ok { | ||
if deniedNetworks == nil { | ||
return ErrAddressValidatorNotInitialized | ||
} | ||
|
||
for _, deniedNet := range deniedNetworks { | ||
if deniedNet == network { | ||
return ErrAddressValidationDenied | ||
} | ||
} | ||
} | ||
return nil | ||
} else { | ||
// only check allowlist, otherwise deny | ||
if a.allowlist == nil { | ||
return ErrAddressValidatorNotInitialized | ||
} | ||
|
||
if allowedNetworks, ok := a.allowlist[address]; ok { | ||
if allowedNetworks == nil { | ||
return ErrAddressValidatorNotInitialized | ||
} | ||
|
||
for _, allowedNet := range allowedNetworks { | ||
if allowedNet == network { | ||
return nil | ||
} | ||
} | ||
} | ||
return ErrAddressValidationDenied | ||
} | ||
} |
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,75 @@ | ||
package water | ||
|
||
// package water instead of water_test to access unexported struct addressValidator and its unexported fields/methods | ||
|
||
import "testing" | ||
|
||
func Test_addressValidator_validate(t *testing.T) { | ||
var a addressValidator | ||
|
||
// test catchAll with nil denylist | ||
a.catchAll = true | ||
|
||
if err := a.validate("random net", "random address"); err != ErrAddressValidatorNotInitialized { | ||
t.Errorf("Expected ErrAddressValidatorNotInitialized, got %v", err) | ||
} | ||
|
||
// test nil denylist entry | ||
a.denylist = map[string][]string{ | ||
"denied address": nil, | ||
} | ||
|
||
if err := a.validate("random net", "denied address"); err != ErrAddressValidatorNotInitialized { | ||
t.Errorf("Expected ErrAddressValidatorNotInitialized, got %v", err) | ||
} | ||
|
||
// test denied address on denied network | ||
a.denylist["denied address"] = []string{"denied net"} | ||
|
||
if err := a.validate("denied net", "denied address"); err != ErrAddressValidationDenied { | ||
t.Errorf("Expected ErrAddressValidationDenied, got %v", err) | ||
} | ||
|
||
// test random network with denied address | ||
if err := a.validate("random net", "denied address"); err != nil { | ||
t.Errorf("Expected nil, got %v", err) | ||
} | ||
|
||
// test random address on denied network | ||
if err := a.validate("denied net", "random address"); err != nil { | ||
t.Errorf("Expected nil, got %v", err) | ||
} | ||
|
||
// test not catchAll with nil allowlist | ||
a.catchAll = false | ||
|
||
if err := a.validate("random net", "random address"); err != ErrAddressValidatorNotInitialized { | ||
t.Errorf("Expected ErrAddressValidatorNotInitialized, got %v", err) | ||
} | ||
|
||
// test nil allowlist entry | ||
a.allowlist = map[string][]string{ | ||
"allowed address": nil, | ||
} | ||
|
||
if err := a.validate("random net", "allowed address"); err != ErrAddressValidatorNotInitialized { | ||
t.Errorf("Expected ErrAddressValidatorNotInitialized, got %v", err) | ||
} | ||
|
||
// test allowed address on allowed network | ||
a.allowlist["allowed address"] = []string{"allowed net"} | ||
|
||
if err := a.validate("allowed net", "allowed address"); err != nil { | ||
t.Errorf("Expected nil, got %v", err) | ||
} | ||
|
||
// test random network with allowed address | ||
if err := a.validate("random net", "allowed address"); err != ErrAddressValidationDenied { | ||
t.Errorf("Expected ErrAddressValidationDenied, got %v", err) | ||
} | ||
|
||
// test random address on allowed network | ||
if err := a.validate("allowed net", "random address"); err != ErrAddressValidationDenied { | ||
t.Errorf("Expected ErrAddressValidationDenied, got %v", err) | ||
} | ||
} |
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
Oops, something went wrong.