feat: improve security of the MaskString function

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
This commit is contained in:
Matthis Holleville
2023-04-11 17:05:30 +02:00
parent 6f0865413f
commit 08f2a89e54
2 changed files with 8 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package util
import (
"context"
"encoding/base64"
"fmt"
"math/rand"
"regexp"
@@ -10,6 +11,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var anonymizePattern = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;':\",./<>?")
func GetParent(client *kubernetes.Client, meta metav1.ObjectMeta) (string, bool) {
if meta.OwnerReferences != nil {
for _, owner := range meta.OwnerReferences {
@@ -101,12 +104,13 @@ func SliceDiff(source, dest []string) []string {
}
func MaskString(input string) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
key := make([]byte, len(input))
result := make([]rune, len(input))
rand.Read(key)
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
result[i] = anonymizePattern[int(key[i])%len(anonymizePattern)]
}
return string(result)
return base64.StdEncoding.EncodeToString([]byte(string(result)))
}
func ReplaceIfMatch(text string, pattern string, replacement string) string {