Skip to content

Commit

Permalink
Add separate violation/results and reporter objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Willie Sana committed Aug 11, 2020
1 parent dbdbe25 commit 6e25de8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
4 changes: 4 additions & 0 deletions pkg/policy/interface.go
Expand Up @@ -31,3 +31,7 @@ type Engine interface {
GetResults() error
Release() error
}

// EngineFactory creates policy engine instances based on iac/cloud type
type EngineFactory struct {
}
16 changes: 16 additions & 0 deletions pkg/policy/opa/constants.go
@@ -1,3 +1,19 @@
/*
Copyright (C) 2020 Accurics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opa

const (
Expand Down
32 changes: 22 additions & 10 deletions pkg/policy/opa/engine.go
Expand Up @@ -28,13 +28,12 @@ import (
"sort"
"text/template"

"github.com/accurics/terrascan/pkg/utils"
"github.com/accurics/terrascan/pkg/results"

"github.com/accurics/terrascan/pkg/utils"
"github.com/open-policy-agent/opa/ast"

"go.uber.org/zap"

"github.com/open-policy-agent/opa/rego"
"go.uber.org/zap"
)

// LoadRegoMetadata Loads rego metadata from a given file
Expand Down Expand Up @@ -258,15 +257,28 @@ func (e *Engine) Evaluate(inputData *interface{}) error {
}

if len(rs) > 0 {
results := rs[0].Expressions[0].Value.([]interface{})
if len(results) > 0 {
r := e.RegoDataMap[k].Metadata
fmt.Printf("[%s] [%s] [%s] %s: %s\n", r.Severity, r.RuleReferenceID, r.Category, r.RuleName, r.Description)
res := rs[0].Expressions[0].Value.([]interface{})
if len(res) > 0 {
// @TODO: Take line number + file info and add to violation
regoData := e.RegoDataMap[k]
// @TODO: Remove this print, should be done by whomever consumes the results below
fmt.Printf("[%s] [%s] [%s] %s: %s\n", regoData.Metadata.Severity, regoData.Metadata.RuleReferenceID,
regoData.Metadata.Category, regoData.Metadata.RuleName, regoData.Metadata.Description)
violation := results.Violation{
Name: regoData.Metadata.RuleName,
Description: regoData.Metadata.Description,
RuleID: regoData.Metadata.RuleReferenceID,
Category: regoData.Metadata.Category,
RuleData: regoData.RawRego,
InputFile: "",
InputData: res,
LineNumber: 0,
}

e.ViolationStore.AddResult(&violation)
continue
}
}

// Store results
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/policy/opa/types.go
Expand Up @@ -57,6 +57,6 @@ type Engine struct {
Context context.Context
RegoFileMap map[string][]byte
RegoDataMap map[string]*RegoData
ViolationStore *results.Store
ViolationStore *results.ViolationStore
stats EngineStats
}
21 changes: 15 additions & 6 deletions pkg/runtime/executor.go
Expand Up @@ -99,17 +99,26 @@ func (e *Executor) Execute() (normalized interface{}, err error) {
}

// create a new policy engine based on IaC type
var engine policy.Engine

if e.iacType == "terraform" {
var engine policy.Engine = &opa.Engine{}
engine = &opa.Engine{}
}

err = engine.Initialize(e.policyPath)
if err != nil {
return normalized, err
}
if err = engine.Initialize(e.policyPath); err != nil {
return normalized, err
}

engine.Evaluate(&normalized)
if err = engine.Evaluate(&normalized); err != nil {
return normalized, err
}

// var reporter publish.Reporter = console.Reporter
/// if err = reporter.ImportData()
// if err = reporter.Publish() {
//
// }

// send notifications, if configured
if err = e.SendNotifications(normalized); err != nil {
return normalized, err
Expand Down

0 comments on commit 6e25de8

Please sign in to comment.