From 10bd6f0ca775e0f990055e4573d3ff2caf2daf90 Mon Sep 17 00:00:00 2001 From: dwertent Date: Thu, 11 Nov 2021 10:32:39 +0200 Subject: [PATCH] support get control --- gitregostore/gitstoremethods.go | 9 +++++++++ gitregostore/gitstoremethods_test.go | 15 ++++++++------- gitregostore/gitstoreutils.go | 8 ++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/gitregostore/gitstoremethods.go b/gitregostore/gitstoremethods.go index a9a29bcf..484e3bb2 100644 --- a/gitregostore/gitstoremethods.go +++ b/gitregostore/gitstoremethods.go @@ -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() diff --git a/gitregostore/gitstoremethods_test.go b/gitregostore/gitstoremethods_test.go index ea8b872a..6e232fc5 100644 --- a/gitregostore/gitstoremethods_test.go +++ b/gitregostore/gitstoremethods_test.go @@ -2,6 +2,8 @@ package gitregostore import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestInitDefaultGitRegoStore(t *testing.T) { @@ -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")) } diff --git a/gitregostore/gitstoreutils.go b/gitregostore/gitstoreutils.go index f3863b41..e978e499 100644 --- a/gitregostore/gitstoreutils.go +++ b/gitregostore/gitstoreutils.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "regexp" "strings" "sync" "time" @@ -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 +}