-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_group.go
110 lines (87 loc) · 2.78 KB
/
security_group.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
package aws
import (
"fmt"
"math/rand"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
// securityGroupNamePrefix is the prefix that is applied to auto-generated
// security group names.
const securityGroupNamePrefix = "bastion-"
// securityGroupDescription is the description that goes into auto-generated
// security groups.
const securityGroupDescription = "Managed by bastion"
// SecurityGroup describes an AWS VPC security group.
type SecurityGroup struct {
_ struct{}
// true if the security group has been created
Created bool `json:"created"`
// The ID of the security group, generated by AWS on creation.
GroupID string `json:"group_id"`
// The name of the security group. This is automatically generated by
// bastion.
GroupName string `json:"group_name"`
// The ID of the VPC the security group resides in, derived from the public
// subnet supplied to bastion.
VpcID string `json:"vpc_id"`
}
// findVpcIDFromSubnet finds the VPC ID from a supplied subnet ID.
func findVpcIDFromSubnet(conn *ec2.EC2, subnet string) (string, error) {
params := &ec2.DescribeSubnetsInput{
SubnetIds: aws.StringSlice([]string{subnet}),
}
resp, err := conn.DescribeSubnets(params)
if err != nil {
return "", err
}
if len(resp.Subnets) < 1 {
return "", fmt.Errorf("Subnet ID %s not found.", subnet)
}
if len(resp.Subnets) > 1 {
panic(fmt.Errorf("More than subnet found for subnet ID %s", subnet))
}
return *resp.Subnets[0].VpcId, nil
}
// generateSecurityGroupName creates an randomly-generated security group name.
func generateSecurityGroupName() string {
id := fmt.Sprintf("%x", rand.Int())
return securityGroupNamePrefix + id
}
// CreateSecurityGroup creates the security group, and returns a SecurityGroup struct.
//
// Note that in the event of errors, SecurityGroup will be in an inconsistent
// state and should not be used.
func CreateSecurityGroup(conn *ec2.EC2, subnet string) (SecurityGroup, error) {
var group SecurityGroup
name := generateSecurityGroupName()
vpc, err := findVpcIDFromSubnet(conn, subnet)
if err != nil {
return group, err
}
group.GroupName = name
group.VpcID = vpc
params := &ec2.CreateSecurityGroupInput{
Description: aws.String(securityGroupDescription),
GroupName: aws.String(name),
VpcId: aws.String(vpc),
}
resp, err := conn.CreateSecurityGroup(params)
if err != nil {
return group, err
}
group.GroupID = *resp.GroupId
group.Created = true
return group, nil
}
// DeleteSecurityGroup deletes the security group.
func DeleteSecurityGroup(conn *ec2.EC2, group SecurityGroup) (SecurityGroup, error) {
params := &ec2.DeleteSecurityGroupInput{
GroupId: aws.String(group.GroupID),
}
_, err := conn.DeleteSecurityGroup(params)
if err != nil {
return group, err
}
group.Created = false
return group, nil
}