-
Notifications
You must be signed in to change notification settings - Fork 53
/
extensions.go
89 lines (76 loc) · 1.98 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package v1
import (
"encoding/hex"
"hash"
"golang.org/x/crypto/blake2b"
"golang.org/x/exp/slices"
)
func (m *PluginManifest) DigestSet() map[string]struct{} {
hm := map[string]struct{}{}
for _, v := range m.Items {
hm[v.GetDigest()] = struct{}{}
}
return hm
}
func (m *PluginManifest) PluginDigests() map[string]string {
hm := map[string]string{}
for _, v := range m.Items {
hm[v.GetId()] = v.GetDigest()
}
return hm
}
func (m *PluginManifestEntry) GetId() string {
return m.GetModule()
}
func (m *PluginManifestEntry) DigestBytes() []byte {
decoded, _ := hex.DecodeString(m.GetDigest())
return decoded
}
func (m *PluginManifestEntry) DigestHash() hash.Hash {
h, _ := blake2b.New256(nil)
return h
}
func (m *PluginManifest) Sort() {
slices.SortFunc(m.Items, func(a, b *PluginManifestEntry) bool {
return a.GetModule() < b.GetModule()
})
}
func (a *PluginArchive) Sort() {
slices.SortFunc(a.Items, func(a, b *PluginArchiveEntry) bool {
return a.GetMetadata().GetModule() < b.GetMetadata().GetModule()
})
}
func (a *PluginArchive) ToManifest() *PluginManifest {
manifest := &PluginManifest{}
for _, entry := range a.Items {
manifest.Items = append(manifest.Items, entry.Metadata)
}
return manifest
}
func (l *PatchList) Sort() {
slices.SortFunc(l.Items, func(a, b *PatchSpec) bool {
if a.GetOp() != b.GetOp() {
return a.GetOp() < b.GetOp()
}
if a.GetModule() != b.GetModule() {
return a.GetModule() < b.GetModule()
}
return a.GetFilename() < b.GetFilename()
})
}
const (
ManifestDigestKey = "manifest-digest"
AgentBuildInfoKey = "agent-build-info"
)
// Returns a hash of the manifest metadata list. This can be used to compare
// manifests between the gateway and agent.
func (m *PluginManifest) Digest() string {
m.Sort()
hash, _ := blake2b.New256(nil)
for _, entry := range m.GetItems() {
hash.Write([]byte(entry.GetModule()))
hash.Write([]byte(entry.GetFilename()))
hash.Write([]byte(entry.GetDigest()))
}
return hex.EncodeToString(hash.Sum(nil))
}