github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/util.go (about) 1 package parser 2 3 import ( 4 "strconv" 5 6 "github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes" 7 8 "github.com/liamg/jfather" 9 "gopkg.in/yaml.v3" 10 ) 11 12 func setPropertyValueFromJson(node jfather.Node, propertyData *PropertyInner) error { 13 14 switch node.Kind() { 15 16 case jfather.KindNumber: 17 propertyData.Type = cftypes.Float64 18 return node.Decode(&propertyData.Value) 19 case jfather.KindBoolean: 20 propertyData.Type = cftypes.Bool 21 return node.Decode(&propertyData.Value) 22 case jfather.KindString: 23 propertyData.Type = cftypes.String 24 return node.Decode(&propertyData.Value) 25 case jfather.KindObject: 26 var childData map[string]*Property 27 if err := node.Decode(&childData); err != nil { 28 return err 29 } 30 propertyData.Type = cftypes.Map 31 propertyData.Value = childData 32 return nil 33 case jfather.KindArray: 34 var childData []*Property 35 if err := node.Decode(&childData); err != nil { 36 return err 37 } 38 propertyData.Type = cftypes.List 39 propertyData.Value = childData 40 return nil 41 default: 42 propertyData.Type = cftypes.String 43 return node.Decode(&propertyData.Value) 44 } 45 46 } 47 48 func setPropertyValueFromYaml(node *yaml.Node, propertyData *PropertyInner) error { 49 if IsIntrinsicFunc(node) { 50 var newContent []*yaml.Node 51 52 newContent = append(newContent, &yaml.Node{ 53 Tag: "!!str", 54 Value: getIntrinsicTag(node.Tag), 55 Kind: yaml.ScalarNode, 56 }) 57 58 newContent = createNode(node, newContent) 59 60 node.Tag = "!!map" 61 node.Kind = yaml.MappingNode 62 node.Content = newContent 63 } 64 65 if node.Content == nil { 66 67 switch node.Tag { 68 69 case "!!int": 70 propertyData.Type = cftypes.Int 71 propertyData.Value, _ = strconv.Atoi(node.Value) 72 case "!!bool": 73 propertyData.Type = cftypes.Bool 74 propertyData.Value, _ = strconv.ParseBool(node.Value) 75 case "!!str", "!!string": 76 propertyData.Type = cftypes.String 77 propertyData.Value = node.Value 78 } 79 return nil 80 } 81 82 switch node.Tag { 83 case "!!map": 84 var childData map[string]*Property 85 if err := node.Decode(&childData); err != nil { 86 return err 87 } 88 propertyData.Type = cftypes.Map 89 propertyData.Value = childData 90 return nil 91 case "!!seq": 92 var childData []*Property 93 if err := node.Decode(&childData); err != nil { 94 return err 95 } 96 propertyData.Type = cftypes.List 97 propertyData.Value = childData 98 return nil 99 } 100 101 return nil 102 } 103 104 func createNode(node *yaml.Node, newContent []*yaml.Node) []*yaml.Node { 105 if node.Content == nil { 106 newContent = append(newContent, &yaml.Node{ 107 Tag: "!!str", 108 Value: node.Value, 109 Kind: yaml.ScalarNode, 110 }) 111 } else { 112 113 newNode := &yaml.Node{ 114 Content: node.Content, 115 Kind: node.Kind, 116 } 117 118 switch node.Kind { 119 case yaml.SequenceNode: 120 newNode.Tag = "!!seq" 121 case yaml.MappingNode: 122 newNode.Tag = "!!map" 123 case yaml.ScalarNode: 124 default: 125 newNode.Tag = node.Tag 126 } 127 newContent = append(newContent, newNode) 128 } 129 return newContent 130 } 131 132 func calculateEndLine(node *yaml.Node) int { 133 if node.Content == nil { 134 return node.Line 135 } 136 137 return calculateEndLine(node.Content[len(node.Content)-1]) 138 139 }