From 24d7c8fa8eae379a1e19e3df71460411cbc9632b Mon Sep 17 00:00:00 2001 From: jweny Date: Thu, 15 Jul 2021 17:57:24 +0800 Subject: [PATCH] Debug: update cel protobuf pkg name --- pkg/cel/cel.go | 9 ++++---- pkg/cel/proto/http.pb.go | 45 ++++++++++++++++++++-------------------- poc/rule/cel.go | 2 +- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/pkg/cel/cel.go b/pkg/cel/cel.go index 52b3d70..ef47f59 100644 --- a/pkg/cel/cel.go +++ b/pkg/cel/cel.go @@ -371,7 +371,7 @@ type CustomLib struct { func InitCelOptions() CustomLib { custom := CustomLib{} custom.envOptions = []cel.EnvOption{ - cel.Container("utils"), + cel.Container("proto"), // 类型注入 cel.Types( &proto.UrlType{}, @@ -381,8 +381,8 @@ func InitCelOptions() CustomLib { ), // 定义变量变量 cel.Declarations( - decls.NewVar("request", decls.NewObjectType("utils.Request")), - decls.NewVar("response", decls.NewObjectType("utils.Response")), + decls.NewVar("request", decls.NewObjectType("proto.Request")), + decls.NewVar("response", decls.NewObjectType("proto.Response")), ), // 定义 cel.Declarations( @@ -407,7 +407,6 @@ func InitCelOptions() CustomLib { } // 如果有set:追加set变量到 cel options -// 这里得注意下 reverse的顺序问题 map可能是随机的 func (c *CustomLib) AddRuleSetOptions(args []yaml.MapItem) { for _, arg := range args { // 在执行之前是不知道变量的类型的,所以统一声明为字符型 @@ -419,7 +418,7 @@ func (c *CustomLib) AddRuleSetOptions(args []yaml.MapItem) { if strings.HasPrefix(v, "randomInt") { d = decls.NewVar(k, decls.Int) } else if strings.HasPrefix(v, "newReverse") { - d = decls.NewVar(k, decls.NewObjectType("util.Reverse")) + d = decls.NewVar(k, decls.NewObjectType("proto.Reverse")) } else { d = decls.NewVar(k, decls.String) } diff --git a/pkg/cel/proto/http.pb.go b/pkg/cel/proto/http.pb.go index 4631acd..68df1fe 100644 --- a/pkg/cel/proto/http.pb.go +++ b/pkg/cel/proto/http.pb.go @@ -7,12 +7,11 @@ package proto import ( - reflect "reflect" - sync "sync" - proto "github.com/golang/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" ) const ( @@ -377,8 +376,8 @@ func (x *Reverse) GetIsDomainNameServer() bool { var File_http_proto protoreflect.FileDescriptor var file_http_proto_rawDesc = []byte{ - 0x0a, 0x0a, 0x68, 0x74, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x75, 0x74, - 0x69, 0x6c, 0x73, 0x22, 0xa7, 0x01, 0x0a, 0x07, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x12, + 0x0a, 0x0a, 0x68, 0x74, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0xa7, 0x01, 0x0a, 0x07, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, @@ -390,11 +389,11 @@ var file_http_proto_rawDesc = []byte{ 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0xed, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x20, 0x0a, 0x03, 0x75, 0x72, 0x6c, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x75, 0x74, 0x69, 0x6c, 0x73, 0x2e, 0x55, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x35, 0x0a, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x03, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x75, 0x74, 0x69, 0x6c, 0x73, 0x2e, 0x52, 0x65, 0x71, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, @@ -405,11 +404,11 @@ var file_http_proto_rawDesc = []byte{ 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0xa1, 0x02, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x20, 0x0a, 0x03, 0x75, 0x72, - 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x75, 0x74, 0x69, 0x6c, 0x73, 0x2e, + 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x36, 0x0a, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, - 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x75, 0x74, 0x69, 0x6c, 0x73, 0x2e, 0x52, 0x65, + 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x04, 0x20, 0x01, @@ -423,8 +422,8 @@ var file_http_proto_rawDesc = []byte{ 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x9a, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x12, 0x20, 0x0a, - 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x75, 0x74, 0x69, - 0x6c, 0x73, 0x2e, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, + 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x55, 0x72, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x6c, 0x61, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x6c, 0x61, 0x67, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, 0x0e, 0x0a, 0x02, 0x69, @@ -432,7 +431,7 @@ var file_http_proto_rawDesc = []byte{ 0x73, 0x5f, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x12, 0x69, 0x73, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x42, 0x07, - 0x5a, 0x05, 0x75, 0x74, 0x69, 0x6c, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x5a, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -449,19 +448,19 @@ func file_http_proto_rawDescGZIP() []byte { var file_http_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_http_proto_goTypes = []interface{}{ - (*UrlType)(nil), // 0: utils.UrlType - (*Request)(nil), // 1: utils.Request - (*Response)(nil), // 2: utils.Response - (*Reverse)(nil), // 3: utils.Reverse - nil, // 4: utils.Request.HeadersEntry - nil, // 5: utils.Response.HeadersEntry + (*UrlType)(nil), // 0: proto.UrlType + (*Request)(nil), // 1: proto.Request + (*Response)(nil), // 2: proto.Response + (*Reverse)(nil), // 3: proto.Reverse + nil, // 4: proto.Request.HeadersEntry + nil, // 5: proto.Response.HeadersEntry } var file_http_proto_depIdxs = []int32{ - 0, // 0: utils.Request.url:type_name -> utils.UrlType - 4, // 1: utils.Request.headers:type_name -> utils.Request.HeadersEntry - 0, // 2: utils.Response.url:type_name -> utils.UrlType - 5, // 3: utils.Response.headers:type_name -> utils.Response.HeadersEntry - 0, // 4: utils.Reverse.url:type_name -> utils.UrlType + 0, // 0: proto.Request.url:type_name -> proto.UrlType + 4, // 1: proto.Request.headers:type_name -> proto.Request.HeadersEntry + 0, // 2: proto.Response.url:type_name -> proto.UrlType + 5, // 3: proto.Response.headers:type_name -> proto.Response.HeadersEntry + 0, // 4: proto.Reverse.url:type_name -> proto.UrlType 5, // [5:5] is the sub-list for method output_type 5, // [5:5] is the sub-list for method input_type 5, // [5:5] is the sub-list for extension type_name diff --git a/poc/rule/cel.go b/poc/rule/cel.go index ad0b26e..8698daf 100644 --- a/poc/rule/cel.go +++ b/poc/rule/cel.go @@ -52,7 +52,7 @@ func (cc *CelController) InitSet(poc *Poc, newReq *proto.Request) (err error) { } out, err := cel2.Evaluate(cc.Env, value, cc.ParamMap) if err != nil { - continue + return err } switch value := out.Value().(type) { // set value 无论是什么类型都先转成string