github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/fn_split.go (about)

     1  package parser
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/aquasecurity/trivy-iac/pkg/scanners/cloudformation/cftypes"
     7  )
     8  
     9  func ResolveSplit(property *Property) (resolved *Property, success bool) {
    10  	if !property.isFunction() {
    11  		return property, true
    12  	}
    13  
    14  	refValue := property.AsMap()["Fn::Split"].AsList()
    15  
    16  	if len(refValue) != 2 {
    17  		return abortIntrinsic(property, "Fn::Split should have exactly 2 values, returning original Property")
    18  	}
    19  
    20  	delimiterProp := refValue[0]
    21  	splitProp := refValue[1]
    22  
    23  	if !splitProp.IsString() || !delimiterProp.IsString() {
    24  		abortIntrinsic(property, "Fn::Split requires two strings as input, returning original Property")
    25  
    26  	}
    27  
    28  	propertyList := createPropertyList(splitProp, delimiterProp, property)
    29  
    30  	return property.deriveResolved(cftypes.List, propertyList), true
    31  }
    32  
    33  func createPropertyList(splitProp *Property, delimiterProp *Property, parent *Property) []*Property {
    34  
    35  	splitString := splitProp.AsString()
    36  	delimiter := delimiterProp.AsString()
    37  
    38  	splits := strings.Split(splitString, delimiter)
    39  	var props []*Property
    40  	for _, split := range splits {
    41  		props = append(props, parent.deriveResolved(cftypes.String, split))
    42  	}
    43  	return props
    44  }