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

detect which cloud provider we are using #38

Merged
merged 10 commits into from
Jul 4, 2023
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PACKAGES:=$(shell go list ./... | grep -v /vendor/)

default: lint

gofmt:
go fmt ./...

lint: gofmt
$(GOPATH)/bin/golint $(PACKAGES)
$(GOPATH)/bin/gosec -quiet -no-fail ./...
$(GOPATH)/bin/golangci-lint run

update-deps:
go clean -modcache
go get -u ./...
go mod tidy
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func checkForNodeCredentials(clientCertificates *[]ClientCertificateKeyPair) err
continue
}
firstCluster := clustersSection[0].(map[string]interface{})
if firstCluster == nil{
if firstCluster == nil {
continue
}
clusterSection := firstCluster["cluster"].(map[string]interface{})
if clusterSection == nil{
if clusterSection == nil {
continue
}
APIServer := clusterSection["server"].(string)
Expand Down Expand Up @@ -606,7 +606,7 @@ func GetNamespaces(connectionString ServerInfo) ([]string, error) {
if !kubectlAuthCanI(connectionString, "get", "namespaces") {
errorString := "[-] Permission Denied: your service account isn't allowed to get namespaces"
println(errorString)
println("Consider trying kubectl-try-all get namespaces to see if any RBAC principals you have can do this.")
println("Consider trying kubectl-try-all get namespaces to see if any RBAC principals you have can do this.")
return []string{}, errors.New(errorString)
}

Expand Down
4 changes: 2 additions & 2 deletions enumerate_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func getAllServicesViaDNS() (*[]serviceHostIPPort, error) {
return &serviceHostIPPorts, nil
}

func enumerateDNS() (error) {
func enumerateDNS() error {

println("\nRequesting SRV record any.any.svc.cluster.local - thank @raesene:\n")
servicesSlicePointer, err := getAllServicesViaDNS()

Expand Down
89 changes: 83 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
module github.com/inguardians/peirates

go 1.16
go 1.17

require (
github.com/aws/aws-sdk-go v1.42.4
github.com/aws/aws-sdk-go v1.44.293
github.com/trung/jwt-tools v0.0.0-20191029184913-432ee57a2445
golang.org/x/net v0.10.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/cli-runtime v0.22.3 // indirect
k8s.io/kubectl v0.22.3
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/daviddengcn/go-colortext v1.0.0 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lithammer/dedent v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230612165344-9532f5667272 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/apimachinery v0.27.3 // indirect
k8s.io/cli-runtime v0.27.3 // indirect
k8s.io/client-go v0.27.3 // indirect
k8s.io/component-base v0.27.3 // indirect
k8s.io/component-helpers v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230614213217-ba0abe644833 // indirect
k8s.io/metrics v0.27.3 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.14.0 // indirect
sigs.k8s.io/kustomize/kustomize/v5 v5.1.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect

)
30 changes: 15 additions & 15 deletions kubeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ func newKubeRequest(path string, cfg RequestConfig) (*http.Request, error) {
// Functions may be optionally passed in to modify the default configuration.
// The default configuration is:
//
// RequestConfig {
// Host: "127.0.0.1",
// Port: 6443, // The default Kubernetes port
// Method: "GET",
// Https: true,
// IgnoreHttpsErrors: true,
// }
// RequestConfig {
// Host: "127.0.0.1",
// Port: 6443, // The default Kubernetes port
// Method: "GET",
// Https: true,
// IgnoreHttpsErrors: true,
// }
//
// For example:
//
// func RequestSimple(path string, host string, port int) string {
// // This passes a function literal (also known as a lambda or anonymous function)
// // to RequestPath to configure the host and port.
// return Request(path, func (cfg *RequestConfig) {
// cfg.Host = host
// cfg.Port = port
// })
// }
// func RequestSimple(path string, host string, port int) string {
// // This passes a function literal (also known as a lambda or anonymous function)
// // to RequestPath to configure the host and port.
// return Request(path, func (cfg *RequestConfig) {
// cfg.Host = host
// cfg.Port = port
// })
// }
func Request(path string, cfgs ...func(*RequestConfig)) string {
cfg := RequestConfig{
Host: "127.0.0.1",
Expand Down
6 changes: 6 additions & 0 deletions peirates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
L "github.com/inguardians/peirates/pkg"
"math/rand"
"net/url"
"os"
Expand Down Expand Up @@ -312,6 +313,11 @@ type PodNamespaceContainerTuple struct {
func Main() {
var err error

var detectCloud string

detectCloud = L.Detect()
fmt.Println("This cloud: " + detectCloud)

// Create a global variable named "connectionString" initialized to default values
connectionString := ImportPodServiceAccountToken()
cmdOpts := CommandLineOptions{connectionConfig: &connectionString}
Expand Down
3 changes: 3 additions & 0 deletions pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Internal

Private application and library code.
139 changes: 139 additions & 0 deletions pkg/cloud_detection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package pkg

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() {
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a copy-paste error on this line that can be easily fixed.

},
{
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)

// Use DoHTTPRequestAndGetBody()
req, err := http.NewRequest(provider.HTTPMethod, provider.URL, nil)
if err != nil {
fmt.Printf("Failed to create request for %s: %v\n", provider.Name, err)
continue
}

if provider.CustomHeader != "" {
req.Header.Set(provider.CustomHeader, provider.CustomHeaderValue)
}

// use DoHTTPRequestAndGetBody
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to make request to %s: %v\n", provider.Name, err)
continue
}
defer resp.Body.Close()

// Use DoHTTPRequestAndGetBody()
if resp.StatusCode == http.StatusOK {
// Check if there's a body string returned that matches ResultString
} else {
fmt.Printf("%s responded with HTTP %d\n", provider.Name, resp.StatusCode)
}
}
}

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 ""
}