Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/output txt #496

Merged
merged 14 commits into from
Aug 2, 2021
Merged
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (c *Config) SetRepositoryAuthorization(repositoryAuthorization string) {
}

func (c *Config) GetPrintOutputType() string {
return valueordefault.GetStringValueOrDefault(c.printOutputType, "text")
return valueordefault.GetStringValueOrDefault(c.printOutputType, "")
}

func (c *Config) SetPrintOutputType(printOutputType string) {
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestNewHorusecConfig(t *testing.T) {
assert.Equal(t, int64(600), configs.GetTimeoutInSecondsAnalysis())
assert.Equal(t, int64(15), configs.GetMonitorRetryInSeconds())
assert.Equal(t, uuid.Nil.String(), configs.GetRepositoryAuthorization())
assert.Equal(t, "text", configs.GetPrintOutputType())
assert.Equal(t, "", configs.GetPrintOutputType())
assert.Equal(t, "", configs.GetJSONOutputFilePath())
assert.Equal(t, 1, len(configs.GetSeveritiesToIgnore()))
assert.Equal(t, 2, len(configs.GetFilesOrPathsToIgnore()))
Expand Down
9 changes: 5 additions & 4 deletions horusec-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"horusecCliEnableGitHistoryAnalysis": false,
"horusecCliEnableInformationSeverity": false,
"horusecCliFalsePositiveHashes": [
"37fa0cfe47519c1b2b6a8e29538571b81fd8787ca4217825ae6d8dcf86d70de8",
"85492fbc829b64336a4f858022fbe52f05e27ee18d7a8fbdf5ffd23991ebd7a9",
"06f6ce2402e20f1e885e5d59f66db4dde44dfdd2eaf821d86b1d066a707c9fff",
"362a89c4517db256b648e9b1d21ddb0d99018e7c7b9f9b45d200ede54a49363d",
Expand All @@ -31,10 +30,10 @@
"9c205ee4b31bea1254f4e8031958995912312a524105469cb49e757d59558496",
"b176f4967e7b0e54faabb9688d1d9ff6f10959d4a34280b9e035bfd63c4f352e",
"2eab7620998c54bcbdb1da9ad96f54c3b6ac7b5e0babbff8f502ec10594479ad",
"85d4e95cd519dda872c8da0bc50b742ef067bb9f1e5b9fea42924eb21c5e688e",
"b9f0d3772a885673b4a968d21eb9c350d25aae332b7c1a9bf113b5af24704ff9",
"e8c6a9744859f048a44a4eb160ce0e22df524507a288cfbfcbfcdc26d2533c63",
"c25edc56029ba81e69515d3bca44fa5545af63cf841d8f219ac57fcd7cb95265"
"c25edc56029ba81e69515d3bca44fa5545af63cf841d8f219ac57fcd7cb95265",
"36f41965e929e9763260c61451ce0a5ca572f8a1a8979390b7c694e54e3dce29"
],
"horusecCliFilesOrPathsToIgnore": [
"**/e2e/**",
Expand All @@ -44,13 +43,15 @@
"**/*_mock.go",
"**/*README.md",
"**/cmd/app/start/analysis/**",
"**/tmp/**",
"**/bin/**",
"**/internal/services/engines/**"
],
"horusecCliHeaders": {},
"horusecCliHorusecApiUri": "http://0.0.0.0:8000",
"horusecCliJsonOutputFilepath": "",
"horusecCliMonitorRetryInSeconds": 10,
"horusecCliPrintOutputType": "text",
"horusecCliPrintOutputType": "",
"horusecCliProjectPath": "./",
"horusecCliRepositoryAuthorization": "00000000-0000-0000-0000-000000000000",
"horusecCliRepositoryName": "",
Expand Down
30 changes: 20 additions & 10 deletions internal/controllers/printresults/print_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (
"path/filepath"
"strings"

enumsVulnerability "github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability"

"github.com/ZupIT/horusec-devkit/pkg/entities/analysis"
"github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
enumsVulnerability "github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

"github.com/ZupIT/horusec/config"
"github.com/ZupIT/horusec/internal/enums/outputtype"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/sonarqube"
"github.com/ZupIT/horusec/internal/utils/file"
)

var (
Expand All @@ -48,6 +49,7 @@ type PrintResults struct {
configs config.IConfig
totalVulns int
sonarqubeService sonarqube.Interface
textOutput string
}

type Interface interface {
Expand Down Expand Up @@ -85,9 +87,9 @@ func (pr *PrintResults) StartPrintResults() (totalVulns int, err error) {

func (pr *PrintResults) factoryPrintByType() error {
switch {
case pr.configs.GetPrintOutputType() == string(outputtype.JSON):
case pr.configs.GetPrintOutputType() == outputtype.JSON:
return pr.runPrintResultsJSON()
case pr.configs.GetPrintOutputType() == string(outputtype.SonarQube):
case pr.configs.GetPrintOutputType() == outputtype.SonarQube:
return pr.runPrintResultsSonarQube()
default:
return pr.runPrintResultsText()
Expand All @@ -107,7 +109,8 @@ func (pr *PrintResults) runPrintResultsText() error {
pr.logSeparator(true)

pr.printTextOutputVulnerability()
return nil

return pr.createTxtOutputFile()
}

func (pr *PrintResults) runPrintResultsJSON() error {
Expand Down Expand Up @@ -216,16 +219,13 @@ func (pr *PrintResults) printTextOutputVulnerability() {
}

pr.printTotalVulnerabilities()

pr.logSeparator(len(pr.analysis.AnalysisVulnerabilities) > 0)
}

func (pr *PrintResults) printTotalVulnerabilities() {
totalVulnerabilities := pr.analysis.GetTotalVulnerabilities()
if totalVulnerabilities > 0 {
pr.printLNF("In this analysis, a total of %v possible vulnerabilities "+
"were found and we classified them into:", totalVulnerabilities)
fmt.Println("")
}
totalVulnerabilitiesBySeverity := pr.GetTotalVulnsBySeverity()
for vulnType, countBySeverity := range totalVulnerabilitiesBySeverity {
Expand Down Expand Up @@ -283,8 +283,6 @@ func (pr *PrintResults) printTextOutputVulnerabilityData(vulnerability *vulnerab

pr.printLNF("ReferenceHash: %s", vulnerability.VulnHash)

fmt.Print("\n")

pr.logSeparator(true)
}

Expand Down Expand Up @@ -368,5 +366,17 @@ func (pr *PrintResults) getProjectPath(path string) string {
}

func (pr *PrintResults) printLNF(text string, args ...interface{}) {
if pr.configs.GetPrintOutputType() == outputtype.Text {
pr.textOutput += fmt.Sprintln(fmt.Sprintf(text, args...))
}

fmt.Println(fmt.Sprintf(text, args...))
}

func (pr *PrintResults) createTxtOutputFile() error {
if pr.configs.GetPrintOutputType() != outputtype.Text {
return nil
}

return file.CreateAndWriteFile(pr.textOutput, pr.configs.GetJSONOutputFilePath())
}
12 changes: 3 additions & 9 deletions internal/enums/outputtype/output_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@

package outputtype

type OutputType string

const (
Text OutputType = "text"
JSON OutputType = "json"
SonarQube OutputType = "sonarqube"
Text = "text"
JSON = "json"
SonarQube = "sonarqube"
)

func (o OutputType) ToString() string {
return string(o)
}
2 changes: 1 addition & 1 deletion internal/enums/outputtype/output_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (

func TestToString(t *testing.T) {
t.Run("Should success parse to string", func(t *testing.T) {
assert.Equal(t, "json", JSON.ToString())
assert.Equal(t, "json", JSON)
})
}
12 changes: 6 additions & 6 deletions internal/usecases/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (au *UseCases) ValidateConfigs(config cliConfig.IConfig) error {
validation.Field(&c.timeoutInSecondsAnalysis, validation.Required, validation.Min(10)),
validation.Field(&c.monitorRetryInSeconds, validation.Required, validation.Min(10)),
validation.Field(&c.repositoryAuthorization, validation.Required, is.UUID),
validation.Field(&c.printOutputType, validation.Required, au.validationOutputTypes()),
validation.Field(&c.printOutputType, au.validationOutputTypes()),
validation.Field(&c.jSONOutputFilePath, validation.By(au.checkAndValidateJSONOutputFilePath(config))),
validation.Field(&c.severitiesToIgnore, validation.By(au.validationSeverities(config))),
validation.Field(&c.filesOrPathsToIgnore),
Expand Down Expand Up @@ -139,8 +139,8 @@ func (au *UseCases) checkIfExistsDuplicatedRiskAcceptHashes(config cliConfig.ICo

func (au *UseCases) checkAndValidateJSONOutputFilePath(config cliConfig.IConfig) func(value interface{}) error {
return func(value interface{}) error {
if config.GetPrintOutputType() == outputtype.JSON.ToString() ||
config.GetPrintOutputType() == outputtype.SonarQube.ToString() {
if config.GetPrintOutputType() == outputtype.JSON ||
config.GetPrintOutputType() == outputtype.SonarQube {
if err := au.validateJSONOutputFilePath(config); err != nil {
return err
}
Expand Down Expand Up @@ -168,9 +168,9 @@ func (au *UseCases) validateJSONOutputFilePath(config cliConfig.IConfig) error {

func (au *UseCases) validationOutputTypes() validation.InRule {
return validation.In(
outputtype.JSON.ToString(),
outputtype.SonarQube.ToString(),
outputtype.Text.ToString(),
outputtype.JSON,
outputtype.SonarQube,
outputtype.Text,
)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/usecases/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestValidateConfigs(t *testing.T) {
config := &cliConfig.Config{}
config.SetWorkDir(&workdir.WorkDir{})
config.NewConfigsFromEnvironments()
config.SetPrintOutputType(outputtype.JSON.ToString())
config.SetPrintOutputType(outputtype.JSON)
config.SetJSONOutputFilePath("")

err := useCases.ValidateConfigs(config)
Expand All @@ -87,7 +87,7 @@ func TestValidateConfigs(t *testing.T) {
config := &cliConfig.Config{}
config.SetWorkDir(&workdir.WorkDir{})
config.NewConfigsFromEnvironments()
config.SetPrintOutputType(outputtype.JSON.ToString())
config.SetPrintOutputType(outputtype.JSON)
config.SetJSONOutputFilePath("test.test")

err := useCases.ValidateConfigs(config)
Expand Down
15 changes: 15 additions & 0 deletions internal/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,18 @@ func getDependencyInfo(paths []string, dependency string) (code, filepath, _ str

return "", "", ""
}

func CreateAndWriteFile(output, filepath string) error {
path, err := filepathLib.Abs(filepath)
if err != nil {
return err
}

file, err := os.Create(path)
if err != nil {
return err
}

_, err = file.Write([]byte(output))
return err
}