diff --git a/gitregostore/gitstoremethods.go b/gitregostore/gitstoremethods.go index 31e14fb2..a9a29bcf 100644 --- a/gitregostore/gitstoremethods.go +++ b/gitregostore/gitstoremethods.go @@ -152,6 +152,32 @@ func (gs *GitRegoStore) fillControlsAndControlIDsInFramework(fw *opapolicy.Frame return nil } +// GetOpaFrameworkListByControlName return a list of fw names this control is in +func (gs *GitRegoStore) GetOpaFrameworkListByControlName(controlName string) []string { + var frameworksNameList []string + fil := gs.FrameworkControlRelations.Filter( + dataframe.F{Colname: "ControlName", Comparator: series.Eq, Comparando: controlName}, + ) + for row := 0; row < fil.Nrow(); row++ { + fwName := fil.Elem(row, 0) + frameworksNameList = append(frameworksNameList, fwName.String()) + } + return frameworksNameList +} + +// GetOpaFrameworkListByControlID return a list of fw names this control is in +func (gs *GitRegoStore) GetOpaFrameworkListByControlID(controlID string) []string { + var frameworksNameList []string + fil := gs.FrameworkControlRelations.Filter( + dataframe.F{Colname: "ControlID", Comparator: series.Eq, Comparando: controlID}, + ) + for row := 0; row < fil.Nrow(); row++ { + fwName := fil.Elem(row, 0) + frameworksNameList = append(frameworksNameList, fwName.String()) + } + return frameworksNameList +} + // GetOPAFrameworks returns all the frameworks of given customer func (gs *GitRegoStore) GetOPAFrameworks() ([]opapolicy.Framework, error) { gs.frameworksLock.RLock() diff --git a/gitregostore/gitstoremethods_test.go b/gitregostore/gitstoremethods_test.go index 65d362c1..07ad3cb7 100644 --- a/gitregostore/gitstoremethods_test.go +++ b/gitregostore/gitstoremethods_test.go @@ -103,9 +103,25 @@ func TestGetPoliciesMethods(t *testing.T) { if err != nil || len(frameworksNames) == 0 { t.Errorf("failed to get frameworks names list %s", err.Error()) } - framework, err := gs.GetOPAFrameworkByName(frameworksNames[0]) if err != nil || framework == nil { t.Errorf("failed to get framework by name: '%s', %s", frameworksNames[0], err.Error()) } + frameworksNames = gs.GetOpaFrameworkListByControlName("Exec into container") + if len(frameworksNames) != 2 || !contains(frameworksNames, "NSA") || !contains(frameworksNames, "MITRE") { + t.Errorf("error in GetOpaFrameworkListByControlName, got wrong list for control 'Exec into container'") + } + frameworksNames = gs.GetOpaFrameworkListByControlID("C-0058") + if len(frameworksNames) != 2 || !contains(frameworksNames, "NSA") || !contains(frameworksNames, "MITRE") { + t.Errorf("error in GetOpaFrameworkListByControlID, got wrong list for control 'C-0058'") + } +} + +func contains(list []string, str string) bool { + for _, a := range list { + if a == str { + return true + } + } + return false }