-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloud_provider.go
119 lines (99 loc) · 2.55 KB
/
cloud_provider.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
package firewall
import (
_ "embed"
"encoding/json"
"github.com/gocarina/gocsv"
)
var cloudProviderBlockList []string = nil
// CloudProviderBlockList returns a slice of IP Ranges of aws, azure and gcp
func CloudProviderBlockList() (blockList []string) {
if cloudProviderBlockList != nil {
// return memoized list
return cloudProviderBlockList
}
var (
awsRanges awsRanges
azureRanges azureRanges
gcpRanges gcpRanges
)
// aws
err := json.Unmarshal(awsFile, &awsRanges)
if err != nil {
panic(err)
}
for _, prefix := range awsRanges.Prefixes {
if prefix.IPPrefix != "" {
blockList = append(blockList, prefix.IPPrefix)
} else if prefix.IPv6Prefix != "" {
blockList = append(blockList, prefix.IPv6Prefix)
}
}
// azure
json.Unmarshal(azureFile, &azureRanges)
for _, prefix := range azureRanges.Prefixes {
blockList = append(blockList, prefix.IPPrefix)
}
// gcp
json.Unmarshal(gcpFile, &gcpRanges)
for _, prefix := range gcpRanges.Prefixes {
if prefix.IPPrefix != "" {
blockList = append(blockList, prefix.IPPrefix)
} else if prefix.IPv6Prefix != "" {
blockList = append(blockList, prefix.IPv6Prefix)
}
}
// linode
var linodeRanges []*csvIPPrefix
gocsv.UnmarshalBytes(linodeFile, &linodeRanges)
for _, prefix := range linodeRanges {
if prefix.IPPrefix != "" {
blockList = append(blockList, prefix.IPPrefix)
}
}
// digital ocean
var digitalOceanRanges []*csvIPPrefix
gocsv.UnmarshalBytes(digitalOceanFile, &digitalOceanRanges)
for _, prefix := range digitalOceanRanges {
if prefix.IPPrefix != "" {
blockList = append(blockList, prefix.IPPrefix)
}
}
// memoize
cloudProviderBlockList = blockList
return blockList
}
var (
//go:embed cloud-provider-data/aws-ip-ranges.json
awsFile []byte
//go:embed cloud-provider-data/azure-ip-ranges.json
azureFile []byte
//go:embed cloud-provider-data/gcp-ip-ranges.json
gcpFile []byte
//go:embed cloud-provider-data/linode-ranges.csv
linodeFile []byte
//go:embed cloud-provider-data/digital-ocean-ranges.csv
digitalOceanFile []byte
)
type awsRanges struct {
Prefixes []awsPrefix `json:"prefixes"`
}
type awsPrefix struct {
IPPrefix string `json:"ip_prefix"`
IPv6Prefix string `json:"ipv6_prefix"`
}
type azureRanges struct {
Prefixes []azurePrefix `json:"prefixes"`
}
type azurePrefix struct {
IPPrefix string `json:"ip_prefix"`
}
type gcpRanges struct {
Prefixes []gcpPrefix `json:"prefixes"`
}
type gcpPrefix struct {
IPPrefix string `json:"ipv4Prefix"`
IPv6Prefix string `json:"ipv6Prefix"`
}
type csvIPPrefix struct {
IPPrefix string `csv:"prefix"`
}