-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud_detection.go
129 lines (116 loc) · 2.95 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
package peirates
import (
"fmt"
"io/ioutil"
"net/http"
"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: "Amazon Web Services",
},
{
Name: "Azure",
URL: "http://169.254.169.254/metadata/v1/InstanceInfo",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "Microsoft Azure",
},
{
Name: "DigitalOcean",
URL: "http://169.254.169.254/metadata/v1.json",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "Microsoft Azure",
},
{
Name: "Google Cloud",
URL: "http://metadata.google.internal/computeMetadata/v1/instance/tags",
HTTPMethod: "GET",
CustomHeader: "Metadata-Flavor",
CustomHeaderValue: "Google",
ResultString: "Google Compute Engine",
},
{
Name: "SoftLayer",
URL: "https://api.service.softlayer.com/rest/v3/SoftLayer_Resource_Metadata/UserMetadata.txt",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "SoftLayer",
},
{
Name: "Vultr",
URL: "http://169.254.169.254/v1.json",
HTTPMethod: "GET",
CustomHeader: "",
CustomHeaderValue: "",
ResultString: "Vultr",
},
}
for _, provider := range providers {
fmt.Printf("Checking %s...\n", provider.Name)
var response string
var lines []HeaderLine
if provider.CustomHeader != "" {
line := HeaderLine{LHS: provider.CustomHeader, RHS: provider.CustomHeaderValue}
lines = append(lines, line)
response = GetRequest(provider.URL, lines, true)
} else {
response = GetRequest(provider.URL, nil, true)
}
if strings.Contains(response, provider.ResultString) {
println("DEBUG: detected provider ", provider.Name)
return provider.Name
}
}
return ""
}
func detectContainer() string {
b, err := ioutil.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 := ioutil.ReadFile("/sys/class/dmi/id/sys_vendor")
if err != nil {
return ""
}
if strings.Contains(string(data), "OpenStack Foundation") {
return "OpenStack"
}
return ""
}
return ""
}