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

(#2120) Add tool sha256 to checksum directories #2121

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions cmd/tool_sha256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"fmt"
"github.com/choria-io/go-choria/config"
iu "github.com/choria-io/go-choria/internal/util"
"github.com/sirupsen/logrus"
"os"
"sync"
)

type tSha256Command struct {
command

directory string
sumsFile string
}

func init() {
cli.commands = append(cli.commands, &tSha256Command{})
}

func (r *tSha256Command) Setup() error {
if machine, ok := cmdWithFullCommand("tool"); ok {
r.cmd = machine.Cmd().Command("sha256", "Checksums a directory of files recursively using SHA256")
r.cmd.Arg("dir", "The directory to recursively checksum").Required().ExistingDirVar(&r.directory)
r.cmd.Flag("validate", "Checksum file used to validate the directory").ExistingFileVar(&r.sumsFile)
}

return nil
}

func (r *tSha256Command) Configure() error {
if debug {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Logging at debug level due to CLI override")
}

cfg, err = config.NewDefaultConfig()
if err != nil {
return err
}

cfg.Choria.SecurityProvider = "file"
cfg.DisableSecurityProviderVerify = true

return err
}

func (r *tSha256Command) Run(wg *sync.WaitGroup) error {
defer wg.Done()

if r.sumsFile == "" {
return r.create()
}

return r.validate()
}

func (r *tSha256Command) validate() error {
ok, err := iu.Sha256VerifyDir(r.sumsFile, r.directory, c.Logger("sha256"), func(file string, ok bool) {
if !ok {
fmt.Println(file)
}
})
if err != nil {
return err
}

if !ok {
return fmt.Errorf("directory did not validate using %s", r.sumsFile)
}

fmt.Printf("Directory %s validates correctly using %s\n", r.directory, r.sumsFile)

return nil
}

func (r *tSha256Command) create() error {
sums, err := iu.Sha256ChecksumDir(r.directory)
if err != nil {
return err
}
fmt.Print(string(sums))

return nil
}
17 changes: 16 additions & 1 deletion internal/util/sha256sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func Sha256ChecksumDir(dir string) ([]byte, error) {
return err
}

_, err = fmt.Fprintf(sums, "%s %s\n", sum, strings.TrimPrefix(strings.TrimPrefix(path, filepath.ToSlash(dir)), `/`))
if dir != "." {
path = strings.TrimPrefix(path, filepath.ToSlash(dir))
}
path = strings.TrimPrefix(path, "/")

_, err = fmt.Fprintf(sums, "%s %s\n", sum, path)

return err
})
Expand Down Expand Up @@ -122,11 +127,17 @@ func Sha256VerifyDir(sumsFile string, dir string, log *logrus.Entry, cb FileRepo
lc := 0
failed := false
for scanner.Scan() {
lc++

line := scanner.Text()
if log != nil {
log.Debugf("Checking line: %v", line)
}

if len(line) == 0 {
continue
}

matches := sumsMatcherRe.FindStringSubmatch(line)
if len(matches) != 3 {
return false, fmt.Errorf("%w: malformed line %d", ErrorInvalidSumsFile, lc)
Expand All @@ -136,6 +147,10 @@ func Sha256VerifyDir(sumsFile string, dir string, log *logrus.Entry, cb FileRepo
log.Debugf("Checking file %v", matches[2])
}

if matches[2] == sumsFile {
continue
}

ok, _, err := FileHasSha256Sum(filepath.Join(dir, matches[2]), matches[1])
switch {
case errors.Is(err, fs.ErrNotExist):
Expand Down
2 changes: 1 addition & 1 deletion internal/util/sha256sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("Internal/Util/Sha256sums", func() {

It("Should handle corrupt sum files", func() {
ok, err := Sha256VerifyDir("testdata/SHA256SUM.corrupt", "testdata", log, func(string, bool) {})
Expect(err).To(MatchError("invalid sums file: malformed line 0"))
Expect(err).To(MatchError("invalid sums file: malformed line 1"))
Expect(ok).To(BeFalse())
})
})
Expand Down
1 change: 1 addition & 0 deletions internal/util/testdata/SHA256SUM.dir
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 .hidden
c03905fcdab297513a620ec81ed46ca44ddb62d41cbbd83eb4a5a3592be26a69 sub/abc.txt
9f28ca60126cb0c438bc90f6d323efb4abf699f976c18a7a88cdb166e45e22ec x.txt
1 change: 1 addition & 0 deletions internal/util/testdata/dir/.hidden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Loading