Skip to content

Commit

Permalink
fix: not allowed to specify create_time and update_time when crea…
Browse files Browse the repository at this point in the history
…te/edit route, service, upstream and consumer (#1110)

Related #933 .
  • Loading branch information
nic-chen authored Dec 25, 2020
1 parent 6fa6a16 commit 0e6e280
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/filter/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ func parseCert(crt, key string) ([]string, error) {
}

func handleSpecialField(resource string, reqBody []byte) ([]byte, error) {
var bodyMap map[string]interface{}
err := json.Unmarshal(reqBody, &bodyMap)
if err != nil {
return reqBody, fmt.Errorf("read request body failed: %s", err)
}
if _, ok := bodyMap["create_time"]; ok {
return reqBody, errors.New("we don't accept create_time from client")
}
if _, ok := bodyMap["update_time"]; ok {
return reqBody, errors.New("we don't accept update_time from client")
}

// remove script, because it's a map, and need to be parsed into lua code
if resource == "routes" {
var route map[string]interface{}
Expand Down
72 changes: 72 additions & 0 deletions api/test/e2e/route_with_management_fileds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,75 @@ func TestRoute_search_by_label(t *testing.T) {
testCaseCheck(tc, t)
}
}

func TestRoute_With_Create_Time(t *testing.T) {
tests := []HttpTestCase{
{
Desc: "create route with create_time",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/routes/r1",
Method: http.MethodPut,
Body: `{
"uri": "/hello",
"create_time": 1608792721,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
{
Desc: "create route with update_time",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/routes/r1",
Method: http.MethodPut,
Body: `{
"uri": "/hello",
"update_time": 1608792721,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
{
Desc: "create route with create_time and update_time",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/routes/r1",
Method: http.MethodPut,
Body: `{
"uri": "/hello",
"create_time": 1608792721,
"update_time": 1608792721,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
},
{
Desc: "make sure the route not created",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusNotFound,
ExpectBody: `{"error_msg":"404 Route Not Found"}`,
},
}

for _, tc := range tests {
testCaseCheck(tc, t)
}
}

0 comments on commit 0e6e280

Please sign in to comment.