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
YiscahLevySilas1 committed Nov 11, 2021
2 parents 06f74c2 + 10bd6f0 commit 1f9db2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
9 changes: 9 additions & 0 deletions gitregostore/gitstoremethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ func (gs *GitRegoStore) GetOPAControlByID(controlID string) (*opapolicy.Control,
return nil, fmt.Errorf("control '%s' not found", controlID)
}

// GetOPAControl returns specific control by the name or ID
func (gs *GitRegoStore) GetOPAControl(c string) (*opapolicy.Control, error) {
if isControlID(c) {
return gs.GetOPAControlByID(c)
} else {
return gs.GetOPAControlByName(c)
}
}

// GetOPAControls returns all the controls of given customer
func (gs *GitRegoStore) GetOPAControls() ([]opapolicy.Control, error) {
gs.controlsLock.RLock()
Expand Down
15 changes: 8 additions & 7 deletions gitregostore/gitstoremethods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gitregostore

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInitDefaultGitRegoStore(t *testing.T) {
Expand Down Expand Up @@ -112,11 +114,10 @@ func TestGetPoliciesMethods(t *testing.T) {
}
}

func contains(list []string, str string) bool {
for _, a := range list {
if a == str {
return true
}
}
return false
func TestIsControlID(t *testing.T) {
assert.True(t, isControlID("c-0001"))
assert.True(t, isControlID("C-0001"))
assert.False(t, isControlID("c-00012"))
assert.False(t, isControlID("t-0001"))
assert.False(t, isControlID("my name"))
}
8 changes: 8 additions & 0 deletions gitregostore/gitstoreutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -396,3 +397,10 @@ func HTTPRespToString(resp *http.Response) (string, error) {

return respStr, err
}

func isControlID(c string) bool {
if m, err := regexp.MatchString(`^[c|C][\-][0-9]{4}$`, c); err == nil {
return m
}
return false
}

0 comments on commit 1f9db2a

Please sign in to comment.