Skip to content

Commit

Permalink
added methods to get fw names list per control
Browse files Browse the repository at this point in the history
  • Loading branch information
YiscahLevySilas1 committed Oct 21, 2021
1 parent 83fb4bc commit 0af3cef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
26 changes: 26 additions & 0 deletions gitregostore/gitstoremethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 17 additions & 1 deletion gitregostore/gitstoremethods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0af3cef

Please sign in to comment.