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/10 01:18:07 UTC
[GitHub] [servicecomb-service-center] aseTo2016 commented on a change in pull request #661: build in role permission check
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