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:03:33 UTC

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

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



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

Review comment:
       直接使用client 即可

##########
File path: server/rest/controller/v4/auth_resource.go
##########
@@ -129,6 +148,8 @@ func (r *AuthResource) Login(w http.ResponseWriter, req *http.Request) {
 	if err != nil {
 		if err == rbac.ErrUnauthorized {
 			log.Error("not authorized", err)
+			ip := util.GetRealIP(req)

Review comment:
       129行已经获取了ip,这里不用再次获取了

##########
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:
       allow 是false,然后client.Banned已经被设置为true, ReleaseAt 会再次刷新,这种场景是不是有点问题?




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