replace print with logger

This commit is contained in:
dwertent
2022-02-06 11:13:33 +02:00
parent 4207f3d6d1
commit 8bdff31693
18 changed files with 45 additions and 40 deletions
+7 -1
View File
@@ -101,7 +101,7 @@ Set-ExecutionPolicy RemoteSigned -scope CurrentUser
| `--use-from` | | Load local framework object from specified path. If not used will download latest |
| `--use-artifacts-from` | | Load artifacts (frameworks, control-config, exceptions) from local directory. If not used will download them | |
| `--use-default` | `false` | Load local framework object from default path. If not used will download latest | `true`/`false` |
| `--exceptions` | | Path to an [exceptions obj](examples/exceptions.json). If not set will download exceptions from Armo management portal |
| `--exceptions` | | Path to an exceptions obj, [examples](examples/exceptions/README.md). Default will download exceptions from Kubescape SaaS |
| `--controls-config` | | Path to a controls-config obj. If not set will download controls-config from ARMO management portal | |
| `--submit` | `false` | If set, Kubescape will send the scan results to Armo management portal where you can see the results in a user-friendly UI, choose your preferred compliance framework, check risk results history and trends, manage exceptions, get remediation recommendations and much more. By default the results are not sent | `true`/`false` |
| `--keep-local` | `false` | Kubescape will not send scan results to Armo management portal. Use this flag if you ran with the `--submit` flag in the past and you do not want to submit your current scan results | `true`/`false` |
@@ -114,6 +114,12 @@ Set-ExecutionPolicy RemoteSigned -scope CurrentUser
### Examples
#### Scan a running Kubernetes cluster and submit results to the [Kubescape SaaS version](https://portal.armo.cloud/)
```
kubescape scan --submit
```
#### Scan a running Kubernetes cluster with [`nsa`](https://www.nsa.gov/Press-Room/News-Highlights/Article/Article/2716980/nsa-cisa-release-kubernetes-hardening-guidance/) framework and submit results to the [Kubescape SaaS version](https://portal.armo.cloud/)
```
kubescape scan framework nsa --submit
+3 -2
View File
@@ -11,6 +11,7 @@ import (
"github.com/armosec/k8s-interface/k8sinterface"
"github.com/armosec/kubescape/cautils/getter"
"github.com/armosec/kubescape/cautils/logger"
corev1 "k8s.io/api/core/v1"
)
@@ -114,7 +115,7 @@ func NewLocalConfig(backendAPI getter.IBackend, customerGUID, clusterName string
if lc.configObj.AccountID != "" {
if err := lc.SetTenant(); err != nil {
fmt.Println(err)
logger.L().Error(err.Error())
}
}
@@ -229,7 +230,7 @@ func NewClusterConfig(k8s *k8sinterface.KubernetesApi, backendAPI getter.IBacken
if c.configObj.AccountID != "" {
if err := c.SetTenant(); err != nil {
fmt.Println(err) // TODO: print to log
logger.L().Error(err.Error())
}
}
+1
View File
@@ -0,0 +1 @@
package clihandler
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/armosec/k8s-interface/k8sinterface"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/getter"
"github.com/armosec/kubescape/cautils/logger"
"github.com/spf13/cobra"
)
@@ -40,7 +41,7 @@ var getCmd = &cobra.Command{
}
return err
}
fmt.Println(key + "=" + val)
logger.L().Info(key + "=" + val)
return nil
},
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/armosec/k8s-interface/k8sinterface"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/getter"
"github.com/armosec/kubescape/cautils/logger"
"github.com/spf13/cobra"
)
@@ -35,7 +36,7 @@ var setClusterCmd = &cobra.Command{
if err := clusterConfig.SetKeyValueInConfigmap(key, data); err != nil {
return err
}
fmt.Println("Value added successfully.")
logger.L().Info("value added successfully.")
return nil
},
}
+2 -4
View File
@@ -122,11 +122,9 @@ func init() {
func flagValidationFramework() {
if scanInfo.Submit && scanInfo.Local {
fmt.Println("You can use `keep-local` or `submit`, but not both")
os.Exit(1)
logger.L().Fatal("you can use `keep-local` or `submit`, but not both")
}
if 100 < scanInfo.FailThreshold {
fmt.Println("bad argument: out of range threshold")
os.Exit(1)
logger.L().Fatal("bad argument: out of range threshold")
}
}
+2 -5
View File
@@ -1,11 +1,9 @@
package cmd
import (
"fmt"
"os"
"github.com/armosec/k8s-interface/k8sinterface"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/clihandler"
"github.com/armosec/kubescape/clihandler/cliinterfaces"
reporterv1 "github.com/armosec/kubescape/resultshandling/reporter/v1"
@@ -41,8 +39,7 @@ var rabcCmd = &cobra.Command{
}
if err := clihandler.Submit(submitInterfaces); err != nil {
fmt.Println(err)
os.Exit(1)
logger.L().Fatal(err.Error())
}
return nil
},
+2 -2
View File
@@ -8,6 +8,7 @@ import (
"github.com/armosec/k8s-interface/k8sinterface"
"github.com/armosec/k8s-interface/workloadinterface"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/clihandler"
"github.com/armosec/kubescape/clihandler/cliinterfaces"
reporterv1 "github.com/armosec/kubescape/resultshandling/reporter/v1"
@@ -79,8 +80,7 @@ var resultsCmd = &cobra.Command{
}
if err := clihandler.Submit(submitInterfaces); err != nil {
fmt.Println(err)
os.Exit(1)
logger.L().Fatal(err.Error())
}
return nil
},
+3
View File
@@ -10,6 +10,9 @@ import (
var submitInfo cliobjects.Submit
var submitCmdExamples = `
`
var submitCmd = &cobra.Command{
Use: "submit <command>",
Short: "Submit an object to the Kubescape SaaS version",
+2 -3
View File
@@ -1,9 +1,8 @@
package cmd
import (
"fmt"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/spf13/cobra"
)
@@ -14,7 +13,7 @@ var versionCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
v := cautils.NewIVersionCheckHandler()
v.CheckLatestVersion(cautils.NewVersionCheckRequest(cautils.BuildNumber, "", "", "version"))
fmt.Println("Your current version is: " + cautils.BuildNumber)
logger.L().Info("Your current version is: " + cautils.BuildNumber)
return nil
},
}
+1 -1
View File
@@ -141,7 +141,7 @@ func setSubmitBehavior(scanInfo *cautils.ScanInfo, tenantConfig cautils.ITenantC
if scanInfo.Submit {
// submit - Create tenant & Submit report
if err := tenantConfig.SetTenant(); err != nil {
fmt.Println(err)
logger.L().Error(err.Error())
}
}
}
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"os"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
)
var INDENT = " "
@@ -27,8 +28,8 @@ func GetWriter(outputFile string) *os.File {
if outputFile != "" {
f, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("failed to open file for writing, reason: ", err.Error())
return os.Stdout
logger.L().Error(fmt.Sprintf("failed to open file for writing, reason: %s", err.Error()))
return os.Stderr
}
return f
}
+2 -2
View File
@@ -6,6 +6,7 @@ import (
"os"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
)
@@ -38,8 +39,7 @@ func (jsonPrinter *JsonPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj
}
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
jsonPrinter.writer.Write(postureReportStr)
}
+4 -5
View File
@@ -6,6 +6,7 @@ import (
"os"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
"github.com/armosec/opa-utils/reporthandling"
)
@@ -31,13 +32,11 @@ func (junitPrinter *JunitPrinter) ActionPrint(opaSessionObj *cautils.OPASessionO
junitResult, err := convertPostureReportToJunitResult(opaSessionObj.PostureReport)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
postureReportStr, err := xml.Marshal(junitResult)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
junitPrinter.writer.Write(postureReportStr)
}
@@ -123,4 +122,4 @@ func convertPostureReportToJunitResult(postureResult *reporthandling.PostureRepo
juResult.Suites = append(juResult.Suites, suite)
}
return &juResult, nil
}
}
@@ -6,6 +6,7 @@ import (
"github.com/armosec/k8s-interface/workloadinterface"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
"github.com/armosec/opa-utils/reporthandling"
)
@@ -90,7 +91,6 @@ func (printer *PrometheusPrinter) ActionPrint(opaSessionObj *cautils.OPASessionO
err := printer.printReports(opaSessionObj.AllResources, opaSessionObj.PostureReport.FrameworkReports)
if err != nil {
fmt.Println(err)
os.Exit(1)
logger.L().Fatal(err.Error())
}
}
+3 -4
View File
@@ -6,6 +6,7 @@ import (
"os"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
reporthandlingv2 "github.com/armosec/opa-utils/reporthandling/v2"
)
@@ -33,13 +34,11 @@ func (junitPrinter *JunitPrinter) FinalizeData(opaSessionObj *cautils.OPASession
func (junitPrinter *JunitPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj) {
junitResult, err := convertPostureReportToJunitResult(opaSessionObj.Report)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
postureReportStr, err := xml.Marshal(junitResult)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
junitPrinter.writer.Write(postureReportStr)
}
@@ -6,6 +6,7 @@ import (
"os"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
)
@@ -30,8 +31,7 @@ func (jsonPrinter *JsonPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj
postureReportStr, err := json.Marshal(opaSessionObj.Report)
if err != nil {
fmt.Println("Failed to convert posture report object!")
os.Exit(1)
logger.L().Fatal("failed to convert posture report object")
}
jsonPrinter.writer.Write(postureReportStr)
}
+2 -3
View File
@@ -1,9 +1,8 @@
package resultshandling
import (
"fmt"
"github.com/armosec/kubescape/cautils"
"github.com/armosec/kubescape/cautils/logger"
"github.com/armosec/kubescape/resultshandling/printer"
"github.com/armosec/kubescape/resultshandling/reporter"
"github.com/armosec/opa-utils/reporthandling"
@@ -30,7 +29,7 @@ func (resultsHandler *ResultsHandler) HandleResults(scanInfo *cautils.ScanInfo)
resultsHandler.printerObj.ActionPrint(opaSessionObj)
if err := resultsHandler.reporterObj.ActionSendReport(opaSessionObj); err != nil {
fmt.Println(err)
logger.L().Error(err.Error())
}
score := opaSessionObj.Report.SummaryDetails.Score