diff --git a/pkg/compiler/checksum.go b/pkg/compiler/checksum.go new file mode 100644 index 00000000..286b1e4c --- /dev/null +++ b/pkg/compiler/checksum.go @@ -0,0 +1,78 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package compiler + +import ( + + //"strconv" + + "crypto/sha256" + "fmt" + "hash" + "io" + "os" + + // . "github.com/mudler/luet/pkg/logger" + "github.com/pkg/errors" +) + +type HashImplementation string + +const ( + SHA256 HashImplementation = "sha256" +) + +type Checksums map[string]string + +type HashOptions struct { + Hasher hash.Hash + Type HashImplementation +} + +// Generate generates all Checksums supported for the artifact +func (c *Checksums) Generate(a Artifact) error { + return c.generateSHA256(a) +} + +func (c Checksums) Compare(d Checksums) error { + for t, sum := range d { + if v, ok := c[t]; ok && v != sum { + return errors.New("Checksum mismsatch") + } + } + return nil +} + +func (c *Checksums) generateSHA256(a Artifact) error { + return c.generateSum(a, HashOptions{Hasher: sha256.New(), Type: SHA256}) +} + +func (c *Checksums) generateSum(a Artifact, opts HashOptions) error { + + f, err := os.Open(a.GetPath()) + if err != nil { + return err + } + defer f.Close() + if _, err := io.Copy(opts.Hasher, f); err != nil { + return err + } + + sum := fmt.Sprintf("%x", opts.Hasher.Sum(nil)) + + (*c)[string(opts.Type)] = sum + return nil +} diff --git a/pkg/compiler/checksum_test.go b/pkg/compiler/checksum_test.go new file mode 100644 index 00000000..5252bb55 --- /dev/null +++ b/pkg/compiler/checksum_test.go @@ -0,0 +1,54 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package compiler_test + +import ( + "io/ioutil" + "os" + + . "github.com/mudler/luet/pkg/compiler" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Checksum", func() { + Context("Generation", func() { + It("Compares successfully", func() { + + tmpdir, err := ioutil.TempDir("", "tree") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmpdir) // clean up + buildsum := Checksums{} + definitionsum := Checksums{} + definitionsum2 := Checksums{} + + err = buildsum.Generate(NewPackageArtifact("../../tests/fixtures/layers/alpine/build.yaml")) + Expect(err).ToNot(HaveOccurred()) + + err = definitionsum.Generate(NewPackageArtifact("../../tests/fixtures/layers/alpine/definition.yaml")) + Expect(err).ToNot(HaveOccurred()) + + err = definitionsum2.Generate(NewPackageArtifact("../../tests/fixtures/layers/alpine/definition.yaml")) + Expect(err).ToNot(HaveOccurred()) + + Expect(definitionsum.Compare(buildsum)).To(HaveOccurred()) + Expect(definitionsum.Compare(definitionsum2)).ToNot(HaveOccurred()) + + }) + }) + +})