-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement in printing results for regoval command and re-organizing…
… Rego policies Signed-off-by: Santosh <ksantosh@intelops.dev>
- Loading branch information
1 parent
a4e242c
commit a9d4e75
Showing
19 changed files
with
367 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package validate | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/open-policy-agent/opa/rego" | ||
) | ||
|
||
func FetchRegoMetadata(policyDir, metaExt, regoExt string) ([]string, []string, error) { | ||
var metaFiles []string | ||
var regoFiles []string | ||
|
||
err := filepath.Walk(policyDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
if filepath.Ext(info.Name()) == metaExt { | ||
metaFiles = append(metaFiles, path) | ||
} else if filepath.Ext(info.Name()) == regoExt { | ||
regoFiles = append(regoFiles, path) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if len(regoFiles) == 0 { | ||
return nil, nil, fmt.Errorf("no Rego policy file found in the directory: %s", policyDir) | ||
} | ||
|
||
return metaFiles, regoFiles, err | ||
} | ||
|
||
// LoadRegoMetadata loads the contents of the metadata files into a slice of pointers to RegoMeta structs | ||
func LoadRegoMetadata(filePaths []string) ([]*regoMetadata, error) { | ||
var metas []*regoMetadata | ||
|
||
for _, path := range filePaths { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
var meta regoMetadata | ||
err = json.NewDecoder(file).Decode(&meta) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
metas = append(metas, &meta) | ||
} | ||
|
||
return metas, nil | ||
} | ||
|
||
// MatchPolicyMetadata matches the RegoMeta policy names with the Rego evaluation results and returns the matched key | ||
func MatchPolicyMetadata(metas []*regoMetadata, results rego.ResultSet) (string, *regoMetadata, error) { | ||
for _, r := range results { | ||
if len(r.Expressions) > 0 { | ||
keys := r.Expressions[0].Value.(map[string]interface{}) | ||
for key := range keys { | ||
for _, meta := range metas { | ||
if key == meta.PolicyName { | ||
return key, meta, nil | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return "", nil, fmt.Errorf("no matching policy name found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package validate | ||
|
||
type regoMetadata struct { | ||
Name string `json:"name"` | ||
PolicyName string `json:"policy_name"` | ||
PolicyFile string `json:"policy_file"` | ||
Severity string `json:"severity"` | ||
Description string `json:"description"` | ||
Benchmark string `json:"benchmark"` | ||
Category string `json:"category"` | ||
} | ||
|
||
const ( | ||
metaExt = ".json" | ||
policyExt = ".rego" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.