diff --git a/Makefile b/Makefile index e2c8914b9..efb0cb2e5 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ coverage: test: $(GO) clean -testcache - $(GO) test -v $(GO_LIST_TO_TEST) -timeout=5m -parallel=1 -failfast -short + $(GO) test -v $(GO_LIST_TO_TEST) -race -timeout=5m -parallel=1 -failfast -short test-e2e: $(GO) clean -testcache diff --git a/horusec-config.json b/horusec-config.json index 312f45a22..59e9f3722 100644 --- a/horusec-config.json +++ b/horusec-config.json @@ -30,7 +30,8 @@ "b176f4967e7b0e54faabb9688d1d9ff6f10959d4a34280b9e035bfd63c4f352e", "316176f18dac308bbcfc3ece628796eb438c8387a7d0da83f583fcacab3a01c4", "1dbef4655a4a2378e67acf89bf9b78c13041634a63a8ef0a84ff5e6237d17216", - "37fa0cfe47519c1b2b6a8e29538571b81fd8787ca4217825ae6d8dcf86d70de8" + "37fa0cfe47519c1b2b6a8e29538571b81fd8787ca4217825ae6d8dcf86d70de8", + "85492fbc829b64336a4f858022fbe52f05e27ee18d7a8fbdf5ffd23991ebd7a9" ], "horusecCliFilesOrPathsToIgnore": [ "**/e2e/**", diff --git a/internal/controllers/analyzer/analyzer.go b/internal/controllers/analyzer/analyzer.go index 811c748de..bd1151eb3 100644 --- a/internal/controllers/analyzer/analyzer.go +++ b/internal/controllers/analyzer/analyzer.go @@ -19,8 +19,8 @@ import ( "log" "os" "os/signal" - "strconv" "strings" + "sync" "time" "github.com/ZupIT/horusec-devkit/pkg/enums/confidence" @@ -32,15 +32,14 @@ import ( enumsAnalysis "github.com/ZupIT/horusec-devkit/pkg/enums/analysis" "github.com/ZupIT/horusec-devkit/pkg/enums/severities" enumsVulnerability "github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/ZupIT/horusec/internal/utils/file" "github.com/google/uuid" "github.com/ZupIT/horusec-devkit/pkg/enums/languages" "github.com/ZupIT/horusec-devkit/pkg/utils/logger" - cliConfig "github.com/ZupIT/horusec/config" - languageDetect "github.com/ZupIT/horusec/internal/controllers/language_detect" + "github.com/ZupIT/horusec/config" + languagedetect "github.com/ZupIT/horusec/internal/controllers/language_detect" "github.com/ZupIT/horusec/internal/controllers/printresults" "github.com/ZupIT/horusec/internal/enums/images" "github.com/ZupIT/horusec/internal/helpers/messages" @@ -78,31 +77,30 @@ type Interface interface { } type Analyzer struct { - monitor *monitor.Monitor - dockerSDK docker.Interface - analysis *analysis.Analysis - config cliConfig.IConfig - languageDetect languageDetect.Interface - printController printresults.Interface - horusecAPIService horusecAPI.IService - formatterService formatters.IService + docker docker.Interface + analysis *analysis.Analysis + config config.IConfig + languageDetect languagedetect.Interface + printController printresults.Interface + horusec horusecAPI.IService + formatter formatters.IService } -func NewAnalyzer(config cliConfig.IConfig) Interface { +func NewAnalyzer(cfg config.IConfig) Interface { entity := &analysis.Analysis{ ID: uuid.New(), CreatedAt: time.Now(), Status: enumsAnalysis.Running, } - dockerAPI := docker.NewDockerAPI(dockerClient.NewDockerClient(), config, entity.ID) + dockerAPI := docker.NewDockerAPI(dockerClient.NewDockerClient(), cfg, entity.ID) return &Analyzer{ - dockerSDK: dockerAPI, - analysis: entity, - config: config, - languageDetect: languageDetect.NewLanguageDetect(config, entity.ID), - printController: printresults.NewPrintResults(entity, config), - horusecAPIService: horusecAPI.NewHorusecAPIService(config), - formatterService: formatters.NewFormatterService(entity, dockerAPI, config, nil), + docker: dockerAPI, + analysis: entity, + config: cfg, + languageDetect: languagedetect.NewLanguageDetect(cfg, entity.ID), + printController: printresults.NewPrintResults(entity, cfg), + horusec: horusecAPI.NewHorusecAPIService(cfg), + formatter: formatters.NewFormatterService(entity, dockerAPI, cfg), } } @@ -128,7 +126,7 @@ func (a *Analyzer) removeHorusecFolder() { err := os.RemoveAll(a.config.GetProjectPath() + file.ReplacePathSeparator("/.horusec")) logger.LogErrorWithLevel(messages.MsgErrorRemoveAnalysisFolder, err) if !a.config.GetDisableDocker() { - a.dockerSDK.DeleteContainersFromAPI() + a.docker.DeleteContainersFromAPI() } } @@ -137,16 +135,14 @@ func (a *Analyzer) runAnalysis() (totalVulns int, err error) { if err != nil { return 0, err } - - a.setMonitor(monitor.NewMonitor()) a.startDetectVulnerabilities(langs) return a.sendAnalysisAndStartPrintResults() } func (a *Analyzer) sendAnalysisAndStartPrintResults() (int, error) { a.formatAnalysisToSendToAPI() - a.horusecAPIService.SendAnalysis(a.analysis) - analysisSaved := a.horusecAPIService.GetAnalysis(a.analysis.ID) + a.horusec.SendAnalysis(a.analysis) + analysisSaved := a.horusec.GetAnalysis(a.analysis.ID) if analysisSaved != nil && analysisSaved.ID != uuid.Nil { a.analysis = analysisSaved } @@ -176,42 +172,60 @@ func (a *Analyzer) formatAnalysisToSendToAPI() { } } -func (a *Analyzer) setMonitor(monitorToSet *monitor.Monitor) { - a.monitor = monitorToSet - a.formatterService.SetMonitor(monitorToSet) -} - +// nolint:funlen,gocyclo +// NOTE: We ignore the funlen and gocyclo lint here because concurrency code is complicated +// +// startDetectVulnerabilities handle execution of all analysis in parallel func (a *Analyzer) startDetectVulnerabilities(langs []languages.Language) { - for _, language := range langs { - for _, projectSubPath := range a.config.GetWorkDir().GetArrayByLanguage(language) { - a.logProjectSubPath(language, projectSubPath) - langFunc := a.mapDetectVulnerabilityByLanguage()[language] - if langFunc != nil { - go langFunc(projectSubPath) - } - } - } + var wg sync.WaitGroup + done := make(chan struct{}) - a.runMonitorTimeout(a.config.GetTimeoutInSecondsAnalysis()) -} + wd := a.config.GetWorkDir() + funcs := a.mapDetectVulnerabilityByLanguage() -func (a *Analyzer) runMonitorTimeout(monitorNumber int64) { - if monitorNumber <= 0 { - a.dockerSDK.DeleteContainersFromAPI() - a.config.SetIsTimeout(true) - } + go func() { + defer close(done) + for _, language := range langs { + for _, projectSubPath := range wd.GetArrayByLanguage(language) { + a.logProjectSubPath(language, projectSubPath) + + if fn, exist := funcs[language]; exist { + wg.Add(1) + go func() { + defer wg.Done() + if err := fn(&wg, projectSubPath); err != nil { + a.setAnalysisError(err) + } + }() + } + } + } + wg.Wait() + }() - if !a.monitor.IsFinished() && !a.config.GetIsTimeout() { - logger.LogInfoWithLevel( - fmt.Sprintf(messages.MsgInfoMonitorTimeoutIn + strconv.Itoa(int(monitorNumber)) + "s")) - time.Sleep(time.Duration(a.config.GetMonitorRetryInSeconds()) * time.Second) - a.runMonitorTimeout(monitorNumber - a.config.GetMonitorRetryInSeconds()) + timeout := a.config.GetTimeoutInSecondsAnalysis() + timer := time.After(time.Duration(timeout) * time.Second) + retry := a.config.GetMonitorRetryInSeconds() + for { + select { + case <-done: + return + case <-timer: + a.docker.DeleteContainersFromAPI() + a.config.SetIsTimeout(true) + return + default: + msg := fmt.Sprintf("%s%ds", messages.MsgInfoMonitorTimeoutIn, timeout) + logger.LogInfoWithLevel(msg) + time.Sleep(time.Duration(retry) * time.Second) + timeout -= retry + } } } //nolint:funlen // all Languages is greater than 15 -func (a *Analyzer) mapDetectVulnerabilityByLanguage() map[languages.Language]func(string) { - return map[languages.Language]func(string){ +func (a *Analyzer) mapDetectVulnerabilityByLanguage() map[languages.Language]func(*sync.WaitGroup, string) error { + return map[languages.Language]func(*sync.WaitGroup, string) error{ languages.CSharp: a.detectVulnerabilityCsharp, languages.Leaks: a.detectVulnerabilityLeaks, languages.Go: a.detectVulnerabilityGo, @@ -233,200 +247,144 @@ func (a *Analyzer) mapDetectVulnerabilityByLanguage() map[languages.Language]fun } } -func (a *Analyzer) detectVulneravilitySwift(projectSubPath string) { - a.monitor.AddProcess(1) - go horusecswift.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulneravilitySwift(_ *sync.WaitGroup, projectSubPath string) error { + horusecswift.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityCsharp(projectSubPath string) { - const TotalProcess = 2 - a.monitor.AddProcess(TotalProcess) - go horuseccsharp.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityCsharp(wg *sync.WaitGroup, projectSubPath string) error { + spawn(wg, horuseccsharp.NewFormatter(a.formatter), projectSubPath) - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.CSharp)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.CSharp)); err != nil { + return err } - - go scs.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + scs.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityLeaks(projectSubPath string) { - const TotalProcess = 2 - a.monitor.AddProcess(TotalProcess) - go horusecleaks.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - a.executeGitLeaks(projectSubPath) -} +func (a *Analyzer) detectVulnerabilityLeaks(wg *sync.WaitGroup, projectSubPath string) error { + spawn(wg, horusecleaks.NewFormatter(a.formatter), projectSubPath) -func (a *Analyzer) executeGitLeaks(projectSubPath string) { - const TotalProcess = 1 if a.config.GetEnableGitHistoryAnalysis() { logger.LogWarnWithLevel(messages.MsgWarnGitHistoryEnable) - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Leaks)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Leaks)); err != nil { + return err } - - go gitleaks.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - } else { - a.monitor.RemoveProcess(TotalProcess) + gitleaks.NewFormatter(a.formatter).StartAnalysis(projectSubPath) } + return nil } -func (a *Analyzer) detectVulnerabilityGo(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Go)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityGo(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Go)); err != nil { + return err } - - go gosec.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + gosec.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityJava(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - go horusecjava.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityJava(_ *sync.WaitGroup, projectSubPath string) error { + horusecjava.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityKotlin(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - go horuseckotlin.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityKotlin(_ *sync.WaitGroup, projectSubPath string) error { + horuseckotlin.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityNginx(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - go horusecnginx.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityNginx(_ *sync.WaitGroup, projectSubPath string) error { + horusecnginx.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityJavascript(projectSubPath string) { - const TotalProcess = 3 - a.monitor.AddProcess(TotalProcess) - go horusecnodejs.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityJavascript(wg *sync.WaitGroup, projectSubPath string) error { + spawn(wg, horusecnodejs.NewFormatter(a.formatter), projectSubPath) - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Javascript)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Javascript)); err != nil { + return err } - - go yarnaudit.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - go npmaudit.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + spawn(wg, yarnaudit.NewFormatter(a.formatter), projectSubPath) + npmaudit.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityPython(projectSubPath string) { - const TotalProcess = 2 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Python)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityPython(wg *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Python)); err != nil { + return err } - - go bandit.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - go safety.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + spawn(wg, bandit.NewFormatter(a.formatter), projectSubPath) + safety.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityRuby(projectSubPath string) { - const TotalProcess = 2 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Ruby)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityRuby(wg *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Ruby)); err != nil { + return err } - - go brakeman.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - go bundler.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + spawn(wg, brakeman.NewFormatter(a.formatter), projectSubPath) + bundler.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityHCL(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.HCL)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityHCL(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.HCL)); err != nil { + return err } - - go hcl.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + hcl.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityYaml(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - go horuseckubernetes.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityYaml(_ *sync.WaitGroup, projectSubPath string) error { + horuseckubernetes.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityC(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.C)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityC(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.C)); err != nil { + return err } - - go flawfinder.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + flawfinder.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityPHP(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.PHP)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityPHP(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.PHP)); err != nil { + return err } - - go phpcs.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + phpcs.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityGeneric(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Generic)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityGeneric(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Generic)); err != nil { + return err } - - go semgrep.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + semgrep.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityDart(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - go horusecdart.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) +func (a *Analyzer) detectVulnerabilityDart(_ *sync.WaitGroup, projectSubPath string) error { + horusecdart.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityElixir(projectSubPath string) { - const TotalProcess = 2 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Elixir)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityElixir(wg *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Elixir)); err != nil { + return err } - - go mixaudit.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) - go sobelow.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + spawn(wg, mixaudit.NewFormatter(a.formatter), projectSubPath) + sobelow.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } -func (a *Analyzer) detectVulnerabilityShell(projectSubPath string) { - const TotalProcess = 1 - a.monitor.AddProcess(TotalProcess) - - if err := a.dockerSDK.PullImage(a.getCustomOrDefaultImage(languages.Shell)); err != nil { - a.setErrorAndRemoveProcess(err, TotalProcess) - return +func (a *Analyzer) detectVulnerabilityShell(_ *sync.WaitGroup, projectSubPath string) error { + if err := a.docker.PullImage(a.getCustomOrDefaultImage(languages.Shell)); err != nil { + return err } - - go shellcheck.NewFormatter(a.formatterService).StartAnalysis(projectSubPath) + shellcheck.NewFormatter(a.formatter).StartAnalysis(projectSubPath) + return nil } func (a *Analyzer) logProjectSubPath(language languages.Language, subPath string) { @@ -460,11 +418,6 @@ func (a *Analyzer) setFalsePositive() *analysis.Analysis { return a.analysis } -func (a *Analyzer) setErrorAndRemoveProcess(err error, processNumber int) { - a.setAnalysisError(err) - a.monitor.RemoveProcess(processNumber) -} - func (a *Analyzer) setAnalysisError(err error) { if err != nil { toAppend := "" @@ -620,3 +573,11 @@ func (a *Analyzer) removeVulnerabilitiesByTypes() *analysis.Analysis { return a.analysis } + +func spawn(wg *sync.WaitGroup, f formatters.IFormatter, src string) { + wg.Add(1) + go func() { + defer wg.Done() + f.StartAnalysis(src) + }() +} diff --git a/internal/controllers/analyzer/analyzer_test.go b/internal/controllers/analyzer/analyzer_test.go index d17dd944a..cafc195c5 100644 --- a/internal/controllers/analyzer/analyzer_test.go +++ b/internal/controllers/analyzer/analyzer_test.go @@ -21,7 +21,6 @@ import ( "testing" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" horusecAPI "github.com/ZupIT/horusec/internal/services/horusec_api" "github.com/ZupIT/horusec/internal/utils/mock" @@ -94,12 +93,12 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) { dockerSDK := docker.NewDockerAPI(dockerMocker, configs, uuid.New()) controller := &Analyzer{ - dockerSDK: dockerSDK, - config: configs, - languageDetect: languageDetectMock, - printController: printResultMock, - horusecAPIService: horusecAPIMock, - formatterService: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs, &monitor.Monitor{}), + docker: dockerSDK, + config: configs, + languageDetect: languageDetectMock, + printController: printResultMock, + horusec: horusecAPIMock, + formatter: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs), } controller.analysis = &entitiesAnalysis.Analysis{ID: uuid.New()} @@ -151,12 +150,12 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) { dockerSDK := docker.NewDockerAPI(dockerMocker, configs, uuid.New()) controller := &Analyzer{ - dockerSDK: dockerSDK, - config: configs, - languageDetect: languageDetectMock, - printController: printResultMock, - horusecAPIService: horusecAPIMock, - formatterService: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs, &monitor.Monitor{}), + docker: dockerSDK, + config: configs, + languageDetect: languageDetectMock, + printController: printResultMock, + horusec: horusecAPIMock, + formatter: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs), } controller.analysis = &entitiesAnalysis.Analysis{ID: uuid.New()} @@ -193,12 +192,12 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) { dockerSDK := docker.NewDockerAPI(dockerMocker, configs, uuid.New()) controller := &Analyzer{ - dockerSDK: dockerSDK, - config: configs, - languageDetect: languageDetectMock, - printController: printResultMock, - horusecAPIService: horusecAPIMock, - formatterService: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs, &monitor.Monitor{}), + docker: dockerSDK, + config: configs, + languageDetect: languageDetectMock, + printController: printResultMock, + horusec: horusecAPIMock, + formatter: formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, dockerSDK, configs), } controller.analysis = &entitiesAnalysis.Analysis{ID: uuid.New()} diff --git a/internal/entities/monitor/monitor.go b/internal/entities/monitor/monitor.go deleted file mode 100644 index 6b6bebe6e..000000000 --- a/internal/entities/monitor/monitor.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package monitor - -type Monitor struct { - process int - started bool -} - -func NewMonitor() *Monitor { - return &Monitor{ - process: 0, - started: false, - } -} - -func (m *Monitor) AddProcess(n int) { - if !m.started { - m.started = true - } - m.process += n -} - -func (m *Monitor) RemoveProcess(n int) { - m.process -= n -} - -func (m *Monitor) IsFinished() bool { - return m.started && m.process <= 0 -} - -func (m *Monitor) IsRunning() bool { - return m.started && m.process > 0 -} - -func (m *Monitor) GetProcess() int { - return m.process -} diff --git a/internal/entities/monitor/monitor_test.go b/internal/entities/monitor/monitor_test.go deleted file mode 100644 index 3bf4dbb41..000000000 --- a/internal/entities/monitor/monitor_test.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package monitor - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewMonitor(t *testing.T) { - t.Run("should creates a Monitor instance", func(t *testing.T) { - monitor := NewMonitor() - assert.NotNil(t, monitor) - }) -} - -func TestAddProcess(t *testing.T) { - t.Run("should increment processes and start the monitor", func(t *testing.T) { - monitor := NewMonitor() - monitor.AddProcess(1) - assert.True(t, monitor.IsRunning()) - assert.Equal(t, 1, monitor.GetProcess()) - }) -} - -func TestRemoveProcess(t *testing.T) { - t.Run("should decrement processes and stop the monitor", func(t *testing.T) { - monitor := NewMonitor() - monitor.AddProcess(1) - monitor.RemoveProcess(1) - - assert.True(t, monitor.IsFinished()) - assert.Equal(t, 0, monitor.GetProcess()) - }) -} diff --git a/internal/services/formatters/c/flawfinder/formatter.go b/internal/services/formatters/c/flawfinder/formatter.go index 6362a5814..4e51a0565 100644 --- a/internal/services/formatters/c/flawfinder/formatter.go +++ b/internal/services/formatters/c/flawfinder/formatter.go @@ -47,7 +47,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startFlawfinder(projectSubPath), tools.Flawfinder, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Flawfinder, languages.C) - f.SetToolFinishedAnalysis() } func (f *Formatter) startFlawfinder(projectSubPath string) error { diff --git a/internal/services/formatters/c/flawfinder/formatter_test.go b/internal/services/formatters/c/flawfinder/formatter_test.go index fd7b55e37..0276ff85b 100644 --- a/internal/services/formatters/c/flawfinder/formatter_test.go +++ b/internal/services/formatters/c/flawfinder/formatter_test.go @@ -23,7 +23,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -67,7 +66,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -86,7 +85,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -102,7 +101,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -117,7 +116,7 @@ func TestStartCFlawfinder(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Flawfinder: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/csharp/scs/formatter.go b/internal/services/formatters/csharp/scs/formatter.go index 7f975f05e..bc7b1ab98 100644 --- a/internal/services/formatters/csharp/scs/formatter.go +++ b/internal/services/formatters/csharp/scs/formatter.go @@ -51,7 +51,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startSecurityCodeScan(projectSubPath), tools.SecurityCodeScan, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.SecurityCodeScan, languages.CSharp) - f.SetToolFinishedAnalysis() } func (f *Formatter) startSecurityCodeScan(projectSubPath string) error { diff --git a/internal/services/formatters/csharp/scs/formatter_test.go b/internal/services/formatters/csharp/scs/formatter_test.go index 543dace16..038dcd02e 100644 --- a/internal/services/formatters/csharp/scs/formatter_test.go +++ b/internal/services/formatters/csharp/scs/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -50,7 +49,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -68,7 +67,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -84,7 +83,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -98,7 +97,7 @@ func TestParseOutput(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{SecurityCodeScan: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -110,7 +109,7 @@ func TestParseStringToStruct(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, nil, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(&entitiesAnalysis.Analysis{}, nil, config) formatter := Formatter{ service, diff --git a/internal/services/formatters/default_engine_formatter.go b/internal/services/formatters/default_engine_formatter.go index c870abd71..8ce3bb52a 100644 --- a/internal/services/formatters/default_engine_formatter.go +++ b/internal/services/formatters/default_engine_formatter.go @@ -50,7 +50,6 @@ func (f *DefaultFormatter) StartAnalysis(src string) { } f.svc.SetAnalysisError(f.execEngineAndParseResults(src), tools.HorusecEngine, src) f.svc.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.HorusecEngine, f.language) - f.svc.SetToolFinishedAnalysis() } func (f *DefaultFormatter) execEngineAndParseResults(src string) error { diff --git a/internal/services/formatters/elixir/mixaudit/formatter.go b/internal/services/formatters/elixir/mixaudit/formatter.go index 7e127f301..3c19861ed 100644 --- a/internal/services/formatters/elixir/mixaudit/formatter.go +++ b/internal/services/formatters/elixir/mixaudit/formatter.go @@ -50,7 +50,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startMixAudit(projectSubPath), tools.MixAudit, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.MixAudit, languages.Elixir) - f.SetToolFinishedAnalysis() } func (f *Formatter) startMixAudit(projectSubPath string) error { diff --git a/internal/services/formatters/elixir/mixaudit/formatter_test.go b/internal/services/formatters/elixir/mixaudit/formatter_test.go index 3daf33e96..f30b9840d 100644 --- a/internal/services/formatters/elixir/mixaudit/formatter_test.go +++ b/internal/services/formatters/elixir/mixaudit/formatter_test.go @@ -20,8 +20,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" - "github.com/ZupIT/horusec/internal/entities/monitor" - "github.com/stretchr/testify/assert" "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" @@ -46,7 +44,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -65,7 +63,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -81,7 +79,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -96,7 +94,7 @@ func TestStartCFlawfinder(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{MixAudit: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/elixir/sobelow/formatter.go b/internal/services/formatters/elixir/sobelow/formatter.go index 370f67c1d..9f86e1b56 100644 --- a/internal/services/formatters/elixir/sobelow/formatter.go +++ b/internal/services/formatters/elixir/sobelow/formatter.go @@ -55,7 +55,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startSobelow(projectSubPath), tools.Sobelow, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Sobelow, languages.Elixir) - f.SetToolFinishedAnalysis() } func (f *Formatter) startSobelow(projectSubPath string) error { diff --git a/internal/services/formatters/elixir/sobelow/formatter_test.go b/internal/services/formatters/elixir/sobelow/formatter_test.go index 2f40eb4bd..8b8bcf3e8 100644 --- a/internal/services/formatters/elixir/sobelow/formatter_test.go +++ b/internal/services/formatters/elixir/sobelow/formatter_test.go @@ -20,8 +20,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" - "github.com/ZupIT/horusec/internal/entities/monitor" - "github.com/stretchr/testify/assert" "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" @@ -55,7 +53,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -74,7 +72,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -90,7 +88,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -105,7 +103,7 @@ func TestStartCFlawfinder(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Sobelow: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/generic/semgrep/formatter.go b/internal/services/formatters/generic/semgrep/formatter.go index 41730730c..ce992fab3 100644 --- a/internal/services/formatters/generic/semgrep/formatter.go +++ b/internal/services/formatters/generic/semgrep/formatter.go @@ -52,7 +52,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startSemgrep(projectSubPath), tools.SecurityCodeScan, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Semgrep, languages.Generic) - f.SetToolFinishedAnalysis() } func (f *Formatter) startSemgrep(projectSubPath string) error { diff --git a/internal/services/formatters/generic/semgrep/formatter_test.go b/internal/services/formatters/generic/semgrep/formatter_test.go index bcbfd155b..85d1e3ba7 100644 --- a/internal/services/formatters/generic/semgrep/formatter_test.go +++ b/internal/services/formatters/generic/semgrep/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -48,7 +47,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -71,7 +70,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -94,7 +93,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -112,7 +111,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -128,7 +127,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -142,7 +141,7 @@ func TestParseOutput(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Semgrep: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/go/gosec/formatter.go b/internal/services/formatters/go/gosec/formatter.go index ecb8ebf9e..7f9fa774c 100644 --- a/internal/services/formatters/go/gosec/formatter.go +++ b/internal/services/formatters/go/gosec/formatter.go @@ -50,7 +50,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startGoSec(projectSubPath), tools.GoSec, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.GoSec, languages.Go) - f.SetToolFinishedAnalysis() } func (f *Formatter) startGoSec(projectSubPath string) error { diff --git a/internal/services/formatters/go/gosec/formatter_test.go b/internal/services/formatters/go/gosec/formatter_test.go index d5972abe9..7a5e97e4e 100644 --- a/internal/services/formatters/go/gosec/formatter_test.go +++ b/internal/services/formatters/go/gosec/formatter_test.go @@ -20,8 +20,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" - "github.com/ZupIT/horusec/internal/entities/monitor" - "github.com/stretchr/testify/assert" "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" @@ -40,7 +38,7 @@ func TestGoLang_StartAnalysis(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config) golangAnalyzer := NewFormatter(service) @@ -72,7 +70,7 @@ func TestGoLang_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(outputAnalysis, nil) - service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config) golangAnalyzer := NewFormatter(service) @@ -89,7 +87,7 @@ func TestGoLang_StartAnalysis(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config) golangAnalyzer := NewFormatter(service) @@ -108,7 +106,7 @@ func TestGoLang_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(outputAnalysis, nil) - service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(&analysis.Analysis{}, dockerAPIControllerMock, config) golangAnalyzer := NewFormatter(service) @@ -124,7 +122,7 @@ func TestGoLang_StartAnalysis(t *testing.T) { config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{GoSec: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(entity, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/hcl/formatter.go b/internal/services/formatters/hcl/formatter.go index 858193610..f7283655c 100644 --- a/internal/services/formatters/hcl/formatter.go +++ b/internal/services/formatters/hcl/formatter.go @@ -51,7 +51,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startTfSec(projectSubPath), tools.TfSec, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.TfSec, languages.HCL) - f.SetToolFinishedAnalysis() } func (f *Formatter) startTfSec(projectSubPath string) error { diff --git a/internal/services/formatters/hcl/formatter_test.go b/internal/services/formatters/hcl/formatter_test.go index 0163ce20b..d1f586615 100644 --- a/internal/services/formatters/hcl/formatter_test.go +++ b/internal/services/formatters/hcl/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -42,7 +41,7 @@ func TestStartHCLTfSec(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -61,7 +60,7 @@ func TestStartHCLTfSec(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -77,7 +76,7 @@ func TestStartHCLTfSec(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -90,7 +89,7 @@ func TestStartHCLTfSec(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{TfSec: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/interface.go b/internal/services/formatters/interface.go index b07e78250..cd46c1456 100644 --- a/internal/services/formatters/interface.go +++ b/internal/services/formatters/interface.go @@ -22,7 +22,6 @@ import ( engine "github.com/ZupIT/horusec-engine" commitAuthor "github.com/ZupIT/horusec/internal/entities/commit_author" dockerEntities "github.com/ZupIT/horusec/internal/entities/docker" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/ZupIT/horusec/internal/entities/toolsconfig" ) @@ -40,9 +39,7 @@ type IService interface { GetConfigProjectPath() string GetToolsConfig() toolsconfig.MapToolConfig GetAnalysis() *entitiesAnalysis.Analysis - SetToolFinishedAnalysis() SetAnalysisError(err error, tool tools.Tool, projectSubPath string) - SetMonitor(monitor *monitor.Monitor) RemoveSrcFolderFromPath(filepath string) string GetCodeWithMaxCharacters(code string, column int) string ToolIsToIgnore(tool tools.Tool) bool diff --git a/internal/services/formatters/javascript/npmaudit/formatter.go b/internal/services/formatters/javascript/npmaudit/formatter.go index 2b5c12958..736b246b1 100644 --- a/internal/services/formatters/javascript/npmaudit/formatter.go +++ b/internal/services/formatters/javascript/npmaudit/formatter.go @@ -54,7 +54,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startNpmAudit(projectSubPath), tools.NpmAudit, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.NpmAudit, languages.Javascript) - f.SetToolFinishedAnalysis() } func (f *Formatter) startNpmAudit(projectSubPath string) error { diff --git a/internal/services/formatters/javascript/npmaudit/formatter_test.go b/internal/services/formatters/javascript/npmaudit/formatter_test.go index fc047a5d2..eea98646d 100644 --- a/internal/services/formatters/javascript/npmaudit/formatter_test.go +++ b/internal/services/formatters/javascript/npmaudit/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -44,7 +43,7 @@ func TestStartNpmAudit(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -63,7 +62,7 @@ func TestStartNpmAudit(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -82,7 +81,7 @@ func TestStartNpmAudit(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -102,7 +101,7 @@ func TestStartNpmAudit(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -119,7 +118,7 @@ func TestStartNpmAudit(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -131,7 +130,7 @@ func TestStartNpmAudit(t *testing.T) { dockerAPIControllerMock := &docker.Mock{} config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{NpmAudit: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -147,7 +146,7 @@ func TestParseOutputNpm(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := Formatter{ service, diff --git a/internal/services/formatters/javascript/yarnaudit/formatter.go b/internal/services/formatters/javascript/yarnaudit/formatter.go index 788890460..73012153c 100644 --- a/internal/services/formatters/javascript/yarnaudit/formatter.go +++ b/internal/services/formatters/javascript/yarnaudit/formatter.go @@ -54,7 +54,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startYarnAudit(projectSubPath), tools.YarnAudit, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.YarnAudit, languages.Javascript) - f.SetToolFinishedAnalysis() } func (f *Formatter) startYarnAudit(projectSubPath string) error { diff --git a/internal/services/formatters/javascript/yarnaudit/formatter_test.go b/internal/services/formatters/javascript/yarnaudit/formatter_test.go index 3191bf8d7..5cfd753ad 100644 --- a/internal/services/formatters/javascript/yarnaudit/formatter_test.go +++ b/internal/services/formatters/javascript/yarnaudit/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -44,7 +43,7 @@ func TestParseOutputYarn(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -64,7 +63,7 @@ func TestParseOutputYarn(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -84,7 +83,7 @@ func TestParseOutputYarn(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -104,7 +103,7 @@ func TestParseOutputYarn(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -121,7 +120,7 @@ func TestParseOutputYarn(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -134,7 +133,7 @@ func TestParseOutputYarn(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{YarnAudit: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -150,7 +149,7 @@ func TestParseOutputNpm(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := Formatter{ service, diff --git a/internal/services/formatters/leaks/gitleaks/formatter.go b/internal/services/formatters/leaks/gitleaks/formatter.go index 5ff16cbc0..90d579d41 100644 --- a/internal/services/formatters/leaks/gitleaks/formatter.go +++ b/internal/services/formatters/leaks/gitleaks/formatter.go @@ -53,7 +53,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startGitLeaks(projectSubPath), tools.GitLeaks, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.GitLeaks, languages.Leaks) - f.SetToolFinishedAnalysis() } func (f *Formatter) startGitLeaks(projectSubPath string) error { diff --git a/internal/services/formatters/leaks/gitleaks/formatter_test.go b/internal/services/formatters/leaks/gitleaks/formatter_test.go index db42e2409..213710132 100644 --- a/internal/services/formatters/leaks/gitleaks/formatter_test.go +++ b/internal/services/formatters/leaks/gitleaks/formatter_test.go @@ -23,7 +23,6 @@ import ( entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" enumsAnalysis "github.com/ZupIT/horusec-devkit/pkg/enums/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -69,7 +68,7 @@ func TestLeaks_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(outputAnalysis, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) leaksAnalyzer := NewFormatter(service) @@ -87,7 +86,7 @@ func TestLeaks_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) leaksAnalyzer := NewFormatter(service) @@ -105,7 +104,7 @@ func TestLeaks_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("some error")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) leaksAnalyzer := NewFormatter(service) @@ -126,7 +125,7 @@ func TestLeaks_StartAnalysis(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(outputAnalysis, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) leaksAnalyzer := NewFormatter(service) @@ -140,7 +139,7 @@ func TestLeaks_StartAnalysis(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{GitLeaks: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/php/phpcs/formatter.go b/internal/services/formatters/php/phpcs/formatter.go index 5b75eb42a..c2ead6151 100644 --- a/internal/services/formatters/php/phpcs/formatter.go +++ b/internal/services/formatters/php/phpcs/formatter.go @@ -51,7 +51,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startPhpCs(projectSubPath), tools.PhpCS, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.PhpCS, languages.PHP) - f.SetToolFinishedAnalysis() } func (f *Formatter) startPhpCs(projectSubPath string) error { diff --git a/internal/services/formatters/php/phpcs/formatter_test.go b/internal/services/formatters/php/phpcs/formatter_test.go index 6cb1379a2..dc2b979f6 100644 --- a/internal/services/formatters/php/phpcs/formatter_test.go +++ b/internal/services/formatters/php/phpcs/formatter_test.go @@ -24,7 +24,6 @@ import ( entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" cliConfig "github.com/ZupIT/horusec/config" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/ZupIT/horusec/internal/entities/workdir" "github.com/ZupIT/horusec/internal/services/docker" "github.com/ZupIT/horusec/internal/services/formatters" @@ -41,7 +40,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") @@ -60,7 +59,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -76,7 +75,7 @@ func TestStartCFlawfinder(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("test")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) assert.NotPanics(t, func() { @@ -89,7 +88,7 @@ func TestStartCFlawfinder(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{PhpCS: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/python/bandit/formatter.go b/internal/services/formatters/python/bandit/formatter.go index 5d8b103da..72f77a87a 100644 --- a/internal/services/formatters/python/bandit/formatter.go +++ b/internal/services/formatters/python/bandit/formatter.go @@ -51,7 +51,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startBandit(projectSubPath), tools.Bandit, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Bandit, languages.Python) - f.SetToolFinishedAnalysis() } func (f *Formatter) startBandit(projectSubPath string) error { diff --git a/internal/services/formatters/python/bandit/formatter_test.go b/internal/services/formatters/python/bandit/formatter_test.go index 8ad2e7e1b..b7d7c47dc 100644 --- a/internal/services/formatters/python/bandit/formatter_test.go +++ b/internal/services/formatters/python/bandit/formatter_test.go @@ -23,7 +23,6 @@ import ( entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" enumHorusec "github.com/ZupIT/horusec-devkit/pkg/enums/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -50,7 +49,7 @@ func TestNewFormatter(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(nil, nil, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(nil, nil, config) assert.IsType(t, NewFormatter(service), &Formatter{}) } @@ -66,7 +65,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("Error")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -86,7 +85,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -106,7 +105,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -126,7 +125,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -145,7 +144,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("some aleatory text", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -159,7 +158,7 @@ func TestFormatter_StartSafety(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Bandit: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/python/safety/formatter.go b/internal/services/formatters/python/safety/formatter.go index e0051787a..6e9258ed0 100644 --- a/internal/services/formatters/python/safety/formatter.go +++ b/internal/services/formatters/python/safety/formatter.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strconv" "strings" @@ -57,7 +58,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startSafety(projectSubPath), tools.Safety, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Safety, languages.Python) - f.SetToolFinishedAnalysis() } func (f *Formatter) startSafety(projectSubPath string) error { @@ -124,7 +124,7 @@ func (f *Formatter) setupVulnerabilitiesSeveritiesSafety( } func (f *Formatter) getVulnerabilityLineByName(line, fileName string) string { - path := fmt.Sprintf("%s/%s", f.GetConfigProjectPath(), fileName) + path := filepath.Join(f.GetConfigProjectPath(), fileName) fileOpened, err := os.Open(path) if err != nil { return "-" diff --git a/internal/services/formatters/python/safety/formatter_test.go b/internal/services/formatters/python/safety/formatter_test.go index 5754ea078..af2954bf2 100644 --- a/internal/services/formatters/python/safety/formatter_test.go +++ b/internal/services/formatters/python/safety/formatter_test.go @@ -23,7 +23,6 @@ import ( entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" enumHorusec "github.com/ZupIT/horusec-devkit/pkg/enums/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -50,7 +49,7 @@ func TestNewFormatter(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(nil, nil, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(nil, nil, config) assert.IsType(t, NewFormatter(service), &Formatter{}) } @@ -66,7 +65,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", errors.New("Error")) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -85,7 +84,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -104,7 +103,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -123,7 +122,7 @@ func TestFormatter_StartSafety(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("some aleatory text", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -137,7 +136,7 @@ func TestFormatter_StartSafety(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Safety: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/ruby/brakeman/formatter.go b/internal/services/formatters/ruby/brakeman/formatter.go index 7fe7b2a9b..ccd4a1e01 100644 --- a/internal/services/formatters/ruby/brakeman/formatter.go +++ b/internal/services/formatters/ruby/brakeman/formatter.go @@ -51,7 +51,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startBrakeman(projectSubPath), tools.Brakeman, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.Brakeman, languages.Ruby) - f.SetToolFinishedAnalysis() } func (f *Formatter) startBrakeman(projectSubPath string) error { diff --git a/internal/services/formatters/ruby/brakeman/formatter_test.go b/internal/services/formatters/ruby/brakeman/formatter_test.go index 2dc67427b..6931d4e6f 100644 --- a/internal/services/formatters/ruby/brakeman/formatter_test.go +++ b/internal/services/formatters/ruby/brakeman/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -45,7 +44,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -69,7 +68,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -93,7 +92,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -114,7 +113,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("invalid output", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -130,7 +129,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -143,7 +142,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{Brakeman: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/ruby/bundler/formatter.go b/internal/services/formatters/ruby/bundler/formatter.go index 1ba75c34d..3765e7e06 100644 --- a/internal/services/formatters/ruby/bundler/formatter.go +++ b/internal/services/formatters/ruby/bundler/formatter.go @@ -56,7 +56,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startBundlerAudit(projectSubPath), tools.BundlerAudit, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.BundlerAudit, languages.Ruby) - f.SetToolFinishedAnalysis() } func (f *Formatter) startBundlerAudit(projectSubPath string) error { diff --git a/internal/services/formatters/ruby/bundler/formatter_test.go b/internal/services/formatters/ruby/bundler/formatter_test.go index b96c67fea..60ac2ea21 100644 --- a/internal/services/formatters/ruby/bundler/formatter_test.go +++ b/internal/services/formatters/ruby/bundler/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -45,7 +44,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -66,7 +65,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("invalid output", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -84,7 +83,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer"). Return("No such file or directory Errno::ENOENT", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -102,7 +101,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer"). Return("No vulnerabilities found", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -118,7 +117,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -131,7 +130,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{BundlerAudit: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/services/formatters/service.go b/internal/services/formatters/service.go index 215701b05..d76e75070 100644 --- a/internal/services/formatters/service.go +++ b/internal/services/formatters/service.go @@ -20,12 +20,12 @@ import ( "path" "strconv" "strings" + "sync" - entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" + "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" "github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability" "github.com/ZupIT/horusec-devkit/pkg/enums/confidence" commitAuthor "github.com/ZupIT/horusec/internal/entities/commit_author" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/ZupIT/horusec/internal/utils/file" vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash" @@ -34,33 +34,32 @@ import ( "github.com/ZupIT/horusec-devkit/pkg/enums/tools" "github.com/ZupIT/horusec-devkit/pkg/utils/logger" engine "github.com/ZupIT/horusec-engine" - cliConfig "github.com/ZupIT/horusec/config" + "github.com/ZupIT/horusec/config" dockerEntities "github.com/ZupIT/horusec/internal/entities/docker" "github.com/ZupIT/horusec/internal/entities/toolsconfig" "github.com/ZupIT/horusec/internal/helpers/messages" customRules "github.com/ZupIT/horusec/internal/services/custom_rules" - dockerService "github.com/ZupIT/horusec/internal/services/docker" + "github.com/ZupIT/horusec/internal/services/docker" "github.com/ZupIT/horusec/internal/services/git" ) type Service struct { - analysis *entitiesAnalysis.Analysis - docker dockerService.Interface + mutex *sync.Mutex + analysis *analysis.Analysis + docker docker.Interface gitService git.IService - monitor *monitor.Monitor - config cliConfig.IConfig + config config.IConfig customRulesService customRules.IService } -func NewFormatterService(analysis *entitiesAnalysis.Analysis, docker dockerService.Interface, config cliConfig.IConfig, - monitorEntity *monitor.Monitor) IService { +func NewFormatterService(analysiss *analysis.Analysis, dockerSvc docker.Interface, cfg config.IConfig) IService { return &Service{ - analysis: analysis, - docker: docker, - gitService: git.NewGitService(config), - monitor: monitorEntity, - config: config, - customRulesService: customRules.NewCustomRulesService(config), + mutex: new(sync.Mutex), + analysis: analysiss, + docker: dockerSvc, + gitService: git.NewGitService(cfg), + config: cfg, + customRulesService: customRules.NewCustomRulesService(cfg), } } @@ -113,12 +112,14 @@ func (s *Service) GetAnalysisID() string { return s.analysis.GetIDString() } -func (s *Service) GetAnalysis() *entitiesAnalysis.Analysis { +func (s *Service) GetAnalysis() *analysis.Analysis { return s.analysis } func (s *Service) SetAnalysisError(err error, tool tools.Tool, projectSubPath string) { if err != nil { + s.mutex.Lock() + defer s.mutex.Unlock() s.addAnalysisError(err) msg := s.GetAnalysisIDErrorMessage(tool, "") if projectSubPath != "" { @@ -141,14 +142,6 @@ func (s *Service) addAnalysisError(err error) { } } -func (s *Service) SetToolFinishedAnalysis() { - s.monitor.RemoveProcess(1) -} - -func (s *Service) SetMonitor(monitorToSet *monitor.Monitor) { - s.monitor = monitorToSet -} - func (s *Service) RemoveSrcFolderFromPath(filepath string) string { if filepath == "" || len(filepath) <= 4 || !strings.Contains(filepath[:4], "src") { return filepath @@ -171,9 +164,8 @@ func (s *Service) GetCodeWithMaxCharacters(code string, column int) string { } func (s *Service) ToolIsToIgnore(tool tools.Tool) bool { - if s.config.GetToolsConfig()[tool].IsToIgnore { - s.SetToolFinishedAnalysis() - return true + if tool, exists := s.config.GetToolsConfig()[tool]; exists { + return tool.IsToIgnore } return false } @@ -241,7 +233,7 @@ func (s *Service) setVulnerabilityDataByFindings(findings []engine.Finding, inde func (s *Service) AddNewVulnerabilityIntoAnalysis(vuln *vulnerability.Vulnerability) { s.GetAnalysis().AnalysisVulnerabilities = append(s.GetAnalysis().AnalysisVulnerabilities, - entitiesAnalysis.AnalysisVulnerabilities{ + analysis.AnalysisVulnerabilities{ Vulnerability: *vuln, }) } @@ -267,12 +259,7 @@ func (s *Service) removeHorusecFolder(filepath string) string { } func (s *Service) IsDockerDisabled() bool { - isDisabled := s.config.GetDisableDocker() - if isDisabled { - s.SetToolFinishedAnalysis() - } - - return isDisabled + return s.config.GetDisableDocker() } func (s *Service) GetCustomRulesByLanguage(lang languages.Language) []engine.Rule { diff --git a/internal/services/formatters/service_mock.go b/internal/services/formatters/service_mock.go index e334c0efb..ca042b6f6 100644 --- a/internal/services/formatters/service_mock.go +++ b/internal/services/formatters/service_mock.go @@ -25,7 +25,6 @@ import ( engine "github.com/ZupIT/horusec-engine" commitAuthor "github.com/ZupIT/horusec/internal/entities/commit_author" dockerEntities "github.com/ZupIT/horusec/internal/entities/docker" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/ZupIT/horusec/internal/entities/toolsconfig" ) @@ -72,18 +71,10 @@ func (m *Mock) GetAnalysis() *entitiesAnalysis.Analysis { return args.Get(0).(*entitiesAnalysis.Analysis) } -func (m *Mock) SetToolFinishedAnalysis() { - _ = m.MethodCalled("SetToolFinishedAnalysis") -} - func (m *Mock) SetAnalysisError(_ error, _ tools.Tool, _ string) { _ = m.MethodCalled("SetAnalysisError") } -func (m *Mock) SetMonitor(_ *monitor.Monitor) { - _ = m.MethodCalled("SetMonitor") -} - func (m *Mock) RemoveSrcFolderFromPath(_ string) string { args := m.MethodCalled("RemoveSrcFolderFromPath") return args.Get(0).(string) diff --git a/internal/services/formatters/service_test.go b/internal/services/formatters/service_test.go index d3f875b45..6de5dcb47 100644 --- a/internal/services/formatters/service_test.go +++ b/internal/services/formatters/service_test.go @@ -25,7 +25,6 @@ import ( entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" commitAuthor "github.com/ZupIT/horusec/internal/entities/commit_author" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -63,7 +62,6 @@ func TestMock_AddWorkDirInCmd(t *testing.T) { _ = mock.GetConfigProjectPath() _ = mock.GetAnalysis() mock.SetAnalysisError(errors.New(""), "", "") - mock.SetMonitor(&monitor.Monitor{}) _ = mock.RemoveSrcFolderFromPath("") _ = mock.GetCodeWithMaxCharacters("", 0) }) @@ -77,7 +75,7 @@ func TestExecuteContainer(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("test", nil) - monitorController := NewFormatterService(analysis, dockerAPIControllerMock, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(analysis, dockerAPIControllerMock, &config.Config{}) result, err := monitorController.ExecuteContainer(&dockerEntities.AnalysisData{}) assert.NoError(t, err) @@ -87,7 +85,7 @@ func TestExecuteContainer(t *testing.T) { func TestGetAnalysisIDErrorMessage(t *testing.T) { t.Run("should success get error message with replaces", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) result := monitorController.GetAnalysisIDErrorMessage(tools.Bandit, "test") @@ -99,7 +97,7 @@ func TestGetAnalysisIDErrorMessage(t *testing.T) { func TestGetCommitAuthor(t *testing.T) { t.Run("should get commit author", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) result := monitorController.GetCommitAuthor("", "") @@ -112,7 +110,7 @@ func TestGetConfigProjectPath(t *testing.T) { cliConfig := &config.Config{} cliConfig.SetProjectPath("test") - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig) result := monitorController.GetConfigProjectPath() @@ -128,7 +126,7 @@ func TestAddWorkDirInCmd(t *testing.T) { CSharp: []string{"test"}, }) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig) result := monitorController.AddWorkDirInCmd("test", "C#", tools.SecurityCodeScan) @@ -141,7 +139,7 @@ func TestAddWorkDirInCmd(t *testing.T) { CSharp: []string{"test"}, }) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, cliConfig) result := monitorController.AddWorkDirInCmd("test", "C#", tools.SecurityCodeScan) @@ -151,7 +149,7 @@ func TestAddWorkDirInCmd(t *testing.T) { func TestLogDebugWithReplace(t *testing.T) { t.Run("should log debug and not panics", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) assert.NotPanics(t, func() { monitorController.LogDebugWithReplace("test", tools.NpmAudit, languages.Javascript) @@ -162,7 +160,7 @@ func TestLogDebugWithReplace(t *testing.T) { func TestGetAnalysisID(t *testing.T) { t.Run("should success get analysis id", func(t *testing.T) { id := uuid.New() - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{ID: id}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{ID: id}, &docker.Mock{}, &config.Config{}) assert.Equal(t, id.String(), monitorController.GetAnalysisID()) }) } @@ -170,21 +168,21 @@ func TestGetAnalysisID(t *testing.T) { func TestGetAnalysis(t *testing.T) { t.Run("should success get analysis", func(t *testing.T) { id := uuid.New() - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{ID: id}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{ID: id}, &docker.Mock{}, &config.Config{}) assert.NotEmpty(t, monitorController.GetAnalysis()) }) } func TestLogAnalysisError(t *testing.T) { t.Run("should not panic when logging error", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) assert.NotPanics(t, func() { monitorController.SetAnalysisError(errors.New("test"), tools.GoSec, "") }) }) t.Run("should not panic when logging error and exists projectSubPath", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) assert.NotPanics(t, func() { monitorController.SetAnalysisError(errors.New("test"), tools.GoSec, "/tmp") @@ -192,57 +190,36 @@ func TestLogAnalysisError(t *testing.T) { }) } -func TestSetLanguageIsFinished(t *testing.T) { - t.Run("should set go as finished", func(t *testing.T) { - currentMonitor := monitor.NewMonitor() - currentMonitor.AddProcess(1) - - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) - monitorController.SetMonitor(currentMonitor) - - monitorController.SetToolFinishedAnalysis() - assert.Equal(t, 0, currentMonitor.GetProcess()) - }) -} - func TestToolIsToIgnore(t *testing.T) { t.Run("should return true when language is match", func(t *testing.T) { - currentMonitor := monitor.NewMonitor() - currentMonitor.AddProcess(1) configs := &config.Config{} configs.SetToolsConfig(toolsconfig.ToolsConfigsStruct{GoSec: toolsconfig.ToolConfig{IsToIgnore: true}}) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs) assert.Equal(t, true, monitorController.ToolIsToIgnore(tools.GoSec)) }) t.Run("should return true when language is match uppercase", func(t *testing.T) { - currentMonitor := monitor.NewMonitor() - currentMonitor.AddProcess(1) configs := &config.Config{} configs.SetToolsConfig(toolsconfig.ToolsConfigsStruct{GoSec: toolsconfig.ToolConfig{IsToIgnore: true}}) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs) assert.Equal(t, true, monitorController.ToolIsToIgnore(tools.GoSec)) }) t.Run("should return true when language is match lowercase and multi tools", func(t *testing.T) { - currentMonitor := monitor.NewMonitor() - currentMonitor.AddProcess(1) configs := &config.Config{} configs.SetToolsConfig(toolsconfig.ToolsConfigsStruct{GoSec: toolsconfig.ToolConfig{IsToIgnore: true}, SecurityCodeScan: toolsconfig.ToolConfig{IsToIgnore: true}}) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs) assert.Equal(t, true, monitorController.ToolIsToIgnore(tools.GoSec)) }) t.Run("should return false when language is not match", func(t *testing.T) { - currentMonitor := monitor.NewMonitor() - currentMonitor.AddProcess(1) configs := &config.Config{} configs.SetToolsConfig(toolsconfig.ToolsConfigsStruct{SecurityCodeScan: toolsconfig.ToolConfig{IsToIgnore: true}}) - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs, &monitor.Monitor{}) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, configs) assert.Equal(t, false, monitorController.ToolIsToIgnore(tools.GoSec)) }) @@ -250,21 +227,21 @@ func TestToolIsToIgnore(t *testing.T) { func TestService_GetCodeWithMaxCharacters(t *testing.T) { t.Run("should return default code", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" column := 0 newCode := monitorController.GetCodeWithMaxCharacters(code, column) assert.Equal(t, "text", newCode) }) t.Run("should return default code if column is negative", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" column := -1 newCode := monitorController.GetCodeWithMaxCharacters(code, column) assert.Equal(t, "text", newCode) }) t.Run("should return 4:105 characters when text is so bigger", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" for i := 0; i < 10; i++ { for i := 0; i <= 9; i++ { @@ -276,7 +253,7 @@ func TestService_GetCodeWithMaxCharacters(t *testing.T) { assert.Equal(t, "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", newCode) }) t.Run("should return first 100 characters when text is so bigger", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" for i := 0; i < 10; i++ { for i := 0; i <= 9; i++ { @@ -288,7 +265,7 @@ func TestService_GetCodeWithMaxCharacters(t *testing.T) { assert.Equal(t, "text012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", newCode) }) t.Run("should return first 100 characters when text contains breaking lines", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := `22: func GetMD5(s string) string { 23: h := md5.New() 24: io.WriteString(h, s) // #nohorus @@ -301,7 +278,7 @@ func TestService_GetCodeWithMaxCharacters(t *testing.T) { `, newCode) }) t.Run("should return first 100 characters when text is so bigger", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" for i := 0; i <= 200; i++ { code += strconv.Itoa(i) @@ -311,7 +288,7 @@ func TestService_GetCodeWithMaxCharacters(t *testing.T) { assert.Equal(t, "4041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889", newCode) }) t.Run("should return first 100 characters when text is so bigger", func(t *testing.T) { - monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}, nil) + monitorController := NewFormatterService(&entitiesAnalysis.Analysis{}, &docker.Mock{}, &config.Config{}) code := "text" for i := 0; i <= 200; i++ { code += strconv.Itoa(i) diff --git a/internal/services/formatters/shell/shellcheck/formatter.go b/internal/services/formatters/shell/shellcheck/formatter.go index f7b368401..e49a273ac 100644 --- a/internal/services/formatters/shell/shellcheck/formatter.go +++ b/internal/services/formatters/shell/shellcheck/formatter.go @@ -53,7 +53,6 @@ func (f *Formatter) StartAnalysis(projectSubPath string) { f.SetAnalysisError(f.startShellCheck(projectSubPath), tools.ShellCheck, projectSubPath) f.LogDebugWithReplace(messages.MsgDebugToolFinishAnalysis, tools.ShellCheck, languages.Shell) - f.SetToolFinishedAnalysis() } func (f *Formatter) startShellCheck(projectSubPath string) error { diff --git a/internal/services/formatters/shell/shellcheck/formatter_test.go b/internal/services/formatters/shell/shellcheck/formatter_test.go index 3742cfd90..774255793 100644 --- a/internal/services/formatters/shell/shellcheck/formatter_test.go +++ b/internal/services/formatters/shell/shellcheck/formatter_test.go @@ -21,7 +21,6 @@ import ( "github.com/ZupIT/horusec/internal/entities/toolsconfig" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" - "github.com/ZupIT/horusec/internal/entities/monitor" "github.com/stretchr/testify/assert" @@ -45,7 +44,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -66,7 +65,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -89,7 +88,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return(output, nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -110,7 +109,7 @@ func TestParseOutput(t *testing.T) { dockerAPIControllerMock.On("SetAnalysisID") dockerAPIControllerMock.On("CreateLanguageAnalysisContainer").Return("invalid output", nil) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -126,7 +125,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetWorkDir(&workdir.WorkDir{}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) @@ -138,7 +137,7 @@ func TestParseOutput(t *testing.T) { config := &cliConfig.Config{} config.SetToolsConfig(toolsconfig.ToolsConfigsStruct{ShellCheck: toolsconfig.ToolConfig{IsToIgnore: true}}) - service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config, &monitor.Monitor{}) + service := formatters.NewFormatterService(analysis, dockerAPIControllerMock, config) formatter := NewFormatter(service) formatter.StartAnalysis("") diff --git a/internal/utils/prompt/prompt_test.go b/internal/utils/prompt/prompt_test.go index 20ea54897..59ce3a1b5 100644 --- a/internal/utils/prompt/prompt_test.go +++ b/internal/utils/prompt/prompt_test.go @@ -23,9 +23,7 @@ import ( func TestPrompt_Ask(t *testing.T) { t.Run("Should run command ask without panics", func(t *testing.T) { assert.NotPanics(t, func() { - go func() { - _, _ = NewPrompt().Ask("", "") - }() + _, _ = NewPrompt().Ask("", "") }) }) } @@ -33,9 +31,7 @@ func TestPrompt_Ask(t *testing.T) { func TestPrompt_Select(t *testing.T) { t.Run("Should run command select without panics", func(t *testing.T) { assert.NotPanics(t, func() { - go func() { - _, _ = NewPrompt().Select("", []string{}) - }() + _, _ = NewPrompt().Select("", []string{}) }) }) }