Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Peer metadata with cloud detection #1552

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added client/netbird
Binary file not shown.
Binary file added client/netbird.exe
Binary file not shown.
18 changes: 18 additions & 0 deletions client/system/detect_cloud/alibabacloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package detect_cloud

import (
"net/http"
)

func detectAlibabaCloud() string {
resp, err := hc.Get("http://100.100.100.200/latest/")
if err != nil {
return ""
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return "Alibaba Cloud"
}
return ""
}
49 changes: 49 additions & 0 deletions client/system/detect_cloud/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package detect_cloud

import (
"net/http"
)

func detectAWS() string {
v1ResultChan := make(chan bool, 1)
v2ResultChan := make(chan bool, 1)

go func() {
v1ResultChan <- detectAWSIDMSv1()
}()

go func() {
v2ResultChan <- detectAWSIDMSv2()
}()

v1Result, v2Result := <-v1ResultChan, <-v2ResultChan

if v1Result || v2Result {
return "Amazon Web Services"
}
return ""
}

func detectAWSIDMSv1() bool {
resp, err := hc.Get("http://169.254.169.254/latest/")
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}

func detectAWSIDMSv2() bool {
req, err := http.NewRequest("PUT", "http://169.254.169.254/latest/api/token", nil)
if err != nil {
return false
}
req.Header.Set("X-aws-ec2-metadata-token-ttl-seconds", "21600")

resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
24 changes: 24 additions & 0 deletions client/system/detect_cloud/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package detect_cloud

import (
"net/http"
)

func detectAzure() string {
req, err := http.NewRequest("GET", "http://169.254.169.254/metadata/instance?api-version=2021-02-01", nil)
if err != nil {
return ""
}
req.Header.Set("Metadata", "true")

resp, err := hc.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return "Microsoft Azure"
}
return ""
}
92 changes: 92 additions & 0 deletions client/system/detect_cloud/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package detect_cloud

import (
"context"
"net/http"
"sync"
"time"
)

/*
This packages is inspired by the work of the original author (https://github.com/perlogix), but it has been modified to fit the needs of the project.
Original project: https://github.com/perlogix/libdetectcloud
*/

var hc = &http.Client{Timeout: 300 * time.Millisecond}

func Detect() string {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

detectVendorFuncs := []func() string{
detectAlibabaCloud,
detectAWS,
detectAzure,
detectDigitalOcean,
detectGCE,
detectOracle,
detectIBMCloud,
detectSoftlayer,
detectVultr,
}

detectSoftwareFuncs := []func() string{
detectOpenStack,
detectContainer,
}

vendorResults := make(chan string, len(detectVendorFuncs))
softwareResults := make(chan string, len(detectSoftwareFuncs))

var wg sync.WaitGroup
wg.Add(len(detectVendorFuncs) + len(detectSoftwareFuncs))
pascal-fischer marked this conversation as resolved.
Show resolved Hide resolved

for _, fn := range detectVendorFuncs {
go func(f func() string) {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if result := f(); result != "" {
vendorResults <- result
cancel()
}
}
}(fn)
}

for _, fn := range detectSoftwareFuncs {
go func(f func() string) {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if result := f(); result != "" {
softwareResults <- result
}
}
}(fn)
}

go func() {
wg.Wait()
close(vendorResults)
close(softwareResults)
}()

for result := range vendorResults {
if result != "" {
return result
}
}

for result := range softwareResults {
if result != "" {
return result
}
}

return ""
}
18 changes: 18 additions & 0 deletions client/system/detect_cloud/digitalocean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package detect_cloud

import (
"net/http"
)

func detectDigitalOcean() string {
resp, err := hc.Get("http://169.254.169.254/metadata/v1/")
if err != nil {
return ""
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return "Digital Ocean"
}
return ""
}
24 changes: 24 additions & 0 deletions client/system/detect_cloud/gce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package detect_cloud

import (
"net/http"
)

func detectGCE() string {
r, err := http.NewRequest("GET", "http://metadata.google.internal", nil)
if err != nil {
return ""
}
r.Header.Add("Metadata-Flavor", "Google")

resp, err := hc.Do(r)
if err != nil {
return ""
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return "Google Compute Engine"
pascal-fischer marked this conversation as resolved.
Show resolved Hide resolved
}
return ""
}
51 changes: 51 additions & 0 deletions client/system/detect_cloud/ibmcloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package detect_cloud

import "net/http"

func detectIBMCloud() string {
v1ResultChan := make(chan bool, 1)
v2ResultChan := make(chan bool, 1)

go func() {
v1ResultChan <- detectIBMSecure()
}()

go func() {
v2ResultChan <- detectIBM()
}()

v1Result, v2Result := <-v1ResultChan, <-v2ResultChan

if v1Result || v2Result {
return "IBM Cloud"
}
return ""
}

func detectIBMSecure() bool {
req, err := http.NewRequest("PUT", "https://api.metadata.cloud.ibm.com/instance_identity/v1/token", nil)
if err != nil {
return false
}

resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}

func detectIBM() bool {
req, err := http.NewRequest("PUT", "http://api.metadata.cloud.ibm.com/instance_identity/v1/token", nil)
if err != nil {
return false
}

resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
27 changes: 27 additions & 0 deletions client/system/detect_cloud/kube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package detect_cloud

import (
"os"
"strings"
)

func detectContainer() string {
b, err := os.ReadFile("/proc/self/cgroup")
lixmal marked this conversation as resolved.
Show resolved Hide resolved
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 ""
}
18 changes: 18 additions & 0 deletions client/system/detect_cloud/openstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package detect_cloud

import (
"net/http"
)

func detectOpenStack() string {
resp, err := hc.Get("http://169.254.169.254/openstack")
if err != nil {
return ""
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return "OpenStack"
}
return ""
}
53 changes: 53 additions & 0 deletions client/system/detect_cloud/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package detect_cloud

import "net/http"

func detectOracle() string {
v1ResultChan := make(chan bool, 1)
v2ResultChan := make(chan bool, 1)

go func() {
v1ResultChan <- detectOracleIDMSv1()
}()

go func() {
v2ResultChan <- detectOracleIDMSv2()
}()

v1Result, v2Result := <-v1ResultChan, <-v2ResultChan

if v1Result || v2Result {
return "Oracle"
}
return ""
}

func detectOracleIDMSv1() bool {
req, err := http.NewRequest("GET", "http://169.254.169.254/opc/v1/instance/", nil)
if err != nil {
return false
}
req.Header.Add("Authorization", "Bearer Oracle")

resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}

func detectOracleIDMSv2() bool {
req, err := http.NewRequest("GET", "http://169.254.169.254/opc/v2/instance/", nil)
if err != nil {
return false
}
req.Header.Add("Authorization", "Bearer Oracle")

resp, err := hc.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
Loading
Loading