-
Notifications
You must be signed in to change notification settings - Fork 567
/
validator.go
169 lines (152 loc) · 3.85 KB
/
validator.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
package pfsload
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"strings"
"github.com/pachyderm/pachyderm/v2/src/internal/client"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/fileset"
"github.com/pachyderm/pachyderm/v2/src/pfs"
)
type Validator struct {
spec *ValidatorSpec
random *rand.Rand
// TODO: Potentially make this pachhash.Output.
hash []byte
count int64
}
func NewValidator(spec *ValidatorSpec, random *rand.Rand) (*Validator, error) {
if spec.Frequency != nil {
if err := validateProb(int(spec.Frequency.Prob)); err != nil {
return nil, err
}
}
return &Validator{
spec: spec,
random: random,
hash: pfs.NewHash().Sum(nil),
}, nil
}
func (v *Validator) Hash() []byte {
return v.hash
}
func (v *Validator) SetHash(hash []byte) {
v.hash = hash
}
func (v *Validator) AddHash(hash []byte) {
v.hash = xor(v.hash, hash)
}
func xor(b1, b2 []byte) []byte {
result := make([]byte, len(b1))
for i, b := range b1 {
result[i] = b ^ b2[i]
}
return result
}
func (v *Validator) Validate(ctx context.Context, client Client, commit *pfs.Commit) error {
if v.spec.Frequency != nil {
freq := v.spec.Frequency
if freq.Count > 0 {
v.count++
if freq.Count > v.count {
return nil
}
v.count = 0
} else if !shouldExecute(v.random, int(freq.Prob)) {
return nil
}
}
err := client.WaitCommitSet(ctx, commit.Id, func(ci *pfs.CommitInfo) error {
if ci.Commit.Repo.Type != pfs.UserRepoType {
return nil
}
hash, err := computeCommitHash(ctx, client, ci.Commit)
if err != nil {
return err
}
if !bytes.Equal(hash, v.hash) {
return errors.Errorf("hash of files at commit %v different from validator hash", ci.Commit)
}
return nil
})
return errors.EnsureStack(err)
}
// TODO: Distribute validation and revert back to being based on file path + content rather than
// file path + size.
func computeCommitHash(ctx context.Context, client Client, commit *pfs.Commit) ([]byte, error) {
hash := pfs.NewHash().Sum(nil)
if err := client.GlobFile(ctx, commit, "**", func(fi *pfs.FileInfo) error {
if strings.HasSuffix(fi.File.Path, "/") {
return nil
}
h := pfs.NewHash()
h.Write([]byte(fi.File.Path))
h.Write([]byte(fmt.Sprintf("%016d", fi.SizeBytes)))
hash = xor(hash, h.Sum(nil))
return nil
}); err != nil {
return nil, errors.EnsureStack(err)
}
return hash, nil
}
type validatorClient struct {
Client
hash []byte
}
func NewValidatorClient(client Client) *validatorClient {
return &validatorClient{
Client: client,
}
}
func (vc *validatorClient) WithCreateFileSetClient(ctx context.Context, cb func(client.ModifyFile) error) (*pfs.CreateFileSetResponse, error) {
resp, err := vc.Client.WithCreateFileSetClient(ctx, func(mf client.ModifyFile) error {
vcfsc := &validatorCreateFileSetClient{
ModifyFile: mf,
}
if err := cb(vcfsc); err != nil {
return err
}
if vcfsc.hash == nil {
return nil
}
if vc.hash == nil {
vc.hash = vcfsc.hash
return nil
}
vc.hash = xor(vc.hash, vcfsc.hash)
return nil
})
return resp, errors.EnsureStack(err)
}
type validatorCreateFileSetClient struct {
client.ModifyFile
hash []byte
}
func (vcfsc *validatorCreateFileSetClient) PutFile(path string, r io.Reader, opts ...client.PutFileOption) error {
path = fileset.Clean(path, false)
sr := &sizeReader{Reader: r}
if err := vcfsc.ModifyFile.PutFile(path, sr, opts...); err != nil {
return errors.EnsureStack(err)
}
hash := pfs.NewHash()
hash.Write([]byte(path))
hash.Write([]byte(fmt.Sprintf("%016d", sr.size)))
if vcfsc.hash == nil {
vcfsc.hash = hash.Sum(nil)
return nil
}
vcfsc.hash = xor(vcfsc.hash, hash.Sum(nil))
return nil
}
type sizeReader struct {
io.Reader
size int64
}
func (sr *sizeReader) Read(data []byte) (int, error) {
n, err := sr.Reader.Read(data)
sr.size += int64(n)
return n, errors.EnsureStack(err)
}