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 2020/07/09 10:12:10 UTC
[GitHub] [servicecomb-service-center] tianxiaoliang opened a new pull request #661: build in role permission check
tianxiaoliang opened a new pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661
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).
---
----------------------------------------------------------------
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 #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452620641
##########
File path: server/rest/controller/v4/auth_resource.go
##########
@@ -53,32 +56,57 @@ func (r *AuthResource) ChangePassword(w http.ResponseWriter, req *http.Request)
a := &model.Account{}
if err = json.Unmarshal(body, a); err != nil {
log.Error("json err", err)
+ controller.WriteError(w, scerror.ErrInvalidParams, errorsEx.ErrMsgJSON)
+ return
+ }
+ err = service.ValidateCreateAccount(a)
+ if err != nil {
controller.WriteError(w, scerror.ErrInvalidParams, err.Error())
return
}
+ err = dao.CreateAccount(context.TODO(), a)
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
[GitHub] [servicecomb-service-center] tianxiaoliang merged pull request #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
tianxiaoliang merged pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661
----------------------------------------------------------------
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] aseTo2016 commented on a change in pull request #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
aseTo2016 commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452568569
##########
File path: server/rest/controller/v4/auth_resource.go
##########
@@ -53,32 +56,57 @@ func (r *AuthResource) ChangePassword(w http.ResponseWriter, req *http.Request)
a := &model.Account{}
if err = json.Unmarshal(body, a); err != nil {
log.Error("json err", err)
+ controller.WriteError(w, scerror.ErrInvalidParams, errorsEx.ErrMsgJSON)
+ return
+ }
+ err = service.ValidateCreateAccount(a)
+ if err != nil {
controller.WriteError(w, scerror.ErrInvalidParams, err.Error())
return
}
+ err = dao.CreateAccount(context.TODO(), a)
Review comment:
这种创建的接口,建议打个日志,不然后续不太好定位问题
##########
File path: server/service/rbac/decision.go
##########
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package rbac
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ "github.com/apache/servicecomb-service-center/server/service/rbac/dao"
+)
+
+func Allow(ctx context.Context, role, project, resource, verbs string) (bool, error) {
+ r := dao.GetRole(ctx, role)
+ if r == nil {
+ log.Warn("empty role info")
+ return false, nil
+ }
+ ps := r.Permissions
+ if len(ps) == 0 {
+ log.Warn("role has no any permissions")
+ return false, nil
+ }
+ p, ok := ps[resource]
+ if !ok {
Review comment:
建议合成一行!ok || p == nil
##########
File path: server/service/rbac/dao/role_dao.go
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dao
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/model"
+)
+
+//TODO delete dead code and use etcd
+var roleMap = map[string]*model.Role{
+ "admin": {Permissions: map[string]*model.Permission{
+ "account": {Verbs: []string{"*"}},
+ "service": {Verbs: []string{"*"}},
+ }},
+ "developer": {Permissions: map[string]*model.Permission{
+ "service": {Verbs: []string{"*"}},
+ }},
+}
+
+func GetRole(ctx context.Context, role string) *model.Role {
+ return roleMap[role]
+}
+func GetRoles(ctx context.Context) []*model.Role {
+ roles := make([]*model.Role, len(roleMap))
Review comment:
roles := make([]*model.Role, len(roleMap))应该是roles := make([]*model.Role, 0, len(roleMap))?
##########
File path: server/plugin/auth/buildin/buildin.go
##########
@@ -54,15 +57,37 @@ func (ba *TokenAuthenticator) Identify(req *http.Request) error {
}
s := strings.Split(v, " ")
if len(s) != 2 {
- return errors.New("invalid auth header")
+ return auth.ErrInvalidHeader
}
to := s[1]
- //TODO rbac
+
claims, err := authr.Authenticate(req.Context(), to)
if err != nil {
log.Errorf(err, "authenticate request failed, %s %s", req.Method, req.RequestURI)
return err
}
+ //TODO rbac
+ m, ok := claims.(map[string]interface{})
+ if !ok {
+ log.Error("claims convert failed", errors.New(errorsEx.ErrMsgConvert))
Review comment:
建议定义一个error errors.New(errorsEx.ErrMsgConvert),下面都有使用这个错误
----------------------------------------------------------------
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] aseTo2016 commented on a change in pull request #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
aseTo2016 commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452641863
##########
File path: server/service/rbac/dao/role_dao.go
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dao
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/model"
+)
+
+//TODO delete dead code and use etcd
+var roleMap = map[string]*model.Role{
+ "admin": {Permissions: map[string]*model.Permission{
+ "account": {Verbs: []string{"*"}},
+ "service": {Verbs: []string{"*"}},
+ }},
+ "developer": {Permissions: map[string]*model.Permission{
+ "service": {Verbs: []string{"*"}},
+ }},
+}
+
+func GetRole(ctx context.Context, role string) *model.Role {
+ return roleMap[role]
+}
+func GetRoles(ctx context.Context) []*model.Role {
+ roles := make([]*model.Role, len(roleMap))
Review comment:
定义的数组roles 为长度和cap都是len(roleMap),再使用append,会导致len(roleMap)个数据都是nil
----------------------------------------------------------------
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 #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452620196
##########
File path: server/service/rbac/dao/role_dao.go
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dao
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/model"
+)
+
+//TODO delete dead code and use etcd
+var roleMap = map[string]*model.Role{
+ "admin": {Permissions: map[string]*model.Permission{
+ "account": {Verbs: []string{"*"}},
+ "service": {Verbs: []string{"*"}},
+ }},
+ "developer": {Permissions: map[string]*model.Permission{
+ "service": {Verbs: []string{"*"}},
+ }},
+}
+
+func GetRole(ctx context.Context, role string) *model.Role {
+ return roleMap[role]
+}
+func GetRoles(ctx context.Context) []*model.Role {
+ roles := make([]*model.Role, len(roleMap))
Review comment:
map最后大小一定是跟slice一样,不需要这么写
----------------------------------------------------------------
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] aseTo2016 commented on a change in pull request #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
aseTo2016 commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452655207
##########
File path: server/rest/controller/v4/auth_resource.go
##########
@@ -40,66 +41,73 @@ type AuthResource struct {
func (r *AuthResource) URLPatterns() []rest.Route {
return []rest.Route{
{Method: http.MethodPost, Path: "/v4/token", Func: r.Login},
+ {Method: http.MethodPost, Path: "/v4/account", Func: r.CreateAccount},
{Method: http.MethodPut, Path: "/v4/account-password", Func: r.ChangePassword},
}
}
-func (r *AuthResource) ChangePassword(w http.ResponseWriter, req *http.Request) {
+func (r *AuthResource) CreateAccount(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error("read body err", err)
controller.WriteError(w, scerror.ErrInternal, err.Error())
return
}
- a := &model.Account{}
+ a := &rbac2.Account{}
if err = json.Unmarshal(body, a); err != nil {
log.Error("json err", err)
+ controller.WriteError(w, scerror.ErrInvalidParams, errorsEx.ErrMsgJSON)
+ return
+ }
+ err = service.ValidateCreateAccount(a)
+ if err != nil {
controller.WriteError(w, scerror.ErrInvalidParams, err.Error())
return
}
- if a.Password == "" {
- controller.WriteError(w, scerror.ErrInvalidParams, "new password is empty")
+ err = dao.CreateAccount(context.TODO(), a)
+ if err != nil {
+ log.Error(errorsEx.ErrMsgCreateAccount, err)
+ controller.WriteError(w, scerror.ErrInternal, errorsEx.ErrMsgCreateAccount)
return
}
- claims := rbac.FromContext(req.Context())
- m, ok := claims.(map[string]interface{})
- if !ok {
- log.Error("claims convert failed", errors.New(util2.ErrMsgConvert))
- controller.WriteError(w, scerror.ErrInvalidParams, util2.ErrMsgConvert)
+}
+func (r *AuthResource) ChangePassword(w http.ResponseWriter, req *http.Request) {
+ body, err := ioutil.ReadAll(req.Body)
+ if err != nil {
+ log.Error("read body err", err)
+ controller.WriteError(w, scerror.ErrInternal, err.Error())
return
}
- accountNameI := m[rbac.ClaimsUser]
- changer, ok := accountNameI.(string)
- if !ok {
- log.Error("claims convert failed", errors.New(util2.ErrMsgConvert))
- controller.WriteError(w, scerror.ErrInternal, util2.ErrMsgConvert)
+ a := &rbac2.Account{}
+ if err = json.Unmarshal(body, a); err != nil {
+ log.Error("json err", err)
+ controller.WriteError(w, scerror.ErrInvalidParams, errorsEx.ErrMsgJSON)
return
}
- roleI := m[rbac.ClaimsRole]
- role, ok := roleI.(string)
- if !ok {
- log.Error("claims convert failed", errors.New(util2.ErrMsgConvert))
- controller.WriteError(w, scerror.ErrInternal, util2.ErrMsgConvert)
+ if a.Password == "" {
Review comment:
a.Password需要对密码的格式进行校验
----------------------------------------------------------------
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 #661: build in role permission check
Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #661:
URL: https://github.com/apache/servicecomb-service-center/pull/661#discussion_r452646199
##########
File path: server/service/rbac/dao/role_dao.go
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dao
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/model"
+)
+
+//TODO delete dead code and use etcd
+var roleMap = map[string]*model.Role{
+ "admin": {Permissions: map[string]*model.Permission{
+ "account": {Verbs: []string{"*"}},
+ "service": {Verbs: []string{"*"}},
+ }},
+ "developer": {Permissions: map[string]*model.Permission{
+ "service": {Verbs: []string{"*"}},
+ }},
+}
+
+func GetRole(ctx context.Context, role string) *model.Role {
+ return roleMap[role]
+}
+func GetRoles(ctx context.Context) []*model.Role {
+ roles := make([]*model.Role, len(roleMap))
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