You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2021/05/28 09:45:58 UTC
[GitHub] [servicecomb-service-center] little-cui opened a new pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
little-cui opened a new pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019
Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/SCB) filed for the change (usually before you start working on it). Trivial changes like typos do not require a JIRA issue. Your pull request should address just this issue, without pulling in other changes.
- [ ] Each commit in the pull request should have a meaningful subject line and body.
- [ ] Format the pull request title like `[SCB-XXX] Fixes bug in ApproximateQuantiles`, where you replace `SCB-XXX` with the appropriate JIRA issue.
- [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [ ] Run `go build` `go test` `go fmt` `go vet` to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
- [ ] Never comment source code, delete it.
- [ ] UT should has "context, subject, expected result" result as test case name, when you call t.Run().
---
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641869089
##########
File path: server/plugin/auth/buildin/parser_test.go
##########
@@ -49,85 +55,216 @@ func TestGetAPIParseFunc(t *testing.T) {
})
serviceIDB = response.ServiceId
- t.Run("get all services api should return no labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
-
- t.Run("create services api without body should return err", func(t *testing.T) {
- reader := strings.NewReader("{}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- _, err = buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.Error(t, err)
- })
-
- t.Run("create services api with serviceName A should return A", func(t *testing.T) {
- reader := strings.NewReader("{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("delete 2 services api should return 2 labels", func(t *testing.T) {
- reader := strings.NewReader("{\n \"serviceIds\": [\"" + serviceIDA + "\", \"" + serviceIDB + "\"]\n}")
-
- request, err := http.NewRequest(http.MethodDelete, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 2, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("get service sub resource api should return service labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices/"+serviceIDA+"/instances?:serviceId="+serviceIDA, nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices/:serviceId/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("discovery A instances api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/instances?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query A api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query all api should return nil labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices", nil)
- assert.NoError(t, err)
+ newRequest := func(method, url string, body string) *http.Request {
+ request, _ := http.NewRequest(method, url, strings.NewReader(body))
+ util.SetRequestContext(request, rest.CtxMatchPattern, url)
+ return request
+ }
+ tests := []struct {
+ name string
+ request *http.Request
+ wantResource *auth.ResourceScope
+ wantErr bool
+ }{
+ {
+ "get all services api should return no labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "create services api without body should return err",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices", "{}"),
+ nil,
+ true,
+ },
+ {
+ "create services api with serviceName A should return A",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices",
+ "{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "create",
+ },
+ false,
+ },
+ {
+ "delete 2 services api should return 2 labels",
+ newRequest(http.MethodDelete, "/v4/:project/registry/microservices",
+ "{\n \"serviceIds\": [\""+serviceIDA+"\", \""+serviceIDB+"\"]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ {
+ "serviceName": "B",
+ },
+ },
+ Verb: "delete",
+ },
+ false,
+ },
+ {
+ "get service sub resource api should return service labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices/:serviceId/instances?:serviceId="+serviceIDA, ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "discovery A instances api should return service A labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/instances?serviceName=A", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern query A api should return service A labels",
+ newRequest(http.MethodGet, "/v4/:project/govern/microservices?serviceName=A", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern query all api should return nil labels",
+ newRequest(http.MethodGet, "/v4/:project/govern/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "batch discovery A instances should return A labels",
+ newRequest(http.MethodPost, "/v4/:project/registry/instances/action",
+ "{\n \"consumerId\": \"\",\n \"instances\": [\n {\n \"instance\": {\n \"serviceId\": \""+serviceIDA+"\",\n \"instanceId\": \"\"\n },\n \"rev\": \"\"\n }\n ]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "batch heartbeat A instances should return A labels",
+ newRequest(http.MethodPost, "/v4/:project/registry/heartbeats",
+ "{\n \"instances\": [\n {\n \"serviceId\": \""+serviceIDA+"\",\n \"instanceId\": \"\"\n }\n ]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "update",
+ },
+ false,
+ },
+ // not registered api
+ {
+ "govern statistics api should apply all",
+ newRequest(http.MethodGet, "/v4/:project/govern/statistics", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern apps api should apply all",
+ newRequest(http.MethodGet, "/v4/:project/govern/apps", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "account api should apply all",
+ newRequest(http.MethodGet, "/v4/accounts", ""),
+ &auth.ResourceScope{
+ Type: "account",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "role api should apply all",
+ newRequest(http.MethodGet, "/v4/roles", ""),
+ &auth.ResourceScope{
+ Type: "role",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "role api should apply all",
+ newRequest(http.MethodGet, "/v1/:project/gov/", ""),
+ &auth.ResourceScope{
+ Type: "governance",
+ Verb: "get",
+ },
+ false,
+ },
+ }
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ resource, err := buildin.GetAPIParseFunc(tt.request.URL.Path)(tt.request)
+ assert.Equal(t, tt.wantErr, err != nil)
+ assert.Equal(t, tt.wantResource == nil, resource == nil)
+ if tt.wantResource == nil {
+ return
+ }
+ assert.Equal(t, tt.wantResource.Type, resource.Type)
+ assert.Equal(t, tt.wantResource.Verb, resource.Verb)
+ assert.Equal(t, tt.wantResource.Labels == nil, resource.Labels == nil)
+ for _, labels := range tt.wantResource.Labels {
+ LOOP:
Review comment:
不要带goto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641868661
##########
File path: server/plugin/auth/buildin/parser_test.go
##########
@@ -49,85 +55,216 @@ func TestGetAPIParseFunc(t *testing.T) {
})
serviceIDB = response.ServiceId
- t.Run("get all services api should return no labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
-
- t.Run("create services api without body should return err", func(t *testing.T) {
- reader := strings.NewReader("{}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- _, err = buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.Error(t, err)
- })
-
- t.Run("create services api with serviceName A should return A", func(t *testing.T) {
- reader := strings.NewReader("{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("delete 2 services api should return 2 labels", func(t *testing.T) {
- reader := strings.NewReader("{\n \"serviceIds\": [\"" + serviceIDA + "\", \"" + serviceIDB + "\"]\n}")
-
- request, err := http.NewRequest(http.MethodDelete, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 2, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("get service sub resource api should return service labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices/"+serviceIDA+"/instances?:serviceId="+serviceIDA, nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices/:serviceId/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("discovery A instances api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/instances?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query A api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query all api should return nil labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices", nil)
- assert.NoError(t, err)
+ newRequest := func(method, url string, body string) *http.Request {
+ request, _ := http.NewRequest(method, url, strings.NewReader(body))
+ util.SetRequestContext(request, rest.CtxMatchPattern, url)
+ return request
+ }
+ tests := []struct {
+ name string
+ request *http.Request
+ wantResource *auth.ResourceScope
+ wantErr bool
+ }{
+ {
+ "get all services api should return no labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "create services api without body should return err",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices", "{}"),
Review comment:
这个ut最好蜱虫和权限冲突的case,。比如允许创建应用a的服务,但是希望创建应用b的
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641875991
##########
File path: server/plugin/auth/buildin/parser_test.go
##########
@@ -49,85 +55,216 @@ func TestGetAPIParseFunc(t *testing.T) {
})
serviceIDB = response.ServiceId
- t.Run("get all services api should return no labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
-
- t.Run("create services api without body should return err", func(t *testing.T) {
- reader := strings.NewReader("{}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- _, err = buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.Error(t, err)
- })
-
- t.Run("create services api with serviceName A should return A", func(t *testing.T) {
- reader := strings.NewReader("{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("delete 2 services api should return 2 labels", func(t *testing.T) {
- reader := strings.NewReader("{\n \"serviceIds\": [\"" + serviceIDA + "\", \"" + serviceIDB + "\"]\n}")
-
- request, err := http.NewRequest(http.MethodDelete, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 2, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("get service sub resource api should return service labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices/"+serviceIDA+"/instances?:serviceId="+serviceIDA, nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices/:serviceId/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("discovery A instances api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/instances?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query A api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query all api should return nil labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices", nil)
- assert.NoError(t, err)
+ newRequest := func(method, url string, body string) *http.Request {
+ request, _ := http.NewRequest(method, url, strings.NewReader(body))
+ util.SetRequestContext(request, rest.CtxMatchPattern, url)
+ return request
+ }
+ tests := []struct {
+ name string
+ request *http.Request
+ wantResource *auth.ResourceScope
+ wantErr bool
+ }{
+ {
+ "get all services api should return no labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "create services api without body should return err",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices", "{}"),
Review comment:
这个ut主要是对parse的保护,你说的权限校验在server/service/rbac/decision_test.go覆盖了
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] little-cui merged pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
little-cui merged pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641868661
##########
File path: server/plugin/auth/buildin/parser_test.go
##########
@@ -49,85 +55,216 @@ func TestGetAPIParseFunc(t *testing.T) {
})
serviceIDB = response.ServiceId
- t.Run("get all services api should return no labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
-
- t.Run("create services api without body should return err", func(t *testing.T) {
- reader := strings.NewReader("{}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- _, err = buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.Error(t, err)
- })
-
- t.Run("create services api with serviceName A should return A", func(t *testing.T) {
- reader := strings.NewReader("{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("delete 2 services api should return 2 labels", func(t *testing.T) {
- reader := strings.NewReader("{\n \"serviceIds\": [\"" + serviceIDA + "\", \"" + serviceIDB + "\"]\n}")
-
- request, err := http.NewRequest(http.MethodDelete, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 2, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("get service sub resource api should return service labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices/"+serviceIDA+"/instances?:serviceId="+serviceIDA, nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices/:serviceId/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("discovery A instances api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/instances?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query A api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query all api should return nil labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices", nil)
- assert.NoError(t, err)
+ newRequest := func(method, url string, body string) *http.Request {
+ request, _ := http.NewRequest(method, url, strings.NewReader(body))
+ util.SetRequestContext(request, rest.CtxMatchPattern, url)
+ return request
+ }
+ tests := []struct {
+ name string
+ request *http.Request
+ wantResource *auth.ResourceScope
+ wantErr bool
+ }{
+ {
+ "get all services api should return no labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "create services api without body should return err",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices", "{}"),
Review comment:
这个ut最好补充和权限冲突的case,。比如允许创建应用a的服务,但是希望创建应用b的
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1019: SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641879708
##########
File path: server/plugin/auth/buildin/parser_test.go
##########
@@ -49,85 +55,216 @@ func TestGetAPIParseFunc(t *testing.T) {
})
serviceIDB = response.ServiceId
- t.Run("get all services api should return no labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
-
- t.Run("create services api without body should return err", func(t *testing.T) {
- reader := strings.NewReader("{}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- _, err = buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.Error(t, err)
- })
-
- t.Run("create services api with serviceName A should return A", func(t *testing.T) {
- reader := strings.NewReader("{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}")
-
- request, err := http.NewRequest(http.MethodPost, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("delete 2 services api should return 2 labels", func(t *testing.T) {
- reader := strings.NewReader("{\n \"serviceIds\": [\"" + serviceIDA + "\", \"" + serviceIDB + "\"]\n}")
-
- request, err := http.NewRequest(http.MethodDelete, "/v4/default/registry/microservices", reader)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 2, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("get service sub resource api should return service labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/microservices/"+serviceIDA+"/instances?:serviceId="+serviceIDA, nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/microservices/:serviceId/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("discovery A instances api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/registry/instances?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/registry/instances")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query A api should return service A labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices?serviceName=A", nil)
- assert.NoError(t, err)
-
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Equal(t, 1, len(labels))
- assert.Equal(t, "A", labels[0]["serviceName"])
- })
-
- t.Run("govern query all api should return nil labels", func(t *testing.T) {
- request, err := http.NewRequest(http.MethodGet, "/v4/default/govern/microservices", nil)
- assert.NoError(t, err)
+ newRequest := func(method, url string, body string) *http.Request {
+ request, _ := http.NewRequest(method, url, strings.NewReader(body))
+ util.SetRequestContext(request, rest.CtxMatchPattern, url)
+ return request
+ }
+ tests := []struct {
+ name string
+ request *http.Request
+ wantResource *auth.ResourceScope
+ wantErr bool
+ }{
+ {
+ "get all services api should return no labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "create services api without body should return err",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices", "{}"),
+ nil,
+ true,
+ },
+ {
+ "create services api with serviceName A should return A",
+ newRequest(http.MethodPost, "/v4/:project/registry/microservices",
+ "{\n \"service\": {\n \"serviceName\": \"A\"\n }\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "create",
+ },
+ false,
+ },
+ {
+ "delete 2 services api should return 2 labels",
+ newRequest(http.MethodDelete, "/v4/:project/registry/microservices",
+ "{\n \"serviceIds\": [\""+serviceIDA+"\", \""+serviceIDB+"\"]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ {
+ "serviceName": "B",
+ },
+ },
+ Verb: "delete",
+ },
+ false,
+ },
+ {
+ "get service sub resource api should return service labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/microservices/:serviceId/instances?:serviceId="+serviceIDA, ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "discovery A instances api should return service A labels",
+ newRequest(http.MethodGet, "/v4/:project/registry/instances?serviceName=A", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern query A api should return service A labels",
+ newRequest(http.MethodGet, "/v4/:project/govern/microservices?serviceName=A", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern query all api should return nil labels",
+ newRequest(http.MethodGet, "/v4/:project/govern/microservices", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "batch discovery A instances should return A labels",
+ newRequest(http.MethodPost, "/v4/:project/registry/instances/action",
+ "{\n \"consumerId\": \"\",\n \"instances\": [\n {\n \"instance\": {\n \"serviceId\": \""+serviceIDA+"\",\n \"instanceId\": \"\"\n },\n \"rev\": \"\"\n }\n ]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "batch heartbeat A instances should return A labels",
+ newRequest(http.MethodPost, "/v4/:project/registry/heartbeats",
+ "{\n \"instances\": [\n {\n \"serviceId\": \""+serviceIDA+"\",\n \"instanceId\": \"\"\n }\n ]\n}"),
+ &auth.ResourceScope{
+ Type: "service",
+ Labels: []map[string]string{
+ {
+ "serviceName": "A",
+ },
+ },
+ Verb: "update",
+ },
+ false,
+ },
+ // not registered api
+ {
+ "govern statistics api should apply all",
+ newRequest(http.MethodGet, "/v4/:project/govern/statistics", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "govern apps api should apply all",
+ newRequest(http.MethodGet, "/v4/:project/govern/apps", ""),
+ &auth.ResourceScope{
+ Type: "service",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "account api should apply all",
+ newRequest(http.MethodGet, "/v4/accounts", ""),
+ &auth.ResourceScope{
+ Type: "account",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "role api should apply all",
+ newRequest(http.MethodGet, "/v4/roles", ""),
+ &auth.ResourceScope{
+ Type: "role",
+ Verb: "get",
+ },
+ false,
+ },
+ {
+ "role api should apply all",
+ newRequest(http.MethodGet, "/v1/:project/gov/", ""),
+ &auth.ResourceScope{
+ Type: "governance",
+ Verb: "get",
+ },
+ false,
+ },
+ }
- labels, err := buildin.GetAPIParseFunc("/v4/:project/govern/microservices")(request)
- assert.NoError(t, err)
- assert.Nil(t, labels)
- })
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ resource, err := buildin.GetAPIParseFunc(tt.request.URL.Path)(tt.request)
+ assert.Equal(t, tt.wantErr, err != nil)
+ assert.Equal(t, tt.wantResource == nil, resource == nil)
+ if tt.wantResource == nil {
+ return
+ }
+ assert.Equal(t, tt.wantResource.Type, resource.Type)
+ assert.Equal(t, tt.wantResource.Verb, resource.Verb)
+ assert.Equal(t, tt.wantResource.Labels == nil, resource.Labels == nil)
+ for _, labels := range tt.wantResource.Labels {
+ LOOP:
Review comment:
DONE
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #1019: WIP SCB-2176 Fix: targetResource label key not exist should be pass
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #1019:
URL: https://github.com/apache/servicecomb-service-center/pull/1019#discussion_r641435725
##########
File path: server/service/rbac/decision.go
##########
@@ -74,7 +74,7 @@ func FilterLabel(targetResourceLabel []map[string]string, permLabelList []map[st
func LabelMatched(targetResourceLabel map[string]string, permLabel map[string]string) bool {
for k, v := range permLabel {
- if vv := targetResourceLabel[k]; vv != v {
+ if vv, ok := targetResourceLabel[k]; ok && vv != v {
Review comment:
该合入可能会造成越权
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org