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/06/16 09:54:06 UTC

[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1056: add account lock to manage login failure

little-cui commented on a change in pull request #1056:
URL: https://github.com/apache/servicecomb-service-center/pull/1056#discussion_r652531616



##########
File path: datasource/etcd/account_lock.go
##########
@@ -0,0 +1,87 @@
+// 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 etcd
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"time"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/client"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+)
+
+type AccountLockManager struct {
+	releaseAfter time.Duration
+}
+
+func (al AccountLockManager) GetLock(ctx context.Context, key string) (*datasource.AccountLock, error) {
+	resp, err := client.Instance().Do(ctx, client.GET,
+		client.WithStrKey(path.GenerateAccountLockKey(key)))
+	if err != nil {
+		return nil, err
+	}
+	if resp.Count == 0 {
+		return nil, datasource.ErrAccountLockNotExist
+	}
+	lock := &datasource.AccountLock{}
+	err = json.Unmarshal(resp.Kvs[0].Value, lock)
+	if err != nil {
+		log.Errorf(err, "format invalid")
+		return nil, err
+	}
+	return lock, nil
+}
+
+func (al AccountLockManager) DeleteLock(ctx context.Context, key string) error {
+	ok, err := client.Delete(ctx, key)
+	if err != nil {
+		log.Errorf(err, "remove lock failed")
+		return datasource.ErrCannotReleaseLock
+	}
+	if ok {
+		log.Info(fmt.Sprintf("%s is released", key))
+		return nil
+	}
+	return datasource.ErrCannotReleaseLock
+}
+
+func NewAccountLockManager(ReleaseAfter time.Duration) datasource.AccountLockManager {
+	return &AccountLockManager{releaseAfter: ReleaseAfter}
+}
+
+func (al AccountLockManager) Ban(ctx context.Context, key string) error {
+	l := &datasource.AccountLock{}
+	l.Key = key
+	l.Status = datasource.StatusBanned
+	l.ReleaseAt = time.Now().Add(al.releaseAfter)

Review comment:
       不能用本地时间,请统一排查使用unix时间戳

##########
File path: server/server.go
##########
@@ -111,14 +112,21 @@ func (s *ServiceCenterServer) initEndpoints() {
 func (s *ServiceCenterServer) initDatasource() {
 	// init datasource
 	kind := datasource.Kind(config.GetString("registry.kind", "", config.WithStandby("registry_plugin")))
+	ReleaseAccountAfter := config.GetString("rbac.releaseLockAfter", "15m")

Review comment:
       rbac.releaseLockAfter同步更新到etc/conf/app.yml,并在配置文件中注释用途

##########
File path: datasource/etcd/account_lock.go
##########
@@ -0,0 +1,87 @@
+// 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 etcd
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"time"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/client"
+	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+)
+
+type AccountLockManager struct {
+	releaseAfter time.Duration
+}
+
+func (al AccountLockManager) GetLock(ctx context.Context, key string) (*datasource.AccountLock, error) {
+	resp, err := client.Instance().Do(ctx, client.GET,
+		client.WithStrKey(path.GenerateAccountLockKey(key)))
+	if err != nil {
+		return nil, err
+	}
+	if resp.Count == 0 {
+		return nil, datasource.ErrAccountLockNotExist
+	}
+	lock := &datasource.AccountLock{}
+	err = json.Unmarshal(resp.Kvs[0].Value, lock)
+	if err != nil {
+		log.Errorf(err, "format invalid")
+		return nil, err
+	}
+	return lock, nil
+}
+
+func (al AccountLockManager) DeleteLock(ctx context.Context, key string) error {
+	ok, err := client.Delete(ctx, key)
+	if err != nil {
+		log.Errorf(err, "remove lock failed")
+		return datasource.ErrCannotReleaseLock
+	}
+	if ok {
+		log.Info(fmt.Sprintf("%s is released", key))
+		return nil
+	}
+	return datasource.ErrCannotReleaseLock
+}
+
+func NewAccountLockManager(ReleaseAfter time.Duration) datasource.AccountLockManager {
+	return &AccountLockManager{releaseAfter: ReleaseAfter}
+}
+
+func (al AccountLockManager) Ban(ctx context.Context, key string) error {
+	l := &datasource.AccountLock{}
+	l.Key = key
+	l.Status = datasource.StatusBanned
+	l.ReleaseAt = time.Now().Add(al.releaseAfter)

Review comment:
       请统一使用unix时间戳




-- 
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