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

Orchestrator - Discovery + Run scan job #3

Merged
merged 23 commits into from
Nov 20, 2022
30 changes: 21 additions & 9 deletions runtime_scan/pkg/provider/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,30 @@ func (c *Client) ListAllRegions(ctx context.Context) ([]Region, error) {
return ret, nil
}

// AND logic - if excludeTags = {tag1:val1, tag2:val2},
// then instance will be excluded only if he have ALL this tags ({tag1:val1, tag2:val2})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// then an instance will be excluded only if it has ALL these tags ({tag1:val1, tag2:val2})

func hasExcludeTags(excludeTags []Tag, instanceTags []ec2types.Tag) bool {
var excludedTagsMap = make(map[string]string)
var instanceTagsMap = make(map[string]string)

for _, tag := range excludeTags {
excludedTagsMap[tag.key] = tag.val
if len(excludeTags) == 0 {
return false
}
for _, instanceTag := range instanceTags {
if val, ok := excludedTagsMap[*instanceTag.Key]; ok {
if strings.Compare(val, *instanceTag.Value) == 0 {
return true
}
if len(instanceTags) == 0 {
return false
}

for _, tag := range instanceTags {
instanceTagsMap[*tag.Key] = *tag.Value
}

for _, tag := range excludeTags {
val, ok := instanceTagsMap[tag.key]
if !ok {
return false
}
if !(strings.Compare(val, tag.val) == 0) {
return false
}
}
return false
return true
}
34 changes: 32 additions & 2 deletions runtime_scan/pkg/provider/aws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func Test_hasExcludedTags(t *testing.T) {
want: false,
},
{
name: "instance has excluded tags",
name: "instance does not have ALL the excluded tags",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth also adding a test where one of the tags in the excluded is not matched (case of partial matching)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also this test- instance does not have ALL the excluded tags

args: args{
excludeTags: []Tag{
{
Expand All @@ -337,10 +337,40 @@ func Test_hasExcludedTags(t *testing.T) {
},
},
},
want: false,
},
{
name: "instance has ALL excluded tags",
args: args{
excludeTags: []Tag{
{
key: tagName1,
val: tagVal1,
},
{
key: tagName2,
val: tagVal2,
},
},
instanceTags: []ec2types.Tag{
{
Key: &tagName1,
Value: &tagVal1,
},
{
Key: &tagName2,
Value: &tagVal2,
},
{
Key: utils.StringPtr("stam"),
Value: utils.StringPtr("stam"),
},
},
},
want: true,
},
{
name: "instance does not have excluded tags",
name: "instance does not have excluded tags at all",
args: args{
excludeTags: []Tag{
{
Expand Down
2 changes: 1 addition & 1 deletion runtime_scan/pkg/provider/aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ScanScope struct {
// Only targets that have these tags will be selected for scanning within the selected scan scope.
// Multiple tags will be treated as an AND operator.
TagSelector []Tag
// Targets that have these tags will be excluded from the scan, even match the tag selector.
// Targets that have these tags will be excluded from the scan, even if they match the tag selector.
// Multiple tags will be treated as an AND operator.
ExcludeTags []Tag
}
Expand Down