-
Notifications
You must be signed in to change notification settings - Fork 312
/
main.go
742 lines (666 loc) · 20.8 KB
/
main.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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/repository"
ru "github.com/pingcap/tiup/pkg/repository/utils"
"github.com/pingcap/tiup/pkg/repository/v0manifest"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/utils"
tiupver "github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
)
var signedRoot bool // generate keys and sign root in new command
func main() {
cmd := &cobra.Command{
Use: "migrate <command>",
Short: "Migrate a repository from v0 format to v1 manifests",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
newCmd := &cobra.Command{
Use: "new <src-dir> <dst-dir>",
Short: "Generate new manifest base on exists ola manifest",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return cmd.Help()
}
return migrate(args[0], args[1], false)
},
}
newCmd.Flags().BoolVar(&signedRoot, "signed-root", false, "generate keys and sign root in new command")
var isPublicKey bool
updateCmd := &cobra.Command{
Use: "update <dir> [signed-root.json ...]",
Short: "Rehash root.json and update snapshot.json, timestamp.json in the repo",
Long: `Rehash root.json and update snapshot.json, timestamp.json in the repo.
If one or more signed root.json files are specified, the signatures
from them are added to the root.json before updating.`,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Help()
}
return update(args[0], args[1:], isPublicKey)
},
}
updateCmd.Flags().BoolVar(&isPublicKey, "public", false, "Indicate that inputs are public keys to be added to key list, not signatures")
rehashCmd := &cobra.Command{
Use: "rehash <src-dir> <dst-dir>",
Short: "Rehash tarballs and update manifests in the repo",
Long: `Rehash tarballs in src-dir and update manifests in the dst-dir repo.
A already populated repo should be in dst-dir, and the root.json will not be changed.`,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return cmd.Help()
}
return migrate(args[0], args[1], true)
},
}
cmd.AddCommand(
newCmd,
updateCmd,
rehashCmd,
)
if err := cmd.Execute(); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}
func readManifest0(srcDir string) (*v0manifest.ComponentManifest, error) {
f, err := os.OpenFile(filepath.Join(srcDir, repository.ManifestFileName), os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, errors.Trace(err)
}
defer f.Close()
m := &v0manifest.ComponentManifest{}
if err := json.NewDecoder(f).Decode(m); err != nil {
return nil, errors.Trace(err)
}
return m, nil
}
func readManifest1(dir, filename string, m *v1manifest.Manifest) error {
f, err := os.Open(filepath.Join(dir, filename))
if err != nil {
return errors.Trace(err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return errors.Trace(err)
}
return json.Unmarshal(bytes, m)
}
func readVersions(srcDir, comp string) (*v0manifest.VersionManifest, error) {
f, err := os.OpenFile(filepath.Join(srcDir, fmt.Sprintf("tiup-component-%s.index", comp)), os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, errors.Trace(err)
}
defer f.Close()
m := &v0manifest.VersionManifest{}
if err := json.NewDecoder(f).Decode(m); err != nil {
return nil, errors.Trace(err)
}
return m, nil
}
func migrate(srcDir, dstDir string, rehash bool) error {
m, err := readManifest0(srcDir)
if err != nil {
return errors.Trace(err)
}
if !rehash {
if err := os.MkdirAll(filepath.Join(dstDir, "keys"), 0755); err != nil {
return errors.Trace(err)
}
if err := os.MkdirAll(filepath.Join(dstDir, "manifests"), 0755); err != nil {
return errors.Trace(err)
}
}
initTime := time.Now().UTC()
var (
root *v1manifest.Root
index *v1manifest.Index
)
signedManifests := make(map[string]*v1manifest.Manifest)
snapshot := v1manifest.NewSnapshot(initTime)
if rehash { // read exist manifests
s := &v1manifest.Manifest{
Signed: &v1manifest.Snapshot{},
}
err = readManifest1(dstDir, filepath.Join("manifests", snapshot.Filename()), s)
if err != nil {
return errors.Trace(err)
}
snapshot = s.Signed.(*v1manifest.Snapshot)
v1manifest.RenewManifest(snapshot, initTime)
r := &v1manifest.Manifest{
Signed: &v1manifest.Root{},
}
fmt.Println("reading root.json")
if err := readManifest1(dstDir, filepath.Join("manifests", root.Filename()), r); err != nil {
return errors.Trace(err)
}
root = r.Signed.(*v1manifest.Root)
i := &v1manifest.Manifest{
Signed: &v1manifest.Index{},
}
fmt.Println("reading index.json")
fname := fmt.Sprintf("%d.%s",
snapshot.Meta["/"+index.Filename()].Version, index.Filename())
if err := readManifest1(dstDir, filepath.Join("manifests", fname), i); err != nil {
return errors.Trace(err)
}
index = i.Signed.(*v1manifest.Index)
v1manifest.RenewManifest(index, initTime)
} else {
root = v1manifest.NewRoot(initTime)
index = v1manifest.NewIndex(initTime)
}
manifests := map[string]v1manifest.ValidManifest{
v1manifest.ManifestTypeRoot: root,
v1manifest.ManifestTypeIndex: index,
}
// TODO: bootstrap a server instead of generating key
keyDir := filepath.Join(dstDir, "keys")
keys := map[string][]*v1manifest.KeyInfo{}
tys := []string{ // root.json is not signed by default
v1manifest.ManifestTypeIndex,
v1manifest.ManifestTypeSnapshot,
v1manifest.ManifestTypeTimestamp,
}
if rehash { // read keys
for _, ty := range tys { // read manifest keys
privKi := make([]*v1manifest.KeyInfo, 0)
for id := range root.Roles[ty].Keys {
pk := &v1manifest.KeyInfo{}
f, err := os.Open(filepath.Join(dstDir, "keys",
fmt.Sprintf("%s-%s.json", id[:v1manifest.ShortKeyIDLength], ty)))
if err != nil {
return errors.Trace(err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return errors.Trace(err)
}
if err = json.Unmarshal(bytes, pk); err != nil {
return errors.Trace(err)
}
privKi = append(privKi, pk)
}
keys[ty] = privKi
}
} else { // generate new keys
if signedRoot {
tys = append(tys, v1manifest.ManifestTypeRoot)
}
for _, ty := range tys {
if err := v1manifest.GenAndSaveKeys(keys, ty, int(v1manifest.ManifestsConfig[ty].Threshold), keyDir); err != nil {
return errors.Trace(err)
}
}
}
// initial manifests
genkey := func() (string, *v1manifest.KeyInfo, error) {
priv, err := v1manifest.GenKeyInfo()
if err != nil {
return "", nil, err
}
id, err := priv.ID()
if err != nil {
return "", nil, err
}
return id, priv, nil
}
var (
ownerkeyID string
ownerkeyInfo = &v1manifest.KeyInfo{}
)
if rehash {
// read owner key (not considering index version, migrate assumes there
// is no owner change between new and rehash
for id := range index.Owners["pingcap"].Keys {
ownerkeyID = id
f, err := os.Open(filepath.Join(dstDir, "keys",
fmt.Sprintf("%s-pingcap.json", ownerkeyID[:v1manifest.ShortKeyIDLength])))
if err != nil {
return errors.Trace(err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return errors.Trace(err)
}
if err = json.Unmarshal(bytes, ownerkeyInfo); err != nil {
return errors.Trace(err)
}
}
} else {
// Initialize the index manifest
ownerkeyID, ownerkeyInfo, err = genkey()
if err != nil {
return errors.Trace(err)
}
// save owner key
if err := v1manifest.SaveKeyInfo(ownerkeyInfo, "pingcap", keyDir); err != nil {
return errors.Trace(err)
}
ownerkeyPub, err := ownerkeyInfo.Public()
if err != nil {
return errors.Trace(err)
}
index.Owners["pingcap"] = v1manifest.Owner{
Name: "PingCAP",
Keys: map[string]*v1manifest.KeyInfo{
ownerkeyID: ownerkeyPub,
},
Threshold: 1,
}
}
hasTiUP := false
for _, comp := range m.Components {
if comp.Name == "tiup" {
hasTiUP = true
break
}
}
// If TiUP it self is not in the v0 repo, build a dummy one from the default tarball
tiupDesc := "Components manager for TiDB ecosystem"
tiupVersions := &v0manifest.VersionManifest{
Description: tiupDesc,
Versions: []v0manifest.VersionInfo{
{
Version: pkgver.Version(tiupver.NewTiUPVersion().SemVer()),
Date: time.Now().Format(time.RFC3339),
Entry: "tiup",
},
},
}
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "arm64"} {
name := fmt.Sprintf("tiup-%s-%s.tar.gz", goos, goarch)
path := filepath.Join(srcDir, name)
if utils.IsExist(path) {
tiupVersions.Versions[0].Platforms = append(tiupVersions.Versions[0].Platforms, repository.PlatformString(goos, goarch))
}
}
}
if !hasTiUP {
// Treat TiUP as a component
tiup := v0manifest.ComponentInfo{
Name: "tiup",
Desc: tiupDesc,
Standalone: false,
Hide: true,
}
m.Components = append(m.Components, tiup)
}
// Initialize the components manifest
for _, comp := range m.Components {
fmt.Println("found component", comp.Name)
var versions *v0manifest.VersionManifest
var err error
versions, err = readVersions(srcDir, comp.Name)
if comp.Name == "tiup" && os.IsNotExist(err) {
versions = tiupVersions
} else if err != nil {
return errors.Trace(err)
}
platforms := map[string]map[string]v1manifest.VersionItem{}
for _, v := range versions.Versions {
for _, p := range v.Platforms {
newp := p
vs, found := platforms[newp]
if !found {
vs = map[string]v1manifest.VersionItem{}
platforms[newp] = vs
}
filename := fmt.Sprintf("/%s-%s-%s.tar.gz", comp.Name, v.Version, strings.Join(
strings.Split(newp, "/"),
"-"))
if comp.Name == "tiup" && utils.IsNotExist(filepath.Join(srcDir, filename)) {
// if not versioned tiup package exist, use the unversioned one
// for v0 manifest to generate a dummy component
filename = fmt.Sprintf("/%s-%s.tar.gz", comp.Name, strings.Join(
strings.Split(newp, "/"),
"-"))
}
fmt.Printf("rehashing %s...\n", filepath.Join(srcDir, filename))
hashes, length, err := ru.HashFile(filepath.Join(srcDir, filename))
if err != nil {
return errors.Trace(err)
}
// due to the nature of our CDN, all files are under the same URI base
vs[v.Version.String()] = v1manifest.VersionItem{
Yanked: false,
URL: filename,
Entry: v.Entry,
Released: v.Date,
FileHash: v1manifest.FileHash{
Hashes: hashes,
Length: uint(length),
},
}
}
}
// add nightly version, the versions for daily builds are all "nightly"
// without date in v0 manifest, we keep that during migrate, but should
// use new format with date number in version string when adding new
// in the future
var nightlyVer string
if versions.Nightly != nil {
v := versions.Nightly
nightlyVer = v.Version.String()
for _, p := range v.Platforms {
newp := p
vs, found := platforms[newp]
if !found {
vs = map[string]v1manifest.VersionItem{}
platforms[newp] = vs
}
filename := fmt.Sprintf("/%s-%s-%s.tar.gz", comp.Name, tiupver.NightlyVersion,
strings.Join(strings.Split(newp, "/"), "-"))
hashes, length, err := ru.HashFile(path.Join(srcDir, filename))
if err != nil {
return errors.Trace(err)
}
// due to the nature of our CDN, all files are under the same URI base
vs[v.Version.String()] = v1manifest.VersionItem{
Yanked: false,
URL: filename,
Entry: v.Entry,
Released: v.Date,
FileHash: v1manifest.FileHash{
Hashes: hashes,
Length: uint(length),
},
}
}
}
component := &v1manifest.Component{
SignedBase: v1manifest.SignedBase{
Ty: v1manifest.ManifestTypeComponent,
SpecVersion: v1manifest.CurrentSpecVersion,
Expires: initTime.Add(v1manifest.ManifestsConfig[v1manifest.ManifestTypeComponent].Expire).Format(time.RFC3339),
Version: 1, // initial repo starts with version 1
},
ID: comp.Name,
Description: comp.Desc,
Nightly: nightlyVer,
Platforms: platforms,
}
name := fmt.Sprintf("%s.json", component.ID)
// update manifest version for component
if rehash {
component.Version = snapshot.Meta["/"+name].Version + 1
}
signedManifests[component.ID], err = v1manifest.SignManifest(component, ownerkeyInfo)
if err != nil {
return errors.Trace(err)
}
index.Components[comp.Name] = v1manifest.ComponentItem{
Yanked: false,
Owner: "pingcap",
URL: fmt.Sprintf("/%s", name),
Standalone: comp.Standalone,
Hidden: comp.Hide,
}
}
// sign index and snapshot
signedManifests[v1manifest.ManifestTypeIndex], err = v1manifest.SignManifest(index, keys[v1manifest.ManifestTypeIndex]...)
if err != nil {
return errors.Trace(err)
}
// snapshot and timestamp are the last two manifests to be initialized
// Initialize timestamp
timestamp := v1manifest.NewTimestamp(initTime)
if rehash {
t := &v1manifest.Manifest{
Signed: &v1manifest.Timestamp{},
}
if err := readManifest1(dstDir, filepath.Join("manifests", timestamp.Filename()), t); err != nil {
return errors.Trace(err)
}
timestamp.Version = t.Signed.(*v1manifest.Timestamp).Version
}
manifests[v1manifest.ManifestTypeTimestamp] = timestamp
manifests[v1manifest.ManifestTypeSnapshot] = snapshot
if !rehash {
// Initialize the root manifest
for _, m := range manifests {
if err := root.SetRole(m, keys[m.Base().Ty]...); err != nil {
return errors.Trace(err)
}
}
if signedRoot {
signedManifests[v1manifest.ManifestTypeRoot], err = v1manifest.SignManifest(root, keys[v1manifest.ManifestTypeRoot]...)
if err != nil {
return errors.Trace(err)
}
} else {
// root manifest is not signed, and need to be manually signed by root key owners
signedManifests[v1manifest.ManifestTypeRoot] = &v1manifest.Manifest{
Signed: root,
}
}
}
// populate snapshot
snapshot, err = snapshot.SetVersions(signedManifests)
if err != nil {
return errors.Trace(err)
}
signedManifests[v1manifest.ManifestTypeSnapshot], err = v1manifest.SignManifest(snapshot, keys[v1manifest.ManifestTypeSnapshot]...)
if err != nil {
return errors.Trace(err)
}
// populate timestamp
timestamp, err = timestamp.SetSnapshot(signedManifests[v1manifest.ManifestTypeSnapshot])
if err != nil {
return errors.Trace(err)
}
if rehash {
timestamp.Version++
}
signedManifests[v1manifest.ManifestTypeTimestamp], err = v1manifest.SignManifest(timestamp, keys[v1manifest.ManifestTypeTimestamp]...)
if err != nil {
return errors.Trace(err)
}
for _, m := range signedManifests {
fname := filepath.Join(dstDir, "manifests", m.Signed.Filename())
switch m.Signed.Base().Ty {
case v1manifest.ManifestTypeRoot:
err := v1manifest.WriteManifestFile(repository.FnameWithVersion(fname, m.Signed.Base().Version), m)
if err != nil {
return errors.Trace(err)
}
// A copy of the newest version which is 1.
err = v1manifest.WriteManifestFile(fname, m)
if err != nil {
return errors.Trace(err)
}
case v1manifest.ManifestTypeComponent, v1manifest.ManifestTypeIndex:
err := v1manifest.WriteManifestFile(repository.FnameWithVersion(fname, m.Signed.Base().Version), m)
if err != nil {
return errors.Trace(err)
}
default:
err = v1manifest.WriteManifestFile(fname, m)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}
func update(dir string, keyFiles []string, isPublicKey bool) error {
manifests := make(map[string]*v1manifest.Manifest)
keys := make(map[string][]*v1manifest.KeyInfo)
currTime := time.Now().UTC()
var err error
// read root.json
root := v1manifest.Manifest{
Signed: &v1manifest.Root{},
}
if err = readManifest1(filepath.Join(dir, "manifests"), v1manifest.ManifestFilenameRoot, &root); err != nil {
return errors.Trace(err)
}
manifests[v1manifest.ManifestTypeRoot] = &root
// if inputs are public keys, just add them to the root's key list and return
if isPublicKey {
for _, fname := range keyFiles {
var kp v1manifest.KeyInfo
f, err := os.Open(fname)
if err != nil {
return errors.Trace(err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return errors.Trace(err)
}
if err := json.Unmarshal(bytes, &kp); err != nil {
return errors.Trace(err)
}
if err := root.Signed.(*v1manifest.Root).AddKey(v1manifest.ManifestTypeRoot, &kp); err != nil {
return errors.Trace(err)
}
}
// write the file
fname := filepath.Join(dir, "manifests", root.Signed.Filename())
err := v1manifest.WriteManifestFile(repository.FnameWithVersion(fname, root.Signed.Base().Version), &root)
if err != nil {
return errors.Trace(err)
}
// A copy of the newest version which is 1.
err = v1manifest.WriteManifestFile(fname, &root)
if err != nil {
return errors.Trace(err)
}
return nil
}
// append signatures
for _, fname := range keyFiles { // signed root files
sr := v1manifest.Manifest{
Signed: &v1manifest.Root{},
}
if err = readManifest1("", fname, &sr); err != nil {
return errors.Trace(err)
}
root.AddSignature(sr.Signatures)
}
// read snapshot.json
snapshotOld := v1manifest.Manifest{
Signed: &v1manifest.Snapshot{},
}
if err := readManifest1(filepath.Join(dir, "manifests"), v1manifest.ManifestFilenameSnapshot, &snapshotOld); err != nil {
return errors.Trace(err)
}
for _, sig := range snapshotOld.Signatures {
id := sig.KeyID[:v1manifest.ShortKeyIDLength]
f, err := os.Open(filepath.Join(dir, "keys", fmt.Sprintf(
"%s-%s", id, v1manifest.ManifestFilenameSnapshot,
)))
if err != nil {
return errors.Trace(err)
}
defer f.Close()
ki := v1manifest.KeyInfo{}
if err := json.NewDecoder(f).Decode(&ki); err != nil {
return errors.Trace(err)
}
if keys[v1manifest.ManifestTypeSnapshot] == nil {
keys[v1manifest.ManifestTypeSnapshot] = make([]*v1manifest.KeyInfo, 0)
}
keys[v1manifest.ManifestTypeSnapshot] = append(keys[v1manifest.ManifestTypeSnapshot], &ki)
}
// update snapshot
snapshot, err := snapshotOld.Signed.(*v1manifest.Snapshot).SetVersions(manifests)
if err != nil {
return errors.Trace(err)
}
manifests[v1manifest.ManifestTypeSnapshot], err = v1manifest.SignManifest(snapshot, keys[v1manifest.ManifestTypeSnapshot]...)
if err != nil {
return errors.Trace(err)
}
// read timestamp.json
tsOld := v1manifest.Manifest{
Signed: &v1manifest.Timestamp{},
}
if err := readManifest1(filepath.Join(dir, "manifests"), v1manifest.ManifestFilenameTimestamp, &tsOld); err != nil {
return errors.Trace(err)
}
manifests[v1manifest.ManifestTypeTimestamp] = &tsOld
for _, sig := range manifests[v1manifest.ManifestTypeTimestamp].Signatures {
id := sig.KeyID[:v1manifest.ShortKeyIDLength]
f, err := os.Open(filepath.Join(dir, "keys", fmt.Sprintf(
"%s-%s", id, v1manifest.ManifestFilenameTimestamp,
)))
if err != nil {
return errors.Trace(err)
}
defer f.Close()
ki := v1manifest.KeyInfo{}
if err := json.NewDecoder(f).Decode(&ki); err != nil {
return errors.Trace(err)
}
if keys[v1manifest.ManifestTypeTimestamp] == nil {
keys[v1manifest.ManifestTypeTimestamp] = make([]*v1manifest.KeyInfo, 0)
}
keys[v1manifest.ManifestTypeTimestamp] = append(keys[v1manifest.ManifestTypeTimestamp], &ki)
}
// update timestamp
timestamp, err := v1manifest.NewTimestamp(currTime).SetSnapshot(manifests[v1manifest.ManifestTypeSnapshot])
if err != nil {
return errors.Trace(err)
}
timestamp.Base().Version = manifests[v1manifest.ManifestTypeTimestamp].Signed.Base().Version + 1
manifests[v1manifest.ManifestTypeTimestamp], err = v1manifest.SignManifest(timestamp, keys[v1manifest.ManifestTypeTimestamp]...)
if err != nil {
return errors.Trace(err)
}
for _, m := range manifests {
fname := filepath.Join(dir, "manifests", m.Signed.Filename())
switch m.Signed.Base().Ty {
case v1manifest.ManifestTypeRoot:
err := v1manifest.WriteManifestFile(repository.FnameWithVersion(fname, m.Signed.Base().Version), m)
if err != nil {
return errors.Trace(err)
}
// A copy of the newest version which is 1.
err = v1manifest.WriteManifestFile(fname, m)
if err != nil {
return errors.Trace(err)
}
default:
err = v1manifest.WriteManifestFile(fname, m)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}