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 support for security group filter type #15

Merged
merged 7 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syncInterval: 10s
filter:
filterType: LoadBalancer
labelName: whitelister
labelValue: true
ipProviders:
Expand Down
19 changes: 19 additions & 0 deletions configs/testConfigs/configWithIncorrectFilterType.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syncInterval: 10s
filter:
filterType: InCorrectType
labelName: whitelister
labelValue: true
ipProviders:
- name: kubernetes
params:
FromPort: 0
ToPort: 65535
IpProtocol: tcp
provider:
name: aws
params:
KeepRuleDescriptionPrefix: "DO NOT REMOVE -"
RemoveRule: true
RoleArn: "arn:aws:iam::111111111111:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
Region: us-west-2

1 change: 1 addition & 0 deletions configs/testConfigs/correctAwsGitConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syncInterval: 10s
filter:
filterType: LoadBalancer
labelName: whitelister
labelValue: true
ipProviders:
Expand Down
19 changes: 19 additions & 0 deletions configs/testConfigs/correctAwsGitConfigWithSG.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syncInterval: 10s
filter:
filterType: SecurityGroup
labelName: whitelister
labelValue: true
ipProviders:
- name: git
params:
AccessToken: "access-token"
URL: "http://github.com/stakater/whitelister-config.git"
Config: "config.yaml"
provider:
name: aws
params:
KeepRuleDescriptionPrefix: "DO NOT REMOVE -"
RemoveRule: true
RoleArn: "arn:aws:iam::111111111111:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
Region: us-west-2

1 change: 1 addition & 0 deletions configs/testConfigs/correctAwsKubernetesConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syncInterval: 10s
filter:
filterType: LoadBalancer
labelName: whitelister
labelValue: true
ipProviders:
Expand Down
1 change: 1 addition & 0 deletions configs/testConfigs/noIpProviderConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
syncInterval: 10s
filter:
filterType: LoadBalancer
labelName: whitelister
labelValue: true
1 change: 1 addition & 0 deletions configs/testConfigs/noProviderConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syncInterval: 10s
filter:
filterType: LoadBalancer
labelName: whitelister
labelValue: true
ipProviders:
Expand Down
13 changes: 7 additions & 6 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

// Config which would be read from the config.yaml
type Config struct {
SyncInterval string `yaml:"syncInterval"`
IpProviders []IpProvider `yaml:"ipProviders"`
Provider Provider `yaml:"provider"`
Filter Filter `yaml:"filter"`
SyncInterval string `yaml:"syncInterval"`
IpProviders []IpProvider `yaml:"ipProviders"`
Provider Provider `yaml:"provider"`
Filter Filter `yaml:"filter"`
}

// IpProvider that the controller will be using to gather whitelist IPs
Expand All @@ -30,8 +30,9 @@ type Provider struct {

// Filter that will be used to filter resources on the provider
type Filter struct {
LabelName string `yaml:"labelName"`
LabelValue string `yaml:"labelValue"`
FilterType FilterType `yaml:"filterType"`
LabelName string `yaml:"labelName"`
LabelValue string `yaml:"labelValue"`
}

// ReadConfig function that reads the yaml file
Expand Down
66 changes: 58 additions & 8 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"reflect"
"testing"
)
Expand All @@ -14,10 +15,11 @@ func TestReadConfig(t *testing.T) {
filePath string
}
tests := []struct {
name string
args args
want Config
wantErr bool
name string
args args
want Config
wantErr bool
errValue error
}{
{
name: "TestingWithCorrectValues",
Expand All @@ -44,15 +46,56 @@ func TestReadConfig(t *testing.T) {
},
},
Filter: Filter{
FilterType: LoadBalancer,
LabelName: "whitelister",
LabelValue: "true",
},
},
wantErr: false,
},
{
name: "TestingWithEmptyFile",
args: args{filePath: configFilePath + "Empty.yaml"},
want: Config{},
name: "TestingWithCorrectValuesForSecurityGroupFilter",
args: args{filePath: configFilePath + "correctAwsGitConfigWithSG.yaml"},
want: Config{
SyncInterval: "10s",
IpProviders: []IpProvider{
{
Name: "git",
Params: map[interface{}]interface{}{
"AccessToken": "access-token",
"URL": "http://github.com/stakater/whitelister-config.git",
"Config": "config.yaml",
},
},
},
Provider: Provider{
Name: "aws",
Params: map[interface{}]interface{}{
"RoleArn": "arn:aws:iam::111111111111:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",
"Region": "us-west-2",
"RemoveRule": true,
"KeepRuleDescriptionPrefix": "DO NOT REMOVE -",
},
},
Filter: Filter{
FilterType: SecurityGroup,
LabelName: "whitelister",
LabelValue: "true",
},
},
wantErr: false,
},
{
name: "TestingWithIncorrectFilterType",
args: args{filePath: configFilePath + "configWithIncorrectFilterType.yaml"},
wantErr: true,
errValue: errors.New("incorrect FilterType :InCorrectType provided"),
},
{
name: "TestingWithEmptyFile",
args: args{filePath: configFilePath + "Empty.yaml"},
want: Config{},
wantErr: false,
},
{
name: "TestingWithFileNotPresent",
Expand All @@ -63,10 +106,17 @@ func TestReadConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReadConfig(tt.args.filePath)
if (err != nil) != tt.wantErr {
if (err == nil && tt.wantErr) || (!tt.wantErr && err != nil) {
t.Errorf("ReadConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil && tt.wantErr && tt.errValue != nil {
fahad-rauf marked this conversation as resolved.
Show resolved Hide resolved
if err.Error() != tt.errValue.Error() {
t.Errorf("ReadConfig() error %v, wantErr %v", err, tt.errValue)
return
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadConfig() = %v, want %v", got, tt.want)
}
Expand Down
56 changes: 56 additions & 0 deletions internal/pkg/config/filtertype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package config

import "fmt"

type FilterType int

const (
LoadBalancer FilterType = iota
SecurityGroup
)

var loadBalancerStr = "LoadBalancer"
var securityGroupStr = "SecurityGroup"

func (filterType FilterType) String() string {
filterTypes := [...]string{
loadBalancerStr,
securityGroupStr,
}

if filterType < LoadBalancer || filterType > SecurityGroup {
return "Unknown"
}

return filterTypes[filterType]
}

func toFilterType(filterTypeStr string) (filterType FilterType, err error) {
switch filterTypeStr {
case loadBalancerStr:
filterType = LoadBalancer

case securityGroupStr:
filterType = SecurityGroup

default:
err = fmt.Errorf("incorrect FilterType :%s provided", filterTypeStr)
}
return
}

func (filterType *FilterType) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string

if err := unmarshal(&value); err != nil {
return err
}

FilterType, err := toFilterType(value)
if err != nil {
return err
}
*filterType = FilterType

return nil
}
2 changes: 1 addition & 1 deletion internal/pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewController(clientset clientset.Interface, config config.Config) (*Contro
if len(controller.ipProviders) == 0 {
return nil, errors.New("No Ip Provider specified")
}
controller.provider = providers.PopulateFromConfig(config.Provider)
controller.provider = providers.PopulateFromConfig(config.Provider, clientset)
if controller.provider == nil {
return nil, errors.New("No Provider specified")
}
Expand Down
Loading