Skip to content

Commit

Permalink
feat: Implement golang native hash command
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvey committed Jan 6, 2022
1 parent 60d42df commit 8a2ad96
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func doBuild(opts buildOpts) (*dag.DAG, error) {
DAG.GenerateDAG(buildPath, opts.registryURL)
logrus.Debug("Generate DAG -- Done")

currentVersion, err := versn.CheckDockerVersionIntegrity(opts.inputDir, shell)
currentVersion, err := versn.CheckDockerVersionIntegrity(opts.inputDir, opts.buildDir)
if err != nil {
return nil, err
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package main

import (
"fmt"
"log"

versionpkg "github.com/radiofrance/dib/version"

cli "github.com/jawher/mow.cli"
)

func cmdHash(cmd *cli.Cmd) {
cmd.Spec = "[BUILD_PATH]"
buildPath := cmd.StringArg("BUILD_PATH", "docker", "Path to the directory containing the Dockerfiles,")

cmd.Action = func() {
fmt.Println("Not implemented yet.") //nolint:forbidigo
hash, err := versionpkg.GetDockerVersionHash(*buildPath)
if err != nil {
log.Fatal(err)
}
fmt.Print(hash) // nolint: forbidigo
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ require (
github.com/jawher/mow.cli v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/wolfeidau/humanhash v1.1.0
gitlab.com/radiofrance/go-container-registry v0.1.1
golang.org/x/mod v0.4.2
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E=
github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down Expand Up @@ -820,6 +822,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
42 changes: 35 additions & 7 deletions version/hash.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
package version

import (
"crypto/sha256"
"errors"
"fmt"
"io"
"sort"
"strings"

"github.com/radiofrance/dib/exec"
"github.com/wolfeidau/humanhash"
"golang.org/x/mod/sumdb/dirhash"
)

// getDockerVersionHash returns the revision hash of the docker directory.
func getDockerVersionHash(exec exec.Executor) (string, error) {
cmd := "find docker -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum | cut -d ' ' -f 1"
res, err := exec.Execute("bash", "-c", cmd)
// GetDockerVersionHash returns the revision hash of the build directory.
func GetDockerVersionHash(buildPath string) (string, error) {
return dirhash.HashDir(buildPath, "", humanReadableHashFn)
}

func humanReadableHashFn(files []string, open func(string) (io.ReadCloser, error)) (string, error) {
hash := sha256.New()
files = append([]string(nil), files...)
sort.Strings(files)
for _, file := range files {
if strings.Contains(file, "\n") {
return "", errors.New("dirhash: filenames with newlines are not supported")
}
readCloser, err := open(file)
if err != nil {
return "", err
}
hashFile := sha256.New()
_, err = io.Copy(hashFile, readCloser)
readCloser.Close()
if err != nil {
return "", err
}
fmt.Fprintf(hash, "%x %s\n", hashFile.Sum(nil), file)
}
humanReadableHash, err := humanhash.Humanize(hash.Sum(nil), 4)
if err != nil {
return "", err
return "", fmt.Errorf("could not humanize hash: %w", err)
}
return strings.TrimSuffix(res, "\n"), nil
return humanReadableHash, nil
}
6 changes: 2 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import (
"io/ioutil"
"path"
"strings"

"github.com/radiofrance/dib/exec"
)

const dockerVersionFilename = ".docker-version"

// CheckDockerVersionIntegrity verifies the consistency of the version hash
// contained in the .docker-version file against the revision hash from git.
// It returns the version if the verification is successful.
func CheckDockerVersionIntegrity(rootPath string, exec exec.Executor) (string, error) {
func CheckDockerVersionIntegrity(rootPath, buildPath string) (string, error) {
fileVersion, err := getDockerVersionFromFile(path.Join(rootPath, dockerVersionFilename))
if err != nil {
return "", err
}

dockerVersionHash, err := getDockerVersionHash(exec)
dockerVersionHash, err := GetDockerVersionHash(path.Join(rootPath, buildPath))
if err != nil {
return "", fmt.Errorf("could not obtain docker-version hash: %w", err)
}
Expand Down

0 comments on commit 8a2ad96

Please sign in to comment.