-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn_builtin.go
65 lines (51 loc) · 1.66 KB
/
fn_builtin.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
package parser
import (
"fmt"
"net"
"github.com/khulnasoft-lab/vul-iac/pkg/scanners/cloudformation/cftypes"
"github.com/apparentlymart/go-cidr/cidr"
)
func GetAzs(property *Property) (*Property, bool) {
return property.deriveResolved(cftypes.List, []*Property{
property.deriveResolved(cftypes.String, "us-east-1a"),
property.deriveResolved(cftypes.String, "us-east-1a"),
property.deriveResolved(cftypes.String, "us-east-1a"),
}), true
}
func GetCidr(property *Property) (*Property, bool) {
if !property.isFunction() {
return property, true
}
refValue := property.AsMap()["Fn::Cidr"]
if refValue.IsNotList() || len(refValue.AsList()) != 3 {
return abortIntrinsic(property, "Fn::Cidr expects a list of 3 attributes")
}
listParts := refValue.AsList()
ipaddressProp := listParts[0]
ipAddress := "10.0.0.0/2"
if ipaddressProp.IsString() {
ipAddress = ipaddressProp.AsString()
}
count := listParts[1].AsInt()
bit := listParts[2].AsInt()
ranges, err := calculateCidrs(ipAddress, count, bit, property)
if err != nil {
return abortIntrinsic(property, "Could not calculate the required ranges")
}
return property.deriveResolved(cftypes.List, ranges), true
}
func calculateCidrs(ipaddress string, count int, bit int, original *Property) ([]*Property, error) {
var cidrProperties []*Property
_, network, err := net.ParseCIDR(ipaddress)
if err != nil {
return nil, err
}
for i := 0; i < count; i++ {
next, err := cidr.Subnet(network, bit, i)
if err != nil {
return nil, fmt.Errorf("failed to create cidr blocks")
}
cidrProperties = append(cidrProperties, original.deriveResolved(cftypes.String, next.String()))
}
return cidrProperties, nil
}