From 44912ffdb350f9a1176822e73020fbd113077e2a Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Fri, 28 Nov 2014 11:48:22 -0500 Subject: [PATCH] app-container: introduce NewHashSHA256 Simple helper to generate a Hash type from a byte slice. --- app-container/schema/types/hash.go | 8 ++++++++ cas/remote.go | 4 +++- cas/utils.go | 8 -------- cmd/run.go | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app-container/schema/types/hash.go b/app-container/schema/types/hash.go index 9a7caa7..9720645 100644 --- a/app-container/schema/types/hash.go +++ b/app-container/schema/types/hash.go @@ -1,6 +1,7 @@ package types import ( + "crypto/sha256" "encoding/json" "errors" "fmt" @@ -69,3 +70,10 @@ func (h Hash) MarshalJSON() ([]byte, error) { } return json.Marshal(h.String()) } + +func NewHashSHA256(b []byte) *Hash { + h := sha256.New() + h.Write(b) + nh, _ := NewHash(fmt.Sprintf("sha256-%x", h.Sum(nil))) + return nh +} diff --git a/cas/remote.go b/cas/remote.go index 25b60af..1b53320 100644 --- a/cas/remote.go +++ b/cas/remote.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/coreos-inc/rkt/app-container/schema/types" ) func NewRemote(name string, mirrors []string) *Remote { @@ -34,7 +36,7 @@ func (r *Remote) Unmarshal(data []byte) { } func (r Remote) Hash() string { - return sha256sum(r.Name) + return types.NewHashSHA256([]byte(r.Name)).String() } func (r Remote) Type() int64 { diff --git a/cas/utils.go b/cas/utils.go index fbcabbf..8265cf7 100644 --- a/cas/utils.go +++ b/cas/utils.go @@ -3,9 +3,7 @@ package cas import ( "compress/bzip2" "compress/gzip" - "crypto/sha256" "errors" - "fmt" "io" "net/url" "strings" @@ -24,12 +22,6 @@ func blockTransform(s string) []string { return pathSlice } -func sha256sum(s string) string { - h := sha256.New() - io.WriteString(h, s) - return fmt.Sprintf("sha256-%x", h.Sum(nil)) -} - func parseAlways(s string) *url.URL { u, _ := url.Parse(s) return u diff --git a/cmd/run.go b/cmd/run.go index 4a3d322..1555b43 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -43,6 +43,21 @@ func findImages(args []string, ds *cas.Store) (out []string, err error) { if err == nil { continue } + + // import the local file if it exists + file, err := os.Open(img) + if err == nil { + hash := types.NewHashSHA256([]byte(img)).String() + key, err := ds.WriteACI(hash, file) + file.Close() + if err != nil { + return nil, fmt.Errorf("%s: %v", img, err) + } + out[i] = key + continue + } + + // download if it is a URL u, err := url.Parse(img) if err != nil { return nil, fmt.Errorf("%s: not a valid URL or hash", img)