Skip to content

Commit

Permalink
Fix misc linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Willie Sana authored and Yusuf Kanchwala committed Aug 10, 2020
1 parent 6bb3463 commit 647282d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/policy/interface.go
Expand Up @@ -16,12 +16,14 @@

package policy

// Manager Policy Manager interface
type Manager interface {
Import() error
Export() error
CreateManager() error
}

// Engine Policy Engine interface
type Engine interface {
Initialize(policyPath string) error
Configure() error
Expand Down
5 changes: 3 additions & 2 deletions pkg/policy/opa/constants.go
@@ -1,7 +1,8 @@
package opa

const (
// RegoMetadataFileSuffix Suffix for files containing rego metadata
RegoMetadataFileSuffix = ".json"
RegoFileSuffix = ".rego"
RuleQueryBase = "data.accurics"
// RuleQueryBase Default package to query
RuleQueryBase = "data.accurics"
)
17 changes: 16 additions & 1 deletion pkg/policy/opa/engine.go
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/open-policy-agent/opa/rego"
)

// Violation Contains data for each violation
type Violation struct {
Name string
Description string
Expand All @@ -46,12 +47,14 @@ type Violation struct {
RuleData interface{}
}

// ResultData Contains full report data
type ResultData struct {
EngineType string
Provider string
Violations []*Violation
}

// RegoMetadata The rego metadata struct which is read and saved from disk
type RegoMetadata struct {
RuleName string `json:"ruleName"`
File string `json:"file"`
Expand All @@ -64,26 +67,30 @@ type RegoMetadata struct {
Version int `json:"version"`
}

// RegoData Stores all information needed to evaluate and report on a rego rule
type RegoData struct {
Metadata RegoMetadata
RawRego []byte
PreparedQuery *rego.PreparedEvalQuery
}

// EngineStats Contains misc stats
type EngineStats struct {
ruleCount int
regoFileCount int
metadataFileCount int
metadataCount int
}

// Engine Implements the policy engine interface
type Engine struct {
Context context.Context
RegoFileMap map[string][]byte
RegoDataMap map[string]*RegoData
stats EngineStats
}

// LoadRegoMetadata Loads rego metadata from a given file
func (e *Engine) LoadRegoMetadata(metaFilename string) (*RegoMetadata, error) {
// Load metadata file if it exists
metadata, err := ioutil.ReadFile(metaFilename)
Expand All @@ -103,6 +110,7 @@ func (e *Engine) LoadRegoMetadata(metaFilename string) (*RegoMetadata, error) {
return &regoMetadata, err
}

// loadRawRegoFilesIntoMap imports raw rego files into a map
func (e *Engine) loadRawRegoFilesIntoMap(currentDir string, regoDataList []*RegoData, regoFileMap *map[string][]byte) error {
for i := range regoDataList {
regoPath := filepath.Join(currentDir, regoDataList[i].Metadata.File)
Expand All @@ -124,6 +132,7 @@ func (e *Engine) loadRawRegoFilesIntoMap(currentDir string, regoDataList []*Rego
return nil
}

// LoadRegoFiles Loads all related rego files from the given policy path into memory
func (e *Engine) LoadRegoFiles(policyPath string) error {
// Walk the file path and find all directories
dirList, err := utils.FindAllDirectories(policyPath)
Expand All @@ -144,7 +153,8 @@ func (e *Engine) LoadRegoFiles(policyPath string) error {
sort.Strings(dirList)
for i := range dirList {
// Find all files in the current dir
fileInfo, err := ioutil.ReadDir(dirList[i])
var fileInfo []os.FileInfo
fileInfo, err = ioutil.ReadDir(dirList[i])
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
zap.S().Error("error while searching for files", zap.String("dir", dirList[i]))
Expand Down Expand Up @@ -207,6 +217,7 @@ func (e *Engine) LoadRegoFiles(policyPath string) error {
return err
}

// CompileRegoFiles Compiles rego files for faster evaluation
func (e *Engine) CompileRegoFiles() error {
for k := range e.RegoDataMap {
compiler, err := ast.CompileModules(map[string]string{
Expand Down Expand Up @@ -247,18 +258,22 @@ func (e *Engine) Initialize(policyPath string) error {
return nil
}

// Configure Configures the OPA engine
func (e *Engine) Configure() error {
return nil
}

// GetResults Fetches results from OPA engine policy evaluation
func (e *Engine) GetResults() error {
return nil
}

// Release Performs any tasks required to free resources
func (e *Engine) Release() error {
return nil
}

// Evaluate Executes compiled OPA queries against the input JSON data
func (e *Engine) Evaluate(inputData *interface{}) error {

sortedKeys := make([]string, len(e.RegoDataMap))
Expand Down

0 comments on commit 647282d

Please sign in to comment.