github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/azure/deployment.go (about)

     1  package azure
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/khulnasoft-lab/defsec/pkg/types"
     7  )
     8  
     9  type Deployment struct {
    10  	Metadata    types.Metadata
    11  	TargetScope Scope
    12  	Parameters  []Parameter
    13  	Variables   []Variable
    14  	Resources   []Resource
    15  	Outputs     []Output
    16  }
    17  
    18  type Parameter struct {
    19  	Variable
    20  	Default    Value
    21  	Decorators []Decorator
    22  }
    23  
    24  type Variable struct {
    25  	Name  string
    26  	Value Value
    27  }
    28  
    29  type Output Variable
    30  
    31  type Resource struct {
    32  	Metadata   types.Metadata
    33  	APIVersion Value
    34  	Type       Value
    35  	Kind       Value
    36  	Name       Value
    37  	Location   Value
    38  	Tags       Value
    39  	Sku        Value
    40  	Properties Value
    41  	Resources  []Resource
    42  }
    43  
    44  type PropertyBag struct {
    45  	Metadata types.Metadata
    46  	Data     map[string]Value
    47  }
    48  
    49  type Decorator struct {
    50  	Name string
    51  	Args []Value
    52  }
    53  
    54  type Scope string
    55  
    56  const (
    57  	ScopeResourceGroup Scope = "resourceGroup"
    58  )
    59  
    60  func (d *Deployment) GetResourcesByType(t string) []Resource {
    61  	var resources []Resource
    62  	for _, r := range d.Resources {
    63  		if r.Type.AsString() == t {
    64  			resources = append(resources, r)
    65  		}
    66  	}
    67  	return resources
    68  }
    69  
    70  func (r *Resource) GetResourcesByType(t string) []Resource {
    71  	var resources []Resource
    72  	for _, res := range r.Resources {
    73  		if res.Type.AsString() == t {
    74  			resources = append(resources, res)
    75  		}
    76  	}
    77  	return resources
    78  }
    79  
    80  func (d *Deployment) GetParameter(parameterName string) interface{} {
    81  
    82  	for _, parameter := range d.Parameters {
    83  		if parameter.Name == parameterName {
    84  			return parameter.Value.Raw()
    85  		}
    86  	}
    87  	return nil
    88  }
    89  
    90  func (d *Deployment) GetVariable(variableName string) interface{} {
    91  
    92  	for _, variable := range d.Variables {
    93  		if variable.Name == variableName {
    94  			return variable.Value.Raw()
    95  		}
    96  	}
    97  	return nil
    98  }
    99  
   100  func (d *Deployment) GetEnvVariable(envVariableName string) interface{} {
   101  
   102  	if envVariable, exists := os.LookupEnv(envVariableName); exists {
   103  		return envVariable
   104  	}
   105  	return nil
   106  }
   107  
   108  func (d *Deployment) GetOutput(outputName string) interface{} {
   109  
   110  	for _, output := range d.Outputs {
   111  		if output.Name == outputName {
   112  			return output.Value.Raw()
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  func (d *Deployment) GetDeployment() interface{} {
   119  
   120  	type template struct {
   121  		Schema         string                 `json:"$schema"`
   122  		ContentVersion string                 `json:"contentVersion"`
   123  		Parameters     map[string]interface{} `json:"parameters"`
   124  		Variables      map[string]interface{} `json:"variables"`
   125  		Resources      []interface{}          `json:"resources"`
   126  		Outputs        map[string]interface{} `json:"outputs"`
   127  	}
   128  
   129  	type templateLink struct {
   130  		URI string `json:"uri"`
   131  	}
   132  
   133  	type properties struct {
   134  		TemplateLink      templateLink           `json:"templateLink"`
   135  		Template          template               `json:"template"`
   136  		TemplateHash      string                 `json:"templateHash"`
   137  		Parameters        map[string]interface{} `json:"parameters"`
   138  		Mode              string                 `json:"mode"`
   139  		ProvisioningState string                 `json:"provisioningState"`
   140  	}
   141  
   142  	deploymentShell := struct {
   143  		Name       string     `json:"name"`
   144  		Properties properties `json:"properties"`
   145  	}{
   146  		Name: "Placeholder Deployment",
   147  		Properties: properties{
   148  			TemplateLink: templateLink{
   149  				URI: "https://placeholder.com",
   150  			},
   151  			Template: template{
   152  				Schema:         "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   153  				ContentVersion: "",
   154  				Parameters:     make(map[string]interface{}),
   155  				Variables:      make(map[string]interface{}),
   156  				Resources:      make([]interface{}, 0),
   157  				Outputs:        make(map[string]interface{}),
   158  			},
   159  		},
   160  	}
   161  
   162  	for _, parameter := range d.Parameters {
   163  		deploymentShell.Properties.Template.Parameters[parameter.Name] = parameter.Value.Raw()
   164  	}
   165  
   166  	for _, variable := range d.Variables {
   167  		deploymentShell.Properties.Template.Variables[variable.Name] = variable.Value.Raw()
   168  	}
   169  
   170  	for _, resource := range d.Resources {
   171  		deploymentShell.Properties.Template.Resources = append(deploymentShell.Properties.Template.Resources, resource)
   172  	}
   173  
   174  	for _, output := range d.Outputs {
   175  		deploymentShell.Properties.Template.Outputs[output.Name] = output.Value.Raw()
   176  	}
   177  
   178  	return deploymentShell
   179  }