-
Notifications
You must be signed in to change notification settings - Fork 7
/
adapt.go
135 lines (112 loc) · 4.56 KB
/
adapt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package compute
import (
"encoding/base64"
defsecTypes "github.com/aquasecurity/defsec/pkg/types"
"github.com/aquasecurity/defsec/pkg/terraform"
"github.com/aquasecurity/defsec/pkg/providers/azure/compute"
)
func Adapt(modules terraform.Modules) compute.Compute {
return adaptCompute(modules)
}
func adaptCompute(modules terraform.Modules) compute.Compute {
var managedDisks []compute.ManagedDisk
var linuxVirtualMachines []compute.LinuxVirtualMachine
var windowsVirtualMachines []compute.WindowsVirtualMachine
for _, module := range modules {
for _, resource := range module.GetResourcesByType("azurerm_linux_virtual_machine") {
linuxVirtualMachines = append(linuxVirtualMachines, adaptLinuxVM(resource))
}
for _, resource := range module.GetResourcesByType("azurerm_windows_virtual_machine") {
windowsVirtualMachines = append(windowsVirtualMachines, adaptWindowsVM(resource))
}
for _, resource := range module.GetResourcesByType("azurerm_virtual_machine") {
if resource.HasChild("os_profile_linux_config") {
linuxVirtualMachines = append(linuxVirtualMachines, adaptLinuxVM(resource))
} else if resource.HasChild("os_profile_windows_config") {
windowsVirtualMachines = append(windowsVirtualMachines, adaptWindowsVM(resource))
}
}
for _, resource := range module.GetResourcesByType("azurerm_managed_disk") {
managedDisks = append(managedDisks, adaptManagedDisk(resource))
}
}
return compute.Compute{
LinuxVirtualMachines: linuxVirtualMachines,
WindowsVirtualMachines: windowsVirtualMachines,
ManagedDisks: managedDisks,
}
}
func adaptManagedDisk(resource *terraform.Block) compute.ManagedDisk {
disk := compute.ManagedDisk{
Metadata: resource.GetMetadata(),
Encryption: compute.Encryption{
Metadata: resource.GetMetadata(),
// encryption is enabled by default - https://github.com/hashicorp/terraform-provider-azurerm/blob/baf55926fe813011003ee4fb0e8e6134fcfcca87/internal/services/compute/managed_disk_resource.go#L288
Enabled: defsecTypes.BoolDefault(true, resource.GetMetadata()),
},
}
encryptionBlock := resource.GetBlock("encryption_settings")
if encryptionBlock.IsNotNil() {
disk.Encryption.Metadata = encryptionBlock.GetMetadata()
enabledAttr := encryptionBlock.GetAttribute("enabled")
disk.Encryption.Enabled = enabledAttr.AsBoolValueOrDefault(true, encryptionBlock)
}
return disk
}
func adaptLinuxVM(resource *terraform.Block) compute.LinuxVirtualMachine {
workingBlock := resource
if resource.TypeLabel() == "azurerm_virtual_machine" {
if b := resource.GetBlock("os_profile"); b.IsNotNil() {
workingBlock = b
}
}
customDataAttr := workingBlock.GetAttribute("custom_data")
customDataVal := defsecTypes.StringDefault("", workingBlock.GetMetadata())
if customDataAttr.IsResolvable() && customDataAttr.IsString() {
encoded, err := base64.StdEncoding.DecodeString(customDataAttr.Value().AsString())
if err != nil {
encoded = []byte(customDataAttr.Value().AsString())
}
customDataVal = defsecTypes.String(string(encoded), customDataAttr.GetMetadata())
}
if resource.TypeLabel() == "azurerm_virtual_machine" {
workingBlock = resource.GetBlock("os_profile_linux_config")
}
disablePasswordAuthAttr := workingBlock.GetAttribute("disable_password_authentication")
disablePasswordAuthVal := disablePasswordAuthAttr.AsBoolValueOrDefault(true, workingBlock)
return compute.LinuxVirtualMachine{
Metadata: resource.GetMetadata(),
VirtualMachine: compute.VirtualMachine{
Metadata: resource.GetMetadata(),
CustomData: customDataVal,
},
OSProfileLinuxConfig: compute.OSProfileLinuxConfig{
Metadata: resource.GetMetadata(),
DisablePasswordAuthentication: disablePasswordAuthVal,
},
}
}
func adaptWindowsVM(resource *terraform.Block) compute.WindowsVirtualMachine {
workingBlock := resource
if resource.TypeLabel() == "azurerm_virtual_machine" {
if b := resource.GetBlock("os_profile"); b.IsNotNil() {
workingBlock = b
}
}
customDataAttr := workingBlock.GetAttribute("custom_data")
customDataVal := defsecTypes.StringDefault("", workingBlock.GetMetadata())
if customDataAttr.IsResolvable() && customDataAttr.IsString() {
encoded, err := base64.StdEncoding.DecodeString(customDataAttr.Value().AsString())
if err != nil {
encoded = []byte(customDataAttr.Value().AsString())
}
customDataVal = defsecTypes.String(string(encoded), customDataAttr.GetMetadata())
}
return compute.WindowsVirtualMachine{
Metadata: resource.GetMetadata(),
VirtualMachine: compute.VirtualMachine{
Metadata: resource.GetMetadata(),
CustomData: customDataVal,
},
}
}