github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_sql_cover.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/DataDog/datadog-agent/pkg/obfuscate"
    12  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    13  	"github.com/GuanceCloud/platypus/pkg/ast"
    14  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    15  	"github.com/GuanceCloud/platypus/pkg/errchain"
    16  )
    17  
    18  func SQLCoverChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    19  	if len(funcExpr.Param) != 1 {
    20  		return runtime.NewRunError(ctx, fmt.Sprintf(
    21  			"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
    22  	}
    23  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
    24  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    25  	}
    26  	return nil
    27  }
    28  
    29  func SQLCover(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    30  	o := obfuscate.NewObfuscator(obfuscate.Config{})
    31  	if len(funcExpr.Param) != 1 {
    32  		return runtime.NewRunError(ctx, fmt.Sprintf(
    33  			"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
    34  	}
    35  
    36  	key, err := getKeyName(funcExpr.Param[0])
    37  	if err != nil {
    38  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    39  	}
    40  
    41  	cont, err := ctx.GetKeyConv2Str(key)
    42  	if err != nil {
    43  		l.Debugf("key `%v' not exist, ignored", key)
    44  		return nil //nolint:nilerr
    45  	}
    46  
    47  	Type := "sql"
    48  
    49  	v, err := obfuscatedResource(o, Type, cont)
    50  	if err != nil {
    51  		l.Debug(err)
    52  		return nil
    53  	}
    54  	_ = addKey2PtWithVal(ctx.InData(), key, v, ast.String, ptinput.KindPtDefault)
    55  
    56  	return nil
    57  }
    58  
    59  func obfuscatedResource(o *obfuscate.Obfuscator, typ, resource string) (string, error) {
    60  	if typ != "sql" {
    61  		return resource, nil
    62  	}
    63  	oq, err := o.ObfuscateSQLString(resource)
    64  	if err != nil {
    65  		err = fmt.Errorf("error obfuscating stats group resource %q: %w", resource, err)
    66  		return "", err
    67  	}
    68  	return oq.Query, nil
    69  }