From 8f1021a69dd3371e8616ec19ee42f1bcd98b3b76 Mon Sep 17 00:00:00 2001 From: Frederick Kautz Date: Mon, 7 Aug 2023 23:02:19 -0700 Subject: [PATCH] Add single file omnibor ADG and ignore symlinks Signed-off-by: Frederick Kautz --- directory_plugin.go | 14 +++++++-- file_plugin.go | 69 ++++++++++++++++++++++++++++++-------------- go.mod | 5 +++- go.sum | 6 ++-- omnitrail.go | 45 +++++++++++++++++++++++++++++ omnitrail_test.go | 43 +-------------------------- posix_plugin_unix.go | 15 ++++++++-- 7 files changed, 126 insertions(+), 71 deletions(-) diff --git a/directory_plugin.go b/directory_plugin.go index cfbc6b0..15de345 100644 --- a/directory_plugin.go +++ b/directory_plugin.go @@ -28,6 +28,18 @@ func (plug *DirectoryPlugin) Sha256ADG(m map[string]string) { } func (plug *DirectoryPlugin) Add(path string) error { + // if this is a broken symlink, ignore + fileInfo, err := os.Lstat(path) + if err != nil { + return err + } + if fileInfo.Mode()&os.ModeSymlink != 0 { + // path is a symlink + if _, err := os.Stat(path); err != nil { + return nil + } + } + stat, err := os.Stat(path) if err != nil { return err @@ -61,8 +73,6 @@ func (plug *DirectoryPlugin) Store(envelope *Envelope) error { continue } } - //sha1tree := make(map[string]omnibor.ArtifactTree) - //sha256tree := make(map[string]omnibor.ArtifactTree) for _, key := range keys { if sha1tree != nil { diff --git a/file_plugin.go b/file_plugin.go index 919bb86..fb4299f 100644 --- a/file_plugin.go +++ b/file_plugin.go @@ -49,56 +49,81 @@ func NewFilePlugin() Plugin { } } -func (plug *FilePlugin) Add(path string) error { - stat, err := os.Stat(path) +func (plug *FilePlugin) Add(filePath string) error { + // ignore broken symlink + localFileInfo, err := os.Lstat(filePath) if err != nil { return err } - if stat.IsDir() { + if localFileInfo.Mode()&os.ModeSymlink != 0 { + if _, err = os.Stat(filePath); err != nil { + if err != nil { + return nil + } + } + } + fileInfo, err := os.Stat(filePath) + // if file is a symlink and the symlink points to a broken path, return nil + if err != nil { + return err + } + + if fileInfo.IsDir() { return nil } - f, err := os.Open(path) + + file, err := os.Open(filePath) if err != nil { return err } // explicitly ignore error from closing file - defer func(f *os.File) { - _ = f.Close() - }(f) + defer func(file *os.File) { + _ = file.Close() + }(file) + + for _, hashAlgo := range plug.algorithms { - for _, algorithm := range plug.algorithms { - _, err := f.Seek(0, 0) + _, err := file.Seek(0, 0) if err != nil { return err } - if strings.HasPrefix(algorithm, "gitoid:") { - var res *gitoid.GitOID - switch algorithm { + if strings.HasPrefix(hashAlgo, "gitoid:") { + + var hashResult *gitoid.GitOID + + switch hashAlgo { case "gitoid:sha1": - res, err = gitoid.New(f, gitoid.WithContentLength(stat.Size())) + hashResult, err = gitoid.New(file, gitoid.WithContentLength(fileInfo.Size())) case "gitoid:sha256": - res, err = gitoid.New(f, gitoid.WithContentLength(stat.Size()), gitoid.WithSha256()) + hashResult, err = gitoid.New(file, gitoid.WithContentLength(fileInfo.Size()), gitoid.WithSha256()) } + if err != nil { return err } - plug.files[algorithm][path] = res.String() + + plug.files[hashAlgo][filePath] = hashResult.String() + } else { - switch algorithm { + + switch hashAlgo { case "sha1": hasher := sha1.New() - _, err = io.Copy(hasher, f) - res := hasher.Sum([]byte{}) - plug.files[algorithm][path] = fmt.Sprintf("%x", res) + _, err = io.Copy(hasher, file) + hashBytes := hasher.Sum([]byte{}) + plug.files[hashAlgo][filePath] = fmt.Sprintf("%x", hashBytes) + case "sha256": hasher := sha256.New() - _, err = io.Copy(hasher, f) - res := hasher.Sum([]byte{}) - plug.files[algorithm][path] = fmt.Sprintf("%x", res) + _, err = io.Copy(hasher, file) + hashBytes := hasher.Sum([]byte{}) + plug.files[hashAlgo][filePath] = fmt.Sprintf("%x", hashBytes) } + } + } return nil diff --git a/go.mod b/go.mod index bba49fc..50bd34c 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,14 @@ go 1.20 require ( github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd + github.com/stretchr/testify v1.8.2 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.8.2 // indirect + github.com/rogpeppe/go-internal v1.8.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0616e34..37e5084 100644 --- a/go.sum +++ b/go.sum @@ -3,12 +3,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d h1:4l+Uq5zFWSagXgGFaKRRVWJrnlzeathyagWgYUltCgY= github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d/go.mod h1:WxWwA3EYuCQjlR5EBUX3uaTS8bh9BOa7BcqVREHQ0uQ= -github.com/omnibor/omnibor-go v0.0.0-20230226193847-c1d2b6d19159 h1:lM5TaZOWo76IG5zElw3ozfnWuwIqOt0/YW410IJGKaE= -github.com/omnibor/omnibor-go v0.0.0-20230226193847-c1d2b6d19159/go.mod h1:ArlQivzDQvZnFe8itjlA3ndPTXd9iWOgqzF31OyIEFQ= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd h1:25EpGVgctk6V3DUa1gqFHvjVbmdWqM+jBZAed7p/krQ= github.com/omnibor/omnibor-go v0.0.0-20230521145532-a77de61a16cd/go.mod h1:ArlQivzDQvZnFe8itjlA3ndPTXd9iWOgqzF31OyIEFQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -17,6 +18,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/omnitrail.go b/omnitrail.go index 197e467..84ce6ab 100644 --- a/omnitrail.go +++ b/omnitrail.go @@ -1,5 +1,10 @@ package omnitrail +import ( + "fmt" + "sort" +) + func NewTrail(option ...Option) Factory { o := &Options{} for _, opt := range option { @@ -23,3 +28,43 @@ func NewTrail(option ...Option) Factory { }, } } + +func FormatADGString(mapping Factory) string { + res := "" + sha1adgs := mapping.Sha1ADGs() + // create a list of all keys sorted in lexical order + keys := make([]string, 0, len(sha1adgs)) + for k := range sha1adgs { + keys = append(keys, k) + } + // sort the keys + sort.Strings(keys) + + for _, k := range keys { + v := sha1adgs[k] + if v != "" { + res += fmt.Sprintln(k) + res += fmt.Sprintln(v) + res += fmt.Sprintln("--") + } + } + res += fmt.Sprintln("----") + + keys = make([]string, 0, len(sha1adgs)) + sha2adgs := mapping.Sha256ADGs() + keys = make([]string, 0, len(sha2adgs)) + for k := range sha2adgs { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { + v := sha2adgs[k] + if v != "" { + res += fmt.Sprintln(k) + res += fmt.Sprintln(v) + res += fmt.Sprintln("--") + } + } + return res +} diff --git a/omnitrail_test.go b/omnitrail_test.go index 6eefdcf..b264356 100644 --- a/omnitrail_test.go +++ b/omnitrail_test.go @@ -2,7 +2,6 @@ package omnitrail import ( "encoding/json" - "fmt" "github.com/stretchr/testify/assert" "os" "os/user" @@ -71,7 +70,7 @@ func testAdd(t *testing.T, name string) { assert.Equal(t, &expectedEnvelope, mapping.Envelope()) - res := formatADGString(mapping) + res := FormatADGString(mapping) expectedBytes, err = os.ReadFile("./test/" + name + ".adg") assert.NoError(t, err) @@ -93,43 +92,3 @@ func getShortestKey(expectedEnvelope *Envelope) string { shortestKey := keys[0] return shortestKey } - -func formatADGString(mapping Factory) string { - res := "" - sha1adgs := mapping.Sha1ADGs() - // create a list of all keys sorted in lexical order - keys := make([]string, 0, len(sha1adgs)) - for k := range sha1adgs { - keys = append(keys, k) - } - // sort the keys - sort.Strings(keys) - - for _, k := range keys { - v := sha1adgs[k] - if v != "" { - res += fmt.Sprintln(k) - res += fmt.Sprintln(v) - res += fmt.Sprintln("--") - } - } - res += fmt.Sprintln("----") - - keys = make([]string, 0, len(sha1adgs)) - sha2adgs := mapping.Sha256ADGs() - keys = make([]string, 0, len(sha2adgs)) - for k := range sha2adgs { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, k := range keys { - v := sha2adgs[k] - if v != "" { - res += fmt.Sprintln(k) - res += fmt.Sprintln(v) - res += fmt.Sprintln("--") - } - } - return res -} diff --git a/posix_plugin_unix.go b/posix_plugin_unix.go index 2abd2a1..d44dc74 100644 --- a/posix_plugin_unix.go +++ b/posix_plugin_unix.go @@ -18,6 +18,18 @@ type posixInfo struct { } func (p *PosixPlugin) Add(path string) error { + // check if symlink is broken + localFileInfo, err := os.Lstat(path) + if err != nil { + return err + } + if localFileInfo.Mode()&os.ModeSymlink != 0 { + if _, err = os.Stat(path); err != nil { + if err != nil { + return nil + } + } + } stat, err := os.Stat(path) if err != nil { return err @@ -48,8 +60,7 @@ func (p *PosixPlugin) Store(envelope *Envelope) error { element.Posix.OwnerUID = strconv.Itoa(int(p.params[path].uid)) element.Posix.OwnerGID = strconv.Itoa(int(p.params[path].gid)) if p.params[path].size != 0 { - siezString := strconv.Itoa(int(p.params[path].size)) - element.Posix.Size = siezString + element.Posix.Size = strconv.Itoa(int(p.params[path].size)) } } return nil