Skip to content

Commit

Permalink
add default init for rego stroe & get control by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-GrunbergerCA committed Oct 20, 2021
1 parent bf0576a commit ce3be1b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gitregostore/datastructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ func InitGitRegoStore(baseUrl string, owner string, repository string, path stri
gs.setObjects()
return gs
}

func InitDefaultGitRegoStore() *GitRegoStore {
return InitGitRegoStore("https://api.github.com/repos", "armosec", "regolibrary", "releases", "latest", "master", 1)
}
27 changes: 27 additions & 0 deletions gitregostore/gitstoremethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func (gs *GitRegoStore) GetOPAControlByName(controlName string) (*opapolicy.Cont
return nil, fmt.Errorf("control '%s' not found", controlName)
}

// GetOPAControlByID returns specific control by the ID
func (gs *GitRegoStore) GetOPAControlByID(controlID string) (*opapolicy.Control, error) {
gs.controlsLock.RLock()
defer gs.controlsLock.RUnlock()
for _, control := range gs.Controls {
if control.ControlID == controlID {
err := gs.fillRulesAndRulesIDsInControl(&control)
if err != nil {
return nil, err
}
return &control, nil
}
}
return nil, fmt.Errorf("control '%s' not found", controlID)
}

// GetOPAControls returns all the controls of given customer
func (gs *GitRegoStore) GetOPAControls() ([]opapolicy.Control, error) {
gs.controlsLock.RLock()
Expand All @@ -113,6 +129,17 @@ func (gs *GitRegoStore) GetOPAControlsNamesList() ([]string, error) {
return controlsNameList, nil
}

func (gs *GitRegoStore) GetOPAControlsIDsList() ([]string, error) {
fmt.Printf("in GetOPAControlsNamesList")
gs.controlsLock.RLock()
defer gs.controlsLock.RUnlock()
var controlsIDList []string
for _, control := range gs.Controls {
controlsIDList = append(controlsIDList, control.ControlID)
}
return controlsIDList, nil
}

// GetOPAControl returns specific policy
func (gs *GitRegoStore) GetOPAControl(policyGUID string) (*opapolicy.Control, error) {
gs.controlsLock.RLock()
Expand Down
17 changes: 17 additions & 0 deletions gitregostore/gitstoremethods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"testing"
)

func TestInitDefaultGitRegoStore(t *testing.T) {

gs := InitDefaultGitRegoStore()
if gs.Rules == nil {
t.Errorf("failed to decode")
}
}

func TestInitGitRegoStoreFromRelease(t *testing.T) {
baseUrl := "https://api.github.com/repos"
owner := "armosec"
Expand Down Expand Up @@ -84,6 +92,15 @@ func TestGetPoliciesMethods(t *testing.T) {
if err != nil || control == nil {
t.Errorf("failed to get control by name: '%s', %s", controlsNames[0], err.Error())
}
controlsIDs, err := gs.GetOPAControlsIDsList()
if err != nil || len(controlsIDs) == 0 {
t.Errorf("failed to get controls ids list %s", err.Error())
}

control, err = gs.GetOPAControlByID(controlsIDs[0])
if err != nil || control == nil {
t.Errorf("failed to get control by ID: '%s', %s", controlsNames[0], err.Error())
}
// Frameworks
frameworks, err := gs.GetOPAFrameworks()
if err != nil || frameworks == nil {
Expand Down

0 comments on commit ce3be1b

Please sign in to comment.