1
1
package parser
2
2
3
3
import (
4
+ "encoding/base64"
4
5
"fmt"
5
6
"strconv"
7
+ "time"
6
8
7
9
"gopkg.in/yaml.v3"
10
+
11
+ "github.com/aquasecurity/trivy/pkg/log"
8
12
)
9
13
10
14
type TagType string
11
15
12
16
const (
13
- TagBool TagType = "!!bool"
14
- TagInt TagType = "!!int"
15
- TagFloat TagType = "!!float"
16
- TagStr TagType = "!!str"
17
- TagString TagType = "!!string"
18
- TagSlice TagType = "!!seq"
19
- TagMap TagType = "!!map"
17
+ TagBool TagType = "!!bool"
18
+ TagInt TagType = "!!int"
19
+ TagFloat TagType = "!!float"
20
+ TagStr TagType = "!!str"
21
+ TagString TagType = "!!string"
22
+ TagSlice TagType = "!!seq"
23
+ TagMap TagType = "!!map"
24
+ TagTimestamp TagType = "!!timestamp"
25
+ TagBinary TagType = "!!binary"
20
26
)
21
27
22
28
type ManifestNode struct {
@@ -33,8 +39,14 @@ func (r *ManifestNode) ToRego() any {
33
39
return nil
34
40
}
35
41
switch r .Type {
36
- case TagBool , TagInt , TagString , TagStr :
42
+ case TagBool , TagInt , TagFloat , TagString , TagStr , TagBinary :
37
43
return r .Value
44
+ case TagTimestamp :
45
+ t , ok := r .Value .(time.Time )
46
+ if ! ok {
47
+ return nil
48
+ }
49
+ return t .Format (time .RFC3339 )
38
50
case TagSlice :
39
51
var output []any
40
52
for _ , node := range r .Value .([]ManifestNode ) {
@@ -58,40 +70,53 @@ func (r *ManifestNode) ToRego() any {
58
70
}
59
71
60
72
func (r * ManifestNode ) UnmarshalYAML (node * yaml.Node ) error {
61
-
62
73
r .StartLine = node .Line
63
74
r .EndLine = node .Line
64
75
r .Type = TagType (node .Tag )
65
76
66
77
switch TagType (node .Tag ) {
67
78
case TagString , TagStr :
68
-
69
79
r .Value = node .Value
70
80
case TagInt :
71
81
val , err := strconv .Atoi (node .Value )
72
82
if err != nil {
73
- return err
83
+ return fmt . Errorf ( "failed to parse int: %w" , err )
74
84
}
75
85
r .Value = val
76
86
case TagFloat :
77
87
val , err := strconv .ParseFloat (node .Value , 64 )
78
88
if err != nil {
79
- return err
89
+ return fmt . Errorf ( "failed to parse float: %w" , err )
80
90
}
81
91
r .Value = val
82
92
case TagBool :
83
93
val , err := strconv .ParseBool (node .Value )
84
94
if err != nil {
85
- return err
95
+ return fmt .Errorf ("failed to parse bool: %w" , err )
96
+ }
97
+ r .Value = val
98
+ case TagTimestamp :
99
+ var val time.Time
100
+ if err := node .Decode (& val ); err != nil {
101
+ return fmt .Errorf ("failed to decode timestamp: %w" , err )
102
+ }
103
+ r .Value = val
104
+ case TagBinary :
105
+ val , err := base64 .StdEncoding .DecodeString (node .Value )
106
+ if err != nil {
107
+ return fmt .Errorf ("failed to decode binary data: %w" , err )
86
108
}
87
109
r .Value = val
88
110
case TagMap :
89
111
return r .handleMapTag (node )
90
112
case TagSlice :
91
113
return r .handleSliceTag (node )
92
-
93
114
default :
94
- return fmt .Errorf ("node tag is not supported %s" , node .Tag )
115
+ log .WithPrefix ("k8s" ).Debug ("Skipping unsupported node tag" ,
116
+ log .String ("tag" , node .Tag ),
117
+ log .FilePath (r .Path ),
118
+ log .Int ("line" , node .Line ),
119
+ )
95
120
}
96
121
return nil
97
122
}
0 commit comments