Skip to content

Commit

Permalink
docker:feature - Validate Docker version from the Docker API (#965)
Browse files Browse the repository at this point in the history
Fixes ##757

Signed-off-by: Luis Guilherme de A <luisguilhermepdea@gmail.com>
  • Loading branch information
luiguip authored Feb 1, 2022
1 parent 389dd6f commit f0df9f4
Showing 1 changed file with 12 additions and 46 deletions.
58 changes: 12 additions & 46 deletions internal/controllers/requirements/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit f0df9f4

Please # to comment.