From ec4a098b1cf678693ee556a0ea50eb6408226173 Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Wed, 15 Mar 2023 17:17:29 +0200 Subject: [PATCH] replace error by warning Signed-off-by: David Wertenteil --- core/cautils/fileutils.go | 9 ++-- core/core/download.go | 2 +- core/core/fix.go | 2 +- core/core/submit.go | 2 +- core/pkg/fixhandler/fixhandler.go | 6 +-- core/pkg/fixhandler/yamlhelper.go | 2 +- core/pkg/hostsensorutils/hostsensordeploy.go | 5 +- .../hostsensorutils/hostsensorgetfrompod.go | 2 +- .../hostsensorutils/hostsensorworkerpool.go | 6 +-- core/pkg/opaprocessor/processorhandler.go | 54 +++++++++---------- core/pkg/policyhandler/handlepullpolicies.go | 4 +- core/pkg/resourcehandler/k8sresources.go | 2 +- .../resultshandling/printer/printresults.go | 4 +- .../resultshandling/printer/v2/htmlprinter.go | 4 +- .../resultshandling/printer/v2/jsonprinter.go | 4 +- core/pkg/resultshandling/printer/v2/junit.go | 4 +- core/pkg/resultshandling/printer/v2/pdf.go | 4 +- .../resultshandling/printer/v2/prometheus.go | 4 +- core/pkg/resultshandling/results.go | 2 +- .../handlerequests/v1/requestshandlerutils.go | 2 +- 20 files changed, 62 insertions(+), 62 deletions(-) diff --git a/core/cautils/fileutils.go b/core/cautils/fileutils.go index 99c05de7..a2be8af9 100644 --- a/core/cautils/fileutils.go +++ b/core/cautils/fileutils.go @@ -48,7 +48,7 @@ func LoadResourcesFromHelmCharts(ctx context.Context, basePath string) (map[stri if err == nil { wls, errs := chart.GetWorkloadsWithDefaultValues() if len(errs) > 0 { - logger.L().Ctx(ctx).Error(fmt.Sprintf("Rendering of Helm chart template '%s', failed: %v", chart.GetName(), errs)) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("Rendering of Helm chart template '%s', failed: %v", chart.GetName(), errs)) continue } @@ -88,7 +88,7 @@ func LoadResourcesFromKustomizeDirectory(ctx context.Context, basePath string) ( kustomizeDirectoryName := GetKustomizeDirectoryName(newBasePath) if len(errs) > 0 { - logger.L().Ctx(ctx).Error(fmt.Sprintf("Rendering yaml from Kustomize failed: %v", errs)) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("Rendering yaml from Kustomize failed: %v", errs)) } for k, v := range wls { @@ -100,15 +100,16 @@ func LoadResourcesFromKustomizeDirectory(ctx context.Context, basePath string) ( func LoadResourcesFromFiles(ctx context.Context, input, rootPath string) map[string][]workloadinterface.IMetadata { files, errs := listFiles(input) if len(errs) > 0 { - logger.L().Ctx(ctx).Error(fmt.Sprintf("%v", errs)) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("%v", errs)) } if len(files) == 0 { + logger.L().Ctx(ctx).Error("no files found to scan", helpers.String("input", input)) return nil } workloads, errs := loadFiles(rootPath, files) if len(errs) > 0 { - logger.L().Ctx(ctx).Error(fmt.Sprintf("%v", errs)) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("%v", errs)) } return workloads diff --git a/core/core/download.go b/core/core/download.go index d5ae0855..ad50719d 100644 --- a/core/core/download.go +++ b/core/core/download.go @@ -84,7 +84,7 @@ func downloadArtifacts(ctx context.Context, downloadInfo *metav1.DownloadInfo) e } for artifact := range artifacts { if err := downloadArtifact(ctx, &metav1.DownloadInfo{Target: artifact, Path: downloadInfo.Path, FileName: fmt.Sprintf("%s.json", artifact)}, artifacts); err != nil { - logger.L().Ctx(ctx).Error("error downloading", helpers.String("artifact", artifact), helpers.Error(err)) + logger.L().Ctx(ctx).Warning("error downloading", helpers.String("artifact", artifact), helpers.Error(err)) } } return nil diff --git a/core/core/fix.go b/core/core/fix.go index a8343aa5..e1e8121a 100644 --- a/core/core/fix.go +++ b/core/core/fix.go @@ -48,7 +48,7 @@ func (ks *Kubescape) Fix(ctx context.Context, fixInfo *metav1.FixInfo) error { if len(errors) > 0 { for _, err := range errors { - logger.L().Ctx(ctx).Error(err.Error()) + logger.L().Ctx(ctx).Warning(err.Error()) } return fmt.Errorf("Failed to fix some resources, check the logs for more details") } diff --git a/core/core/submit.go b/core/core/submit.go index 40de42fb..1f5fa708 100644 --- a/core/core/submit.go +++ b/core/core/submit.go @@ -43,7 +43,7 @@ func (ks *Kubescape) SubmitExceptions(ctx context.Context, credentials *cautils. // load cached config tenantConfig := getTenantConfig(credentials, "", "", getKubernetesApi()) if err := tenantConfig.SetTenant(); err != nil { - logger.L().Ctx(ctx).Error("failed setting account ID", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("failed setting account ID", helpers.Error(err)) } // load exceptions from file diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index bcea1735..60676d29 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -142,13 +142,13 @@ func (h *FixHandler) PrepareResourcesToFix(ctx context.Context) []ResourceFixInf relativePath, documentIndex, err := h.getFilePathAndIndex(resourcePath) if err != nil { - logger.L().Ctx(ctx).Error("Skipping invalid resource path: " + resourcePath) + logger.L().Ctx(ctx).Warning("Skipping invalid resource path: " + resourcePath) continue } absolutePath := path.Join(h.localBasePath, relativePath) if _, err := os.Stat(absolutePath); err != nil { - logger.L().Ctx(ctx).Error("Skipping missing file: " + absolutePath) + logger.L().Ctx(ctx).Warning("Skipping missing file: " + absolutePath) continue } @@ -220,7 +220,7 @@ func (h *FixHandler) ApplyChanges(ctx context.Context, resourcesToFix []Resource err = writeFixesToFile(filepath, fixedYamlString) if err != nil { - logger.L().Ctx(ctx).Error(fmt.Sprintf("Failed to write fixes to file %s, %v", filepath, err.Error())) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("Failed to write fixes to file %s, %v", filepath, err.Error())) errors = append(errors, err) } } diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index 22a20ad4..bacf3b2f 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -306,7 +306,7 @@ func readDocuments(ctx context.Context, reader io.Reader, decoder yqlib.Decoder) func safelyCloseFile(ctx context.Context, file *os.File) { err := file.Close() if err != nil { - logger.L().Ctx(ctx).Error("Error Closing File") + logger.L().Ctx(ctx).Warning("Error Closing File") } } diff --git a/core/pkg/hostsensorutils/hostsensordeploy.go b/core/pkg/hostsensorutils/hostsensordeploy.go index b1f32a19..a7d2e11c 100644 --- a/core/pkg/hostsensorutils/hostsensordeploy.go +++ b/core/pkg/hostsensorutils/hostsensordeploy.go @@ -87,7 +87,7 @@ func (hsh *HostSensorHandler) Init(ctx context.Context) error { } hsh.populatePodNamesToNodeNames(ctx) if err := hsh.checkPodForEachNode(); err != nil { - logger.L().Ctx(ctx).Error("failed to validate host-sensor pods status", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("failed to validate host-sensor pods status", helpers.Error(err)) } cautils.StopSpinner() return nil @@ -237,9 +237,10 @@ func (hsh *HostSensorHandler) populatePodNamesToNodeNames(ctx context.Context) { LabelSelector: fmt.Sprintf("name=%s", hsh.DaemonSet.Spec.Template.Labels["name"]), }) if err != nil { - logger.L().Ctx(ctx).Error("failed to watch over daemonset pods - are we missing watch pods permissions?", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("failed to watch over DaemonSet pods - are we missing watch pods permissions?", helpers.Error(err)) } if watchRes == nil { + logger.L().Ctx(ctx).Error("failed to watch over DaemonSet pods, will not be able to get host-sensor data") return } for eve := range watchRes.ResultChan() { diff --git a/core/pkg/hostsensorutils/hostsensorgetfrompod.go b/core/pkg/hostsensorutils/hostsensorgetfrompod.go index 74e7da07..e2a3f8a2 100644 --- a/core/pkg/hostsensorutils/hostsensorgetfrompod.go +++ b/core/pkg/hostsensorutils/hostsensorgetfrompod.go @@ -214,7 +214,7 @@ func (hsh *HostSensorHandler) GetKubeletConfigurations(ctx context.Context) ([]h for resIdx := range res { jsonBytes, ery := yaml.YAMLToJSON(res[resIdx].Data) if ery != nil { - logger.L().Ctx(ctx).Error("failed to convert kubelet configurations from yaml to json", helpers.Error(ery)) + logger.L().Ctx(ctx).Warning("failed to convert kubelet configurations from yaml to json", helpers.Error(ery)) continue } res[resIdx].SetData(jsonBytes) diff --git a/core/pkg/hostsensorutils/hostsensorworkerpool.go b/core/pkg/hostsensorutils/hostsensorworkerpool.go index 654e4aab..2d231d69 100644 --- a/core/pkg/hostsensorutils/hostsensorworkerpool.go +++ b/core/pkg/hostsensorutils/hostsensorworkerpool.go @@ -48,10 +48,10 @@ func (wp *workerPool) hostSensorWorker(ctx context.Context, hsh *HostSensorHandl for job := range wp.jobs { hostSensorDataEnvelope, err := hsh.getResourcesFromPod(job.podName, job.nodeName, job.requestKind, job.path) if err != nil { - logger.L().Ctx(ctx).Error("failed to get data", helpers.String("path", job.path), helpers.String("podName", job.podName), helpers.Error(err)) - } else { - wp.results <- hostSensorDataEnvelope + logger.L().Ctx(ctx).Warning("failed to get data", helpers.String("path", job.path), helpers.String("podName", job.podName), helpers.Error(err)) + continue } + wp.results <- hostSensorDataEnvelope } } diff --git a/core/pkg/opaprocessor/processorhandler.go b/core/pkg/opaprocessor/processorhandler.go index f835d0e2..ded9d4ce 100644 --- a/core/pkg/opaprocessor/processorhandler.go +++ b/core/pkg/opaprocessor/processorhandler.go @@ -56,8 +56,7 @@ func (opap *OPAProcessor) ProcessRulesListenner(ctx context.Context, progressLis // process if err := opap.Process(ctx, opap.OPASessionObj.AllPolicies, progressListener); err != nil { - logger.L().Ctx(ctx).Error(err.Error()) - // Return error? + logger.L().Ctx(ctx).Warning(err.Error()) } // edit results @@ -92,7 +91,7 @@ func (opap *OPAProcessor) Process(ctx context.Context, policies *cautils.Policie resourcesAssociatedControl, err := opap.processControl(ctx, &control) if err != nil { - logger.L().Ctx(ctx).Error(err.Error()) + logger.L().Ctx(ctx).Warning(err.Error()) } if len(resourcesAssociatedControl) == 0 { @@ -142,7 +141,7 @@ func (opap *OPAProcessor) processControl(ctx context.Context, control *reporthan for i := range control.Rules { resourceAssociatedRule, err := opap.processRule(ctx, &control.Rules[i], control.FixedInput) if err != nil { - logger.L().Ctx(ctx).Error(err.Error()) + logger.L().Ctx(ctx).Warning(err.Error()) continue } @@ -212,34 +211,33 @@ func (opap *OPAProcessor) processRule(ctx context.Context, rule *reporthandling. ruleResponses, err := opap.runOPAOnSingleRule(ctx, rule, inputRawResources, ruleData, RuleRegoDependenciesData) if err != nil { - // TODO - Handle error - logger.L().Ctx(ctx).Error(err.Error()) - } else { - // ruleResponse to ruleResult - for i := range ruleResponses { - failedResources := objectsenvelopes.ListMapToMeta(ruleResponses[i].GetFailedResources()) - for j := range failedResources { - ruleResult := &resourcesresults.ResourceAssociatedRule{} - if r, k := resources[failedResources[j].GetID()]; k { - ruleResult = r - } + return resources, err + } - ruleResult.SetStatus(apis.StatusFailed, nil) - for j := range ruleResponses[i].FailedPaths { - ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FailedPath: ruleResponses[i].FailedPaths[j]}) - } - for j := range ruleResponses[i].FixPaths { - ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FixPath: ruleResponses[i].FixPaths[j]}) - } - if ruleResponses[i].FixCommand != "" { - ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FixCommand: ruleResponses[i].FixCommand}) - } - resources[failedResources[j].GetID()] = ruleResult + // ruleResponse to ruleResult + for i := range ruleResponses { + failedResources := objectsenvelopes.ListMapToMeta(ruleResponses[i].GetFailedResources()) + for j := range failedResources { + ruleResult := &resourcesresults.ResourceAssociatedRule{} + if r, k := resources[failedResources[j].GetID()]; k { + ruleResult = r } + + ruleResult.SetStatus(apis.StatusFailed, nil) + for j := range ruleResponses[i].FailedPaths { + ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FailedPath: ruleResponses[i].FailedPaths[j]}) + } + for j := range ruleResponses[i].FixPaths { + ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FixPath: ruleResponses[i].FixPaths[j]}) + } + if ruleResponses[i].FixCommand != "" { + ruleResult.Paths = append(ruleResult.Paths, armotypes.PosturePaths{FixCommand: ruleResponses[i].FixCommand}) + } + resources[failedResources[j].GetID()] = ruleResult } } - return resources, err + return resources, nil } func (opap *OPAProcessor) runOPAOnSingleRule(ctx context.Context, rule *reporthandling.PolicyRule, k8sObjects []map[string]interface{}, getRuleData func(*reporthandling.PolicyRule) string, ruleRegoDependenciesData resources.RegoDependenciesData) ([]reporthandling.RuleResponse, error) { @@ -275,7 +273,7 @@ func (opap *OPAProcessor) runRegoOnK8s(ctx context.Context, rule *reporthandling // Eval results, err := opap.regoEval(k8sObjects, compiled, &store) if err != nil { - logger.L().Ctx(ctx).Error(err.Error()) + logger.L().Ctx(ctx).Warning(err.Error()) } return results, nil diff --git a/core/pkg/policyhandler/handlepullpolicies.go b/core/pkg/policyhandler/handlepullpolicies.go index 95291527..70c683df 100644 --- a/core/pkg/policyhandler/handlepullpolicies.go +++ b/core/pkg/policyhandler/handlepullpolicies.go @@ -37,7 +37,7 @@ func (policyHandler *PolicyHandler) getPolicies(ctx context.Context, policyIdent if err == nil { policiesAndResources.Exceptions = exceptionPolicies } else { - logger.L().Ctx(ctx).Error("failed to load exceptions", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("failed to load exceptions", helpers.Error(err)) } // get account configuration @@ -45,7 +45,7 @@ func (policyHandler *PolicyHandler) getPolicies(ctx context.Context, policyIdent if err == nil { policiesAndResources.RegoInputData.PostureControlInputs = controlsInputs } else { - logger.L().Ctx(ctx).Error(err.Error()) + logger.L().Ctx(ctx).Warning(err.Error()) } cautils.StopSpinner() diff --git a/core/pkg/resourcehandler/k8sresources.go b/core/pkg/resourcehandler/k8sresources.go index e5c9c709..6865181e 100644 --- a/core/pkg/resourcehandler/k8sresources.go +++ b/core/pkg/resourcehandler/k8sresources.go @@ -223,7 +223,7 @@ func (k8sHandler *K8sResourceHandler) collectAPIServerInfoResource(allResources func (k8sHandler *K8sResourceHandler) GetClusterAPIServerInfo(ctx context.Context) *version.Info { clusterAPIServerInfo, err := k8sHandler.k8s.DiscoveryClient.ServerVersion() if err != nil { - logger.L().Ctx(ctx).Error("failed to discover API server information", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("failed to discover API server information", helpers.Error(err)) return nil } return clusterAPIServerInfo diff --git a/core/pkg/resultshandling/printer/printresults.go b/core/pkg/resultshandling/printer/printresults.go index 3c8a33f5..7a790fc6 100644 --- a/core/pkg/resultshandling/printer/printresults.go +++ b/core/pkg/resultshandling/printer/printresults.go @@ -32,12 +32,12 @@ type IPrinter interface { func GetWriter(ctx context.Context, outputFile string) *os.File { if outputFile != "" { if err := os.MkdirAll(filepath.Dir(outputFile), os.ModePerm); err != nil { - logger.L().Ctx(ctx).Error(fmt.Sprintf("failed to create directory, reason: %s", err.Error())) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("failed to create directory, reason: %s", err.Error())) return os.Stdout } f, err := os.Create(outputFile) if err != nil { - logger.L().Ctx(ctx).Error(fmt.Sprintf("failed to open file for writing, reason: %s", err.Error())) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("failed to open file for writing, reason: %s", err.Error())) return os.Stdout } return f diff --git a/core/pkg/resultshandling/printer/v2/htmlprinter.go b/core/pkg/resultshandling/printer/v2/htmlprinter.go index 7a3cdbb4..d6e771f7 100644 --- a/core/pkg/resultshandling/printer/v2/htmlprinter.go +++ b/core/pkg/resultshandling/printer/v2/htmlprinter.go @@ -110,9 +110,9 @@ func (hp *HtmlPrinter) ActionPrint(ctx context.Context, opaSessionObj *cautils.O err := tpl.Execute(hp.writer, reportingCtx) if err != nil { logger.L().Ctx(ctx).Error("failed to render template", helpers.Error(err)) - } else { - printer.LogOutputFile(hp.writer.Name()) + return } + printer.LogOutputFile(hp.writer.Name()) } diff --git a/core/pkg/resultshandling/printer/v2/jsonprinter.go b/core/pkg/resultshandling/printer/v2/jsonprinter.go index 71b35072..60f8d4ce 100644 --- a/core/pkg/resultshandling/printer/v2/jsonprinter.go +++ b/core/pkg/resultshandling/printer/v2/jsonprinter.go @@ -51,7 +51,7 @@ func (jp *JsonPrinter) ActionPrint(ctx context.Context, opaSessionObj *cautils.O if _, err := jp.writer.Write(r); err != nil { logger.L().Ctx(ctx).Error("failed to write results", helpers.Error(err)) - } else { - printer.LogOutputFile(jp.writer.Name()) + return } + printer.LogOutputFile(jp.writer.Name()) } diff --git a/core/pkg/resultshandling/printer/v2/junit.go b/core/pkg/resultshandling/printer/v2/junit.go index 097796c5..24f1b3a1 100644 --- a/core/pkg/resultshandling/printer/v2/junit.go +++ b/core/pkg/resultshandling/printer/v2/junit.go @@ -120,9 +120,9 @@ func (jp *JunitPrinter) ActionPrint(ctx context.Context, opaSessionObj *cautils. if _, err := jp.writer.Write(postureReportStr); err != nil { logger.L().Ctx(ctx).Error("failed to write results", helpers.Error(err)) - } else { - printer.LogOutputFile(jp.writer.Name()) + return } + printer.LogOutputFile(jp.writer.Name()) } func testsSuites(results *cautils.OPASessionObj) *JUnitTestSuites { diff --git a/core/pkg/resultshandling/printer/v2/pdf.go b/core/pkg/resultshandling/printer/v2/pdf.go index 77256fcc..833a87ad 100644 --- a/core/pkg/resultshandling/printer/v2/pdf.go +++ b/core/pkg/resultshandling/printer/v2/pdf.go @@ -98,9 +98,9 @@ func (pp *PdfPrinter) ActionPrint(ctx context.Context, opaSessionObj *cautils.OP if _, err := pp.writer.Write(outBuff.Bytes()); err != nil { logger.L().Ctx(ctx).Error("failed to write results", helpers.Error(err)) - } else { - printer.LogOutputFile(pp.writer.Name()) + return } + printer.LogOutputFile(pp.writer.Name()) } // printHeader prints the Kubescape logo and report date diff --git a/core/pkg/resultshandling/printer/v2/prometheus.go b/core/pkg/resultshandling/printer/v2/prometheus.go index 64a97967..639e697d 100644 --- a/core/pkg/resultshandling/printer/v2/prometheus.go +++ b/core/pkg/resultshandling/printer/v2/prometheus.go @@ -53,7 +53,7 @@ func (pp *PrometheusPrinter) ActionPrint(ctx context.Context, opaSessionObj *cau if _, err := pp.writer.Write([]byte(metrics.String())); err != nil { logger.L().Ctx(ctx).Error("failed to write results", helpers.Error(err)) - } else { - printer.LogOutputFile(pp.writer.Name()) + return } + printer.LogOutputFile(pp.writer.Name()) } diff --git a/core/pkg/resultshandling/results.go b/core/pkg/resultshandling/results.go index 7f235110..5fa88ad1 100644 --- a/core/pkg/resultshandling/results.go +++ b/core/pkg/resultshandling/results.go @@ -116,7 +116,7 @@ func NewPrinter(ctx context.Context, printFormat, formatVersion string, verboseM return printerv2.NewSARIFPrinter() default: if printFormat != printer.PrettyFormat { - logger.L().Ctx(ctx).Error(fmt.Sprintf("Invalid format \"%s\", default format \"pretty-printer\" is applied", printFormat)) + logger.L().Ctx(ctx).Warning(fmt.Sprintf("Invalid format \"%s\", default format \"pretty-printer\" is applied", printFormat)) } return printerv2.NewPrettyPrinter(verboseMode, formatVersion, attackTree, viewType) } diff --git a/httphandler/handlerequests/v1/requestshandlerutils.go b/httphandler/handlerequests/v1/requestshandlerutils.go index b3ff5d7c..017c18bb 100644 --- a/httphandler/handlerequests/v1/requestshandlerutils.go +++ b/httphandler/handlerequests/v1/requestshandlerutils.go @@ -38,7 +38,7 @@ func (handler *HTTPHandler) executeScan() { response.Response = err.Error() } } else { - logger.L().Success("done scanning", helpers.String("ID", scanReq.scanID)) + logger.L().Ctx(scanReq.ctx).Success("done scanning", helpers.String("ID", scanReq.scanID)) if scanReq.scanQueryParams.ReturnResults { response.Type = utilsapisv1.ResultsV1ScanResponseType response.Response = results