Skip to content
Snippets Groups Projects
Commit 429b1c57 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

Merge branch '28-bundle-convention-in-sync-app' into 'main'

Policy bundles convention in sync app

Closes #28

See merge request !18
parents bd693c97 bcb474c0
No related branches found
No related tags found
1 merge request!18Policy bundles convention in sync app
Pipeline #51569 passed with stage
in 1 minute and 8 seconds
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
...@@ -24,7 +23,8 @@ import ( ...@@ -24,7 +23,8 @@ import (
const ( const (
repoFolder = "policies" repoFolder = "policies"
policyFileExt = ".rego" policyFilename = "policy.rego"
dataFilename = "data.json"
policyDatabase = "policy" policyDatabase = "policy"
policyCollection = "policies" policyCollection = "policies"
) )
...@@ -36,6 +36,7 @@ type Policy struct { ...@@ -36,6 +36,7 @@ type Policy struct {
Version string Version string
Rego string Rego string
Locked bool Locked bool
Data interface{}
LastUpdate time.Time LastUpdate time.Time
} }
...@@ -138,7 +139,7 @@ func iterateRepo() (map[string]*Policy, error) { ...@@ -138,7 +139,7 @@ func iterateRepo() (map[string]*Policy, error) {
if err != nil { if err != nil {
return err return err
} }
if !d.IsDir() && filepath.Ext(d.Name()) == policyFileExt { if !d.IsDir() && d.Name() == policyFilename {
policy, err := createPolicy(p, d) policy, err := createPolicy(p, d)
if err != nil { if err != nil {
return err return err
...@@ -153,26 +154,37 @@ func iterateRepo() (map[string]*Policy, error) { ...@@ -153,26 +154,37 @@ func iterateRepo() (map[string]*Policy, error) {
// createPolicy instantiates a Policy struct out of a policy file on given path // createPolicy instantiates a Policy struct out of a policy file on given path
func createPolicy(p string, d os.DirEntry) (*Policy, error) { func createPolicy(p string, d os.DirEntry) (*Policy, error) {
filename := d.Name() // path to Rego policy must be {group}/{name}/{version}/policy.rego
group := path.Base(path.Dir(p)) // strings.Split on the path give us an array containing at least group, name, version and filename
ss := strings.Split(strings.TrimSuffix(filename, policyFileExt), "_") ss := strings.Split(p, "/")
if len(ss) < 2 { if len(ss) < 4 {
return nil, fmt.Errorf("failed to get policy name and version out of policy filename: %s", filename) return nil, fmt.Errorf("failed to get policy filename, name, version and group out of policy path: %s", p)
} }
name := ss[0]
version := ss[1] filename := ss[len(ss)-1] // last element in the array is filename
version := ss[len(ss)-2] // second last element is the version
name := ss[len(ss)-3] // third last element is the policy name
group := ss[len(ss)-4] // fourth last element is the policy group
bytes, err := os.ReadFile(p) bytes, err := os.ReadFile(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
regoSrc := string(bytes) regoSrc := string(bytes)
// check if there is a data.json file in the same folder as the policy
dataBytes, err := os.ReadFile(strings.TrimSuffix(p, policyFilename) + dataFilename)
if err != nil && !strings.Contains(err.Error(), "no such file or directory") {
return nil, err
}
data := string(dataBytes)
return &Policy{ return &Policy{
Filename: filename, Filename: filename,
Name: name, Name: name,
Group: group, Group: group,
Version: version, Version: version,
Rego: regoSrc, Rego: regoSrc,
Data: data,
Locked: false, Locked: false,
}, nil }, nil
} }
...@@ -264,6 +276,7 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error ...@@ -264,6 +276,7 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error
"filename": policy.Filename, "filename": policy.Filename,
"locked": policy.Locked, "locked": policy.Locked,
"rego": policy.Rego, "rego": policy.Rego,
"data": policy.Data,
"lastUpdate": time.Now(), "lastUpdate": time.Now(),
}, },
}) })
...@@ -281,6 +294,7 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error ...@@ -281,6 +294,7 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error
func (p1 *Policy) equals(p2 *Policy) bool { func (p1 *Policy) equals(p2 *Policy) bool {
if p1.Rego == p2.Rego && if p1.Rego == p2.Rego &&
p1.Data == p2.Data &&
p1.Name == p2.Name && p1.Name == p2.Name &&
p1.Version == p2.Version && p1.Version == p2.Version &&
p1.Filename == p2.Filename && p1.Filename == p2.Filename &&
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment