forked from endophage/gotuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
243 lines (227 loc) · 5.99 KB
/
client.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
package client
import (
"encoding/json"
"fmt"
"io"
"path"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
tuf "github.com/endophage/gotuf"
"github.com/endophage/gotuf/data"
"github.com/endophage/gotuf/keys"
"github.com/endophage/gotuf/signed"
"github.com/endophage/gotuf/store"
"github.com/endophage/gotuf/utils"
)
type Client struct {
local *tuf.TufRepo
remote store.RemoteStore
keysDB *keys.KeyDB
}
func NewClient(local *tuf.TufRepo, remote store.RemoteStore, keysDB *keys.KeyDB) *Client {
return &Client{
local: local,
remote: remote,
keysDB: keysDB,
}
}
// Update an in memory copy of the TUF Repo. If an error is returned, the
// Client instance should be considered corrupted and discarded as it may
// be left in a partially updated state
func (c *Client) Update() error {
err := c.update()
if err != nil {
switch err.(type) {
case tuf.ErrSigVerifyFail:
case tuf.ErrMetaExpired:
case tuf.ErrLocalRootExpired:
if err := c.downloadRoot(); err != nil {
logrus.Errorf("Client Update (Root):", err)
return err
}
default:
return err
}
}
// If we error again, we now have the latest root and just want to fail
// out as there's no expectation the problem can be resolved automatically
return c.update()
}
func (c *Client) update() error {
err := c.downloadTimestamp()
if err != nil {
logrus.Errorf("Client Update (Timestamp): ", err.Error())
return err
}
err = c.downloadSnapshot()
if err != nil {
logrus.Errorf("Client Update (Snapshot): ", err.Error())
return err
}
err = c.checkRoot()
if err != nil {
return err
}
err = c.downloadTargets("targets")
if err != nil {
logrus.Errorf("Client Update (Targets): ", err.Error())
return err
}
return nil
}
// checkRoot determines if the hash, and size are still those reported
// in the snapshot file. It will also check the expiry, however, if the
// hash and size in snapshot are unchanged but the root file has expired,
// there is little expectation that the situation can be remedied.
func (c Client) checkRoot() error {
return nil
}
// downloadRoot is responsible for downloading the root.json
func (c *Client) downloadRoot() error {
role := data.RoleName("root")
size := c.local.Snapshot.Signed.Meta[role].Length
raw, err := c.remote.GetMeta(role, size)
if err != nil {
return err
}
s := &data.Signed{}
err = json.Unmarshal(raw, s)
if err != nil {
return err
}
err = signed.Verify(s, role, 0, c.keysDB)
if err != nil {
return err
}
c.local.SetRoot(s)
return nil
}
// downloadTimestamp is responsible for downloading the timestamp.json
func (c *Client) downloadTimestamp() error {
role := data.RoleName("timestamp")
raw, err := c.remote.GetMeta(role, 5<<20)
if err != nil {
return err
}
s := &data.Signed{}
err = json.Unmarshal(raw, s)
if err != nil {
return err
}
err = signed.Verify(s, role, 0, c.keysDB)
if err != nil {
return err
}
c.local.SetTimestamp(s)
return nil
}
// downloadSnapshot is responsible for downloading the snapshot.json
func (c *Client) downloadSnapshot() error {
role := data.RoleName("snapshot")
size := c.local.Timestamp.Signed.Meta[role].Length
raw, err := c.remote.GetMeta(role, size)
if err != nil {
return err
}
s := &data.Signed{}
err = json.Unmarshal(raw, s)
if err != nil {
return err
}
err = signed.Verify(s, role, 0, c.keysDB)
if err != nil {
return err
}
c.local.SetSnapshot(s)
return nil
}
// downloadTargets is responsible for downloading any targets file
// including delegates roles. It will download the whole tree of
// delegated roles below the given one
func (c *Client) downloadTargets(role string) error {
role = data.RoleName(role) // this will really only do something for base targets role
snap := c.local.Snapshot.Signed
root := c.local.Root.Signed
r := c.keysDB.GetRole(role)
if r == nil {
return fmt.Errorf("Invalid role: %s", role)
}
keyIDs := r.KeyIDs
s, err := c.GetTargetsFile(role, keyIDs, snap.Meta, root.ConsistentSnapshot, r.Threshold)
if err != nil {
logrus.Error("Error getting targets file:", err)
return err
}
err = c.local.SetTargets(role, s)
if err != nil {
return err
}
t := c.local.Targets[role].Signed
for _, r := range t.Delegations.Roles {
err := c.downloadTargets(r.Name)
if err != nil {
logrus.Error("Failed to download ", role, err)
return err
}
}
return nil
}
func (c Client) GetTargetsFile(roleName string, keyIDs []string, snapshotMeta data.Files, consistent bool, threshold int) (*data.Signed, error) {
rolePath, err := c.RoleTargetsPath(roleName, snapshotMeta, consistent)
if err != nil {
return nil, err
}
r, err := c.remote.GetMeta(rolePath, snapshotMeta[roleName].Length)
if err != nil {
return nil, err
}
s := &data.Signed{}
err = json.Unmarshal(r, s)
if err != nil {
logrus.Error("Error unmarshalling targets file:", err)
return nil, err
}
err = signed.Verify(s, roleName, 0, c.keysDB)
if err != nil {
return nil, err
}
return s, nil
}
func (c Client) RoleTargetsPath(roleName string, snapshotMeta data.Files, consistent bool) (string, error) {
if consistent {
roleMeta, ok := snapshotMeta[roleName]
if !ok {
return "", fmt.Errorf("Consistent Snapshots Enabled but no meta found for target role")
}
if _, ok := roleMeta.Hashes["sha256"]; !ok {
return "", fmt.Errorf("Consistent Snapshots Enabled and sha256 not found for targets file in snapshot meta")
}
dir := filepath.Dir(roleName)
if strings.Contains(roleName, "/") {
lastSlashIdx := strings.LastIndex(roleName, "/")
roleName = roleName[lastSlashIdx+1:]
}
roleName = path.Join(
dir,
fmt.Sprintf("%s.%s.json", roleMeta.Hashes["sha256"], roleName),
)
}
return roleName, nil
}
func (c Client) TargetMeta(path string) *data.FileMeta {
return c.local.FindTarget(path)
}
func (c Client) DownloadTarget(dst io.Writer, path string, meta *data.FileMeta) error {
reader, err := c.remote.GetTarget(path)
if err != nil {
return err
}
defer reader.Close()
r := io.TeeReader(
io.LimitReader(reader, meta.Length),
dst,
)
err = utils.ValidateTarget(r, meta)
return err
}