You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2020/12/02 10:20:43 UTC

[apisix-dashboard] branch master updated: fix: hack to fix the checking of `remote_addrs` (#952)

This is an automated email from the ASF dual-hosted git repository.

chenjunxu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 17ad69b  fix: hack to fix the checking of `remote_addrs` (#952)
17ad69b is described below

commit 17ad69b5ecfa80b55550d35d68681f55c84f5274
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Wed Dec 2 18:20:34 2020 +0800

    fix: hack to fix the checking of `remote_addrs` (#952)
    
    * fix: hack to fix the checking of `remote_addrs`
---
 api/internal/core/store/validate.go      |  13 ++++
 api/internal/core/store/validate_test.go | 129 +++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)

diff --git a/api/internal/core/store/validate.go b/api/internal/core/store/validate.go
index 57ba2a4..c47b634 100644
--- a/api/internal/core/store/validate.go
+++ b/api/internal/core/store/validate.go
@@ -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:
@@ -201,6 +210,10 @@ func checkConf(reqBody interface{}) error {
 		if err := checkUpstream(route.Upstream); err != nil {
 			return err
 		}
+		// todo: this is a temporary method, we'll drop it later
+		if err := checkRemoteAddr(route.RemoteAddrs); err != nil {
+			return err
+		}
 	case *entity.Service:
 		service := reqBody.(*entity.Service)
 		if err := checkUpstream(service.Upstream); err != nil {
diff --git a/api/internal/core/store/validate_test.go b/api/internal/core/store/validate_test.go
index 2d2db73..369329f 100644
--- a/api/internal/core/store/validate_test.go
+++ b/api/internal/core/store/validate_test.go
@@ -247,5 +247,134 @@ 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) {
+	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"]
+			}`,
+		},
+		{
+			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"),
+		},
+	}
+
+	// todo: add a test case for "remote_addr": ""
+
+	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)
+	}
 }