cubefs

Форк
0
310 строк · 8.2 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package auditlog
16

17
import (
18
	"sort"
19
	"strings"
20
)
21

22
const (
23
	// upper letter[A-Z]
24
	UpperLetter = 0x01
25
	// lower letter[a-z]
26
	LowerLetter = 0x02
27
	// digital[0-9]
28
	Digital = 0x04
29
	// underline[_]
30
	Underline = 0x08
31
	// xdigit[0-9a-fA-F]
32
	HexDigital = 0x10
33
	// [\r\n]
34
	Newline = 0x20
35
	// [+]
36
	AddLetter = 0x40
37
	// [-]
38
	SubLetter = 0x80
39
	// [*]
40
	Asterisk = 0x100
41
	// [/]
42
	SlantLetter = 0x200
43
	// [<]
44
	LtLetter = 0x400
45
	// [>]
46
	GtLetter = 0x800
47
	// [=]
48
	EqLetter = 0x1000
49
	// [\\]
50
	RSlantLetter = 0x2000
51
	// [.]
52
	DotLetter = 0x4000
53
	// [:], colon
54
	ColonLetter = 0x8000
55
	// [%]
56
	PercentLetter = 0x10000
57
	// [&]
58
	AndLetter = 0x20000
59
	// [|]
60
	OrLetter = 0x40000
61
	// [ ]
62
	SpaceLetter = 0x80000
63
	// [r]
64
	RLetter = 0x100000
65
	// [t]
66
	TLetter = 0x200000
67
	// [n]
68
	NLetter = 0x400000
69
	// [w]
70
	WLetter = 0x800000
71
	// [,]
72
	CommaLetter = 0x1000000
73
	// [;]
74
	SemiColonLetter = 0x2000000
75
	// [\t]
76
	TabLetter = 0x4000000
77
	// ["]
78
	QuotLetter = 0x8000000
79
	// [`]
80
	BacktickLetter = 0x10000000
81
	// [!]
82
	ExclamaLetter = 0x20000000
83
)
84

85
const (
86
	ALPHA = UpperLetter | LowerLetter
87
)
88

89
var table = []uint32{
90
	0,                        //   [0]
91
	0,                        //   [1]
92
	0,                        //   [2]
93
	0,                        //   [3]
94
	0,                        //   [4]
95
	0,                        //   [5]
96
	0,                        //   [6]
97
	0,                        //   [7]
98
	0,                        //   [8]
99
	TabLetter,                //   [9]
100
	Newline,                  //   [10]
101
	0,                        //   [11]
102
	0,                        //   [12]
103
	Newline,                  //   [13]
104
	0,                        //   [14]
105
	0,                        //   [15]
106
	0,                        //   [16]
107
	0,                        //   [17]
108
	0,                        //   [18]
109
	0,                        //   [19]
110
	0,                        //   [20]
111
	0,                        //   [21]
112
	0,                        //   [22]
113
	0,                        //   [23]
114
	0,                        //   [24]
115
	0,                        //   [25]
116
	0,                        //   [26]
117
	0,                        //   [27]
118
	0,                        //   [28]
119
	0,                        //   [29]
120
	0,                        //   [30]
121
	0,                        //   [31]
122
	SpaceLetter,              //   [32]
123
	ExclamaLetter,            // ! [33]
124
	QuotLetter,               // " [34]
125
	0,                        // # [35]
126
	0,                        // $ [36]
127
	PercentLetter,            // % [37]
128
	AndLetter,                // & [38]
129
	0,                        // ' [39]
130
	0,                        // ( [40]
131
	0,                        // ) [41]
132
	Asterisk,                 // * [42]
133
	AddLetter,                // + [43]
134
	CommaLetter,              // , [44]
135
	SubLetter,                // - [45]
136
	DotLetter,                // . [46]
137
	SlantLetter,              // / [47]
138
	Digital | HexDigital,     // 0 [48]
139
	Digital | HexDigital,     // 1 [49]
140
	Digital | HexDigital,     // 2 [50]
141
	Digital | HexDigital,     // 3 [51]
142
	Digital | HexDigital,     // 4 [52]
143
	Digital | HexDigital,     // 5 [53]
144
	Digital | HexDigital,     // 6 [54]
145
	Digital | HexDigital,     // 7 [55]
146
	Digital | HexDigital,     // 8 [56]
147
	Digital | HexDigital,     // 9 [57]
148
	ColonLetter,              // : [58]
149
	SemiColonLetter,          // ; [59]
150
	LtLetter,                 // < [60]
151
	EqLetter,                 // = [61]
152
	GtLetter,                 // > [62]
153
	0,                        // ? [63]
154
	0,                        // @ [64]
155
	UpperLetter | HexDigital, // A [65]
156
	UpperLetter | HexDigital, // B [66]
157
	UpperLetter | HexDigital, // C [67]
158
	UpperLetter | HexDigital, // D [68]
159
	UpperLetter | HexDigital, // E [69]
160
	UpperLetter | HexDigital, // F [70]
161
	UpperLetter,              // G [71]
162
	UpperLetter,              // H [72]
163
	UpperLetter,              // I [73]
164
	UpperLetter,              // J [74]
165
	UpperLetter,              // K [75]
166
	UpperLetter,              // L [76]
167
	UpperLetter,              // M [77]
168
	UpperLetter,              // N [78]
169
	UpperLetter,              // O [79]
170
	UpperLetter,              // P [80]
171
	UpperLetter,              // Q [81]
172
	UpperLetter,              // R [82]
173
	UpperLetter,              // S [83]
174
	UpperLetter,              // T [84]
175
	UpperLetter,              // U [85]
176
	UpperLetter,              // V [86]
177
	UpperLetter,              // W [87]
178
	UpperLetter,              // X [88]
179
	UpperLetter,              // Y [89]
180
	UpperLetter,              // Z [90]
181
	0,                        // [ [91]
182
	RSlantLetter,             // \ [92]
183
	0,                        // ] [93]
184
	0,                        // ^ [94]
185
	Underline,                // _ [95]
186
	BacktickLetter,           // ` [96]
187
	LowerLetter | HexDigital, // a [97]
188
	LowerLetter | HexDigital, // b [98]
189
	LowerLetter | HexDigital, // c [99]
190
	LowerLetter | HexDigital, // d [100]
191
	LowerLetter | HexDigital, // e [101]
192
	LowerLetter | HexDigital, // f [102]
193
	LowerLetter,              // g [103]
194
	LowerLetter,              // h [104]
195
	LowerLetter,              // i [105]
196
	LowerLetter,              // j [106]
197
	LowerLetter,              // k [107]
198
	LowerLetter,              // l [108]
199
	LowerLetter,              // m [109]
200
	NLetter | LowerLetter,    // n [110]
201
	LowerLetter,              // o [111]
202
	LowerLetter,              // p [112]
203
	LowerLetter,              // q [113]
204
	RLetter | LowerLetter,    // r [114]
205
	LowerLetter,              // s [115]
206
	TLetter | LowerLetter,    // t [116]
207
	LowerLetter,              // u [117]
208
	LowerLetter,              // v [118]
209
	WLetter | LowerLetter,    // w [119]
210
	LowerLetter,              // x [120]
211
	LowerLetter,              // y [121]
212
	LowerLetter,              // z [122]
213
	0,                        // { [123]
214
	OrLetter,                 // | [124]
215
	0,                        // } [125]
216
	0,                        // ~ [126]
217
	0,                        // del [127]
218
}
219

220
func apiName(service, method, path, host, params string, maxApiLevel int, apiName string) (api string) {
221
	return apiWithParams(service, method, path, host, params, maxApiLevel)
222
}
223

224
func genXlogTags(service string, xlogs []string, respLength int64) []string {
225
	var tags []string
226
	switch service {
227
	case "UP":
228
		if stringsContain(xlogs, []string{"up.pop", "up.transform"}) {
229
			tags = append(tags, "fop")
230
		}
231
		if stringsContain(xlogs, []string{"UP.CB"}) {
232
			tags = append(tags, "callback")
233
		}
234
		if stringsContain(xlogs, []string{"lcy"}) {
235
			tags = append(tags, "lcy")
236
		}
237
	case "IO":
238
		if stringsContain(xlogs, []string{"io.op", "io.rop", "io.pop"}) {
239
			tags = append(tags, "fop")
240
		}
241
		if stringsContain(xlogs, []string{"gS.h"}) {
242
			tags = append(tags, "mirror")
243
		}
244
		if stringsContain(xlogs, []string{"IO.CB"}) {
245
			tags = append(tags, "callback")
246
		}
247
		if stringsContain(xlogs, []string{"AZF"}) && respLength > 1 {
248
			tags = append(tags, "allzero")
249
		}
250
	default:
251
	}
252
	return tags
253
}
254

255
func stringsContain(ss []string, subss []string) bool {
256
	for _, str := range ss {
257
		for _, substr := range subss {
258
			if strings.Contains(str, substr) {
259
				return true
260
			}
261
		}
262
	}
263
	return false
264
}
265

266
func sortAndUniq(ss []string) []string {
267
	length := len(ss)
268
	if length <= 1 {
269
		return ss
270
	}
271
	sort.Strings(ss)
272
	ret := []string{ss[0]}
273
	for i := 1; i < length; i++ {
274
		if ss[i] != ss[i-1] {
275
			ret = append(ret, ss[i])
276
		}
277
	}
278
	return ret
279
}
280

281
func hitXWarns(respXWarns, needXWarns []string) []string {
282
	var hits []string
283
	for _, r := range respXWarns {
284
		for _, n := range needXWarns {
285
			if r == n {
286
				hits = append(hits, r)
287
			}
288
		}
289
	}
290
	return hits
291
}
292

293
func is(typeMask uint32, c rune) bool {
294
	if uint(c) < uint(len(table)) {
295
		return (typeMask & table[c]) != 0
296
	}
297
	return false
298
}
299

300
func isType(typeMask uint32, str string) bool {
301
	if str == "" {
302
		return false
303
	}
304
	for _, c := range str {
305
		if !is(typeMask, c) {
306
			return false
307
		}
308
	}
309
	return true
310
}
311

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.