diff --git a/internal/controllers/requirements/docker/docker.go b/internal/controllers/requirements/docker/docker.go index c8f6d4e94..89f4479f3 100644 --- a/internal/controllers/requirements/docker/docker.go +++ b/internal/controllers/requirements/docker/docker.go @@ -15,15 +15,15 @@ package docker import ( + "context" "errors" "fmt" - "os/exec" "strconv" - "strings" "github.com/ZupIT/horusec-devkit/pkg/utils/logger" "github.com/ZupIT/horusec/internal/helpers/messages" + "github.com/ZupIT/horusec/internal/services/docker/client" ) const ( @@ -40,60 +40,38 @@ var ( ) func Validate() error { - response, err := validateIfDockerIsInstalled() + version, err := validateIfDockerIsInstalled() if err != nil { return err } - return validateIfDockerIsSupported(response) + return validateIfDockerIsRunningInMinVersion(version) } func validateIfDockerIsInstalled() (string, error) { - response, err := execDockerVersion() + response, err := getDockerVersion() if err != nil { logger.LogInfo(messages.MsgInfoHowToInstallDocker) return "", err } - - if !checkIfContainsDockerVersion(response) { - return "", ErrDockerNotInstalled - } - return response, checkIfDockerIsRunning() -} - -func validateIfDockerIsSupported(version string) error { - err := validateIfDockerIsRunningInMinVersion(version) - if err != nil { - return err - } - return nil + return response, nil } -func execDockerVersion() (string, error) { - responseBytes, err := exec.Command("docker", "-v").CombinedOutput() +func getDockerVersion() (string, error) { + dockerClient := client.NewDockerClient() + version, err := dockerClient.ServerVersion(context.Background()) if err != nil { - logger.LogErrorWithLevel( - messages.MsgErrorWhenCheckRequirementsDocker, errors.New(string(responseBytes))) + logger.LogErrorWithLevel(messages.MsgErrorWhenCheckRequirementsDocker, err) return "", err } - return strings.ToLower(string(responseBytes)), nil -} - -func checkIfDockerIsRunning() error { - responseBytes, err := exec.Command("docker", "ps").CombinedOutput() - if err != nil { - logger.LogErrorWithLevel( - messages.MsgErrorWhenCheckDockerRunning, errors.New(string(responseBytes))) - } - return err + return version.Version, nil } func validateIfDockerIsRunningInMinVersion(response string) error { - version, subversion, err := extractDockerVersionFromString(response) + version, subversion, err := getVersionAndSubVersion(response) if err != nil { logger.LogErrorWithLevel(messages.MsgErrorWhenDockerIsLowerVersion, ErrMinVersion) return err } - if version <= MinVersionDockerAccept && subversion < MinSubVersionDockerAccept { fmt.Print("\n") logger.LogInfo(messages.MsgInfoDockerLowerVersion) @@ -103,18 +81,6 @@ func validateIfDockerIsRunningInMinVersion(response string) error { return nil } -func 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, ErrDockerNotInstalled - } - return getVersionAndSubVersion(responseSpited[1]) -} - -func checkIfContainsDockerVersion(response string) bool { - return strings.Contains(strings.ToLower(response), "docker version ") -} - func getVersionAndSubVersion(fullVersion string) (int, int, error) { version, err := strconv.Atoi(fullVersion[0:2]) if err != nil {