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

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new 6e7baae  return if err (#852)
6e7baae is described below

commit 6e7baae664288da4772c7957997348098cc23b4a
Author: Shawn <[email protected]>
AuthorDate: Tue Feb 9 14:12:14 2021 +0800

    return if err (#852)
---
 .gitignore                  |  2 +-
 datasource/etcd/account.go  |  8 +++-----
 datasource/mongo/account.go |  6 ++----
 pkg/pravicy/password.go     | 32 ++++++++++++++++++++++++++++++++
 server/service/rbac/rbac.go |  1 +
 5 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index b9518c5..1c85ed4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,6 @@ docs/_static
 docs/_templates
 
 .DS_Store
-
+main
 # go mod
 go.sum
\ No newline at end of file
diff --git a/datasource/etcd/account.go b/datasource/etcd/account.go
index 897196c..3306c79 100644
--- a/datasource/etcd/account.go
+++ b/datasource/etcd/account.go
@@ -19,6 +19,7 @@ import (
        "context"
        "encoding/json"
        "fmt"
+       "github.com/apache/servicecomb-service-center/pkg/pravicy"
 
        "github.com/apache/servicecomb-service-center/datasource"
        "github.com/apache/servicecomb-service-center/datasource/etcd/client"
@@ -27,8 +28,6 @@ import (
        "github.com/apache/servicecomb-service-center/pkg/log"
        "github.com/apache/servicecomb-service-center/pkg/rbacframe"
        "github.com/apache/servicecomb-service-center/pkg/util"
-       "github.com/go-chassis/foundation/stringutil"
-       "golang.org/x/crypto/bcrypt"
 )
 
 func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) 
error {
@@ -51,12 +50,11 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbacframe.Account) e
        if exist {
                return datasource.ErrAccountDuplicated
        }
-       hash, err := bcrypt.GenerateFromPassword([]byte(a.Password), 14)
+       a.Password, err = pravicy.HashPassword(a.Password)
        if err != nil {
-               log.Errorf(err, "pwd hash failed")
+               log.Error("pwd hash failed", err)
                return err
        }
-       a.Password = stringutil.Bytes2str(hash)
        a.ID = util.GenerateUUID()
        value, err := json.Marshal(a)
        if err != nil {
diff --git a/datasource/mongo/account.go b/datasource/mongo/account.go
index 58338c2..99811e6 100644
--- a/datasource/mongo/account.go
+++ b/datasource/mongo/account.go
@@ -20,15 +20,14 @@ package mongo
 import (
        "context"
        "errors"
+       "github.com/apache/servicecomb-service-center/pkg/pravicy"
 
        "github.com/apache/servicecomb-service-center/datasource"
        "github.com/apache/servicecomb-service-center/datasource/mongo/client"
        "github.com/apache/servicecomb-service-center/pkg/log"
        "github.com/apache/servicecomb-service-center/pkg/rbacframe"
        "github.com/apache/servicecomb-service-center/pkg/util"
-       "github.com/go-chassis/foundation/stringutil"
        "go.mongodb.org/mongo-driver/bson"
-       "golang.org/x/crypto/bcrypt"
 )
 
 func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) 
error {
@@ -40,12 +39,11 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbacframe.Account) e
        if exist {
                return datasource.ErrAccountDuplicated
        }
-       hash, err := bcrypt.GenerateFromPassword([]byte(a.Password), 14)
+       a.Password, err = pravicy.HashPassword(a.Password)
        if err != nil {
                log.Error("pwd hash failed", err)
                return err
        }
-       a.Password = stringutil.Bytes2str(hash)
        a.ID = util.GenerateUUID()
        _, err = client.GetMongoClient().Insert(ctx, CollectionAccount, a)
        if err != nil {
diff --git a/pkg/pravicy/password.go b/pkg/pravicy/password.go
new file mode 100644
index 0000000..9be80b9
--- /dev/null
+++ b/pkg/pravicy/password.go
@@ -0,0 +1,32 @@
+/*
+ * 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 pravicy
+
+import (
+       "github.com/go-chassis/foundation/stringutil"
+       "golang.org/x/crypto/bcrypt"
+)
+
+//HashPassword
+func HashPassword(pwd string) (string, error) {
+       hash, err := bcrypt.GenerateFromPassword([]byte(pwd), 14)
+       if err != nil {
+               return "", err
+       }
+       return stringutil.Bytes2str(hash), nil
+}
diff --git a/server/service/rbac/rbac.go b/server/service/rbac/rbac.go
index 66f0656..08348ce 100644
--- a/server/service/rbac/rbac.go
+++ b/server/service/rbac/rbac.go
@@ -87,6 +87,7 @@ func readPrivateKey() {
        err = archaius.Set("rbac_private_key", string(data))
        if err != nil {
                log.Fatal("can not init rbac", err)
+               return
        }
        log.Info("read private key success")
 }

Reply via email to