Skip to content

Commit

Permalink
errors:chore - move errors from enums/error pkg to respective pkgs (#862
Browse files Browse the repository at this point in the history
)

Move errors declarations on enums/errors package to their packages that
use these errors. Since all errors was moved, the enums/error package
was also removed.

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup authored Dec 14, 2021
1 parent ec070ac commit cd839ef
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 64 deletions.
17 changes: 11 additions & 6 deletions internal/controllers/requirements/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

errorsEnums "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/helpers/messages"
)

Expand All @@ -32,7 +31,13 @@ const (
MinSubVersionDockerAccept = 0o3
)

var ErrMinVersion = fmt.Errorf("%v.%v", MinVersionDockerAccept, MinSubVersionDockerAccept)
var (
// ErrMinVersion occur when the installed Docker version is not the minimum supported.
ErrMinVersion = fmt.Errorf("%v.%v", MinVersionDockerAccept, MinSubVersionDockerAccept)

// ErrDockerNotInstalled occurs when Docker is not installed.
ErrDockerNotInstalled = errors.New("docker not found. Please check and try again")
)

type RequirementDocker struct{}

Expand All @@ -55,7 +60,7 @@ func (r *RequirementDocker) validateIfDockerIsInstalled() (string, error) {
return "", err
}
if !r.checkIfContainsDockerVersion(response) {
return "", errorsEnums.ErrDockerNotInstalled
return "", ErrDockerNotInstalled
}
return response, r.checkIfDockerIsRunning()
}
Expand Down Expand Up @@ -106,7 +111,7 @@ func (r *RequirementDocker) validateIfDockerIsRunningInMinVersion(response strin
func (r *RequirementDocker) extractDockerVersionFromString(response string) (int, int, error) {
responseSpited := strings.Split(strings.ToLower(response), "docker version ")
if len(responseSpited) < 1 || len(responseSpited) > 1 && len(responseSpited[1]) < 8 {
return 0, 0, errorsEnums.ErrDockerNotInstalled
return 0, 0, ErrDockerNotInstalled
}
return r.getVersionAndSubVersion(responseSpited[1])
}
Expand All @@ -118,11 +123,11 @@ func (r *RequirementDocker) checkIfContainsDockerVersion(response string) bool {
func (r *RequirementDocker) getVersionAndSubVersion(fullVersion string) (int, int, error) {
version, err := strconv.Atoi(fullVersion[0:2])
if err != nil {
return 0, 0, errorsEnums.ErrDockerNotInstalled
return 0, 0, ErrDockerNotInstalled
}
subversion, err := strconv.Atoi(fullVersion[3:5])
if err != nil {
return 0, 0, errorsEnums.ErrDockerNotInstalled
return 0, 0, ErrDockerNotInstalled
}
return version, subversion, nil
}
25 changes: 17 additions & 8 deletions internal/controllers/requirements/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package git

import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"

"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

errorsEnums "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/helpers/messages"
)

Expand All @@ -31,7 +31,16 @@ const (
MinSubVersionGitAccept = 0o1
)

var ErrMinVersion = fmt.Errorf("%v.%v", MinVersionGitAccept, MinSubVersionGitAccept)
var (
// ErrMinVersion is the error logged when the installed Git version is not the minimum supported.
ErrMinVersion = fmt.Errorf("%v.%v", MinVersionGitAccept, MinSubVersionGitAccept)

// ErrGitNotInstalled occurs when Git is not installed.
ErrGitNotInstalled = errors.New("git not found. Please check and try again")

// ErrGitLowerVersion occur when the installed Git version is not the minimum supported.
ErrGitLowerVersion = errors.New("git version is lower of 2.01. Please check and try again")
)

type RequirementGit struct{}

Expand All @@ -53,7 +62,7 @@ func (r *RequirementGit) validateIfGitIsInstalled() (string, error) {
return "", err
}
if !r.checkIfContainsGitVersion(response) {
return "", errorsEnums.ErrGitNotInstalled
return "", ErrGitNotInstalled
}
return response, nil
}
Expand Down Expand Up @@ -83,18 +92,18 @@ func (r *RequirementGit) validateIfGitIsRunningInMinVersion(response string) err
}
if version < MinVersionGitAccept {
logger.LogErrorWithLevel(messages.MsgErrorWhenGitIsLowerVersion, ErrMinVersion)
return errorsEnums.ErrGitLowerVersion
return ErrGitLowerVersion
} else if version == MinVersionGitAccept && subversion < MinSubVersionGitAccept {
logger.LogErrorWithLevel(messages.MsgErrorWhenGitIsLowerVersion, ErrMinVersion)
return errorsEnums.ErrGitLowerVersion
return ErrGitLowerVersion
}
return nil
}

func (r *RequirementGit) extractGitVersionFromString(response string) (int, int, error) {
responseSpited := strings.Split(strings.ToLower(response), "git version ")
if len(responseSpited) < 1 || len(responseSpited) > 1 && len(responseSpited[1]) < 3 {
return 0, 0, errorsEnums.ErrGitNotInstalled
return 0, 0, ErrGitNotInstalled
}
return r.getVersionAndSubVersion(responseSpited[1])
}
Expand All @@ -106,11 +115,11 @@ func (r *RequirementGit) checkIfContainsGitVersion(response string) bool {
func (r *RequirementGit) getVersionAndSubVersion(fullVersion string) (int, int, error) {
version, err := strconv.Atoi(fullVersion[0:1])
if err != nil {
return 0, 0, errorsEnums.ErrGitNotInstalled
return 0, 0, ErrGitNotInstalled
}
subversion, err := strconv.Atoi(fullVersion[2:4])
if err != nil {
return 0, 0, errorsEnums.ErrGitNotInstalled
return 0, 0, ErrGitNotInstalled
}
return version, subversion, nil
}
41 changes: 0 additions & 41 deletions internal/enums/errors/errors.go

This file was deleted.

6 changes: 4 additions & 2 deletions internal/services/docker/docker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import (

"github.com/ZupIT/horusec/config"
"github.com/ZupIT/horusec/internal/entities/docker"
enumErrors "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/enums/images"
"github.com/ZupIT/horusec/internal/helpers/messages"
)

// ErrImageTagCmdRequired occurs when an docker image or docker command is empty to start analysis.
var ErrImageTagCmdRequired = errors.New("image or cmd is empty")

// Docker is the interface that abstract the Docker API.
type Docker interface {
CreateLanguageAnalysisContainer(data *docker.AnalysisData) (containerOutPut string, err error)
Expand Down Expand Up @@ -122,7 +124,7 @@ func New(client Client, cfg *config.Config, analysisID uuid.UUID) *API {

func (d *API) CreateLanguageAnalysisContainer(data *docker.AnalysisData) (containerOutPut string, err error) {
if data.IsInvalid() {
return "", enumErrors.ErrImageTagCmdRequired
return "", ErrImageTagCmdRequired
}

return d.logStatusAndExecuteCRDContainer(data.GetCustomOrDefaultImage(), d.replaceCMDAnalysisID(data.CMD))
Expand Down
5 changes: 2 additions & 3 deletions internal/services/docker/docker_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

cliConfig "github.com/ZupIT/horusec/config"
dockerEntities "github.com/ZupIT/horusec/internal/entities/docker"
errorsenum "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/services/docker/client"
"github.com/ZupIT/horusec/internal/utils/testutil"
)
Expand Down Expand Up @@ -72,7 +71,7 @@ func TestDockerAPI_CreateLanguageAnalysisContainer(t *testing.T) {
})

assert.Error(t, err)
assert.ErrorIs(t, err, errorsenum.ErrImageTagCmdRequired)
assert.ErrorIs(t, err, ErrImageTagCmdRequired)
})

t.Run("Should return error when cmd is empty", func(t *testing.T) {
Expand All @@ -83,7 +82,7 @@ func TestDockerAPI_CreateLanguageAnalysisContainer(t *testing.T) {
})

assert.Error(t, err)
assert.ErrorIs(t, err, errorsenum.ErrImageTagCmdRequired)
assert.ErrorIs(t, err, ErrImageTagCmdRequired)
})

t.Run("Should return error when pull image aleatory", func(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/services/formatters/ruby/brakeman/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package brakeman

import (
"encoding/json"
"errors"
"strings"

"github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
Expand All @@ -24,14 +25,15 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

dockerEntities "github.com/ZupIT/horusec/internal/entities/docker"
errorsEnums "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/enums/images"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/formatters"
"github.com/ZupIT/horusec/internal/services/formatters/ruby/brakeman/entities"
vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash"
)

var ErrNotFoundRailsProject = errors.New("brakeman only works on Ruby On Rails project")

type Formatter struct {
formatters.IService
}
Expand Down Expand Up @@ -84,7 +86,7 @@ func (f *Formatter) parseOutput(containerOutput, projectSubPath string) error {

func (f *Formatter) newContainerOutputFromString(containerOutput string) (output entities.Output, err error) {
if f.isNotFoundRailsProject(containerOutput) {
return entities.Output{}, errorsEnums.ErrNotFoundRailsProject
return entities.Output{}, ErrNotFoundRailsProject
}

err = json.Unmarshal([]byte(containerOutput), &output)
Expand Down
9 changes: 7 additions & 2 deletions internal/services/formatters/ruby/bundler/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bundler

import (
"bufio"
"errors"
"os"
"path/filepath"
"strconv"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

dockerEntities "github.com/ZupIT/horusec/internal/entities/docker"
errorsEnums "github.com/ZupIT/horusec/internal/enums/errors"
"github.com/ZupIT/horusec/internal/enums/images"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/formatters"
Expand All @@ -37,6 +37,11 @@ import (
vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash"
)

// ErrGemLockNotFound occurs when bundles does not find gemfile.lock.
//
// nolint: lll
var ErrGemLockNotFound = errors.New("project doesn't have a gemfile.lock file, it would be a good idea to commit it so horusec can check for vulnerabilities")

type Formatter struct {
formatters.IService
}
Expand Down Expand Up @@ -85,7 +90,7 @@ func (f *Formatter) getDockerConfig(projectSubPath string) *dockerEntities.Analy

func (f *Formatter) verifyGemLockError(output string) error {
if strings.Contains(output, "No such file or directory") && strings.Contains(output, "Errno::ENOENT") {
return errorsEnums.ErrGemLockNotFound
return ErrGemLockNotFound
}

return nil
Expand Down

0 comments on commit cd839ef

Please sign in to comment.