forked from theupdateframework/go-tuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
1094 lines (964 loc) · 26 KB
/
repo.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tuf
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"path"
"sort"
"strings"
"time"
"github.com/secure-systems-lab/go-securesystemslib/cjson"
"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/internal/roles"
"github.com/theupdateframework/go-tuf/internal/signer"
"github.com/theupdateframework/go-tuf/pkg/keys"
"github.com/theupdateframework/go-tuf/sign"
"github.com/theupdateframework/go-tuf/util"
"github.com/theupdateframework/go-tuf/verify"
)
// topLevelMetadata determines the order signatures are verified when committing.
var topLevelMetadata = []string{
"root.json",
"targets.json",
"snapshot.json",
"timestamp.json",
}
// TargetsWalkFunc is a function of a target path name and a target payload used to
// execute some function on each staged target file. For example, it may normalize path
// names and generate target file metadata with additional custom metadata.
type TargetsWalkFunc func(path string, target io.Reader) error
type LocalStore interface {
// GetMeta returns a map from metadata file names (e.g. root.json) to their raw JSON payload or an error.
GetMeta() (map[string]json.RawMessage, error)
// SetMeta is used to update a metadata file name with a JSON payload.
SetMeta(string, json.RawMessage) error
// WalkStagedTargets calls targetsFn for each staged target file in paths.
//
// If paths is empty, all staged target files will be walked.
WalkStagedTargets(paths []string, targetsFn TargetsWalkFunc) error
// FileIsStaged determines if a metadata file is currently staged, to avoid incrementing
// version numbers repeatedly while staged.
FileIsStaged(filename string) bool
// Commit is used to publish staged files to the repository
//
// This will also reset the staged meta to signal incrementing version numbers.
// TUF 1.0 requires that the root metadata version numbers in the repository does not
// gaps. To avoid this, we will only increment the number once until we commit.
Commit(bool, map[string]int, map[string]data.Hashes) error
// GetSigners return a list of signers for a role.
GetSigners(string) ([]keys.Signer, error)
// SaveSigner adds a signer to a role.
SaveSigner(string, keys.Signer) error
// Clean is used to remove all staged metadata files.
Clean() error
}
type PassphraseChanger interface {
// ChangePassphrase changes the passphrase for a role keys file.
ChangePassphrase(string) error
}
type Repo struct {
local LocalStore
hashAlgorithms []string
meta map[string]json.RawMessage
prefix string
indent string
}
func NewRepo(local LocalStore, hashAlgorithms ...string) (*Repo, error) {
return NewRepoIndent(local, "", "", hashAlgorithms...)
}
func NewRepoIndent(local LocalStore, prefix string, indent string, hashAlgorithms ...string) (*Repo, error) {
r := &Repo{
local: local,
hashAlgorithms: hashAlgorithms,
prefix: prefix,
indent: indent,
}
var err error
r.meta, err = local.GetMeta()
if err != nil {
return nil, err
}
return r, nil
}
func (r *Repo) Init(consistentSnapshot bool) error {
t, err := r.topLevelTargets()
if err != nil {
return err
}
if len(t.Targets) > 0 {
return ErrInitNotAllowed
}
root := data.NewRoot()
root.ConsistentSnapshot = consistentSnapshot
// Set root version to 1 for a new root.
root.Version = 1
err = r.setTopLevelMeta("root.json", root)
if err == nil {
fmt.Println("Repository initialized")
}
return err
}
func (r *Repo) topLevelKeysDB() (*verify.DB, error) {
db := verify.NewDB()
root, err := r.root()
if err != nil {
return nil, err
}
for id, k := range root.Keys {
if err := db.AddKey(id, k); err != nil {
// TUF is considering in TAP-12 removing the
// requirement that the keyid hash algorithm be derived
// from the public key. So to be forwards compatible,
// we ignore `ErrWrongID` errors.
//
// TAP-12: https://github.com/theupdateframework/taps/blob/master/tap12.md
if _, ok := err.(verify.ErrWrongID); !ok {
return nil, err
}
}
}
for name, role := range root.Roles {
if err := db.AddRole(name, role); err != nil {
return nil, err
}
}
return db, nil
}
func (r *Repo) root() (*data.Root, error) {
rootJSON, ok := r.meta["root.json"]
if !ok {
return data.NewRoot(), nil
}
s := &data.Signed{}
if err := json.Unmarshal(rootJSON, s); err != nil {
return nil, err
}
root := &data.Root{}
if err := json.Unmarshal(s.Signed, root); err != nil {
return nil, err
}
return root, nil
}
func (r *Repo) snapshot() (*data.Snapshot, error) {
snapshotJSON, ok := r.meta["snapshot.json"]
if !ok {
return data.NewSnapshot(), nil
}
s := &data.Signed{}
if err := json.Unmarshal(snapshotJSON, s); err != nil {
return nil, err
}
snapshot := &data.Snapshot{}
if err := json.Unmarshal(s.Signed, snapshot); err != nil {
return nil, err
}
return snapshot, nil
}
func (r *Repo) RootVersion() (int, error) {
root, err := r.root()
if err != nil {
return -1, err
}
return root.Version, nil
}
func (r *Repo) GetThreshold(keyRole string) (int, error) {
root, err := r.root()
if err != nil {
return -1, err
}
role, ok := root.Roles[keyRole]
if !ok {
return -1, ErrInvalidRole{keyRole}
}
return role.Threshold, nil
}
func (r *Repo) SetThreshold(keyRole string, t int) error {
if !roles.IsTopLevelRole(keyRole) {
// Delegations are not currently supported, so return an error if this is not a
// top-level metadata file.
return ErrInvalidRole{keyRole}
}
root, err := r.root()
if err != nil {
return err
}
role, ok := root.Roles[keyRole]
if !ok {
return ErrInvalidRole{keyRole}
}
if role.Threshold == t {
// Change was a no-op.
return nil
}
role.Threshold = t
if !r.local.FileIsStaged("root.json") {
root.Version++
}
return r.setTopLevelMeta("root.json", root)
}
func (r *Repo) Targets() (data.TargetFiles, error) {
targets, err := r.topLevelTargets()
if err != nil {
return nil, err
}
return targets.Targets, nil
}
func (r *Repo) SetTargetsVersion(v int) error {
t, err := r.topLevelTargets()
if err != nil {
return err
}
t.Version = v
return r.setTopLevelMeta("targets.json", t)
}
func (r *Repo) TargetsVersion() (int, error) {
t, err := r.topLevelTargets()
if err != nil {
return -1, err
}
return t.Version, nil
}
func (r *Repo) SetTimestampVersion(v int) error {
ts, err := r.timestamp()
if err != nil {
return err
}
ts.Version = v
return r.setTopLevelMeta("timestamp.json", ts)
}
func (r *Repo) TimestampVersion() (int, error) {
ts, err := r.timestamp()
if err != nil {
return -1, err
}
return ts.Version, nil
}
func (r *Repo) SetSnapshotVersion(v int) error {
s, err := r.snapshot()
if err != nil {
return err
}
s.Version = v
return r.setTopLevelMeta("snapshot.json", s)
}
func (r *Repo) SnapshotVersion() (int, error) {
s, err := r.snapshot()
if err != nil {
return -1, err
}
return s.Version, nil
}
func (r *Repo) topLevelTargets() (*data.Targets, error) {
targetsJSON, ok := r.meta["targets.json"]
if !ok {
return data.NewTargets(), nil
}
s := &data.Signed{}
if err := json.Unmarshal(targetsJSON, s); err != nil {
return nil, err
}
targets := &data.Targets{}
if err := json.Unmarshal(s.Signed, targets); err != nil {
return nil, err
}
return targets, nil
}
func (r *Repo) timestamp() (*data.Timestamp, error) {
timestampJSON, ok := r.meta["timestamp.json"]
if !ok {
return data.NewTimestamp(), nil
}
s := &data.Signed{}
if err := json.Unmarshal(timestampJSON, s); err != nil {
return nil, err
}
timestamp := &data.Timestamp{}
if err := json.Unmarshal(s.Signed, timestamp); err != nil {
return nil, err
}
return timestamp, nil
}
func (r *Repo) ChangePassphrase(keyRole string) error {
if !roles.IsTopLevelRole(keyRole) {
return ErrInvalidRole{keyRole}
}
if p, ok := r.local.(PassphraseChanger); ok {
return p.ChangePassphrase(keyRole)
}
return ErrChangePassphraseNotSupported
}
func (r *Repo) GenKey(role string) ([]string, error) {
return r.GenKeyWithExpires(role, data.DefaultExpires(role))
}
func (r *Repo) GenKeyWithExpires(keyRole string, expires time.Time) (keyids []string, err error) {
signer, err := keys.GenerateEd25519Key()
if err != nil {
return []string{}, err
}
if err = r.AddPrivateKeyWithExpires(keyRole, signer, expires); err != nil {
return []string{}, err
}
keyids = signer.PublicData().IDs()
return
}
func (r *Repo) AddPrivateKey(role string, signer keys.Signer) error {
return r.AddPrivateKeyWithExpires(role, signer, data.DefaultExpires(role))
}
func (r *Repo) AddPrivateKeyWithExpires(keyRole string, signer keys.Signer, expires time.Time) error {
if !roles.IsTopLevelRole(keyRole) {
return ErrInvalidRole{keyRole}
}
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
if err := r.local.SaveSigner(keyRole, signer); err != nil {
return err
}
if err := r.AddVerificationKeyWithExpiration(keyRole, signer.PublicData(), expires); err != nil {
return err
}
return nil
}
func (r *Repo) AddVerificationKey(keyRole string, pk *data.PublicKey) error {
return r.AddVerificationKeyWithExpiration(keyRole, pk, data.DefaultExpires(keyRole))
}
func (r *Repo) AddVerificationKeyWithExpiration(keyRole string, pk *data.PublicKey, expires time.Time) error {
root, err := r.root()
if err != nil {
return err
}
role, ok := root.Roles[keyRole]
if !ok {
role = &data.Role{KeyIDs: []string{}, Threshold: 1}
root.Roles[keyRole] = role
}
changed := false
if role.AddKeyIDs(pk.IDs()) {
changed = true
}
if root.AddKey(pk) {
changed = true
}
if !changed {
return nil
}
root.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("root.json") {
root.Version++
}
return r.setTopLevelMeta("root.json", root)
}
func validExpires(expires time.Time) bool {
return time.Until(expires) > 0
}
func (r *Repo) RootKeys() ([]*data.PublicKey, error) {
root, err := r.root()
if err != nil {
return nil, err
}
role, ok := root.Roles["root"]
if !ok {
return nil, nil
}
// We might have multiple key ids that correspond to the same key, so
// make sure we only return unique keys.
seen := make(map[string]struct{})
rootKeys := []*data.PublicKey{}
for _, id := range role.KeyIDs {
key, ok := root.Keys[id]
if !ok {
return nil, fmt.Errorf("tuf: invalid root metadata")
}
found := false
if _, ok := seen[id]; ok {
found = true
break
}
if !found {
for _, id := range key.IDs() {
seen[id] = struct{}{}
}
rootKeys = append(rootKeys, key)
}
}
return rootKeys, nil
}
func (r *Repo) RevokeKey(role, id string) error {
return r.RevokeKeyWithExpires(role, id, data.DefaultExpires("root"))
}
func (r *Repo) RevokeKeyWithExpires(keyRole, id string, expires time.Time) error {
if !roles.IsTopLevelRole(keyRole) {
return ErrInvalidRole{keyRole}
}
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
root, err := r.root()
if err != nil {
return err
}
key, ok := root.Keys[id]
if !ok {
return ErrKeyNotFound{keyRole, id}
}
role, ok := root.Roles[keyRole]
if !ok {
return ErrKeyNotFound{keyRole, id}
}
// Create a list of filtered key IDs that do not contain the revoked key IDs.
filteredKeyIDs := make([]string, 0, len(role.KeyIDs))
// There may be multiple keyids that correspond to this key, so
// filter all of them out.
for _, keyID := range role.KeyIDs {
if !key.ContainsID(keyID) {
filteredKeyIDs = append(filteredKeyIDs, keyID)
}
}
if len(filteredKeyIDs) == len(role.KeyIDs) {
return ErrKeyNotFound{keyRole, id}
}
role.KeyIDs = filteredKeyIDs
root.Roles[keyRole] = role
// Only delete the key from root.Keys if the key is no longer in use by
// any other role.
key_in_use := false
for _, role := range root.Roles {
for _, keyID := range role.KeyIDs {
if key.ContainsID(keyID) {
key_in_use = true
}
}
}
if !key_in_use {
for _, keyID := range key.IDs() {
delete(root.Keys, keyID)
}
}
root.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("root.json") {
root.Version++
}
err = r.setTopLevelMeta("root.json", root)
if err == nil {
fmt.Println("Revoked", keyRole, "key with ID", id, "in root metadata")
}
return err
}
func (r *Repo) jsonMarshal(v interface{}) ([]byte, error) {
b, err := cjson.EncodeCanonical(v)
if err != nil {
return []byte{}, err
}
if r.prefix == "" && r.indent == "" {
return b, nil
}
var out bytes.Buffer
if err := json.Indent(&out, b, r.prefix, r.indent); err != nil {
return []byte{}, err
}
return out.Bytes(), nil
}
func (r *Repo) setTopLevelMeta(roleFilename string, meta interface{}) error {
keys, err := r.getSortedSigningKeys(strings.TrimSuffix(roleFilename, ".json"))
if err != nil {
return err
}
s, err := sign.Marshal(meta, keys...)
if err != nil {
return err
}
b, err := r.jsonMarshal(s)
if err != nil {
return err
}
r.meta[roleFilename] = b
return r.local.SetMeta(roleFilename, b)
}
func (r *Repo) Sign(roleFilename string) error {
role := strings.TrimSuffix(roleFilename, ".json")
if !roles.IsTopLevelRole(role) {
return ErrInvalidRole{role}
}
s, err := r.SignedMeta(roleFilename)
if err != nil {
return err
}
keys, err := r.getSortedSigningKeys(role)
if err != nil {
return err
}
if len(keys) == 0 {
return ErrInsufficientKeys{roleFilename}
}
for _, k := range keys {
sign.Sign(s, k)
}
b, err := r.jsonMarshal(s)
if err != nil {
return err
}
r.meta[roleFilename] = b
err = r.local.SetMeta(roleFilename, b)
if err == nil {
fmt.Println("Signed", roleFilename, "with", len(keys), "key(s)")
}
return err
}
// AddOrUpdateSignature allows users to add or update a signature generated with an external tool.
// The name must be a valid metadata file name, like root.json.
func (r *Repo) AddOrUpdateSignature(roleFilename string, signature data.Signature) error {
role := strings.TrimSuffix(roleFilename, ".json")
if !roles.IsTopLevelRole(role) {
return ErrInvalidRole{role}
}
// Check key ID is in valid for the role.
db, err := r.topLevelKeysDB()
if err != nil {
return err
}
roleData := db.GetRole(role)
if roleData == nil {
return ErrInvalidRole{role}
}
if !roleData.ValidKey(signature.KeyID) {
return verify.ErrInvalidKey
}
s, err := r.SignedMeta(roleFilename)
if err != nil {
return err
}
// Add or update signature.
signatures := make([]data.Signature, 0, len(s.Signatures)+1)
for _, sig := range s.Signatures {
if sig.KeyID != signature.KeyID {
signatures = append(signatures, sig)
}
}
signatures = append(signatures, signature)
s.Signatures = signatures
// Check signature on signed meta. Ignore threshold errors as this may not be fully
// signed.
if err := db.VerifySignatures(s, role); err != nil {
if _, ok := err.(verify.ErrRoleThreshold); !ok {
return err
}
}
b, err := r.jsonMarshal(s)
if err != nil {
return err
}
r.meta[roleFilename] = b
return r.local.SetMeta(roleFilename, b)
}
// getSortedSigningKeys returns available signing keys, sorted by key ID.
//
// Only keys contained in the keys db are returned (i.e. local keys which have
// been revoked are omitted), except for the root role in which case all local
// keys are returned (revoked root keys still need to sign new root metadata so
// clients can verify the new root.json and update their keys db accordingly).
func (r *Repo) getSortedSigningKeys(name string) ([]keys.Signer, error) {
signingKeys, err := r.local.GetSigners(name)
if err != nil {
return nil, err
}
if name == "root" {
sorted := make([]keys.Signer, len(signingKeys))
copy(sorted, signingKeys)
sort.Sort(signer.ByIDs(sorted))
return sorted, nil
}
db, err := r.topLevelKeysDB()
if err != nil {
return nil, err
}
role := db.GetRole(name)
if role == nil {
return nil, nil
}
if len(role.KeyIDs) == 0 {
return nil, nil
}
keys := make([]keys.Signer, 0, len(role.KeyIDs))
for _, key := range signingKeys {
for _, id := range key.PublicData().IDs() {
if _, ok := role.KeyIDs[id]; ok {
keys = append(keys, key)
}
}
}
sort.Sort(signer.ByIDs(keys))
return keys, nil
}
// Used to retrieve the signable portion of the metadata when using an external signing tool.
func (r *Repo) SignedMeta(roleFilename string) (*data.Signed, error) {
b, ok := r.meta[roleFilename]
if !ok {
return nil, ErrMissingMetadata{roleFilename}
}
s := &data.Signed{}
if err := json.Unmarshal(b, s); err != nil {
return nil, err
}
return s, nil
}
func (r *Repo) AddTarget(path string, custom json.RawMessage) error {
return r.AddTargets([]string{path}, custom)
}
func (r *Repo) AddTargets(paths []string, custom json.RawMessage) error {
return r.AddTargetsWithExpires(paths, custom, data.DefaultExpires("targets"))
}
func (r *Repo) AddTargetsWithDigest(digest string, digestAlg string, length int64, path string, custom json.RawMessage) error {
expires := data.DefaultExpires("targets")
// TODO: support delegated targets
t, err := r.topLevelTargets()
if err != nil {
return err
}
meta := data.FileMeta{Length: length, Hashes: make(data.Hashes, 1)}
meta.Hashes[digestAlg], err = hex.DecodeString(digest)
if err != nil {
return err
}
// If custom is provided, set custom, otherwise maintain existing custom
// metadata
if len(custom) > 0 {
meta.Custom = &custom
} else if t, ok := t.Targets[path]; ok {
meta.Custom = t.Custom
}
t.Targets[path] = data.TargetFileMeta{FileMeta: meta}
return r.writeTargetWithExpires(t, expires)
}
func (r *Repo) AddTargetWithExpires(path string, custom json.RawMessage, expires time.Time) error {
return r.AddTargetsWithExpires([]string{path}, custom, expires)
}
func (r *Repo) AddTargetsWithExpires(paths []string, custom json.RawMessage, expires time.Time) error {
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
t, err := r.topLevelTargets()
if err != nil {
return err
}
normalizedPaths := make([]string, len(paths))
for i, path := range paths {
normalizedPaths[i] = util.NormalizeTarget(path)
}
if err := r.local.WalkStagedTargets(normalizedPaths, func(path string, target io.Reader) (err error) {
meta, err := util.GenerateTargetFileMeta(target, r.hashAlgorithms...)
if err != nil {
return err
}
path = util.NormalizeTarget(path)
// if we have custom metadata, set it, otherwise maintain
// existing metadata if present
if len(custom) > 0 {
meta.Custom = &custom
} else if t, ok := t.Targets[path]; ok {
meta.Custom = t.Custom
}
// G2 -> we no longer desire any readers to ever observe non-prefix targets.
delete(t.Targets, "/"+path)
t.Targets[path] = meta
return nil
}); err != nil {
return err
}
return r.writeTargetWithExpires(t, expires)
}
func (r *Repo) writeTargetWithExpires(t *data.Targets, expires time.Time) error {
t.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("targets.json") {
t.Version++
}
err := r.setTopLevelMeta("targets.json", t)
if err == nil {
fmt.Println("Added/staged targets:")
for k := range t.Targets {
fmt.Println("*", k)
}
}
return err
}
func (r *Repo) RemoveTarget(path string) error {
return r.RemoveTargets([]string{path})
}
func (r *Repo) RemoveTargets(paths []string) error {
return r.RemoveTargetsWithExpires(paths, data.DefaultExpires("targets"))
}
func (r *Repo) RemoveTargetWithExpires(path string, expires time.Time) error {
return r.RemoveTargetsWithExpires([]string{path}, expires)
}
// If paths is empty, all targets will be removed.
func (r *Repo) RemoveTargetsWithExpires(paths []string, expires time.Time) error {
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
t, err := r.topLevelTargets()
if err != nil {
return err
}
removed_targets := []string{}
if len(paths) == 0 {
for rt := range t.Targets {
removed_targets = append(removed_targets, rt)
}
t.Targets = make(data.TargetFiles)
} else {
removed := false
for _, path := range paths {
path = util.NormalizeTarget(path)
if _, ok := t.Targets[path]; !ok {
fmt.Println("The following target is not present:", path)
continue
}
removed = true
// G2 -> we no longer desire any readers to ever observe non-prefix targets.
delete(t.Targets, "/"+path)
delete(t.Targets, path)
removed_targets = append(removed_targets, path)
}
if !removed {
return nil
}
}
t.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("targets.json") {
t.Version++
}
err = r.setTopLevelMeta("targets.json", t)
if err == nil {
fmt.Println("Removed targets:")
for _, v := range removed_targets {
fmt.Println("*", v)
}
if len(t.Targets) != 0 {
fmt.Println("Added/staged targets:")
for k := range t.Targets {
fmt.Println("*", k)
}
} else {
fmt.Println("There are no added/staged targets")
}
}
return err
}
func (r *Repo) Snapshot() error {
return r.SnapshotWithExpires(data.DefaultExpires("snapshot"))
}
func (r *Repo) snapshotMetadata() []string {
return []string{"targets.json"}
}
func (r *Repo) SnapshotWithExpires(expires time.Time) error {
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
snapshot, err := r.snapshot()
if err != nil {
return err
}
db, err := r.topLevelKeysDB()
if err != nil {
return err
}
for _, metaName := range r.snapshotMetadata() {
if err := r.verifySignature(metaName, db); err != nil {
return err
}
var err error
snapshot.Meta[metaName], err = r.snapshotFileMeta(metaName)
if err != nil {
return err
}
}
snapshot.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("snapshot.json") {
snapshot.Version++
}
err = r.setTopLevelMeta("snapshot.json", snapshot)
if err == nil {
fmt.Println("Staged snapshot.json metadata with expiration date:", snapshot.Expires)
}
return err
}
func (r *Repo) Timestamp() error {
return r.TimestampWithExpires(data.DefaultExpires("timestamp"))
}
func (r *Repo) TimestampWithExpires(expires time.Time) error {
if !validExpires(expires) {
return ErrInvalidExpires{expires}
}
db, err := r.topLevelKeysDB()
if err != nil {
return err
}
if err := r.verifySignature("snapshot.json", db); err != nil {
return err
}
timestamp, err := r.timestamp()
if err != nil {
return err
}
timestamp.Meta["snapshot.json"], err = r.timestampFileMeta("snapshot.json")
if err != nil {
return err
}
timestamp.Expires = expires.Round(time.Second)
if !r.local.FileIsStaged("timestamp.json") {
timestamp.Version++
}
err = r.setTopLevelMeta("timestamp.json", timestamp)
if err == nil {
fmt.Println("Staged timestamp.json metadata with expiration date:", timestamp.Expires)
}
return err
}
func (r *Repo) fileVersions() (map[string]int, error) {
root, err := r.root()
if err != nil {
return nil, err
}
targets, err := r.topLevelTargets()
if err != nil {
return nil, err
}
snapshot, err := r.snapshot()
if err != nil {
return nil, err
}
versions := make(map[string]int)
versions["root.json"] = root.Version
versions["targets.json"] = targets.Version
versions["snapshot.json"] = snapshot.Version
return versions, nil
}
func (r *Repo) fileHashes() (map[string]data.Hashes, error) {
hashes := make(map[string]data.Hashes)
timestamp, err := r.timestamp()
if err != nil {
return nil, err
}
snapshot, err := r.snapshot()
if err != nil {
return nil, err
}
if m, ok := snapshot.Meta["targets.json"]; ok {
hashes["targets.json"] = m.Hashes
}
if m, ok := timestamp.Meta["snapshot.json"]; ok {
hashes["snapshot.json"] = m.Hashes
}
t, err := r.topLevelTargets()
if err != nil {
return nil, err
}
for name, meta := range t.Targets {
hashes[path.Join("targets", name)] = meta.Hashes
}
return hashes, nil
}
func (r *Repo) Commit() error {
// check we have all the metadata
for _, name := range topLevelMetadata {
if _, ok := r.meta[name]; !ok {
return ErrMissingMetadata{name}
}
}
// check roles are valid
root, err := r.root()
if err != nil {
return err
}
for name, role := range root.Roles {
if len(role.KeyIDs) < role.Threshold {
return ErrNotEnoughKeys{name, len(role.KeyIDs), role.Threshold}
}
}