github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/cloudformation/parser/fn_get_attr.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 ResolveGetAtt(property *Property) (resolved *Property, success bool) {
    10  	if !property.isFunction() {
    11  		return property, true
    12  	}
    13  
    14  	refValueProp := property.AsMap()["Fn::GetAtt"]
    15  
    16  	var refValue []string
    17  
    18  	if refValueProp.IsString() {
    19  		refValue = strings.Split(refValueProp.AsString(), ".")
    20  	}
    21  
    22  	if refValueProp.IsList() {
    23  		for _, p := range refValueProp.AsList() {
    24  			refValue = append(refValue, p.AsString())
    25  		}
    26  	}
    27  
    28  	if len(refValue) != 2 {
    29  		return abortIntrinsic(property, "Fn::GetAtt should have exactly 2 values, returning original Property")
    30  	}
    31  
    32  	logicalId := refValue[0]
    33  	attribute := refValue[1]
    34  
    35  	referencedResource := property.ctx.GetResourceByLogicalID(logicalId)
    36  	if referencedResource == nil || referencedResource.IsNil() {
    37  		return property.deriveResolved(cftypes.String, ""), true
    38  	}
    39  
    40  	referencedProperty := referencedResource.GetProperty(attribute)
    41  	if referencedProperty.IsNil() {
    42  		return property.deriveResolved(cftypes.String, referencedResource.ID()), true
    43  	}
    44  
    45  	return property.deriveResolved(referencedProperty.Type(), referencedProperty.RawValue()), true
    46  }