-
Notifications
You must be signed in to change notification settings - Fork 120
/
cloud_detection.go
144 lines (127 loc) · 3.27 KB
/
cloud_detection.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
136
137
138
139
140
141
142
143
144
package peirates
import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
"time"
)
var hc = &http.Client{Timeout: 300 * time.Millisecond}
type CloudProvider struct {
Name string
URL string
HTTPMethod string
CustomHeader string
CustomHeaderValue string
ResultString string
}
func populateAndCheckCloudProviders() string {
providers := []CloudProvider{
{
Name: "AWS",
URL: "http://169.254.169.254/latest/",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "meta-data",
},
{
Name: "Azure",
URL: "http://169.254.169.254/metadata/instance?api-version=2024-03-15",
HTTPMethod: "GET",
CustomHeader: "Metadata",
CustomHeaderValue: "true",
ResultString: "AzurePublicCloud",
},
{
Name: "Google Cloud",
URL: "http://metadata.google.internal/computeMetadata/",
HTTPMethod: "GET",
CustomHeader: "Metadata-Flavor",
CustomHeaderValue: "Google",
ResultString: "v1/",
},
{
Name: "DigitalOcean",
URL: "http://169.254.169.254/metadata/v1/dns/",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "nameservers",
},
{
Name: "AWS (IMDSv2)",
URL: "http://169.254.169.254/latest/api/token",
HTTPMethod: "PUT",
CustomHeader: "X-aws-ec2-metadata-token-ttl-seconds",
CustomHeaderValue: "21600",
ResultString: "",
},
}
// Check to see if we are on a cloud provider at all before checking every single cloud provider's Metadata API.
client := http.Client{
Timeout: 1 * time.Second,
}
url := "http://169.254.169.254/"
// IMDSv2 will return a 401 in this case
_, err := client.Get(url)
if err != nil {
return "-- Public Cloud Provider not detected --"
}
// Now check each cloud provider's metadata API.
for _, provider := range providers {
fmt.Printf("Checking %s...\n", provider.Name)
var response string
var statusCode int
var lines []HeaderLine
if provider.CustomHeader != "" {
line := HeaderLine{LHS: provider.CustomHeader, RHS: provider.CustomHeaderValue}
lines = append(lines, line)
response, statusCode, err = GetRequest(provider.URL, lines, true)
} else {
response, statusCode, err = GetRequest(provider.URL, nil, true)
}
if err != nil {
continue
}
if provider.Name == "AWS (IMDSv2)" {
if statusCode == http.StatusOK {
return provider.Name
}
}
if strings.Contains(response, provider.ResultString) {
return provider.Name
}
}
return "-- Public Cloud Metadata API not detected --"
}
func detectContainer() string {
b, err := os.ReadFile("/proc/self/cgroup")
if err != nil {
return ""
}
fc := string(b)
kube := strings.Contains(fc, "kube")
container := strings.Contains(fc, "containerd")
if kube {
return "K8S Container"
}
if container {
return "Container"
}
return ""
}
func detectOpenStack() string {
if runtime.GOOS != "windows" {
data, err := os.ReadFile("/sys/class/dmi/id/sys_vendor")
if err != nil {
return ""
}
if strings.Contains(string(data), "OpenStack Foundation") {
return "OpenStack"
}
return ""
}
return ""
}