Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 3a89320

Browse files
committed
Merge pull request #588 from endophage/fix_snapshot_expiry
Fix server signed snapshot expiry/regeneration
2 parents 4904c88 + cb2dd07 commit 3a89320

6 files changed

Lines changed: 54 additions & 26 deletions

File tree

client/client.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ import (
2727
)
2828

2929
func init() {
30-
data.SetDefaultExpiryTimes(
31-
map[string]int{
32-
"root": 3650,
33-
"targets": 1095,
34-
"snapshot": 1095,
35-
},
36-
)
30+
data.SetDefaultExpiryTimes(notary.NotaryDefaultExpiries)
3731
}
3832

3933
// ErrRepoNotInitialized is returned when trying to publish an uninitialized

const.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package notary
22

3+
import (
4+
"time"
5+
)
6+
37
// application wide constants
48
const (
59
// MaxDownloadSize is the maximum size we'll download for metadata if no limit is given
@@ -24,4 +28,23 @@ const (
2428
RootKeysSubdir = "root_keys"
2529
// NonRootKeysSubdir is the subdirectory under PrivDir where non-root private keys are stored
2630
NonRootKeysSubdir = "tuf_keys"
31+
32+
// Day is a duration of one day
33+
Day = 24 * time.Hour
34+
Year = 365 * Day
35+
36+
// NotaryRootExpiry is the duration representing the expiry time of the Root role
37+
NotaryRootExpiry = 10 * Year
38+
NotaryTargetsExpiry = 3 * Year
39+
NotarySnapshotExpiry = 3 * Year
40+
NotaryTimestampExpiry = 14 * Day
2741
)
42+
43+
// NotaryDefaultExpiries is the construct used to configure the default expiry times of
44+
// the various role files.
45+
var NotaryDefaultExpiries = map[string]time.Duration{
46+
"root": NotaryRootExpiry,
47+
"targets": NotaryTargetsExpiry,
48+
"snapshot": NotarySnapshotExpiry,
49+
"timestamp": NotaryTimestampExpiry,
50+
}

server/server.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/Sirupsen/logrus"
1010
"github.com/docker/distribution/health"
1111
"github.com/docker/distribution/registry/auth"
12+
"github.com/docker/notary"
1213
"github.com/docker/notary/server/handlers"
1314
"github.com/docker/notary/tuf/data"
1415
"github.com/docker/notary/tuf/signed"
@@ -19,11 +20,7 @@ import (
1920
)
2021

2122
func init() {
22-
data.SetDefaultExpiryTimes(
23-
map[string]int{
24-
"timestamp": 14,
25-
},
26-
)
23+
data.SetDefaultExpiryTimes(notary.NotaryDefaultExpiries)
2724
}
2825

2926
func prometheusOpts(operation string) prometheus.SummaryOpts {

server/timestamp/timestamp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/notary/tuf/signed"
99

1010
"github.com/Sirupsen/logrus"
11+
"github.com/docker/notary/server/snapshot"
1112
"github.com/docker/notary/server/storage"
1213
)
1314

@@ -49,7 +50,7 @@ func GetOrCreateTimestampKey(gun string, store storage.MetaStore, crypto signed.
4950
// a new timestamp is generated either because none exists, or because the current
5051
// one has expired. Once generated, the timestamp is saved in the store.
5152
func GetOrCreateTimestamp(gun string, store storage.MetaStore, cryptoService signed.CryptoService) ([]byte, error) {
52-
snapshot, err := store.GetCurrent(gun, "snapshot")
53+
snapshot, err := snapshot.GetOrCreateSnapshot(gun, store, cryptoService)
5354
if err != nil {
5455
return nil, err
5556
}

server/timestamp/timestamp_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func TestGetTimestamp(t *testing.T) {
5252
store := storage.NewMemStorage()
5353
crypto := signed.NewEd25519()
5454

55-
snapshot := &data.SignedSnapshot{}
55+
snapshot := &data.SignedSnapshot{
56+
Signed: data.Snapshot{
57+
Expires: data.DefaultExpires(data.CanonicalSnapshotRole),
58+
},
59+
}
5660
snapJSON, _ := json.Marshal(snapshot)
5761

5862
store.UpdateCurrent("gun", storage.MetaUpdate{Role: "snapshot", Version: 0, Data: snapJSON})
@@ -68,7 +72,11 @@ func TestGetTimestampNewSnapshot(t *testing.T) {
6872
store := storage.NewMemStorage()
6973
crypto := signed.NewEd25519()
7074

71-
snapshot := data.SignedSnapshot{}
75+
snapshot := &data.SignedSnapshot{
76+
Signed: data.Snapshot{
77+
Expires: data.DefaultExpires(data.CanonicalSnapshotRole),
78+
},
79+
}
7280
snapshot.Signed.Version = 0
7381
snapJSON, _ := json.Marshal(snapshot)
7482

@@ -80,7 +88,11 @@ func TestGetTimestampNewSnapshot(t *testing.T) {
8088
ts1, err := GetOrCreateTimestamp("gun", store, crypto)
8189
assert.Nil(t, err, "GetTimestamp errored")
8290

83-
snapshot = data.SignedSnapshot{}
91+
snapshot = &data.SignedSnapshot{
92+
Signed: data.Snapshot{
93+
Expires: data.DefaultExpires(data.CanonicalSnapshotRole),
94+
},
95+
}
8496
snapshot.Signed.Version = 1
8597
snapJSON, _ = json.Marshal(snapshot)
8698

tuf/data/types.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/Sirupsen/logrus"
1414
"github.com/docker/go/canonical/json"
15+
"github.com/docker/notary"
1516
)
1617

1718
// SigAlgorithm for types of signatures
@@ -171,16 +172,16 @@ func NewDelegations() *Delegations {
171172
}
172173
}
173174

174-
// defines number of days in which something should expire
175-
var defaultExpiryTimes = map[string]int{
176-
CanonicalRootRole: 365,
177-
CanonicalTargetsRole: 90,
178-
CanonicalSnapshotRole: 7,
179-
CanonicalTimestampRole: 1,
175+
// These values are recommended TUF expiry times.
176+
var defaultExpiryTimes = map[string]time.Duration{
177+
CanonicalRootRole: notary.Year,
178+
CanonicalTargetsRole: 90 * notary.Day,
179+
CanonicalSnapshotRole: 7 * notary.Day,
180+
CanonicalTimestampRole: notary.Day,
180181
}
181182

182183
// SetDefaultExpiryTimes allows one to change the default expiries.
183-
func SetDefaultExpiryTimes(times map[string]int) {
184+
func SetDefaultExpiryTimes(times map[string]time.Duration) {
184185
for key, value := range times {
185186
if _, ok := defaultExpiryTimes[key]; !ok {
186187
logrus.Errorf("Attempted to set default expiry for an unknown role: %s", key)
@@ -192,10 +193,10 @@ func SetDefaultExpiryTimes(times map[string]int) {
192193

193194
// DefaultExpires gets the default expiry time for the given role
194195
func DefaultExpires(role string) time.Time {
195-
var t time.Time
196-
if t, ok := defaultExpiryTimes[role]; ok {
197-
return time.Now().AddDate(0, 0, t)
196+
if d, ok := defaultExpiryTimes[role]; ok {
197+
return time.Now().Add(d)
198198
}
199+
var t time.Time
199200
return t.UTC().Round(time.Second)
200201
}
201202

0 commit comments

Comments
 (0)