plugin/discovery: SHA256() method to get the hash of each plugin
This will be used later to verify that installed plugins match what's available on the releases server.
This commit is contained in:
parent
a94e9a4362
commit
0a08214a74
|
@ -1,6 +1,10 @@
|
||||||
package discovery
|
package discovery
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/blang/semver"
|
"github.com/blang/semver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,3 +30,21 @@ type PluginMeta struct {
|
||||||
func (m PluginMeta) VersionObj() (semver.Version, error) {
|
func (m PluginMeta) VersionObj() (semver.Version, error) {
|
||||||
return semver.Make(m.Version)
|
return semver.Make(m.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SHA256 returns a SHA256 hash of the content of the referenced executable
|
||||||
|
// file, or an error if the file's contents cannot be read.
|
||||||
|
func (m PluginMeta) SHA256() ([]byte, error) {
|
||||||
|
f, err := os.Open(m.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
h := sha256.New()
|
||||||
|
_, err = io.Copy(h, f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum(nil), nil
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package discovery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMetaSHA256(t *testing.T) {
|
||||||
|
m := PluginMeta{
|
||||||
|
Path: "test-fixtures/current-style-plugins/mockos_mockarch/terraform-foo-bar-V0.0.1",
|
||||||
|
}
|
||||||
|
hash, err := m.SHA256()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := fmt.Sprintf("%x", hash)
|
||||||
|
want := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // (hash of empty file)
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("incorrect hash %s; want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue