-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
151 lines (122 loc) · 3.43 KB
/
main.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
145
146
147
148
149
150
151
package main
import (
"context"
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"log"
"os"
)
var (
checkForVPC bool
awsProfile, region, vpcID, internetFacingFlag *string
loadBalancers []types.LoadBalancer
conf aws.Config
err error
)
func main() {
ParseFlags()
fmt.Printf("Using AWS Profile: %s\n", *awsProfile)
conf = InitConfig()
svc := elasticloadbalancingv2.NewFromConfig(conf)
loadBalancers = FetchAllELBs(svc)
ParseLoadBalancers()
}
func ParseLoadBalancers() {
if loadBalancers != nil {
if *internetFacingFlag == "internet" {
fmt.Println("Internet Facing Load Balancers")
loadBalancers = FetchInternetFacingELBs(loadBalancers)
} else if *internetFacingFlag == "internal" {
fmt.Println("Internal Facing Load Balancers")
loadBalancers = FetchInternalFacingELBs(loadBalancers)
} else {
fmt.Println("All Load Balancers")
}
if checkForVPC {
loadBalancers = FilterByVPC(loadBalancers)
}
returnString := ""
for _, elb := range loadBalancers {
returnString = fmt.Sprintf("%s%s\n", returnString, PrintLBInfo(elb))
}
fmt.Printf("%v\n", returnString)
}
}
func InitConfig() aws.Config {
c, er := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(*region),
config.WithSharedConfigProfile(*awsProfile))
if er != nil {
log.Fatal(er)
}
return c
}
func FetchAllELBs(svc *elasticloadbalancingv2.Client) []types.LoadBalancer {
els, e := svc.DescribeLoadBalancers(context.TODO(), &elasticloadbalancingv2.DescribeLoadBalancersInput{})
if e != nil {
log.Fatal(e.Error())
}
return els.LoadBalancers
}
func ParseFlags() {
awsProfile = flag.String("profile", "", "aws profile name")
region = flag.String("region", "eu-west-1", "The AWS Region to search in.")
vpcID = flag.String("vpc", "", "Set a VPC ID to run this against.")
internetFacingFlag = flag.String("internet", "all", "Option: all, internet, internal. 'all' returns everything. 'internal' fetches internal facing Load Balancers and 'internet' will face internet-facing Load Balancers")
flag.Parse()
if *awsProfile == ""{
prof := os.Getenv("AWS_PROFILE")
awsProfile = &prof
}
if *awsProfile == "" {
//TODO: Search for kube2iam/internal locations
}
if *vpcID != "" {
checkForVPC = true
}
}
func FilterByVPC(lbs []types.LoadBalancer) []types.LoadBalancer {
var elbs []types.LoadBalancer
fmt.Printf("VPC-ID: %s\n\n", *vpcID)
for _, elb := range lbs{
if *elb.VpcId == *vpcID{
elbs = append(elbs, elb)
}
}
return elbs
}
func FetchInternetFacingELBs(lbs []types.LoadBalancer) []types.LoadBalancer {
var elbs []types.LoadBalancer
for _, elb := range lbs{
if elb.Scheme == types.LoadBalancerSchemeEnumInternetFacing{
elbs = append(elbs, elb)
}
}
return elbs
}
func FetchInternalFacingELBs(lbs []types.LoadBalancer) []types.LoadBalancer {
var elbs []types.LoadBalancer
for _, elb := range lbs{
if elb.Scheme == types.LoadBalancerSchemeEnumInternal{
elbs = append(elbs, elb)
}
}
return elbs
}
func PrintLBInfo(elb types.LoadBalancer) string {
var printInfo string
printItems := map[string]string{
"Name": *elb.LoadBalancerName,
"ARN": *elb.LoadBalancerArn,
"VPC-ID": *elb.VpcId,
"Scheme": fmt.Sprintf("%v", elb.Scheme),
}
for k, v := range printItems{
printInfo = fmt.Sprintf("%s%s:\t\t%s\n", printInfo, k, v)
}
return printInfo
}