From c4e01363e75832170376bde275952a96ea6fa76a Mon Sep 17 00:00:00 2001 From: yiscah Date: Sun, 7 Nov 2021 16:41:27 +0200 Subject: [PATCH] canceled infinite append of opa obj from repo --- gitregostore/gitstoremethods_test.go | 44 +++++++++++++++------------- gitregostore/gitstoreutils.go | 18 ++++++------ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/gitregostore/gitstoremethods_test.go b/gitregostore/gitstoremethods_test.go index b7b18372..ea8b872a 100644 --- a/gitregostore/gitstoremethods_test.go +++ b/gitregostore/gitstoremethods_test.go @@ -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) diff --git a/gitregostore/gitstoreutils.go b/gitregostore/gitstoreutils.go index 702242ff..6d518048 100644 --- a/gitregostore/gitstoreutils.go +++ b/gitregostore/gitstoreutils.go @@ -95,6 +95,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 { @@ -157,8 +166,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 } @@ -168,10 +175,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 } @@ -186,11 +190,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 }