forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cluster_hardware.go
167 lines (152 loc) · 5.53 KB
/
data_cluster_hardware.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
// Copyright 2020 PingCAP, Inc.
//
// 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 telemetry
import (
"context"
"regexp"
"slices"
"strings"
"github.com/iancoleman/strcase"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/sqlexec"
)
var (
// Explicitly list all allowed field names to avoid potential leaking sensitive info when new fields are added later.
sortedCPUAllowedFieldNames = []string{"cpu-logical-cores", "cpu-physical-cores", "cpu-frequency", "cache", "cpu-vendor-id", "l1-cache-size", "l1-cache-line-size", "l2-cache-size", "l2-cache-line-size", "l3-cache-size", "l3-cache-line-size"}
sortedDiskAllowedFieldNames = []string{"fstype", "opts", "total", "free", "used", "free-percent", "used-percent"} // path is not included
regexDiskAllowedNames = regexp.MustCompile(`^(disk\d+s\d+|rootfs|devtmpfs|sd[a-z]\d*|vd[a-z]\d*|hd[a-z]\d*|nvme\d+(n\d+(p\d)?)?|md[\da-z]+)$`)
sortedDiskAllowedPaths = []string{"/", "/boot", "/dev", "/private/var/vm", "/System/Volumes/Data"}
sortedMemoryAllowedFieldNames = []string{"capacity"}
)
func init() {
slices.Sort(sortedCPUAllowedFieldNames)
slices.Sort(sortedDiskAllowedFieldNames)
slices.Sort(sortedDiskAllowedPaths)
slices.Sort(sortedMemoryAllowedFieldNames)
}
type clusterHardwareItem struct {
InstanceType string `json:"instanceType"`
ListenHostHash string `json:"listenHostHash"`
ListenPort string `json:"listenPort"`
CPU map[string]string `json:"cpu,omitempty"`
Memory map[string]string `json:"memory,omitempty"`
Disk map[string]map[string]string `json:"disk,omitempty"`
}
func normalizeDiskName(name string) string {
const prefix = "/dev/"
if strings.HasPrefix(name, prefix) {
return name[len(prefix):]
}
return name
}
func isNormalizedDiskNameAllowed(name string) bool {
return regexDiskAllowedNames.MatchString(name)
}
func normalizeFieldName(name string) string {
return strcase.ToLowerCamel(name)
}
func getClusterHardware(ctx context.Context, sctx sessionctx.Context) ([]*clusterHardwareItem, error) {
exec := sctx.(sqlexec.RestrictedSQLExecutor)
rows, _, err := exec.ExecRestrictedSQL(ctx, nil, `SELECT TYPE, INSTANCE, DEVICE_TYPE, DEVICE_NAME, NAME, VALUE FROM information_schema.cluster_hardware`)
if err != nil {
return nil, errors.Trace(err)
}
// WARNING: The key of this variable is not hashed, it must not be returned directly.
itemsByInstance := make(map[string]*clusterHardwareItem)
L:
for _, row := range rows {
if row.Len() < 6 {
continue L
}
instance := row.GetString(1)
activeItem, ok := itemsByInstance[instance]
if !ok {
hostHash, port, err := parseAddressAndHash(instance)
if err != nil {
return nil, err
}
activeItem = &clusterHardwareItem{
InstanceType: row.GetString(0),
ListenHostHash: hostHash,
ListenPort: port,
CPU: make(map[string]string),
Memory: make(map[string]string),
Disk: make(map[string]map[string]string),
}
itemsByInstance[instance] = activeItem
}
deviceType := row.GetString(2)
deviceName := row.GetString(3)
fieldName := row.GetString(4)
fieldValue := row.GetString(5)
switch deviceType {
case "cpu":
if deviceName != "cpu" {
continue L
}
if !sortedStringContains(sortedCPUAllowedFieldNames, fieldName) {
continue L
}
activeItem.CPU[normalizeFieldName(fieldName)] = fieldValue
case "memory":
if deviceName != "memory" {
continue L
}
if !sortedStringContains(sortedMemoryAllowedFieldNames, fieldName) {
continue L
}
activeItem.Memory[normalizeFieldName(fieldName)] = fieldValue
case "disk":
var hashedDeviceName string
normalizedDiskName := normalizeDiskName(deviceName)
if isNormalizedDiskNameAllowed(normalizedDiskName) {
// Use plain text only when it is in a list that we know safe.
hashedDeviceName = normalizedDiskName
} else {
hashedDeviceName, err = hashString(normalizedDiskName)
if err != nil {
return nil, err
}
}
activeDiskItem, ok := activeItem.Disk[hashedDeviceName]
if !ok {
activeDiskItem = make(map[string]string)
activeDiskItem[normalizeFieldName("deviceName")] = hashedDeviceName
activeItem.Disk[hashedDeviceName] = activeDiskItem
}
if fieldName == "path" {
var path string
if sortedStringContains(sortedDiskAllowedPaths, fieldValue) {
// Use plain text only when it is in a list that we know safe.
path = fieldValue
} else {
path, err = hashString(fieldValue)
if err != nil {
return nil, err
}
}
activeDiskItem[normalizeFieldName("path")] = path
} else if sortedStringContains(sortedDiskAllowedFieldNames, fieldName) {
activeDiskItem[normalizeFieldName(fieldName)] = fieldValue
}
}
}
r := make([]*clusterHardwareItem, 0, len(itemsByInstance))
for _, item := range itemsByInstance {
r = append(r, item)
}
// TODO: Go's map has random order. Better to generate a fixed-order result.
return r, nil
}