/
fn_sql_cover.go
73 lines (62 loc) · 1.98 KB
/
fn_sql_cover.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package funcs
import (
"fmt"
"github.com/DataDog/datadog-agent/pkg/obfuscate"
"github.com/GuanceCloud/cliutils/pipeline/ptinput"
"github.com/GuanceCloud/platypus/pkg/ast"
"github.com/GuanceCloud/platypus/pkg/engine/runtime"
"github.com/GuanceCloud/platypus/pkg/errchain"
)
func SQLCoverChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
if len(funcExpr.Param) != 1 {
return runtime.NewRunError(ctx, fmt.Sprintf(
"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
}
if _, err := getKeyName(funcExpr.Param[0]); err != nil {
return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
}
return nil
}
func SQLCover(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
o := obfuscate.NewObfuscator(obfuscate.Config{})
if len(funcExpr.Param) != 1 {
return runtime.NewRunError(ctx, fmt.Sprintf(
"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
}
key, err := getKeyName(funcExpr.Param[0])
if err != nil {
return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
}
cont, err := ctx.GetKeyConv2Str(key)
if err != nil {
l.Debugf("key `%v' not exist, ignored", key)
return nil //nolint:nilerr
}
Type := "sql"
v, err := obfuscatedResource(o, Type, cont)
if err != nil {
l.Debug(err)
return nil
}
if err := addKey2PtWithVal(ctx.InData(), key, v, ast.String,
ptinput.KindPtDefault); err != nil {
l.Debug(err)
return nil
}
return nil
}
func obfuscatedResource(o *obfuscate.Obfuscator, typ, resource string) (string, error) {
if typ != "sql" {
return resource, nil
}
oq, err := o.ObfuscateSQLString(resource)
if err != nil {
err = fmt.Errorf("error obfuscating stats group resource %q: %w", resource, err)
return "", err
}
return oq.Query, nil
}