Skip to content

Commit

Permalink
Counting generic queries with aggregation value on metadata (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriopeixotocx authored Feb 24, 2021
1 parent 858c4c9 commit 12baa85
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "A sensitive port, such as port 23 or port 110, is open for the whole network in either TCP or UDP protocol",
"descriptionUrl": "https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_securitygroup_module.html#parameter-rules",
"platform": "Ansible"
"platform": "Ansible",
"aggregation": 35
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Cx

import data.generic.ansible as lib

getFieldName(field) = name {
upper(field) == "NETWORK PORTS SECURITY"
name := "azure_rm_securitygroup"
Expand Down Expand Up @@ -110,7 +112,7 @@ CxPolicy[result] {

############# document and resource
document := input.document[i]
tasks := getTasks(document)
tasks := lib.getTasks(document)
task := tasks[t]

resource := task[field].rules[r]
Expand Down Expand Up @@ -139,11 +141,3 @@ CxPolicy[result] {
"keyActualValue": sprintf("%s (%s:%d) is allowed in %s.%s.rules", [portName, protocol, portNumber, ruleName, field]),
}
}

getTasks(document) = result {
result := [body | playbook := document.playbooks[0]; body := playbook.tasks]
count(result) != 0
} else = result {
result := [body | playbook := document.playbooks[_]; body := playbook]
count(result) != 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "The EC2 instance has a sensitive port connection exposed to the entire network",
"descriptionUrl": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html",
"platform": "CloudFormation"
"platform": "CloudFormation",
"aggregation": 61
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "The load balancer of the application with a sensitive port connection is exposed to the entire internet.",
"descriptionUrl": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-elb.html",
"platform": "CloudFormation"
"platform": "CloudFormation",
"aggregation": 183
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "A sensitive port, such as port 23 or port 110, is open for the whole network in either TCP or UDP protocol",
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group",
"platform": "Terraform"
"platform": "Terraform",
"aggregation": 48
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "A sensitive port, such as port 23 or port 110, is open for a small public network in either TCP or UDP protocol",
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group",
"platform": "Terraform"
"platform": "Terraform",
"aggregation": 48
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "A sensitive port, such as port 23 or port 110, is open for the whole network in either TCP or UDP protocol",
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule",
"platform": "Terraform"
"platform": "Terraform",
"aggregation": 48
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": "Network Ports Security",
"descriptionText": "A sensitive port, such as port 23 or port 110, is open for small public network in either TCP or UDP protocol",
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule",
"platform": "Terraform"
"platform": "Terraform",
"aggregation": 48
}
8 changes: 4 additions & 4 deletions internal/tracker/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ type CITracker struct {
}

// TrackQueryLoad adds a loaded query
func (c *CITracker) TrackQueryLoad() {
c.LoadedQueries++
func (c *CITracker) TrackQueryLoad(queryAggregation int) {
c.LoadedQueries += queryAggregation
}

// TrackQueryExecution adds a query executed
func (c *CITracker) TrackQueryExecution() {
c.ExecutedQueries++
func (c *CITracker) TrackQueryExecution(queryAggregation int) {
c.ExecutedQueries += queryAggregation
}

// TrackFileFound adds a found file to be scanned
Expand Down
4 changes: 2 additions & 2 deletions internal/tracker/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func TestCITracker(t *testing.T) {
FailedSimilarityID: tt.fields.FailedSimilarityID,
}
t.Run(fmt.Sprintf(tt.name+"_LoadedQueries"), func(t *testing.T) {
c.TrackQueryLoad()
c.TrackQueryLoad(1)
require.Equal(t, 1, c.LoadedQueries)
})

t.Run(fmt.Sprintf(tt.name+"_TrackQueryExecution"), func(t *testing.T) {
c.TrackQueryExecution()
c.TrackQueryExecution(1)
require.Equal(t, 1, c.ExecutedQueries)
})

Expand Down
20 changes: 15 additions & 5 deletions pkg/engine/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type QueriesSource interface {
// TrackQueryExecution increments the number of queries executed
// FailedDetectLine decrements the number of queries executed
type Tracker interface {
TrackQueryLoad()
TrackQueryExecution()
TrackQueryLoad(queryAggregation int)
TrackQueryExecution(queryAggregation int)
FailedDetectLine()
FailedComputeSimilarityID()
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func NewInspector(
continue
}

tracker.TrackQueryLoad()
tracker.TrackQueryLoad(metadata.Aggregation)

opaQueries = append(opaQueries, &preparedQuery{
opaQuery: opaQuery,
Expand All @@ -155,8 +155,10 @@ func NewInspector(
}
failedQueries := make(map[string]error)

queriesNumber := sumAllAggregatedQueries(opaQueries)

log.Info().
Msgf("Inspector initialized, number of queries=%d\n", len(opaQueries))
Msgf("Inspector initialized, number of queries=%d\n", queriesNumber)

return &Inspector{
queries: opaQueries,
Expand All @@ -167,6 +169,14 @@ func NewInspector(
}, nil
}

func sumAllAggregatedQueries(opaQueries []*preparedQuery) int {
sum := 0
for _, query := range opaQueries {
sum += query.metadata.Aggregation
}
return sum
}

func startProgressBar(hideProgress bool, total int, wg *sync.WaitGroup, progressChannel chan float64) {
wg.Add(1)
progressBar := consoleHelpers.NewProgressBar("Executing queries: ", 10, float64(total), progressChannel)
Expand Down Expand Up @@ -222,7 +232,7 @@ func (c *Inspector) Inspect(

vulnerabilities = append(vulnerabilities, vuls...)

c.tracker.TrackQueryExecution()
c.tracker.TrackQueryExecution(query.metadata.Aggregation)
}
close(currentQuery)
wg.Wait()
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func TestNewInspector(t *testing.T) { // nolint
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket#acl",
"platform": "CloudFormation",
},
Aggregation: 1,
},
})
type args struct {
Expand Down
14 changes: 10 additions & 4 deletions pkg/engine/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,17 @@ func ReadQuery(queryDir string) (model.QueryMetadata, error) {
metadata := ReadMetadata(queryDir)
platform := getPlatform(queryDir)

aggregation := 1
if agg, ok := metadata["aggregation"]; ok {
aggregation = int(agg.(float64))
}

return model.QueryMetadata{
Query: path.Base(filepath.ToSlash(queryDir)),
Content: string(queryContent),
Metadata: metadata,
Platform: platform,
Query: path.Base(filepath.ToSlash(queryDir)),
Content: string(queryContent),
Metadata: metadata,
Platform: platform,
Aggregation: aggregation,
}, nil
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/engine/query/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func TestFilesystemSource_GetQueries(t *testing.T) {
"severity": "HIGH",
"platform": "CloudFormation",
},
Platform: "unknown",
Platform: "unknown",
Aggregation: 1,
},
},
wantErr: false,
Expand All @@ -218,7 +219,12 @@ func TestFilesystemSource_GetQueries(t *testing.T) {
t.Errorf("FilesystemSource.GetQueries() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.Equal(t, tt.want, got)
wantStr, err := test.StringifyStruct(tt.want)
require.Nil(t, err)
gotStr, err := test.StringifyStruct(got)
require.Nil(t, err)

require.Equal(t, tt.want, got, "want = %s\ngot = %s", wantStr, gotStr)
})
}
}
Expand Down Expand Up @@ -256,13 +262,18 @@ func Test_ReadMetadata(t *testing.T) {
"queryName": "<QUERY_NAME>",
"severity": "HIGH",
"platform": "<PLATFORM>",
"aggregation": float64(1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReadMetadata(tt.args.queryDir); !reflect.DeepEqual(got, tt.want) {
t.Errorf("readMetadata() = %v, want %v", got, tt.want)
gotStr, err := test.StringifyStruct(got)
require.Nil(t, err)
wantStr, err := test.StringifyStruct(tt.want)
require.Nil(t, err)
t.Errorf("readMetadata()\ngot = %v\nwant = %v", gotStr, wantStr)
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type QueryMetadata struct {
Content string
Metadata map[string]interface{}
Platform string
// special field for generic queries
// represents how many queries are aggregated into a single rego file
Aggregation int
}

// Vulnerability is a representation of a detected vulnerability in scanned files
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/type-test01/template01/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"category": null,
"descriptionText": "<TEXT>",
"descriptionUrl": "#",
"platform": "<PLATFORM>"
"platform": "<PLATFORM>",
"aggregation": 1
}

0 comments on commit 12baa85

Please sign in to comment.