-
Notifications
You must be signed in to change notification settings - Fork 49
/
util.go
183 lines (166 loc) · 5.98 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright The Karpor Authors.
//
// Licensed 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 cluster
import (
"context"
"crypto/md5"
"encoding/base64"
"fmt"
"net"
"net/url"
"sort"
"strings"
"time"
"github.com/KusionStack/karpor/pkg/util/ctxutil"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
)
// maskContent to apply MD5 hash and mask the content.
func maskContent(content string) string {
// Apply MD5 hash
hash := fmt.Sprintf("%x", md5.Sum([]byte(content)))
// Calculate the range for masking
maskLength := len(hash) * 3 / 4 // Three quarters of the hash length
start := len(hash) / 8 // Start masking a quarter in
end := start + maskLength // End masking
masked := hash[:start] + strings.Repeat("*", maskLength) + hash[end:]
return masked
}
// checkEndpointConnectivity checks the network connectivity of the Kubernetes
// API endpoint.
func checkEndpointConnectivity(endpoint string) error {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
host := u.Host
if u.Port() == "" {
host = fmt.Sprintf("%s:443", u.Hostname()) // Kubernetes API default port is 443
}
// Set timeout duration
timeout := 5 * time.Second
conn, err := net.DialTimeout("tcp", host, timeout)
if err != nil {
return err
}
defer conn.Close()
return nil
}
// buildClientConfigFromKubeConfig generates a clientConfig from the provided
// KubeConfig.
func buildClientConfigFromKubeConfig(config *KubeConfig) (*rest.Config, error) {
// Create an initial rest.Config object.
clientConfig := &rest.Config{}
// Set the API server and authentication details.
if len(config.Clusters) > 0 {
cluster := config.Clusters[0].Cluster
clientConfig.Host = cluster.Server
if plain, err := base64.StdEncoding.DecodeString(cluster.CertificateAuthorityData); err != nil {
return nil, errors.Wrapf(
err,
"invalid certificate-authority-data for cluster %s",
config.Clusters[0].Name,
)
} else {
clientConfig.CAData = plain
}
}
if len(config.Users) > 0 {
user := config.Users[0].User
clientConfig.Username = user.Username
clientConfig.Password = user.Password
if plain, err := base64.StdEncoding.DecodeString(user.ClientCertificateData); err != nil {
return nil, fmt.Errorf(
"invalid client-certificate-data for user %s: %v",
config.Users[0].Name,
err,
)
} else {
clientConfig.CertData = plain
}
if plain, err := base64.StdEncoding.DecodeString(user.ClientKeyData); err != nil {
return nil, fmt.Errorf(
"invalid client-key-data for user %s: %v",
config.Users[0].Name,
err,
)
} else {
clientConfig.KeyData = plain
}
}
return clientConfig, nil
}
// SanitizeUnstructuredCluster masks sensitive information within a Unstructured
// cluster object, such as user credentials and certificate data.
func SanitizeUnstructuredCluster(
ctx context.Context,
cluster *unstructured.Unstructured,
) (*unstructured.Unstructured, error) {
log := ctxutil.GetLogger(ctx)
// Inform that the unmarshaling process has started.
log.Info("Sanitizing unstructured cluster...")
sanitized := cluster
if token, ok := sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["credential"].(map[string]interface{})["serviceAccountToken"]; ok {
sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["credential"].(map[string]interface{})["serviceAccountToken"] = maskContent(
token.(string),
)
}
if x509, ok := sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["credential"].(map[string]interface{})["x509"]; ok &&
x509 != nil {
sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["credential"].(map[string]interface{})["x509"].(map[string]interface{})["certificate"] = []byte(
maskContent(x509.(map[string]interface{})["certificate"].(string)),
)
sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["credential"].(map[string]interface{})["x509"].(map[string]interface{})["privateKey"] = []byte(
maskContent(x509.(map[string]interface{})["privateKey"].(string)),
)
}
if caBundle, ok := sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["caBundle"]; ok {
sanitized.Object["spec"].(map[string]interface{})["access"].(map[string]interface{})["caBundle"] = []byte(
maskContent(caBundle.(string)),
)
}
if _, ok := sanitized.Object["metadata"].(map[string]interface{})["annotations"]; ok {
sanitized.Object["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["kubectl.kubernetes.io/last-applied-configuration"] = "[redacted]"
}
return sanitized, nil
}
// SortUnstructuredList returns a sorted unstructured.UnstructuredList based on criteria
func SortUnstructuredList(
unList *unstructured.UnstructuredList,
criteria SortCriteria,
descending bool,
) (*unstructured.UnstructuredList, error) {
switch criteria {
case ByTimestamp:
sort.Slice(unList.Items, func(i, j int) bool {
if descending {
return unList.Items[i].GetCreationTimestamp().
UTC().
After(unList.Items[j].GetCreationTimestamp().UTC())
} else {
return unList.Items[i].GetCreationTimestamp().UTC().Before(unList.Items[j].GetCreationTimestamp().UTC())
}
})
case ByName:
sort.Slice(unList.Items, func(i, j int) bool {
if descending {
return unList.Items[i].GetName() > unList.Items[j].GetName()
} else {
return unList.Items[i].GetName() < unList.Items[j].GetName()
}
})
}
return unList, nil
}