This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.go
286 lines (257 loc) · 8.2 KB
/
types.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Package verify implements verification of seal files previously written with the seal command
package verify
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/Byron/godi/api"
"github.com/Byron/godi/codec"
"github.com/Byron/godi/io"
)
const (
Name = "verify"
)
// A type representing all arguments required to drive a Seal operation
type Command struct {
api.BasicRunner
}
// Implements information about a verify operation
type VerifyResult struct {
api.BasicResult // will contain the actual file information from the disk file
ifinfo api.FileInfo // the file information we have seen in the index
}
// Keeps some information on a per-tree level
type treeInfo struct {
signatureMismatches, missingFiles, numFiles uint
sealBroken bool
}
// NewCommand returns an initialized verify command
func NewCommand(indices []string, nReaders int) (*Command, error) {
c := Command{}
return &c, c.Init(nReaders, 0, indices, api.Info, nil)
}
func (s *Command) Generate() <-chan api.Result {
return api.Generate(s.RootedReaders, s,
func(trees []string, files chan<- api.FileInfo, results chan<- api.Result) {
for _, index := range s.Items {
// Only work in indices that are assigned to us
found := false
for _, tree := range trees {
if filepath.Dir(index) == tree {
found = true
break
}
}
if !found {
continue
}
c := codec.NewByPath(index)
if c == nil {
panic("Should have a codec here - this was checked before")
}
fd, err := os.Open(index)
if err != nil {
results <- &VerifyResult{
BasicResult: api.BasicResult{
Err: &codec.DecodeError{Msg: err.Error()},
Finfo: api.FileInfo{
Path: index,
RelaPath: filepath.Base(index),
},
},
}
continue
}
// Figure out the path to use - for now we use the relative one
// NOTE: We need to use the relative one as our read-controller device map is based on that.
// If it was the absolute file path we use here, it could possibly point to a file far away,
// in any case our read controller map will not yield the expected result unless we set it
// up here, which is dangerous as it is async ! So let's not use the absolute path, ever !
indexDir := filepath.Dir(index)
err = c.Deserialize(fd, files, func(v *api.FileInfo) bool {
select {
case <-s.Done:
return false
default:
{
v.Path = filepath.Join(indexDir, v.RelaPath)
return true
}
}
})
fd.Close()
if err != nil {
results <- &VerifyResult{
BasicResult: api.BasicResult{
Err: err,
Finfo: api.FileInfo{
Path: index,
RelaPath: filepath.Base(index),
},
},
}
continue
}
} // for each index
})
}
func (s *Command) Gather(rctrl *io.ReadChannelController, files <-chan api.FileInfo, results chan<- api.Result) {
makeResult := func(f, source *api.FileInfo, err error) api.Result {
res := VerifyResult{
BasicResult: api.BasicResult{
Finfo: *f,
Prio: api.Info,
Err: err,
},
}
if source != nil {
// We just copy the originally retrieved file-info
res.ifinfo = *source
} else {
panic("Should have received the original fileInfo, to obtain the sealed values for hashes")
}
return &res
}
api.Gather(files, results, &s.Stats, makeResult, rctrl, nil)
}
func (s *Command) Aggregate(results <-chan api.Result) <-chan api.Result {
// Associates a root with with the respective tree information
// This comes at the disadvantage that we can't differentiate if there are multiple seals underneath the same root.
treeInfoMap := make(map[string]*treeInfo)
resultHandler := func(r api.Result, accumResult chan<- api.Result) bool {
vr := r.(*VerifyResult)
ti, hasTi := treeInfoMap[vr.Finfo.Root()]
if !hasTi {
ti = &treeInfo{}
treeInfoMap[vr.Finfo.Root()] = ti
}
if vr.Err != nil {
if os.IsNotExist(vr.Err) || os.IsPermission(vr.Err) {
ti.missingFiles += 1
vr.Msg = fmt.Sprintf("MISSING %s: %s", SymbolMismatch, vr.Finfo.Path)
accumResult <- vr
return false
} else if serr, isFileSizeType := vr.Err.(*api.FileSizeMismatch); isFileSizeType {
// The file-size changed, thus the hashes will be different to. Say it accordingly.
ti.signatureMismatches += 1
ti.numFiles += 1
vr.Msg = fmt.Sprintf("SIZE %s: %s sealed with size %dB, got size %dB", SymbolMismatch, serr.Path, serr.Want, serr.Got)
accumResult <- vr
return false
} else if _, isSealSigMismatch := vr.Err.(*codec.SignatureMismatchError); isSealSigMismatch {
ti.sealBroken = true
vr.Msg = fmt.Sprintf("SEAL %s: '%s' was modified after sealing or is corrupted - don't trust the verify results", SymbolMismatch, vr.Finfo.Path)
accumResult <- vr
return false
} else if _, isDecodeErr := vr.Err.(*codec.DecodeError); isDecodeErr {
ti.sealBroken = true
vr.Msg = fmt.Sprintf("SEAL %s", "Failed to decode seal at '%s' with error '%s' - verify results can't be trusted", SymbolFail, vr.Finfo.Path, vr.Err.Error())
accumResult <- vr
return false
} else {
// It's some other error - just push it forward
accumResult <- vr
return false
}
}
hasError := false
vr.Prio = api.Info
// From here on, it must be a file with no obvious error
ti.numFiles += 1
if (len(vr.ifinfo.Sha1) > 0 && bytes.Compare(vr.ifinfo.Sha1, vr.Finfo.Sha1) != 0) ||
(len(vr.ifinfo.MD5) > 0 && bytes.Compare(vr.ifinfo.MD5, vr.Finfo.MD5) != 0) {
vr.Msg = fmt.Sprintf("HASH %s: %s flipped at least one bit", SymbolMismatch, vr.Finfo.Path)
vr.Err = &api.FileHashMismatch{Path: vr.Finfo.Path}
ti.signatureMismatches += 1
hasError = true
vr.Prio = api.Error
} else {
vr.Msg = fmt.Sprintf("%s: %s", SymbolOK, vr.Finfo.Path)
}
accumResult <- vr
return !hasError
}
finalizer := func(
accumResult chan<- api.Result) {
count := 0
stats := ""
for treeRoot, ti := range treeInfoMap {
count += 1
s.Stats.ErrCount -= ti.signatureMismatches
s.Stats.ErrCount -= ti.missingFiles
// the last result we produce has the final statistics
if count == len(treeInfoMap) {
stats = fmt.Sprintf(" [%s]%s",
s.Stats.DeltaString(&s.Stats, s.Stats.Elapsed(), io.StatsClientSep),
s.Stats.String(),
)
}
if ti.signatureMismatches == 0 && ti.missingFiles == 0 && !ti.sealBroken {
// Make sure we don't pretend it's fine, just because none of the read files SO FAR had an issue
ss := SymbolSuccess
suffix := ""
if s.Stats.ErrCount > 0 {
ss = SymbolFail
suffix = ", but didn't read entire seal"
}
accumResult <- &VerifyResult{
BasicResult: api.BasicResult{
Msg: fmt.Sprintf(
"VERIFY %s: None of %d file(s) changed based on seal in '%s'%s%s",
ss,
ti.numFiles,
treeRoot,
suffix,
stats,
),
Prio: api.Valuable,
},
}
} else {
suffix := ""
if ti.missingFiles > 0 {
suffix = fmt.Sprintf(", with %d missing,", ti.missingFiles)
}
accumResult <- &VerifyResult{
BasicResult: api.BasicResult{
Msg: fmt.Sprintf(
"VERIFY %s: %d of %d file(s) have changed%s based on seal in '%s'%s",
SymbolFail,
ti.signatureMismatches,
ti.numFiles,
suffix,
treeRoot,
stats,
),
Prio: api.Valuable,
},
}
}
} // end for each treeInfo
} // end finalizer
return api.Aggregate(results, s.Done, resultHandler, finalizer, &s.Stats)
}
func (s *Command) Init(numReaders, numWriters int, items []string, maxLogLevel api.Importance, filters []api.FileFilter) (err error) {
if len(items) == 0 {
return errors.New("Please provide at least one seal file")
}
validItems, err := api.ParseSources(items, true)
if err != nil {
return
}
indexDirs := make([]string, len(validItems))
for i, index := range validItems {
if codec := codec.NewByPath(index); codec == nil {
return fmt.Errorf("Unknown seal file format: '%s'", index)
}
if _, err := os.Stat(index); err != nil {
return fmt.Errorf("Cannot access seal file at '%s'", index)
}
indexDirs[i] = filepath.Dir(index)
}
s.InitBasicRunner(numReaders, indexDirs, maxLogLevel, filters)
s.Items = validItems
return nil
}