-
Notifications
You must be signed in to change notification settings - Fork 116
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
Add allowed ip check #605
Merged
Merged
Add allowed ip check #605
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1be9bbf
Add allowed ip check
marc-gr da9e6c1
Disable allowed ip checks for system tests
marc-gr 686d862
Embed IPs file and change docs
marc-gr 5801ce0
Update validate.go
marc-gr 6839c88
Add allowed for ipv6
marc-gr 86ef3b0
Trim possible empty IPs
marc-gr c96514e
Merge remote-tracking branch 'upstream/master' into allowed-ip-check
marc-gr 610ebaa
Merge remote-tracking branch 'upstream/master' into allowed-ip-check
marc-gr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,15 @@ | ||
1.128.3.4 | ||
175.16.199.1 | ||
216.160.83.57 | ||
216.160.83.61 | ||
81.2.69.143 | ||
81.2.69.144 | ||
81.2.69.145 | ||
81.2.69.193 | ||
89.160.20.112 | ||
89.160.20.156 | ||
67.43.156.12 | ||
67.43.156.13 | ||
67.43.156.14 | ||
67.43.156.15 | ||
2a02:cf40:add:4002:91f2:a9b2:e09a:6fc6 |
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 |
---|---|---|
|
@@ -5,8 +5,10 @@ | |
package fields | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
|
@@ -31,6 +33,9 @@ type Validator struct { | |
numericKeywordFields map[string]struct{} | ||
|
||
disabledDependencyManagement bool | ||
|
||
enabledAllowedIPCheck bool | ||
allowedIPs map[string]struct{} | ||
} | ||
|
||
// ValidatorOption represents an optional flag that can be passed to CreateValidatorForDataStream. | ||
|
@@ -64,6 +69,14 @@ func WithDisabledDependencyManagement() ValidatorOption { | |
} | ||
} | ||
|
||
// WithEnabledAllowedIPCheck configures the validator to perform check on the IP values against an allowed list. | ||
func WithEnabledAllowedIPCheck() ValidatorOption { | ||
return func(v *Validator) error { | ||
v.enabledAllowedIPCheck = true | ||
return nil | ||
} | ||
} | ||
|
||
// CreateValidatorForDataStream function creates a validator for the data stream. | ||
func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOption) (v *Validator, err error) { | ||
v = new(Validator) | ||
|
@@ -72,6 +85,9 @@ func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOp | |
return nil, err | ||
} | ||
} | ||
|
||
v.allowedIPs = initializeAllowedIPsList() | ||
|
||
v.Schema, err = loadFieldsForDataStream(dataStreamRootPath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't load fields for data stream (path: %s)", dataStreamRootPath) | ||
|
@@ -99,6 +115,25 @@ func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOp | |
return v, nil | ||
} | ||
|
||
//go:embed _static/allowed_geo_ips.txt | ||
var allowedGeoIPs string | ||
|
||
func initializeAllowedIPsList() map[string]struct{} { | ||
m := map[string]struct{}{ | ||
"0.0.0.0": {}, "255.255.255.255": {}, | ||
"0:0:0:0:0:0:0:0": {}, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff": {}, | ||
} | ||
for _, ip := range strings.Split(allowedGeoIPs, "\n") { | ||
marc-gr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ip = strings.Trim(ip, " \n\t") | ||
if ip == "" { | ||
continue | ||
} | ||
m[ip] = struct{}{} | ||
} | ||
|
||
return m | ||
} | ||
|
||
func loadFieldsForDataStream(dataStreamRootPath string) ([]FieldDefinition, error) { | ||
fieldsDir := filepath.Join(dataStreamRootPath, "fields") | ||
files, err := filepath.Glob(filepath.Join(fieldsDir, "*.yml")) | ||
|
@@ -306,7 +341,7 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va | |
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil { | ||
return err | ||
} | ||
case "date", "ip", "keyword", "text": | ||
case "date", "keyword", "text": | ||
var valStr string | ||
valStr, valid = val.(string) | ||
if !valid { | ||
|
@@ -316,6 +351,20 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va | |
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil { | ||
return err | ||
} | ||
case "ip": | ||
var valStr string | ||
valStr, valid = val.(string) | ||
if !valid { | ||
break | ||
} | ||
|
||
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil { | ||
return err | ||
} | ||
|
||
if v.enabledAllowedIPCheck && !v.isAllowedIPValue(valStr) { | ||
return fmt.Errorf("the IP %q is not one of the allowed test IPs", valStr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if this emitted a link to the list of acceptable IPs. |
||
} | ||
case "float", "long", "double": | ||
_, valid = val.(float64) | ||
default: | ||
|
@@ -328,6 +377,30 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va | |
return nil | ||
} | ||
|
||
// isAllowedIPValue checks if the provided IP is allowed for testing | ||
// The set of allowed IPs are: | ||
// - private IPs as described in RFC 1918 & RFC 4193 | ||
// - public IPs allowed by MaxMind for testing | ||
// - 0.0.0.0 and 255.255.255.255 for IPv4 | ||
// - 0:0:0:0:0:0:0:0 and ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff for IPv6 | ||
func (v *Validator) isAllowedIPValue(s string) bool { | ||
if _, found := v.allowedIPs[s]; found { | ||
return true | ||
} | ||
|
||
ip := net.ParseIP(s) | ||
if ip == nil { | ||
return false | ||
} | ||
|
||
if ip.IsPrivate() || ip.IsLoopback() || | ||
ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// ensureSingleElementValue extracts single entity from a potential array, which is a valid field representation | ||
// in Elasticsearch. For type assertion we need a single value. | ||
func ensureSingleElementValue(val interface{}) (interface{}, bool) { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this testing takes output from the net package, this should also probably have "::" since an IPv6 address renders as the abbreviation.