Merge pull request #986 from dwertent/master

Cosmetic changes
This commit is contained in:
David Wertenteil
2022-12-20 10:33:21 +02:00
committed by GitHub
8 changed files with 68 additions and 29 deletions

View File

@@ -60,9 +60,11 @@ func (policyHandler *PolicyHandler) getScanPolicies(policyIdentifier []cautils.P
if err != nil {
return frameworks, policyDownloadError(err)
}
if err := validateFramework(receivedFramework); err != nil {
return frameworks, err
}
if receivedFramework != nil {
frameworks = append(frameworks, *receivedFramework)
cache := getter.GetDefaultPath(rule.Name + ".json")
if err := getter.SaveInFile(receivedFramework, cache); err != nil {
logger.L().Warning("failed to cache file", helpers.String("file", cache), helpers.Error(err))

View File

@@ -1,23 +0,0 @@
package policyhandler
// func TestGetPoliciesFromBackend(t *testing.T) {
// notification := reporthandling.PolicyNotification{
// Rules: []reporthandling.PolicyIdentifier{
// {
// Kind: reporthandling.KindFramework,
// Name: "mitretest",
// },
// },
// }
// // os.Setenv(cacli., "")
// ph := PolicyHandler{
// cacli: &cacli.Cacli{},
// }
// f, err := ph.GetPoliciesFromBackend(&notification)
// if err != nil {
// t.Error(err)
// }
// if len(f) == 0 {
// t.Errorf("empty")
// }
// }

View File

@@ -5,6 +5,7 @@ import (
"strings"
apisv1 "github.com/kubescape/opa-utils/httpserver/apis/v1"
"github.com/kubescape/opa-utils/reporthandling"
"github.com/kubescape/kubescape/v2/core/cautils"
)
@@ -21,3 +22,16 @@ func policyDownloadError(err error) error {
}
return err
}
// validate the framework
func validateFramework(framework *reporthandling.Framework) error {
if framework == nil {
return fmt.Errorf("received empty framework")
}
// validate the controls are not empty
if len(framework.Controls) == 0 {
return fmt.Errorf("failed to load controls for framework: %s: empty list of controls", framework.Name)
}
return nil
}

View File

@@ -0,0 +1,48 @@
package policyhandler
import (
"testing"
"github.com/kubescape/opa-utils/reporthandling"
)
func Test_validateFramework(t *testing.T) {
type args struct {
framework *reporthandling.Framework
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty framework",
args: args{
framework: &reporthandling.Framework{
Controls: []reporthandling.Control{},
},
},
wantErr: true,
},
{
name: "none empty framework",
args: args{
framework: &reporthandling.Framework{
Controls: []reporthandling.Control{
{
ControlID: "c-0001",
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := validateFramework(tt.args.framework); (err != nil) != tt.wantErr {
t.Errorf("validateControls() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -88,7 +88,6 @@ func (k8sHandler *K8sResourceHandler) GetResources(sessionObj *cautils.OPASessio
logger.L().Info("Requesting images vulnerabilities results")
cautils.StartSpinner()
if err := k8sHandler.registryAdaptors.collectImagesVulnerabilities(k8sResourcesMap, allResources, ksResourceMap); err != nil {
logger.L().Warning("failed to collect image vulnerabilities", helpers.Error(err), helpers.String("Read more here", "https://hub.armosec.io/docs/configuration-of-image-vulnerabilities"))
cautils.SetInfoMapForResources(fmt.Sprintf("failed to pull image scanning data: %s. for more information: https://hub.armosec.io/docs/configuration-of-image-vulnerabilities", err.Error()), imgVulnResources, sessionObj.InfoMap)
} else {
if isEmptyImgVulns(*ksResourceMap) {

View File

@@ -142,7 +142,7 @@ func (report *ReportEventReceiver) setResults(reportObj *reporthandlingv2.Postur
// set result.RawResource
resourceID := v.GetResourceID()
if _, ok := allResources[resourceID]; !ok {
return fmt.Errorf("expected to find raw resource object for '%s'", resourceID)
continue
}
resource := reporthandling.NewResourceIMetadata(allResources[resourceID])
if r, ok := resourcesSource[resourceID]; ok {