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

Add allowed ip check #605

Merged
merged 8 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions docs/howto/ingest_geoip.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,7 @@

Elasticsearch provides default GeoIP databases that can be downloaded in runtime and which weights ~70 MB. This can be
a root cause of flakiness of package tests, so elastic-package embeds small samples of GeoIP databases, that can identify
accurately only few ranges of IP addresses:

```
1.128.3.4
175.16.199.1
216.160.83.57
216.160.83.61
67.43.156.12
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
```
accurately only few ranges of IP addresses included [here](../../internal/fields/_static/allowed_geo_ips.txt)

If you want the ingest pipeline to include a "geo" section in the event, feel free to use one of above IP addresses.
Embedded databases contain information about: cities, countries and ASNs.
15 changes: 15 additions & 0 deletions internal/fields/_static/allowed_geo_ips.txt
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
69 changes: 68 additions & 1 deletion internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package fields

import (
_ "embed"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -99,6 +115,20 @@ 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": {},
}
for _, ip := range strings.Split(allowedGeoIPs, "\n") {
marc-gr marked this conversation as resolved.
Show resolved Hide resolved
m[ip] = struct{}{}
}

return m
}

func loadFieldsForDataStream(dataStreamRootPath string) ([]FieldDefinition, error) {
fieldsDir := filepath.Join(dataStreamRootPath, "fields")
files, err := filepath.Glob(filepath.Join(fieldsDir, "*.yml"))
Expand Down Expand Up @@ -306,7 +336,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 {
Expand All @@ -316,6 +346,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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Expand All @@ -328,6 +372,29 @@ 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
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) {
Expand Down
6 changes: 5 additions & 1 deletion internal/testrunner/runners/pipeline/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (r *runner) run() ([]testrunner.TestResult, error) {

tr.TimeElapsed = time.Since(startTime)
fieldsValidator, err := fields.CreateValidatorForDataStream(dataStreamPath,
fields.WithNumericKeywordFields(tc.config.NumericKeywordFields))
fields.WithNumericKeywordFields(tc.config.NumericKeywordFields),
// explicitly enabled for pipeline tests only
// since system tests can have dynamic public IPs
fields.WithEnabledAllowedIPCheck(),
)
if err != nil {
return nil, errors.Wrapf(err, "creating fields validator for data stream failed (path: %s, test case file: %s)", dataStreamPath, testCaseFile)
}
Expand Down