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/14 11:29:19 UTC

[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #666: block user brute force login attempts

tianxiaoliang commented on a change in pull request #666:
URL: https://github.com/apache/servicecomb-service-center/pull/666#discussion_r454288918



##########
File path: server/service/rbac/blocker.go
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 (
+	"sync"
+	"time"
+
+	"golang.org/x/time/rate"
+)
+
+const (
+	MaxAttempts = 5
+
+	BlockInterval = 1 * time.Hour
+)
+
+var BanTime = 1 * time.Hour
+
+type Client struct {
+	limiter   *rate.Limiter
+	Key       string
+	Banned    bool
+	ReleaseAt time.Time //at this time client can be allow to attempt to do something
+}
+
+var clients sync.Map
+
+func BannedList() []*Client {
+	cs := make([]*Client, 0)
+	clients.Range(func(key, value interface{}) bool {
+		client := value.(*Client)
+		if client.Banned && time.Now().After(client.ReleaseAt) {
+			client.Banned = false
+			client.ReleaseAt = time.Time{}
+			return true
+		}
+		cs = append(cs, value.(*Client))
+		return true
+	})
+	return cs
+}
+
+//CountFailure can cause a client banned
+// it use time/rate to allow certainty failure,
+//but will ban client if rate limiter can not accept failures
+func CountFailure(key string) {
+	var c interface{}
+	var client *Client
+	var ok bool
+	now := time.Now()
+	if c, ok = clients.Load(key); !ok {
+		client = &Client{
+			Key:       key,
+			limiter:   rate.NewLimiter(rate.Every(BlockInterval), MaxAttempts),
+			ReleaseAt: time.Time{},
+		}
+		clients.Store(key, client)
+	} else {
+		client = c.(*Client)
+	}
+
+	allow := client.limiter.AllowN(time.Now(), 1)

Review comment:
       不会出现这种情况,因为ban了之后根本走不进这个逻辑




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