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

fix: hack to fix the checking of remote_addrs #952

Merged
merged 4 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions api/internal/core/store/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ func checkUpstream(upstream *entity.UpstreamDef) error {
return nil
}

func checkRemoteAddr(remoteAddrs []string) error {
for _, remoteAddr := range remoteAddrs {
if remoteAddr == "" {
return fmt.Errorf("schema validate failed: invalid field remote_addrs")
}
}
return nil
}

func checkConf(reqBody interface{}) error {
switch bodyType := reqBody.(type) {
case *entity.Route:
Expand All @@ -201,6 +210,9 @@ func checkConf(reqBody interface{}) error {
if err := checkUpstream(route.Upstream); err != nil {
return err
}
if err := checkRemoteAddr(route.RemoteAddrs); err != nil {
membphis marked this conversation as resolved.
Show resolved Hide resolved
return err
}
case *entity.Service:
service := reqBody.(*entity.Service)
if err := checkUpstream(service.Upstream); err != nil {
Expand Down
127 changes: 127 additions & 0 deletions api/internal/core/store/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,132 @@ func TestAPISIXJsonSchemaValidator_checkUpstream(t *testing.T) {
err = validator.Validate(route5)
assert.NotNil(t, err)
assert.EqualError(t, err, "schema validate failed: (root): Does not match pattern '^((uri|server_name|server_addr|request_uri|remote_port|remote_addr|query_string|host|hostname)|arg_[0-9a-zA-z_-]+)$'")
}

func TestAPISIXJsonSchemaValidator_Route_checkRemoteAddr(t *testing.T) {
Copy link
Member

@membphis membphis Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test case: "remote_addr": ""

tests := []struct {
caseDesc string
giveContent string
wantNewErr error
wantValidateErr error
}{
{
caseDesc: "correct remote_addr",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addr": "127.0.0.1"
}`,
},
{
caseDesc: "correct remote_addr (CIDR)",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addr": "192.168.1.0/24"
}`,
},
{
caseDesc: "invalid remote_addr",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addr": "127.0.0."
}`,
wantValidateErr: fmt.Errorf("schema validate failed: remote_addr: Must validate at least one schema (anyOf)\nremote_addr: Does not match pattern '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$'"),
},
{
caseDesc: "correct remote_addrs",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addrs": ["127.0.0.1", "192.0.0.0/8", "::1", "fe80::1/64"]
}`,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no assert, is it default ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is. if it cause err, test will fail.

{
caseDesc: "invalid remote_addrs",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addrs": ["127.0.0.", "192.0.0.0/128", "::1"]
}`,
wantValidateErr: fmt.Errorf("schema validate failed: remote_addrs.0: Must validate at least one schema (anyOf)\nremote_addrs.0: Does not match pattern '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$'\nremote_addrs.1: Must validate at least one schema (anyOf)\nremote_addrs.1: Does not match pattern '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$'"),
},
{
caseDesc: "invalid remote_addrs (an empty string item)",
giveContent: `{
"id": "1",
"uri": "/*",
"upstream": {
"nodes": [{
"host": "127.0.0.1",
"port": 8080,
"weight": 1
}],
"type": "roundrobin"
},
"remote_addrs": [""]
}`,
wantValidateErr: fmt.Errorf("schema validate failed: invalid field remote_addrs"),
},
}

for _, tc := range tests {
validator, err := NewAPISIXJsonSchemaValidator("main.route")
if err != nil {
assert.Equal(t, tc.wantNewErr, err, tc.caseDesc)
continue
}
route := &entity.Route{}
err = json.Unmarshal([]byte(tc.giveContent), route)
assert.Nil(t, err, tc.caseDesc)

err = validator.Validate(route)
if tc.wantValidateErr == nil {
assert.Equal(t, nil, err, tc.caseDesc)
continue
}

assert.Equal(t, tc.wantValidateErr, err, tc.caseDesc)
}
}