@@ -19,12 +19,19 @@ import (
1919 "google.golang.org/protobuf/reflect/protoregistry"
2020 "google.golang.org/protobuf/types/dynamicpb"
2121
22+ "github.com/open-policy-agent/opa/v1/ast"
2223 "github.com/open-policy-agent/opa/v1/logging"
2324 "github.com/open-policy-agent/opa/v1/util"
2425)
2526
26- var v2Info = map [string ]string {"ext_authz" : "v2" , "encoding" : "encoding/json" }
27- var v3Info = map [string ]string {"ext_authz" : "v3" , "encoding" : "protojson" }
27+ var v2Info = ast .NewObject (
28+ keyValue ("ext_authz" , "v2" ),
29+ keyValue ("encoding" , "encoding/json" ),
30+ )
31+ var v3Info = ast .NewObject (
32+ keyValue ("ext_authz" , "v3" ),
33+ keyValue ("encoding" , "protojson" ),
34+ )
2835
2936// RequestToInput - Converts a CheckRequest in either protobuf 2 or 3 to an input map
3037func RequestToInput (req interface {}, logger logging.Logger , protoSet * protoregistry.Files , skipRequestBodyParse bool ) (map [string ]interface {}, error ) {
@@ -33,7 +40,8 @@ func RequestToInput(req interface{}, logger logging.Logger, protoSet *protoregis
3340
3441 var rawBody []byte
3542 var path , body string
36- var headers , version map [string ]string
43+ var headers map [string ]string
44+ var version ast.Value
3745
3846 // NOTE: The path/body/headers blocks look silly, but they allow us to retrieve
3947 // the parts of the incoming request we care about, without having to convert
@@ -84,32 +92,24 @@ func RequestToInput(req interface{}, logger logging.Logger, protoSet *protoregis
8492 return input , nil
8593}
8694
87- func getParsedPathAndQuery (path string ) ([]interface {} , map [string ]interface {} , error ) {
95+ func getParsedPathAndQuery (path string ) ([]string , map [string ]any , error ) {
8896 parsedURL , err := url .Parse (path )
8997 if err != nil {
9098 return nil , nil , err
9199 }
92100
93101 parsedPath := strings .Split (strings .TrimLeft (parsedURL .Path , "/" ), "/" )
94- parsedPathInterface := make ([]interface {}, len (parsedPath ))
95- for i , v := range parsedPath {
96- parsedPathInterface [i ] = v
97- }
98102
99103 query := parsedURL .Query ()
100- parsedQueryInterface := make (map [string ]interface {} , len (query ))
104+ parsedQueryInterface := make (map [string ]any , len (query ))
101105 for paramKey , paramValues := range query {
102- queryValues := make ([]interface {}, len (paramValues ))
103- for i , v := range paramValues {
104- queryValues [i ] = v
105- }
106- parsedQueryInterface [paramKey ] = queryValues
106+ parsedQueryInterface [paramKey ] = paramValues
107107 }
108108
109- return parsedPathInterface , parsedQueryInterface , nil
109+ return parsedPath , parsedQueryInterface , nil
110110}
111111
112- func getParsedBody (logger logging.Logger , headers map [string ]string , body string , rawBody []byte , parsedPath []interface {} , protoSet * protoregistry.Files ) (interface {}, bool , error ) {
112+ func getParsedBody (logger logging.Logger , headers map [string ]string , body string , rawBody []byte , parsedPath []string , protoSet * protoregistry.Files ) (interface {}, bool , error ) {
113113 var data interface {}
114114
115115 if val , ok := headers ["content-type" ]; ok {
@@ -269,7 +269,7 @@ func getParsedBody(logger logging.Logger, headers map[string]string, body string
269269 return data , false , nil
270270}
271271
272- func getGRPCBody (logger logging.Logger , in []byte , parsedPath []interface {} , data interface {}, files * protoregistry.Files ) (found , truncated bool , _ error ) {
272+ func getGRPCBody (logger logging.Logger , in []byte , parsedPath []string , data interface {}, files * protoregistry.Files ) (found , truncated bool , _ error ) {
273273
274274 // the first 5 bytes are part of gRPC framing. We need to remove them to be able to parse
275275 // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
@@ -294,12 +294,12 @@ func getGRPCBody(logger logging.Logger, in []byte, parsedPath []interface{}, dat
294294 in = in [5 : size + 5 ]
295295
296296 // Note: we've already checked that len(path)>=2
297- svc , err := findService (parsedPath [0 ].( string ) , files )
297+ svc , err := findService (parsedPath [0 ], files )
298298 if err != nil {
299299 logger .WithFields (map [string ]interface {}{"err" : err }).Debug ("could not find service" )
300300 return false , false , nil
301301 }
302- msgDesc , err := findMessageInputDesc (parsedPath [1 ].( string ) , svc )
302+ msgDesc , err := findMessageInputDesc (parsedPath [1 ], svc )
303303 if err != nil {
304304 logger .WithFields (map [string ]interface {}{"err" : err }).Debug ("could not find message" )
305305 return false , false , nil
0 commit comments