-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory_plugin.go
209 lines (183 loc) · 4.51 KB
/
directory_plugin.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package omnitrail
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/omnibor/omnibor-go"
)
type DirectoryPlugin struct {
algorithms []string
// algorithm -> path -> hash
directories map[string]bool
sha1adgs map[string]omnibor.ArtifactTree
sha256adgs map[string]omnibor.ArtifactTree
AllowList []string
}
func (plug *DirectoryPlugin) isAllowedDirectory(path string) bool {
for _, allowedPath := range plug.AllowList {
if strings.HasPrefix(path, allowedPath) {
return true
}
}
return false
}
func (plug *DirectoryPlugin) Sha1ADG(m map[string]string) {
for _, v := range plug.sha1adgs {
m[v.Identity()] = v.String()
}
}
func (plug *DirectoryPlugin) Sha256ADG(m map[string]string) {
for _, v := range plug.sha256adgs {
m[v.Identity()] = v.String()
}
}
func (plug *DirectoryPlugin) Add(path string) error {
// if this is a broken symlink, ignore
fileInfo, err := os.Lstat(path)
if err != nil {
// if it's a symlink and the symlink is bad, ignore and return
if os.IsNotExist(err) {
return nil
}
return err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
// path is a symlink
targetPath, err := os.Readlink(path)
if err != nil {
// if it's a symlink and the symlink is bad, ignore and return
if os.IsNotExist(err) {
return nil
}
return err
}
if !filepath.IsAbs(targetPath) {
targetPath = filepath.Join(filepath.Dir(path), targetPath)
}
if !plug.isAllowedDirectory(targetPath) {
return fmt.Errorf("path %s is not in the allow list", path)
}
if _, err := os.Stat(targetPath); err != nil {
return nil
}
}
stat, err := os.Stat(path)
if err != nil {
return err
}
if stat.IsDir() {
plug.directories[path] = true
}
return nil
}
func (plug *DirectoryPlugin) Store(envelope *Envelope) error {
envelope.Header.Features["directory"] = Feature{Algorithms: plug.algorithms}
// get a list of all keys from plug.directories
keys := make([]string, 0, len(plug.directories))
for path := range plug.directories {
keys = append(keys, path)
}
var sha1tree map[string]omnibor.ArtifactTree
var sha256tree map[string]omnibor.ArtifactTree
for _, algorithm := range plug.algorithms {
switch algorithm {
case "gitoid:sha1":
sha1tree = make(map[string]omnibor.ArtifactTree)
case "gitoid:sha256":
sha256tree = make(map[string]omnibor.ArtifactTree)
default:
continue
}
}
for _, key := range keys {
if sha1tree != nil {
sha1tree[key] = omnibor.NewSha1OmniBOR()
}
if sha256tree != nil {
sha256tree[key] = omnibor.NewSha256OmniBOR()
}
}
for path, element := range envelope.Mapping {
dir := filepath.Dir(path)
if _, ok := sha1tree[dir]; ok {
err := sha1tree[dir].AddExistingReference(element.Sha1Gitoid)
if err != nil {
return err
}
err = sha256tree[dir].AddExistingReference(element.Sha256Gitoid)
if err != nil {
return err
}
}
}
// sort the keys from the longest length to the shortest length
sort.Slice(keys, func(i, j int) bool {
return len(keys[i]) > len(keys[j])
})
if sha1tree != nil {
err := plug.addKeysToTree(keys, sha1tree)
if err != nil {
return err
}
}
if sha256tree != nil {
err := plug.addKeysToTree(keys, sha256tree)
if err != nil {
return err
}
}
for key, value := range sha1tree {
if _, ok := envelope.Mapping[key]; !ok {
envelope.Mapping[key] = &Element{
Type: "directory",
}
}
e := envelope.Mapping[key]
e.Sha1Gitoid = value.Identity()
envelope.Mapping[key] = e
}
for key, value := range sha256tree {
if _, ok := envelope.Mapping[key]; !ok {
envelope.Mapping[key] = &Element{
Type: "directory",
}
}
e := envelope.Mapping[key]
e.Sha256Gitoid = value.Identity()
envelope.Mapping[key] = e
}
for k, v := range sha1tree {
plug.sha1adgs[k] = v
}
for k, v := range sha256tree {
plug.sha256adgs[k] = v
}
return nil
}
func (plug *DirectoryPlugin) addKeysToTree(keys []string, tree map[string]omnibor.ArtifactTree) error {
for _, key := range keys {
dir := filepath.Dir(key)
if _, ok := tree[dir]; ok {
err := tree[dir].AddExistingReference(tree[key].Identity())
if err != nil {
return err
}
}
}
return nil
}
func (plug *DirectoryPlugin) SetAllowList(allowList []string) {
plug.AllowList = allowList
}
func NewDirectoryPlugin() Plugin {
algorithms := []string{"gitoid:sha1", "gitoid:sha256"}
sort.Strings(algorithms)
return &DirectoryPlugin{
algorithms: algorithms,
directories: make(map[string]bool),
sha1adgs: make(map[string]omnibor.ArtifactTree),
sha256adgs: make(map[string]omnibor.ArtifactTree),
}
}