-
Notifications
You must be signed in to change notification settings - Fork 123
/
parser.go
1161 lines (1006 loc) · 31.3 KB
/
parser.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package parser
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"regexp"
"strconv"
"strings"
"unsafe"
"github.com/bufbuild/protovalidate-go"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/lexer"
"github.com/goccy/go-yaml/parser"
"github.com/goccy/go-yaml/printer"
"github.com/goccy/go-yaml/token"
"github.com/stoewer/go-strcase"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
sourcev1 "github.com/cerbos/cerbos/api/genpb/cerbos/source/v1"
"github.com/cerbos/cerbos/internal/util"
)
const (
base10 = 10
bitSize32 = 32
bitSize64 = 64
)
var ErrNotFound = errors.New("not found")
type PanicError struct {
Cause any
Context []byte
}
func (pe PanicError) Error() string {
return fmt.Sprintf("panic: %v", pe.Cause)
}
var protoErrPrefix = regexp.MustCompile(`proto:(\x{00a0}|\x{0020})+\(line\s+\d+:\d+\):\s*`)
// Find a single document from the multi-document stream.
// TODO(cell): Optimize!
// For our use case, this could be optimized by storing the offset of each document and directly seeking to that offset.
// However, there are a couple of problems with that:
// 1. The offsets reported by the parser are not always reliable (I am yet to figure out why)
// 2. If YAML anchors have been used, we need to resolve those first by reading through the entire file anyway
// However, this is a relatively niche case and we can handle that case lazily (seek first, read, and resolve anchors only if they exist in the doc)
//
// In the interest of time, I am leaving those optimizations for later.
func Find[T proto.Message](r io.Reader, match func(T) bool, out T, opts ...UnmarshalOpt) (SourceCtx, error) {
contents, err := io.ReadAll(r)
if err != nil {
return SourceCtx{}, fmt.Errorf("failed to read contents: %w", err)
}
f, err := parse(contents, false)
if err != nil {
return SourceCtx{}, err
}
if len(f.Docs) == 0 {
return SourceCtx{}, ErrNotFound
}
u := &unmarshaler[T]{unmarshalOpts: unmarshalOpts{}}
for _, o := range opts {
o(&u.unmarshalOpts)
}
for _, doc := range f.Docs {
bodyNode, ok := doc.Body.(ast.MapNode)
if !ok {
// Ignore documents not structured as policies
continue
}
proto.Reset(out)
refOut := out.ProtoReflect()
uctx := newUnmarshalCtx(doc)
if err := u.unmarshalMapping(uctx, bodyNode, refOut); err != nil {
continue
}
if !match(out) {
continue
}
return uctx.toSourceCtx(), u.validate(uctx, out)
}
return SourceCtx{}, ErrNotFound
}
func Unmarshal[T proto.Message](r io.Reader, factory func() T, opts ...UnmarshalOpt) ([]T, []SourceCtx, error) {
contents, err := io.ReadAll(r)
if err != nil {
return nil, nil, fmt.Errorf("failed to read contents: %w", err)
}
return UnmarshalBytes(contents, factory, opts...)
}
func UnmarshalBytes[T proto.Message](contents []byte, factory func() T, opts ...UnmarshalOpt) (_ []T, _ []SourceCtx, outErr error) {
f, err := parse(contents, true)
if err != nil {
return nil, nil, err
}
if len(f.Docs) == 0 {
if len(bytes.TrimSpace(contents)) > 0 {
// Special case for unterminated strings. See test case 20.
return nil, nil, NewUnmarshalError(&sourcev1.Error{
Kind: sourcev1.Error_KIND_PARSE_ERROR,
Message: "invalid document: contents are not valid YAML or JSON",
Position: &sourcev1.Position{Line: 1, Column: 1, Path: "$"},
})
}
return nil, nil, nil
}
u := &unmarshaler[T]{unmarshalOpts: unmarshalOpts{}}
for _, o := range opts {
o(&u.unmarshalOpts)
}
outMsg := make([]T, 0, len(f.Docs))
outSrc := make([]SourceCtx, 0, len(f.Docs))
invalidDocSeen := false
for _, doc := range f.Docs {
msg := factory()
uctx := newUnmarshalCtx(doc)
bodyNode, ok := doc.Body.(ast.MapNode)
if !ok {
// ignore commented out documents
if doc.Body.Type() == ast.CommentType {
continue
}
// If given an invalid file with multiple lines of text, the parser generates a "doc" for each line.
// Ignore a consecutive run of such docs.
if invalidDocSeen {
continue
}
outErr = errors.Join(outErr, uctx.perrorf(doc.Body, "invalid document: contents are not valid YAML or JSON"))
outMsg = append(outMsg, msg)
outSrc = append(outSrc, uctx.toSourceCtx())
invalidDocSeen = true
continue
}
invalidDocSeen = false
if err := u.unmarshalMapping(uctx, bodyNode, msg.ProtoReflect()); err != nil {
outErr = errors.Join(outErr, err)
} else if err := u.validate(uctx, msg); err != nil {
outErr = errors.Join(outErr, err)
}
outMsg = append(outMsg, msg)
outSrc = append(outSrc, uctx.toSourceCtx())
}
return outMsg, outSrc, outErr
}
func parse(contents []byte, detectProblems bool) (_ *ast.File, outErr error) {
defer func() {
if r := recover(); r != nil {
outErr = PanicError{Cause: r, Context: contents}
}
}()
t := lexer.Tokenize(unsafe.String(unsafe.SliceData(contents), len(contents)))
if detectProblems && !util.IsJSON(contents) {
if errs := detectStringStartingWithQuote(t); len(errs) > 0 {
for _, err := range errs {
outErr = errors.Join(outErr, NewUnmarshalError(err))
}
return nil, outErr
}
}
return parser.Parse(t, parser.ParseComments)
}
func detectStringStartingWithQuote(tokens token.Tokens) (outErrs []*sourcev1.Error) {
var errPrinter printer.Printer
i := 0
flowBlock := 0
for {
if i >= len(tokens) {
break
}
tok := tokens[i]
// Check whether we are inside a flow block (e.g. foo: {"x": "y"})
if tok.Indicator == token.FlowCollectionIndicator {
if tok.Type == token.MappingStartType || tok.Type == token.SequenceStartType {
flowBlock++
} else {
flowBlock--
}
}
if flowBlock > 0 {
i++
continue
}
if tok.Prev != nil && tok.Prev.Type != token.MappingValueType {
i++
continue
}
if !(tok.Type == token.DoubleQuoteType || tok.Type == token.SingleQuoteType) {
i++
continue
}
if tok.Next == nil || tok.Next.Type == token.MappingValueType || tok.Next.Position.Line != tok.Position.Line {
i++
continue
}
i++
invalid := false
for t := tok.Next; t != nil && t.Position.Line == tok.Position.Line; t = t.Next {
switch t.Type {
case token.CollectEntryType, token.CommentType, token.AnchorType, token.MappingEndType:
default:
invalid = true
}
i++
}
if invalid {
outErrs = append(outErrs, &sourcev1.Error{
Kind: sourcev1.Error_KIND_PARSE_ERROR,
Message: "invalid YAML string: use a literal or folded block for strings containing quotes",
Position: &sourcev1.Position{
Line: uint32(tok.Position.Line),
Column: uint32(tok.Position.Column),
},
Context: errPrinter.PrintErrorToken(tok, false),
})
}
}
return outErrs
}
type unmarshalOpts struct {
validator *protovalidate.Validator
ignoreUnknownFields bool
}
type UnmarshalOpt func(*unmarshalOpts)
// WithIgnoreUnknownFields ignores unknown fields not defined in the protobuf schema.
func WithIgnoreUnknownFields() UnmarshalOpt {
return func(uo *unmarshalOpts) {
uo.ignoreUnknownFields = true
}
}
// WithValidate validates the unmarshaled message using protovalidate.
func WithValidator(validator *protovalidate.Validator) UnmarshalOpt {
return func(uo *unmarshalOpts) {
uo.validator = validator
}
}
type unmarshaler[T proto.Message] struct {
anchors map[string]ast.Node
unmarshalOpts
}
func (u *unmarshaler[T]) unmarshalMapping(uctx *unmarshalCtx, v ast.MapNode, out protoreflect.Message) error {
fields := out.Descriptor().Fields()
seen := make(map[protowire.Number]string, fields.Len())
seenOneOfs := make(map[int]string)
items := v.MapRange()
// Find all the merge keys first and populate the message because they need to overwritten by new values
// Basically, ensuring that "bar" is set to "baz" in the following case regardless of what value "bar" has
// in the "anchor" map.
// x:
// bar: baz
// <<: *anchor
for items.Next() {
if items.Key().Type() == ast.MergeKeyType {
mn, err := u.resolveMerge(uctx, items.Value())
if err != nil {
return err
}
if err := u.unmarshalMapping(uctx, mn, out); err != nil {
return err
}
}
}
items = v.MapRange()
for items.Next() {
kn := items.Key()
var keyValue string
switch kt := kn.(type) {
case *ast.StringNode:
keyValue = kt.Value
case *ast.MergeKeyNode:
// already handled above
continue
default:
return uctx.perrorf(kn, "unexpected key type %s", kn.Type())
}
field := fields.ByJSONName(keyValue)
if field == nil {
field = fields.ByTextName(keyValue)
}
if field == nil {
if u.ignoreUnknownFields {
continue
}
return uctx.perrorf(kn, "unknown field %q", keyValue)
}
if prev, ok := seen[field.Number()]; ok {
return uctx.perrorf(kn, "duplicate field definition: previous definition at %s", prev)
}
seen[field.Number()] = pos(kn)
switch {
case field.IsList():
list := out.Mutable(field).List()
if err := u.unmarshalList(uctx.forField(field, kn), items.Value(), field, list); err != nil {
return err
}
case field.IsMap():
mmap := out.Mutable(field).Map()
nn, err := u.resolveNode(uctx, items.Value())
if err != nil {
return err
}
mn, ok := nn.(ast.MapNode)
if !ok {
return uctx.perrorf(items.Value(), "expected map got %s", nn.Type())
}
if err := u.unmarshalMap(uctx.forField(field, kn), mn, field, mmap, true); err != nil {
return err
}
default:
if oof := field.ContainingOneof(); oof != nil {
idx := oof.Index()
if prev, ok := seenOneOfs[idx]; ok {
return uctx.perrorf(kn, "invalid value: oneof field is already set at %s", prev)
}
seenOneOfs[idx] = pos(kn)
}
if err := u.unmarshalSingular(uctx.forField(field, kn), items.Value(), field, out); err != nil {
return err
}
}
}
return nil
}
func (u *unmarshaler[T]) resolveMerge(uctx *unmarshalCtx, n ast.Node) (ast.MapNode, error) {
if an, ok := n.(*ast.AliasNode); ok {
anchorName := an.Value.GetToken().Value
aliased, ok := u.anchors[anchorName]
if !ok {
return nil, uctx.perrorf(n, "unknown anchor %q", anchorName)
}
resolved, err := u.resolveAlias(uctx, aliased)
if err != nil {
return nil, err
}
mn, ok := resolved.(ast.MapNode)
if !ok {
return nil, uctx.perrorf(n, "expected map alias got %s", resolved.Type())
}
return mn, nil
}
return nil, uctx.perrorf(n, "not an alias")
}
func (u *unmarshaler[T]) resolveNode(uctx *unmarshalCtx, n ast.Node) (ast.Node, error) {
switch t := n.(type) {
case *ast.AnchorNode:
anchorName := t.Name.GetToken().Value
if _, ok := u.anchors[anchorName]; ok {
return nil, uctx.perrorf(n, "duplicate anchor definition %q", t.String())
}
if u.anchors == nil {
u.anchors = make(map[string]ast.Node)
}
u.anchors[anchorName] = t.Value
return u.resolveNode(uctx, t.Value)
case *ast.AliasNode:
anchorName := t.Value.GetToken().Value
an, ok := u.anchors[anchorName]
if !ok {
return nil, uctx.perrorf(n, "unknown anchor %q", anchorName)
}
return u.resolveAlias(uctx, an)
case *ast.TagNode:
return u.resolveAlias(uctx, t.Value)
default:
return n, nil
}
}
// adapted from https://github.com/goccy/go-yaml/blob/31fe1baacec127337140701face2e64a356075fd/decode.go#L355
func (u *unmarshaler[T]) resolveAlias(uctx *unmarshalCtx, n ast.Node) (ast.Node, error) {
switch nn := n.(type) {
case *ast.AnchorNode:
return u.resolveAlias(uctx, nn.Value)
case *ast.MappingNode:
for idx, v := range nn.Values {
value, err := u.resolveAlias(uctx, v)
if err != nil {
return nil, err
}
vv, ok := value.(*ast.MappingValueNode)
if !ok {
return nil, uctx.perrorf(vv, "unexpected node type %s", vv.Type())
}
nn.Values[idx] = vv
}
case *ast.TagNode:
value, err := u.resolveAlias(uctx, nn.Value)
if err != nil {
return nil, err
}
nn.Value = value
case *ast.MappingKeyNode:
value, err := u.resolveAlias(uctx, nn.Value)
if err != nil {
return nil, err
}
nn.Value = value
case *ast.MappingValueNode:
//nolint:nestif
if nn.Key.Type() == ast.MergeKeyType && nn.Value.Type() == ast.AliasType {
value, err := u.resolveAlias(uctx, nn.Value)
if err != nil {
return nil, err
}
keyColumn := nn.Key.GetToken().Position.Column
requiredColumn := keyColumn + 2 //nolint:mnd
value.AddColumn(requiredColumn)
nn.Value = value
} else {
key, err := u.resolveAlias(uctx, nn.Key)
if err != nil {
return nil, err
}
k, ok := key.(ast.MapKeyNode)
if !ok {
return nil, uctx.perrorf(key, "unexpected node type %s", key.Type())
}
nn.Key = k
value, err := u.resolveAlias(uctx, nn.Value)
if err != nil {
return nil, err
}
nn.Value = value
}
case *ast.SequenceNode:
for idx, v := range nn.Values {
value, err := u.resolveAlias(uctx, v)
if err != nil {
return nil, err
}
nn.Values[idx] = value
}
case *ast.AliasNode:
aliasName := nn.Value.GetToken().Value
node, ok := u.anchors[aliasName]
if !ok {
return nil, uctx.perrorf(n, "unknown alias %s", aliasName)
}
return u.resolveAlias(uctx, node)
}
return n, nil
}
func (u *unmarshaler[T]) unmarshalList(uctx *unmarshalCtx, n ast.Node, fd protoreflect.FieldDescriptor, list protoreflect.List) error {
nn, err := u.resolveNode(uctx, n)
if err != nil {
return err
}
sn, ok := nn.(*ast.SequenceNode)
if !ok {
return uctx.perrorf(n, "expected sequence got %s", nn.Type())
}
switch fd.Kind() {
case protoreflect.MessageKind, protoreflect.GroupKind:
for i, item := range sn.Values {
val := list.NewElement()
if err := u.unmarshalMessage(uctx.forListItem(i, item), item, val.Message()); err != nil {
return err
}
list.Append(val)
}
default:
for i, item := range sn.Values {
val, err := u.unmarshalScalar(uctx.forListItem(i, item), item, fd)
if err != nil {
return err
}
list.Append(val)
}
}
return nil
}
func (u *unmarshaler[T]) unmarshalMap(uctx *unmarshalCtx, n ast.MapNode, fd protoreflect.FieldDescriptor, mmap protoreflect.Map, overwriteKeys bool) error {
var valueFn func(*unmarshalCtx, ast.Node) (protoreflect.Value, error)
switch fd.MapValue().Kind() {
case protoreflect.MessageKind, protoreflect.GroupKind:
valueFn = func(uctx *unmarshalCtx, n ast.Node) (protoreflect.Value, error) {
val := mmap.NewValue()
if err := u.unmarshalMessage(uctx, n, val.Message()); err != nil {
return protoreflect.Value{}, err
}
return val, nil
}
default:
valueFn = func(uctx *unmarshalCtx, n ast.Node) (protoreflect.Value, error) {
return u.unmarshalScalar(uctx, n, fd.MapValue())
}
}
items := n.MapRange()
for items.Next() {
key := items.Key()
if key.Type() == ast.MergeKeyType {
kn, err := u.resolveMerge(uctx, items.Value())
if err != nil {
return err
}
if err := u.unmarshalMap(uctx, kn, fd, mmap, false); err != nil {
return err
}
continue
}
keyVal, err := u.unmarshalMapKey(uctx, key, fd.MapKey())
if err != nil {
return err
}
if !overwriteKeys && mmap.Has(keyVal) {
continue
}
val, err := valueFn(uctx.forMapItem(keyVal.String(), items.Value()), items.Value())
if err != nil {
return err
}
mmap.Set(keyVal, val)
}
return nil
}
func (u *unmarshaler[T]) unmarshalSingular(uctx *unmarshalCtx, n ast.Node, fd protoreflect.FieldDescriptor, out protoreflect.Message) error {
if k := fd.Kind(); k == protoreflect.MessageKind || k == protoreflect.GroupKind {
value := out.NewField(fd)
if err := u.unmarshalMessage(uctx, n, value.Message()); err != nil {
return err
}
out.Set(fd, value)
return nil
}
value, err := u.unmarshalScalar(uctx, n, fd)
if err != nil {
return err
}
out.Set(fd, value)
return nil
}
func (u *unmarshaler[T]) unmarshalScalar(uctx *unmarshalCtx, n ast.Node, fd protoreflect.FieldDescriptor) (protoreflect.Value, error) {
nn, err := u.resolveNode(uctx, n)
if err != nil {
return protoreflect.Value{}, err
}
switch fd.Kind() {
case protoreflect.BoolKind:
return u.unmarshalBool(uctx, nn)
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
return u.unmarshalInt(uctx, nn, bitSize32)
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return u.unmarshalInt(uctx, nn, bitSize64)
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
return u.unmarshalUint(uctx, nn, bitSize32)
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return u.unmarshalUint(uctx, nn, bitSize64)
case protoreflect.EnumKind:
return u.unmarshalEnum(uctx, nn, fd)
case protoreflect.FloatKind:
return u.unmarshalFloat(uctx, nn, bitSize32)
case protoreflect.DoubleKind:
return u.unmarshalFloat(uctx, nn, bitSize64)
case protoreflect.StringKind:
return u.unmarshalString(uctx, nn)
case protoreflect.BytesKind:
return u.unmarshalBytes(uctx, nn)
default:
return protoreflect.Value{}, uctx.perrorf(n, "unknown scalar type")
}
}
func (u *unmarshaler[T]) unmarshalBool(uctx *unmarshalCtx, n ast.Node) (protoreflect.Value, error) {
bn, ok := n.(*ast.BoolNode)
if !ok {
return protoreflect.Value{}, uctx.perrorf(n, "expected boolean value got %s", n.Type())
}
return protoreflect.ValueOfBool(bn.Value), nil
}
func (u *unmarshaler[T]) unmarshalEnum(uctx *unmarshalCtx, n ast.Node, fd protoreflect.FieldDescriptor) (protoreflect.Value, error) {
switch t := n.(type) {
case *ast.StringNode:
if ev := fd.Enum().Values().ByName(protoreflect.Name(t.Value)); ev != nil {
return protoreflect.ValueOfEnum(ev.Number()), nil
}
return protoreflect.Value{}, uctx.perrorf(n, "invalid enum value %q", t.Value)
case *ast.IntegerNode:
switch tv := t.Value.(type) {
case uint64:
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(tv)), nil
case int64:
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(tv)), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "invalid enum value %q", t.Value)
}
case *ast.NullNode:
return protoreflect.ValueOfEnum(0), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "invalid enum value")
}
}
func (u *unmarshaler[T]) unmarshalString(uctx *unmarshalCtx, n ast.Node) (protoreflect.Value, error) {
switch t := n.(type) {
case *ast.StringNode:
return protoreflect.ValueOfString(t.Value), nil
case *ast.LiteralNode:
return protoreflect.ValueOfString(t.Value.Value), nil
case *ast.NullNode:
return protoreflect.ValueOf(""), nil
case *ast.IntegerNode:
switch tv := t.Value.(type) {
case uint64:
return protoreflect.ValueOfString(strconv.FormatUint(tv, 10)), nil
case int64:
return protoreflect.ValueOfString(strconv.FormatInt(tv, 10)), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "unexpected integer value %q", t.Value)
}
case *ast.BoolNode:
return protoreflect.ValueOf(t.String()), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "expected string value got %s", n.Type())
}
}
//nolint:dupl
func (u *unmarshaler[T]) unmarshalInt(uctx *unmarshalCtx, n ast.Node, bitSize int) (protoreflect.Value, error) {
switch t := n.(type) {
case *ast.IntegerNode:
switch tv := t.Value.(type) {
case uint64:
if bitSize == bitSize32 {
return protoreflect.ValueOfInt32(int32(tv)), nil
}
return protoreflect.ValueOfInt64(int64(tv)), nil
case int64:
if bitSize == bitSize32 {
return protoreflect.ValueOfInt32(int32(tv)), nil
}
return protoreflect.ValueOfInt64(tv), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "invalid integer value %q", t.Value)
}
case *ast.StringNode:
s := strings.TrimSpace(t.Value)
v, err := strconv.ParseInt(s, base10, bitSize)
if err != nil {
return protoreflect.Value{}, uctx.perrorf(n, "invalid integer value %q: %v", v, err)
}
if bitSize == bitSize32 {
return protoreflect.ValueOfInt32(int32(v)), nil
}
return protoreflect.ValueOfInt64(v), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "expected integer value got %s", n.Type())
}
}
//nolint:dupl
func (u *unmarshaler[T]) unmarshalUint(uctx *unmarshalCtx, n ast.Node, bitSize int) (protoreflect.Value, error) {
switch t := n.(type) {
case *ast.IntegerNode:
switch tv := t.Value.(type) {
case uint64:
if bitSize == bitSize32 {
return protoreflect.ValueOfUint32(uint32(tv)), nil
}
return protoreflect.ValueOfUint64(tv), nil
case int64:
if bitSize == bitSize32 {
return protoreflect.ValueOfUint32(uint32(tv)), nil
}
return protoreflect.ValueOfUint64(uint64(tv)), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "invalid integer value %q", t.Value)
}
case *ast.StringNode:
s := strings.TrimSpace(t.Value)
v, err := strconv.ParseUint(s, base10, bitSize)
if err != nil {
return protoreflect.Value{}, uctx.perrorf(n, "invalid integer value %q: %v", v, err)
}
if bitSize == bitSize32 {
return protoreflect.ValueOfUint32(uint32(v)), nil
}
return protoreflect.ValueOfUint64(v), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "expected integer value got %s", n.Type())
}
}
func (u *unmarshaler[T]) unmarshalFloat(uctx *unmarshalCtx, n ast.Node, bitSize int) (protoreflect.Value, error) {
switch t := n.(type) {
case *ast.IntegerNode:
switch tv := t.Value.(type) {
case uint64:
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(tv)), nil
}
return protoreflect.ValueOfFloat64(float64(tv)), nil
case int64:
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(tv)), nil
}
return protoreflect.ValueOfFloat64(float64(tv)), nil
default:
return protoreflect.Value{}, uctx.perrorf(n, "invalid float value %q", t.Value)
}
case *ast.FloatNode:
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(t.Value)), nil
}
return protoreflect.ValueOfFloat64(t.Value), nil
case *ast.StringNode:
switch t.Value {
case "NaN":
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(math.NaN())), nil
}
return protoreflect.ValueOfFloat64(math.NaN()), nil
case "Infinity":
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(math.Inf(+1))), nil
}
return protoreflect.ValueOfFloat64(math.Inf(+1)), nil
case "-Infinity":
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(math.Inf(-1))), nil
}
return protoreflect.ValueOfFloat64(math.Inf(-1)), nil
default:
s := strings.TrimSpace(t.Value)
v, err := strconv.ParseFloat(s, bitSize)
if err != nil {
return protoreflect.Value{}, uctx.perrorf(n, "invalid float value %q: %v", s, err)
}
if bitSize == bitSize32 {
return protoreflect.ValueOfFloat32(float32(v)), nil
}
return protoreflect.ValueOfFloat64(v), nil
}
default:
return protoreflect.Value{}, uctx.perrorf(n, "expected float value got %s", n.Type())
}
}
func (u *unmarshaler[T]) unmarshalBytes(uctx *unmarshalCtx, n ast.Node) (protoreflect.Value, error) {
var s string
switch t := n.(type) {
case *ast.StringNode:
s = t.Value
case *ast.LiteralNode:
s = t.Value.Value
default:
return protoreflect.Value{}, uctx.perrorf(n, "expected string value got %s", n.Type())
}
enc := base64.StdEncoding
if strings.ContainsAny(s, "-_") {
enc = base64.URLEncoding
}
if len(s)%4 != 0 {
enc = enc.WithPadding(base64.NoPadding)
}
b, err := enc.DecodeString(s)
if err != nil {
return protoreflect.Value{}, uctx.perrorf(n, "failed to decode bytes: %v", err)
}
return protoreflect.ValueOfBytes(b), nil
}
func (u *unmarshaler[T]) unmarshalMessage(uctx *unmarshalCtx, n ast.Node, out protoreflect.Message) error {
nn, err := u.resolveNode(uctx, n)
if err != nil {
return err
}
if out.Descriptor().FullName().Parent() == "google.protobuf" {
return u.unmarshalWKT(uctx, n, out)
}
mn, ok := nn.(ast.MapNode)
if !ok {
return uctx.perrorf(n, "expected object got %s", nn.Type())
}
return u.unmarshalMapping(uctx, mn, out)
}
func (u *unmarshaler[T]) unmarshalWKT(uctx *unmarshalCtx, n ast.Node, out protoreflect.Message) error {
// Google's well-known type handling is hidden inside an internal package and can't be used directly.
// The code is complicated and copying it here is probably not a good idea.
// Cerbos policies don't use any WKTs. Only the test suite definitions and fixtures use them so
// resorting to this slightly inefficient hack is OK for now.
nodeStr := n.String()
jsonBytes, err := yaml.YAMLToJSON(unsafe.Slice(unsafe.StringData(nodeStr), len(nodeStr)))
if err != nil {
return uctx.perrorf(n, "failed to convert well-known type: %v", err)
}
if err := protojson.Unmarshal(jsonBytes, out.Interface()); err != nil {
errStr := protoErrPrefix.ReplaceAllString(err.Error(), "")
return uctx.perrorf(n, "failed to parse value: %s", errStr)
}
return nil
}
func (u *unmarshaler[T]) unmarshalMapKey(uctx *unmarshalCtx, n ast.Node, fd protoreflect.FieldDescriptor) (protoreflect.MapKey, error) {
sn, ok := n.(*ast.StringNode)
if !ok {
return protoreflect.MapKey{}, uctx.perrorf(n, "expected string got %s", n.Type())
}
switch fd.Kind() {
case protoreflect.StringKind:
return protoreflect.ValueOfString(sn.Value).MapKey(), nil
case protoreflect.BoolKind:
switch sn.Value {
case "true":
return protoreflect.ValueOfBool(true).MapKey(), nil
case "false":
return protoreflect.ValueOfBool(false).MapKey(), nil
default:
return protoreflect.MapKey{}, uctx.perrorf(n, "invalid boolean value %q", sn.Value)
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
v, err := strconv.ParseInt(sn.Value, 10, 32)
if err != nil {
return protoreflect.MapKey{}, uctx.perrorf(n, "invalid integer value %q: %v", sn.Value, err)
}
return protoreflect.ValueOfInt32(int32(v)).MapKey(), nil
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
v, err := strconv.ParseInt(sn.Value, 10, 64)
if err != nil {
return protoreflect.MapKey{}, uctx.perrorf(n, "invalid integer value %q: %v", sn.Value, err)
}
return protoreflect.ValueOfInt64(v).MapKey(), nil
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
v, err := strconv.ParseUint(sn.Value, 10, 32)
if err != nil {
return protoreflect.MapKey{}, uctx.perrorf(n, "invalid integer value %q: %v", sn.Value, err)
}
return protoreflect.ValueOfUint32(uint32(v)).MapKey(), nil
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
v, err := strconv.ParseUint(sn.Value, 10, 64)
if err != nil {
return protoreflect.MapKey{}, uctx.perrorf(n, "invalid integer value %q: %v", sn.Value, err)
}
return protoreflect.ValueOfUint64(v).MapKey(), nil
default:
return protoreflect.MapKey{}, uctx.perrorf(n, "unsupported map key type %s", fd.Kind())
}
}
func (u *unmarshaler[T]) validate(uctx *unmarshalCtx, msg T) (outErr error) {
if u.validator == nil {
return nil
}
err := u.validator.Validate(msg)
if err == nil {
return nil
}
verrs := new(protovalidate.ValidationError)
if !errors.As(err, &verrs) {
return err
}
for _, v := range verrs.Violations {
path := v.GetFieldPath()
outErr = errors.Join(outErr, uctx.verrorf(path, v.GetMessage()))
}
return outErr
}
func pos(n ast.Node) string {
if n == nil {
return ""
}
tok := n.GetToken()
if tok == nil {
return ""
}
return fmt.Sprintf("[%d:%d]", tok.Position.Line, tok.Position.Column)
}
type unmarshalCtx struct {
errPrinter printer.Printer
srcCtx *sourcev1.SourceContext
doc *ast.DocumentNode
protoPath string
}
func newUnmarshalCtx(doc *ast.DocumentNode) *unmarshalCtx {
var startPos *sourcev1.StartPosition
if doc != nil && doc.GetToken() != nil {
if p := doc.GetToken().Position; p != nil {
startPos = &sourcev1.StartPosition{Line: uint32(p.Line), Column: uint32(p.Column), Offset: uint32(p.Offset)}
}
}