-
Notifications
You must be signed in to change notification settings - Fork 9
/
describe_sg.go
76 lines (69 loc) · 1.89 KB
/
describe_sg.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
package cliaws
import (
"context"
"fmt"
"os"
"github.com/alexflint/go-arg"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/nathants/libaws/lib"
)
func init() {
lib.Commands["ec2-describe-sg"] = ec2DescribeSg
lib.Args["ec2-describe-sg"] = ec2DescribeSgArgs{}
}
type ec2DescribeSgArgs struct {
Vpc string `arg:"positional,required"`
Sg string `arg:"positional,required"`
}
func (ec2DescribeSgArgs) Description() string {
return "\ndescribe a sg\n"
}
func printSg(proto string, fromPort, toPort int, src string) {
if proto == "-1" {
proto = "All"
}
var port string
if fromPort != toPort {
port = fmt.Sprintf("%d-%d", fromPort, toPort)
} else {
port = fmt.Sprint(fromPort)
}
fmt.Println(proto, port, src)
}
func ec2DescribeSg() {
var args ec2DescribeSgArgs
arg.MustParse(&args)
ctx := context.Background()
sgID, err := lib.EC2SgID(ctx, args.Vpc, args.Sg)
if err != nil {
lib.Logger.Fatal("error: ", err)
}
out, err := lib.EC2Client().DescribeSecurityGroupsWithContext(ctx, &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{aws.String(sgID)},
})
if err != nil {
lib.Logger.Fatal("error: ", err)
}
fmt.Fprintln(os.Stderr, "protocol port source")
for _, perm := range out.SecurityGroups[0].IpPermissions {
if perm.FromPort == nil {
perm.FromPort = aws.Int64(0)
}
if perm.ToPort == nil {
perm.ToPort = aws.Int64(65535)
}
for _, ip := range perm.IpRanges {
printSg(*perm.IpProtocol, int(*perm.FromPort), int(*perm.ToPort), *ip.CidrIp)
}
for _, ip := range perm.Ipv6Ranges {
printSg(*perm.IpProtocol, int(*perm.FromPort), int(*perm.ToPort), *ip.CidrIpv6)
}
for _, ip := range perm.UserIdGroupPairs {
printSg(*perm.IpProtocol, int(*perm.FromPort), int(*perm.ToPort), *ip.GroupId)
}
for _, ip := range perm.PrefixListIds {
printSg(*perm.IpProtocol, int(*perm.FromPort), int(*perm.ToPort), *ip.PrefixListId)
}
}
}