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

add global option to skip unassigning security groups matching a pattern #500

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import "strings"

// GlobalConfig configuration for global settings
type GlobalConfig struct {
EnableDeleteIsolationSegments bool `yaml:"enable-delete-isolation-segments"`
EnableUnassignSecurityGroups bool `yaml:"enable-unassign-security-groups"`
RunningSecurityGroups []string `yaml:"running-security-groups"`
StagingSecurityGroups []string `yaml:"staging-security-groups"`
SharedDomains map[string]SharedDomain `yaml:"shared-domains"`
EnableDeleteSharedDomains bool `yaml:"enable-remove-shared-domains"`
MetadataPrefix string `yaml:"metadata-prefix"`
EnableServiceAccess bool `yaml:"enable-service-access"`
IgnoreLegacyServiceAccess bool `yaml:"ignore-legacy-service-access"`
ServiceAccess []*Broker `yaml:"service-access"`
ProtectedUsers []string `yaml:"protected-users"`
EnableDeleteIsolationSegments bool `yaml:"enable-delete-isolation-segments"`
EnableUnassignSecurityGroups bool `yaml:"enable-unassign-security-groups"`
SkipUnassignSecurityGroupRegex string `yaml:"skip-unassign-security-group-regex"`
RunningSecurityGroups []string `yaml:"running-security-groups"`
StagingSecurityGroups []string `yaml:"staging-security-groups"`
SharedDomains map[string]SharedDomain `yaml:"shared-domains"`
EnableDeleteSharedDomains bool `yaml:"enable-remove-shared-domains"`
MetadataPrefix string `yaml:"metadata-prefix"`
EnableServiceAccess bool `yaml:"enable-service-access"`
IgnoreLegacyServiceAccess bool `yaml:"ignore-legacy-service-access"`
ServiceAccess []*Broker `yaml:"service-access"`
ProtectedUsers []string `yaml:"protected-users"`
}

type PlanInfo struct {
Expand Down
10 changes: 10 additions & 0 deletions configcommands/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configcommands
import (
"errors"
"fmt"
"regexp"

"github.com/vmwarepivotallabs/cf-mgmt/config"
)
Expand All @@ -14,6 +15,7 @@ type GlobalConfigurationCommand struct {
EnableDeleteSharedDomains string `long:"enable-delete-shared-domains" description:"Enable removing shared domains" choice:"true" choice:"false"`
EnableServiceAccess string `long:"enable-service-access" description:"Enable managing service access" choice:"true" choice:"false"`
EnableUnassignSecurityGroups string `long:"enable-unassign-security-groups" description:"Enable unassigning security groups" choice:"true" choice:"false"`
SkipUnassignSecurityGroupRegex string `long:"skip-unassign-security-group-regex" description:"Skip unassigning security groups for names matching regex" default:""`
MetadataPrefix string `long:"metadata-prefix" description:"Prefix for org/space metadata"`
StagingSecurityGroups []string `long:"staging-security-group" description:"Staging Security Group to add"`
RemoveStagingSecurityGroups []string `long:"remove-staging-security-group" description:"Staging Security Group to remove"`
Expand Down Expand Up @@ -49,6 +51,14 @@ func (c *GlobalConfigurationCommand) Execute([]string) error {
globalConfig.MetadataPrefix = c.MetadataPrefix
}

if c.SkipUnassignSecurityGroupRegex != "" {
_, err := regexp.Compile(c.SkipUnassignSecurityGroupRegex)
if err != nil {
return fmt.Errorf("Must specify an expresion which compiles")
}
globalConfig.SkipUnassignSecurityGroupRegex = c.SkipUnassignSecurityGroupRegex
}

globalConfig.StagingSecurityGroups = c.updateSecGroups(globalConfig.StagingSecurityGroups, c.StagingSecurityGroups, c.RemoveStagingSecurityGroups)
globalConfig.RunningSecurityGroups = c.updateSecGroups(globalConfig.RunningSecurityGroups, c.RunningSecurityGroups, c.RemoveRunningSecurityGroups)

Expand Down
44 changes: 44 additions & 0 deletions configcommands/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,50 @@ var _ = Describe("Global", func() {
})
})

Context("SecurityGroupNameRegexToIgnore", func() {
It("Should set the regex", func() {
command.SkipUnassignSecurityGroupRegex = "^skip-this-asg-prefix-*$"
err := command.Execute(nil)
Expect(err).ShouldNot(HaveOccurred())
globalConfig, err := configManager.GetGlobalConfig()
Expect(err).ShouldNot(HaveOccurred())
Expect(globalConfig.SkipUnassignSecurityGroupRegex).To(Equal("^skip-this-asg-prefix-*$"))
})
It("Should allow an empty regex", func() {
err := command.Execute(nil)
Expect(err).ShouldNot(HaveOccurred())
globalConfig, err := configManager.GetGlobalConfig()
Expect(err).ShouldNot(HaveOccurred())
Expect(globalConfig.SkipUnassignSecurityGroupRegex).To(Equal(""))
})
It("Should error for invalid regex", func() {
command.SkipUnassignSecurityGroupRegex = "broken-regex(["
err := command.Execute(nil)
Expect(err).Should(HaveOccurred())
globalConfig, err := configManager.GetGlobalConfig()
Expect(err).ShouldNot(HaveOccurred())
Expect(globalConfig.SkipUnassignSecurityGroupRegex).To(Equal(""))
})
It("Should not modify config if argument has been omitted", func() {
// set specific value and save to config file
command.SkipUnassignSecurityGroupRegex = "asg-to-skip-when-unassigning"
err := command.Execute(nil)
Expect(err).ShouldNot(HaveOccurred())
globalConfig, err := configManager.GetGlobalConfig()
Expect(err).ShouldNot(HaveOccurred())
Expect(globalConfig.SkipUnassignSecurityGroupRegex).To(Equal("asg-to-skip-when-unassigning"))

// execute omitting the option does not change the existing value in file
command = &GlobalConfigurationCommand{}
command.ConfigDirectory = configDir
err = command.Execute(nil)
Expect(err).ShouldNot(HaveOccurred())
globalConfig, err = configManager.GetGlobalConfig()
Expect(err).ShouldNot(HaveOccurred())
Expect(globalConfig.SkipUnassignSecurityGroupRegex).To(Equal("asg-to-skip-when-unassigning"))
})
})

Context("MetadataPrefix", func() {
It("Should be unset", func() {
err := command.Execute(nil)
Expand Down
1 change: 1 addition & 0 deletions docs/config/global/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Help Options:
--enable-delete-shared-domains=[true|false] Enable removing shared domains
--enable-service-access=[true|false] Enable managing service access
--enable-unassign-security-groups=[true|false] Enable unassigning security groups
--skip-unassign-security-group-regex= Skip unassigning security groups for names matching regex
--metadata-prefix= Prefix for org/space metadata
--staging-security-group= Staging Security Group to add
--remove-staging-security-group= Staging Security Group to remove
Expand Down
17 changes: 17 additions & 0 deletions securitygroup/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/cloudfoundry-community/go-cfclient/v3/resource"
Expand Down Expand Up @@ -43,6 +44,18 @@ func (m *DefaultManager) CreateApplicationSecurityGroups() error {
return err
}

globalConfig, err := m.Cfg.GetGlobalConfig()
if err != nil {
return errors.Wrapf(err, "Getting global config")
}
var skipUnassignSecurityGroupRegex *regexp.Regexp
if globalConfig.SkipUnassignSecurityGroupRegex != "" {
skipUnassignSecurityGroupRegex, err = regexp.Compile(globalConfig.SkipUnassignSecurityGroupRegex)
if err != nil {
return errors.Wrapf(err, "Compiling regex from gloabl config key skipUnassignSecurityGroupRegex")
}
}

for _, input := range spaceConfigs {
space, err := m.SpaceManager.FindSpace(input.Org, input.Space)
if err != nil {
Expand Down Expand Up @@ -105,6 +118,10 @@ func (m *DefaultManager) CreateApplicationSecurityGroups() error {
lo.G.Debugf("Existing space security groups after %+v", existingSpaceSecurityGroups)
for sgName, _ := range existingSpaceSecurityGroups {
if sgInfo, ok := sgs[sgName]; ok {
if globalConfig.SkipUnassignSecurityGroupRegex != "" && skipUnassignSecurityGroupRegex.MatchString(sgName) {
lo.G.Debugf("Skip unassign as security group name %s matches global config regex", sgName)
continue
}
err := m.UnassignSecurityGroupToSpace(space, sgInfo)
if err != nil {
return err
Expand Down
59 changes: 59 additions & 0 deletions securitygroup/securitygroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ var _ = Describe("given Security Group Manager", func() {
},
},
}, nil)
fakeReader.GetGlobalConfigReturns(&config.GlobalConfig{
SkipUnassignSecurityGroupRegex: "^skip-this-asg-prefix-.*$",
}, nil)
})

It("Should assign global group to space", func() {
Expand Down Expand Up @@ -311,6 +314,62 @@ var _ = Describe("given Security Group Manager", func() {
Expect(fakeClient.UnBindRunningSecurityGroupCallCount()).Should(Equal(0))
})

It("Should not unbind space specific group to space which is ignore-listed", func() {
spaceConfigs := []config.SpaceConfig{
config.SpaceConfig{
EnableSecurityGroup: false,
Space: "space1",
Org: "org1",
ASGs: []string{},
EnableUnassignSecurityGroup: true,
},
}
fakeReader.GetSpaceConfigsReturns(spaceConfigs, nil)
fakeClient.ListAllReturns([]*resource.SecurityGroup{
{
Name: "skip-this-asg-prefix-example",
GUID: "skip-this-asg-prefix-example-guid",
Rules: []resource.SecurityGroupRule{},
Relationships: resource.SecurityGroupsRelationships{
StagingSpaces: resource.ToManyRelationships{
Data: []resource.Relationship{
resource.Relationship{GUID: "space1-guid"},
},
},
RunningSpaces: resource.ToManyRelationships{
Data: []resource.Relationship{
resource.Relationship{GUID: "space1-guid"},
},
},
},
},
}, nil)
fakeClient.ListRunningForSpaceAllReturns([]*resource.SecurityGroup{
{
Name: "skip-this-asg-prefix-example",
GUID: "skip-this-asg-prefix-example-guid",
Rules: []resource.SecurityGroupRule{},
Relationships: resource.SecurityGroupsRelationships{
StagingSpaces: resource.ToManyRelationships{
Data: []resource.Relationship{
resource.Relationship{GUID: "space1-guid"},
},
},
RunningSpaces: resource.ToManyRelationships{
Data: []resource.Relationship{
resource.Relationship{GUID: "space1-guid"},
},
},
},
},
}, nil)
err := securityMgr.CreateApplicationSecurityGroups()
Expect(err).ShouldNot(HaveOccurred())
Expect(fakeClient.ListRunningForSpaceAllCallCount()).Should(Equal(1))
Expect(fakeClient.BindRunningSecurityGroupCallCount()).Should(Equal(0))
Expect(fakeClient.UnBindRunningSecurityGroupCallCount()).Should(Equal(0))
})

It("Should unbind space specific group to space", func() {
spaceConfigs := []config.SpaceConfig{
config.SpaceConfig{
Expand Down