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 docs URL to IP check error #623

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions internal/fields/testdata/fields/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
- name: constant
type: constant_keyword
value: correct
- name: ip_address
type: ip
5 changes: 5 additions & 0 deletions internal/fields/testdata/ip-address-allowed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"ip_address": "::"
}
}
5 changes: 5 additions & 0 deletions internal/fields/testdata/ip-address-forbidden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"ip_address": "98.76.54.32"
}
}
4 changes: 2 additions & 2 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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": {},
"0:0:0:0:0:0:0:0": {}, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff": {}, "::": {},
}
for _, ip := range strings.Split(allowedGeoIPs, "\n") {
ip = strings.Trim(ip, " \n\t")
Expand Down Expand Up @@ -363,7 +363,7 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
}

if v.enabledAllowedIPCheck && !v.isAllowedIPValue(valStr) {
return fmt.Errorf("the IP %q is not one of the allowed test IPs", valStr)
return fmt.Errorf("the IP %q is not one of the allowed test IPs (see: https://github.com/elastic/elastic-package/blob/master/internal/fields/_static/allowed_geo_ips.txt)", valStr)
}
case "float", "long", "double":
_, valid = val.(float64)
Expand Down
17 changes: 16 additions & 1 deletion internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestValidate_WithNumericKeywordFields(t *testing.T) {
require.Empty(t, errs)
}

func TestValidate_constant_keyword(t *testing.T) {
func TestValidate_constantKeyword(t *testing.T) {
validator, err := CreateValidatorForDataStream("testdata")
require.NoError(t, err)
require.NotNil(t, validator)
Expand All @@ -75,6 +75,21 @@ func TestValidate_constant_keyword(t *testing.T) {
require.Empty(t, errs)
}

func TestValidate_ipAddress(t *testing.T) {
validator, err := CreateValidatorForDataStream("testdata", WithEnabledAllowedIPCheck())
require.NoError(t, err)
require.NotNil(t, validator)

e := readSampleEvent(t, "testdata/ip-address-forbidden.json")
errs := validator.ValidateDocumentBody(e)
require.Len(t, errs, 1)
require.Contains(t, errs[0].Error(), `the IP "98.76.54.32" is not one of the allowed test IPs`)

e = readSampleEvent(t, "testdata/ip-address-allowed.json")
errs = validator.ValidateDocumentBody(e)
require.Empty(t, errs)
}

func Test_parseElementValue(t *testing.T) {
for _, test := range []struct {
key string
Expand Down