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

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 515bb8bff feat(plugins): secret key and token are not empty now (#6704)
515bb8bff is described below

commit 515bb8bff7b8b02760a55ebd23930e65d6d926c6
Author: Lynwee <[email protected]>
AuthorDate: Fri Dec 29 11:08:54 2023 +0800

    feat(plugins): secret key and token are not empty now (#6704)
---
 backend/core/utils/strings.go                   | 16 +++++++
 backend/core/utils/strings_test.go              | 57 +++++++++++++++++++++++++
 backend/plugins/ae/models/connection.go         |  2 +-
 backend/plugins/circleci/models/connection.go   |  3 +-
 backend/plugins/feishu/models/connection.go     |  3 +-
 backend/plugins/gitee/models/connection.go      |  3 +-
 backend/plugins/gitlab/models/connection.go     |  3 +-
 backend/plugins/jira/models/connection.go       |  3 +-
 backend/plugins/opsgenie/models/connection.go   |  3 +-
 backend/plugins/pagerduty/models/connection.go  |  3 +-
 backend/plugins/slack/models/connection.go      |  3 +-
 backend/plugins/sonarqube/models/connection.go  |  3 +-
 backend/plugins/teambition/models/connection.go |  3 +-
 backend/plugins/trello/models/connection.go     |  3 +-
 14 files changed, 96 insertions(+), 12 deletions(-)

diff --git a/backend/core/utils/strings.go b/backend/core/utils/strings.go
index ec0805ffa..ca84c26cf 100644
--- a/backend/core/utils/strings.go
+++ b/backend/core/utils/strings.go
@@ -21,6 +21,7 @@ import (
        "crypto/rand"
        "github.com/apache/incubator-devlake/core/errors"
        "math/big"
+       "strings"
 )
 
 const letterBytes = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
@@ -65,3 +66,18 @@ func RandLetterBytes(n int) (string, errors.Error) {
 
        return string(ret), nil
 }
+
+func SanitizeString(s string) string {
+       if s == "" {
+               return s
+       }
+       strLen := len(s)
+       if strLen <= 2 {
+               return strings.Repeat("*", strLen)
+       }
+       prefixLen, suffixLen := 2, 2
+       if strLen <= 5 {
+               prefixLen, suffixLen = 1, 1
+       }
+       return strings.Replace(s, s[prefixLen:strLen-suffixLen], 
strings.Repeat("*", strLen-prefixLen-suffixLen), -1)
+}
diff --git a/backend/core/utils/strings_test.go 
b/backend/core/utils/strings_test.go
index f6764a43a..6e865f43c 100644
--- a/backend/core/utils/strings_test.go
+++ b/backend/core/utils/strings_test.go
@@ -62,3 +62,60 @@ func TestRandLetterBytes(t *testing.T) {
                })
        }
 }
+
+func TestSanitizeString(t *testing.T) {
+       type args struct {
+               s string
+       }
+       tests := []struct {
+               name string
+               args args
+               want string
+       }{
+               {
+                       name: "test-1",
+                       args: args{s: ""},
+                       want: "",
+               },
+               {
+                       name: "test-2",
+                       args: args{s: "s"},
+                       want: "*",
+               },
+               {
+                       name: "test-3",
+                       args: args{s: "ss"},
+                       want: "**",
+               },
+               {
+                       name: "test-4",
+                       args: args{s: "s1s"},
+                       want: "s*s",
+               },
+               {
+                       name: "test-5",
+                       args: args{s: "s12s"},
+                       want: "s**s",
+               },
+               {
+                       name: "test-6",
+                       args: args{s: "s123s"},
+                       want: "s***s",
+               },
+               {
+                       name: "test-7",
+                       args: args{s: "s1234s"},
+                       want: "s1**4s",
+               },
+               {
+                       name: "test-8",
+                       args: args{s: "s123456789s"},
+                       want: "s1*******9s",
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       assert.Equalf(t, tt.want, SanitizeString(tt.args.s), 
"SanitizeString(%v)", tt.args.s)
+               })
+       }
+}
diff --git a/backend/plugins/ae/models/connection.go 
b/backend/plugins/ae/models/connection.go
index d13767c7b..d06f705e3 100644
--- a/backend/plugins/ae/models/connection.go
+++ b/backend/plugins/ae/models/connection.go
@@ -66,7 +66,7 @@ func (AeConnection) TableName() string {
 }
 
 func (connection AeConnection) Sanitize() AeConnection {
-       connection.AeAppKey.SecretKey = ""
+       connection.AeAppKey.SecretKey = 
utils.SanitizeString(connection.AeAppKey.SecretKey)
        return connection
 }
 
diff --git a/backend/plugins/circleci/models/connection.go 
b/backend/plugins/circleci/models/connection.go
index 60d661581..eb5bcfd4e 100644
--- a/backend/plugins/circleci/models/connection.go
+++ b/backend/plugins/circleci/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/utils"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "net/http"
 )
@@ -45,6 +46,6 @@ func (CircleciConnection) TableName() string {
 }
 
 func (connection CircleciConnection) Sanitize() CircleciConnection {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
diff --git a/backend/plugins/feishu/models/connection.go 
b/backend/plugins/feishu/models/connection.go
index c9646db61..76320579f 100644
--- a/backend/plugins/feishu/models/connection.go
+++ b/backend/plugins/feishu/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -34,7 +35,7 @@ type FeishuConn struct {
 }
 
 func (conn *FeishuConn) Sanitize() FeishuConn {
-       conn.SecretKey = ""
+       conn.SecretKey = utils.SanitizeString(conn.SecretKey)
        return *conn
 }
 
diff --git a/backend/plugins/gitee/models/connection.go 
b/backend/plugins/gitee/models/connection.go
index 996009113..c80eea5df 100644
--- a/backend/plugins/gitee/models/connection.go
+++ b/backend/plugins/gitee/models/connection.go
@@ -18,6 +18,7 @@ limitations under the License.
 package models
 
 import (
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -41,7 +42,7 @@ type GiteeConn struct {
 }
 
 func (connection GiteeConn) Sanitize() GiteeConn {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
 
diff --git a/backend/plugins/gitlab/models/connection.go 
b/backend/plugins/gitlab/models/connection.go
index aad7c6b98..a8f22016a 100644
--- a/backend/plugins/gitlab/models/connection.go
+++ b/backend/plugins/gitlab/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -43,7 +44,7 @@ func (conn *GitlabConn) SetupAuthentication(request 
*http.Request) errors.Error
 }
 
 func (conn *GitlabConn) Sanitize() GitlabConn {
-       conn.Token = ""
+       conn.Token = utils.SanitizeString(conn.Token)
        return *conn
 }
 
diff --git a/backend/plugins/jira/models/connection.go 
b/backend/plugins/jira/models/connection.go
index 0229988f8..ae5666a06 100644
--- a/backend/plugins/jira/models/connection.go
+++ b/backend/plugins/jira/models/connection.go
@@ -18,6 +18,7 @@ limitations under the License.
 package models
 
 import (
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -46,7 +47,7 @@ type JiraConn struct {
 
 func (jc *JiraConn) Sanitize() JiraConn {
        jc.Password = ""
-       jc.AccessToken.Token = ""
+       jc.AccessToken.Token = utils.SanitizeString(jc.AccessToken.Token)
        return *jc
 }
 
diff --git a/backend/plugins/opsgenie/models/connection.go 
b/backend/plugins/opsgenie/models/connection.go
index 67da098d4..10080b54a 100644
--- a/backend/plugins/opsgenie/models/connection.go
+++ b/backend/plugins/opsgenie/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -41,7 +42,7 @@ type OpsgenieConn struct {
 }
 
 func (connection OpsgenieConn) Sanitize() OpsgenieConn {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
 
diff --git a/backend/plugins/pagerduty/models/connection.go 
b/backend/plugins/pagerduty/models/connection.go
index e287b4f48..c51d3f9de 100644
--- a/backend/plugins/pagerduty/models/connection.go
+++ b/backend/plugins/pagerduty/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -64,6 +65,6 @@ func (PagerDutyConnection) TableName() string {
 }
 
 func (connection PagerDutyConnection) Sanitize() PagerDutyConnection {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
diff --git a/backend/plugins/slack/models/connection.go 
b/backend/plugins/slack/models/connection.go
index e227e9ba7..758b208b1 100644
--- a/backend/plugins/slack/models/connection.go
+++ b/backend/plugins/slack/models/connection.go
@@ -18,6 +18,7 @@ limitations under the License.
 package models
 
 import (
+       "github.com/apache/incubator-devlake/core/utils"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 )
 
@@ -28,7 +29,7 @@ type SlackConn struct {
 }
 
 func (connection SlackConn) Sanitize() SlackConn {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
 
diff --git a/backend/plugins/sonarqube/models/connection.go 
b/backend/plugins/sonarqube/models/connection.go
index f881c6e3a..3a078df96 100644
--- a/backend/plugins/sonarqube/models/connection.go
+++ b/backend/plugins/sonarqube/models/connection.go
@@ -20,6 +20,7 @@ package models
 import (
        "encoding/base64"
        "fmt"
+       "github.com/apache/incubator-devlake/core/utils"
        "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
@@ -51,7 +52,7 @@ type SonarqubeConn struct {
 }
 
 func (connection SonarqubeConn) Sanitize() SonarqubeConn {
-       connection.Token = ""
+       connection.Token = utils.SanitizeString(connection.Token)
        return connection
 }
 
diff --git a/backend/plugins/teambition/models/connection.go 
b/backend/plugins/teambition/models/connection.go
index d58c07035..950d2135b 100644
--- a/backend/plugins/teambition/models/connection.go
+++ b/backend/plugins/teambition/models/connection.go
@@ -20,6 +20,7 @@ package models
 import (
        "fmt"
        "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/utils"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "github.com/golang-jwt/jwt/v5"
        "net/http"
@@ -35,7 +36,7 @@ type TeambitionConn struct {
 }
 
 func (tc TeambitionConn) Sanitize() TeambitionConn {
-       tc.SecretKey = ""
+       tc.SecretKey = utils.SanitizeString(tc.SecretKey)
        return tc
 }
 
diff --git a/backend/plugins/trello/models/connection.go 
b/backend/plugins/trello/models/connection.go
index 9fce2389a..e3be97cdf 100644
--- a/backend/plugins/trello/models/connection.go
+++ b/backend/plugins/trello/models/connection.go
@@ -20,6 +20,7 @@ package models
 import (
        "fmt"
        "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/utils"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "net/http"
 )
@@ -31,7 +32,7 @@ type TrelloConn struct {
 }
 
 func (tc *TrelloConn) Sanitize() TrelloConn {
-       tc.SecretKey = ""
+       tc.SecretKey = utils.SanitizeString(tc.SecretKey)
        return *tc
 }
 

Reply via email to