From 2a82d6cd21d522c026c88316f4da46331ed9e71f Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 24 Jan 2023 11:32:55 +0200 Subject: [PATCH] Implementing progress bar for control processing Signed-off-by: Ben --- core/cautils/display.go | 28 +++++++++++++++++++++++ core/core/scan.go | 2 +- core/pkg/opaprocessor/processorhandler.go | 19 ++++++++++----- go.mod | 8 ++++--- go.sum | 14 +++++++++--- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/core/cautils/display.go b/core/cautils/display.go index eff1c31a..a64c271a 100644 --- a/core/cautils/display.go +++ b/core/cautils/display.go @@ -7,6 +7,7 @@ import ( spinnerpkg "github.com/briandowns/spinner" "github.com/fatih/color" "github.com/mattn/go-isatty" + "github.com/schollz/progressbar/v3" ) var FailureDisplay = color.New(color.Bold, color.FgHiRed).FprintfFunc() @@ -39,3 +40,30 @@ func StopSpinner() { } spinner.Stop() } + +type ProgressHandler struct { + title string + pb *progressbar.ProgressBar +} + +func NewProgressHandler(title string) *ProgressHandler { + return &ProgressHandler{title: title} +} + +func (p *ProgressHandler) Start(allSteps int) { + if isatty.IsTerminal(os.Stdout.Fd()) { + p.pb = progressbar.Default(int64(allSteps), p.title) + } else { + p.pb = nil + } +} + +func (p *ProgressHandler) ProgressJob(step int, message string) { + if p.pb != nil { + p.pb.Add(step) + p.pb.Describe(message) + } +} + +func (p *ProgressHandler) Stop() { +} diff --git a/core/core/scan.go b/core/core/scan.go index c643a0dc..f39eb434 100644 --- a/core/core/scan.go +++ b/core/core/scan.go @@ -163,7 +163,7 @@ func (ks *Kubescape) Scan(scanInfo *cautils.ScanInfo) (*resultshandling.ResultsH // ========================= opa testing ===================== deps := resources.NewRegoDependenciesData(k8sinterface.GetK8sConfig(), interfaces.tenantConfig.GetContextName()) reportResults := opaprocessor.NewOPAProcessor(scanData, deps) - if err := reportResults.ProcessRulesListenner(); err != nil { + if err := reportResults.ProcessRulesListenner(cautils.NewProgressHandler("")); err != nil { // TODO - do something return resultsHandling, fmt.Errorf("%w", err) } diff --git a/core/pkg/opaprocessor/processorhandler.go b/core/pkg/opaprocessor/processorhandler.go index f6ace4ff..7d52b6ca 100644 --- a/core/pkg/opaprocessor/processorhandler.go +++ b/core/pkg/opaprocessor/processorhandler.go @@ -27,6 +27,12 @@ import ( const ScoreConfigPath = "/resources/config" +type IJobProgressNotificationClient interface { + Start(allSteps int) + ProgressJob(step int, message string) + Stop() +} + type OPAProcessor struct { regoDependenciesData *resources.RegoDependenciesData *cautils.OPASessionObj @@ -42,14 +48,14 @@ func NewOPAProcessor(sessionObj *cautils.OPASessionObj, regoDependenciesData *re regoDependenciesData: regoDependenciesData, } } -func (opap *OPAProcessor) ProcessRulesListenner() error { +func (opap *OPAProcessor) ProcessRulesListenner(progressListener IJobProgressNotificationClient) error { opap.OPASessionObj.AllPolicies = ConvertFrameworksToPolicies(opap.Policies, cautils.BuildNumber) ConvertFrameworksToSummaryDetails(&opap.Report.SummaryDetails, opap.Policies, opap.OPASessionObj.AllPolicies) // process - if err := opap.Process(opap.OPASessionObj.AllPolicies); err != nil { + if err := opap.Process(opap.OPASessionObj.AllPolicies, progressListener); err != nil { logger.L().Error(err.Error()) // Return error? } @@ -64,12 +70,15 @@ func (opap *OPAProcessor) ProcessRulesListenner() error { return nil } -func (opap *OPAProcessor) Process(policies *cautils.Policies) error { +func (opap *OPAProcessor) Process(policies *cautils.Policies, progressListener IJobProgressNotificationClient) error { opap.loggerStartScanning() - cautils.StartSpinner() + progressListener.Start(len(policies.Controls)) + defer progressListener.Stop() for _, toPin := range policies.Controls { + progressListener.ProgressJob(1, fmt.Sprintf("Control %s", toPin.ControlID)) + control := toPin resourcesAssociatedControl, err := opap.processControl(&control) @@ -94,8 +103,6 @@ func (opap *OPAProcessor) Process(policies *cautils.Policies) error { opap.Report.ReportGenerationTime = time.Now().UTC() - cautils.StopSpinner() - opap.loggerDoneScanning() return nil diff --git a/go.mod b/go.mod index b907e0af..75b0e488 100644 --- a/go.mod +++ b/go.mod @@ -22,11 +22,12 @@ require ( github.com/kubescape/opa-utils v0.0.223 github.com/kubescape/rbac-utils v0.0.19 github.com/libgit2/git2go/v33 v33.0.9 - github.com/mattn/go-isatty v0.0.16 + github.com/mattn/go-isatty v0.0.17 github.com/mikefarah/yq/v4 v4.29.1 github.com/olekukonko/tablewriter v0.0.5 github.com/open-policy-agent/opa v0.45.0 github.com/owenrumney/go-sarif/v2 v2.1.2 + github.com/schollz/progressbar/v3 v3.13.0 github.com/sigstore/cosign v1.13.1 github.com/spf13/cobra v1.6.1 github.com/stretchr/testify v1.8.1 @@ -221,9 +222,10 @@ require ( github.com/magiconair/properties v1.8.6 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/miekg/pkcs11 v1.1.1 // indirect + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -249,7 +251,7 @@ require ( github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect - github.com/rivo/uniseg v0.2.0 // indirect + github.com/rivo/uniseg v0.4.3 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58 // indirect github.com/sassoftware/relic v0.0.0-20210427151427-dfb082b79b74 // indirect diff --git a/go.sum b/go.sum index 4056b57f..c95ed72d 100644 --- a/go.sum +++ b/go.sum @@ -1044,6 +1044,7 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc= github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0= +github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= @@ -1132,13 +1133,15 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo= @@ -1157,6 +1160,8 @@ github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WT github.com/mikefarah/yq/v4 v4.29.1 h1:P5O/V7nP4a3ahx/tddwpu3DPL5JgyyH4qG+DAEfJvPg= github.com/mikefarah/yq/v4 v4.29.1/go.mod h1:5W3SgDwOG0zBPSjGf7ecreDiFlH16fd7Qj43F2UFCvU= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= @@ -1367,8 +1372,9 @@ github.com/qur/ar v0.0.0-20130629153254-282534b91770/go.mod h1:SjlYv2m9lpV0UW6K7 github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= +github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -1394,6 +1400,8 @@ github.com/sassoftware/go-rpmutils v0.1.1/go.mod h1:euhXULoBpvAxqrBHEyJS4Tsu3hHx github.com/sassoftware/relic v0.0.0-20210427151427-dfb082b79b74 h1:sUNzanSKA9z/h8xXl+ZJoxIYZL0Qx306MmxqRrvUgr0= github.com/sassoftware/relic v0.0.0-20210427151427-dfb082b79b74/go.mod h1:YlB8wFIZmFLZ1JllNBfSURzz52fBxbliNgYALk1UDmk= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/schollz/progressbar/v3 v3.13.0 h1:9TeeWRcjW2qd05I8Kf9knPkW4vLM/hYoa6z9ABvxje8= +github.com/schollz/progressbar/v3 v3.13.0/go.mod h1:ZBYnSuLAX2LU8P8UiKN/KgF2DY58AJC8yfVYLPC8Ly4= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/secure-systems-lab/go-securesystemslib v0.4.0 h1:b23VGrQhTA8cN2CbBw7/FulN9fTtqYUdS5+Oxzt+DUE= github.com/secure-systems-lab/go-securesystemslib v0.4.0/go.mod h1:FGBZgq2tXWICsxWQW1msNf49F0Pf2Op5Htayx335Qbs=