Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 635818a

Browse files
authored
Merge pull request #4490 from andrestc/ec2-sg-readonly
drivers/amazonec2: adds flag to prevent mutating security groups
2 parents 8941c31 + c6a7c45 commit 635818a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

drivers/amazonec2/amazonec2.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type Driver struct {
8686
SecurityGroupName string
8787
SecurityGroupNames []string
8888

89+
SecurityGroupReadOnly bool
8990
OpenPorts []string
9091
Tags string
9192
ReservationId string
@@ -161,6 +162,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
161162
Usage: "AWS VPC subnet id",
162163
EnvVar: "AWS_SUBNET_ID",
163164
},
165+
mcnflag.BoolFlag{
166+
Name: "amazonec2-security-group-readonly",
167+
Usage: "Skip adding default rules to security groups",
168+
EnvVar: "AWS_SECURITY_GROUP_READONLY",
169+
},
164170
mcnflag.StringSliceFlag{
165171
Name: "amazonec2-security-group",
166172
Usage: "AWS VPC security group",
@@ -348,6 +354,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
348354
d.VpcId = flags.String("amazonec2-vpc-id")
349355
d.SubnetId = flags.String("amazonec2-subnet-id")
350356
d.SecurityGroupNames = flags.StringSlice("amazonec2-security-group")
357+
d.SecurityGroupReadOnly = flags.Bool("amazonec2-security-group-readonly")
351358
d.Tags = flags.String("amazonec2-tags")
352359
zone := flags.String("amazonec2-zone")
353360
d.Zone = zone[:]
@@ -1141,6 +1148,10 @@ func (d *Driver) configureSecurityGroups(groupNames []string) error {
11411148
}
11421149

11431150
func (d *Driver) configureSecurityGroupPermissions(group *ec2.SecurityGroup) ([]*ec2.IpPermission, error) {
1151+
if d.SecurityGroupReadOnly {
1152+
log.Debug("Skipping permission configuration on security groups")
1153+
return nil, nil
1154+
}
11441155
hasPorts := make(map[string]bool)
11451156
for _, p := range group.IpPermissions {
11461157
if p.FromPort != nil {

drivers/amazonec2/amazonec2_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ func TestConfigureSecurityGroupPermissionsDockerAndSsh(t *testing.T) {
9898
assert.Empty(t, perms)
9999
}
100100

101+
func TestConfigureSecurityGroupPermissionsSkipReadOnly(t *testing.T) {
102+
driver := NewTestDriver()
103+
driver.SecurityGroupReadOnly = true
104+
perms, err := driver.configureSecurityGroupPermissions(securityGroup)
105+
106+
assert.Nil(t, err)
107+
assert.Len(t, perms, 0)
108+
}
109+
101110
func TestConfigureSecurityGroupPermissionsOpenPorts(t *testing.T) {
102111
driver := NewTestDriver()
103112
driver.OpenPorts = []string{"8888/tcp", "8080/udp", "9090"}

0 commit comments

Comments
 (0)