Skip to content

Commit

Permalink
feat: improve security of the MaskString function
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Apr 11, 2023
1 parent 6f08654 commit 08f2a89
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ k8sgpt analyze --explain --filter=Service --output=json --anonymize

With this option, the data is anonymized before being sent to the AI Backend. During the analysis execution, `k8sgpt` retrieves sensitive data (Kubernetes object names, labels, etc.). This data is masked when sent to the AI backend and replaced by a key that can be used to de-anonymize the data when the solution is returned to the user.

For example:
<details>

1. Error reported during analysis:
```bash
Expand Down
10 changes: 7 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"context"
"encoding/base64"
"fmt"
"math/rand"
"regexp"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 08f2a89

Please sign in to comment.