Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/armosec/opa-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 9, 2021
2 parents 34af37b + 25e7edc commit 4c80eab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
44 changes: 24 additions & 20 deletions gitregostore/gitstoremethods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,30 @@ func TestInitGitRegoStoreFromRelease(t *testing.T) {
// fmt.Println(gs.URL)
}

func TestInitGitRegoStoreFromRepo(t *testing.T) {
// baseUrl := "https://api.github.com/repos"
// owner := "armosec"
// repository := "regolibrary"
// path := "git/trees"
// tag := ""
// branch := "dev"
// frequency := 15
// gs := InitGitRegoStore(baseUrl, owner, repository, path, tag, branch, frequency)
// if gs.Controls == nil {
// t.Errorf("failed to decode controls")
// }
// if gs.Frameworks == nil {
// t.Errorf("failed to decode frameworks")
// }
// if gs.Rules == nil {
// t.Errorf("failed to decode rules")
// }
// fmt.Println(gs.URL)
}
// func TestInitGitRegoStoreFromRepo(t *testing.T) {
// baseUrl := "https://api.github.com/repos"
// owner := "armosec"
// repository := "regolibrary"
// path := "git/trees"
// tag := ""
// branch := "dev"
// frequency := 1
// gs := InitGitRegoStore(baseUrl, owner, repository, path, tag, branch, frequency)
// if gs.Controls == nil {
// t.Errorf("failed to decode controls")
// }
// if gs.Frameworks == nil {
// t.Errorf("failed to decode frameworks")
// }
// if gs.Rules == nil {
// t.Errorf("failed to decode rules")
// }
// fmt.Println(gs.URL)
// time.Sleep(time.Duration(frequency) * time.Minute)
// if len(gs.Frameworks) > 4 {
// t.Errorf("failed to decode controls")
// }
// }

func TestGetPoliciesMethods(t *testing.T) {
gs := InitDefaultGitRegoStore(-1)
Expand Down
30 changes: 19 additions & 11 deletions gitregostore/gitstoreutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ func (gs *GitRegoStore) setObjectsFromRepoLoop() {
wg.Add(1)

go func() {
f := true
for {
if err := gs.setObjectsFromRepoOnce(); err != nil {
fmt.Println(err)
}
wg.Done()
if f {
wg.Done() // first update to done
f = false
}
if !gs.Watch {
return
}
Expand All @@ -95,6 +99,15 @@ func (gs *GitRegoStore) setObjectsFromRepoOnce() error {
if err != nil {
return fmt.Errorf("failed to unmarshal response body from '%s', reason: %s", gs.URL, err.Error())
}
gs.frameworksLock.Lock()
gs.controlsLock.Lock()
gs.rulesLock.Lock()
defer gs.frameworksLock.Unlock()
defer gs.controlsLock.Unlock()
defer gs.rulesLock.Unlock()
gs.Frameworks = []opapolicy.Framework{}
gs.Controls = []opapolicy.Control{}
gs.Rules = []opapolicy.PolicyRule{}

// use only json files from relevant dirs
for _, path := range trees.TREE {
Expand Down Expand Up @@ -157,8 +170,6 @@ func (gs *GitRegoStore) setFramework(respStr string) error {
if err := JSONDecoder(respStr).Decode(framework); err != nil {
return err
}
gs.frameworksLock.Lock()
defer gs.frameworksLock.Unlock()
gs.Frameworks = append(gs.Frameworks, *framework)
return nil
}
Expand All @@ -168,10 +179,7 @@ func (gs *GitRegoStore) setControl(respStr string) error {
if err := JSONDecoder(respStr).Decode(control); err != nil {
return err
}
gs.controlsLock.Lock()
defer gs.controlsLock.Unlock()
gs.Controls = append(gs.Controls, *control)

return nil
}

Expand All @@ -186,11 +194,7 @@ func (gs *GitRegoStore) setRulesWithRawRego(respStr string, path string) error {
return err
}
rule.Rule = respString

gs.rulesLock.Lock()
defer gs.rulesLock.Unlock()
gs.Rules = append(gs.Rules, *rule)

return nil
}

Expand All @@ -200,11 +204,15 @@ func (gs *GitRegoStore) setObjectsFromReleaseLoop() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
f := true
for {
if err := gs.setObjectsFromReleaseOnce(); err != nil {
fmt.Println(err)
}
wg.Done()
if f {
wg.Done() // first update to done
f = false
}
if !gs.Watch {
return
}
Expand Down

0 comments on commit 4c80eab

Please sign in to comment.