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

     1  package parser
     2  
     3  import (
     4  	defsecTypes "github.com/aquasecurity/defsec/pkg/types"
     5  )
     6  
     7  type SourceFormat string
     8  
     9  const (
    10  	YamlSourceFormat SourceFormat = "yaml"
    11  	JsonSourceFormat SourceFormat = "json"
    12  )
    13  
    14  type FileContexts []*FileContext
    15  
    16  type FileContext struct {
    17  	filepath     string
    18  	lines        []string
    19  	SourceFormat SourceFormat
    20  	Parameters   map[string]*Parameter  `json:"Parameters" yaml:"Parameters"`
    21  	Resources    map[string]*Resource   `json:"Resources" yaml:"Resources"`
    22  	Globals      map[string]*Resource   `json:"Globals" yaml:"Globals"`
    23  	Mappings     map[string]interface{} `json:"Mappings,omitempty" yaml:"Mappings"`
    24  	Conditions   map[string]Property    `json:"Conditions,omitempty" yaml:"Conditions"`
    25  }
    26  
    27  func (t *FileContext) GetResourceByLogicalID(name string) *Resource {
    28  	for n, r := range t.Resources {
    29  		if name == n {
    30  			return r
    31  		}
    32  	}
    33  	return nil
    34  }
    35  
    36  func (t *FileContext) GetResourcesByType(names ...string) []*Resource {
    37  	var resources []*Resource
    38  	for _, r := range t.Resources {
    39  		for _, name := range names {
    40  			if name == r.Type() {
    41  				//
    42  				resources = append(resources, r)
    43  			}
    44  		}
    45  	}
    46  	return resources
    47  }
    48  
    49  func (t *FileContext) Metadata() defsecTypes.Metadata {
    50  	rng := defsecTypes.NewRange(t.filepath, 1, len(t.lines), "", nil)
    51  
    52  	return defsecTypes.NewMetadata(rng, NewCFReference("Template", rng).String())
    53  }
    54  
    55  func (t *FileContext) OverrideParameters(params map[string]any) {
    56  	for key := range t.Parameters {
    57  		if val, ok := params[key]; ok {
    58  			t.Parameters[key].UpdateDefault(val)
    59  		}
    60  	}
    61  }