mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-05-11 17:44:47 +00:00
feat: improve security of the MaskString function
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
This commit is contained in:
parent
6f0865413f
commit
08f2a89e54
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user