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

Commit 111b01d

Browse files
committed
added nearexpiry functionality and removed an unused testutil function
Signed-off-by: avaid96 <avaid1996@gmail.com>
1 parent acc4e1b commit 111b01d

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

client/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func (r *NotaryRepository) publish(cl changelist.Changelist) error {
612612
// check if our root file is nearing expiry or dirty. Resign if it is. If
613613
// root is not dirty but we are publishing for the first time, then just
614614
// publish the existing root we have.
615-
if nearExpiry(r.tufRepo.Root) || r.tufRepo.Root.Dirty {
615+
if nearExpiry(r.tufRepo.Root.Signed.SignedCommon) || r.tufRepo.Root.Dirty {
616616
rootJSON, err := serializeCanonicalRole(r.tufRepo, data.CanonicalRootRole)
617617
if err != nil {
618618
return err
@@ -781,7 +781,10 @@ func (r *NotaryRepository) Update(forWrite bool) error {
781781
}
782782
return err
783783
}
784+
// we can be assured if we are at this stage that the repo we built is good
785+
// no need to test the following function call for an error as it will always be fine should the repo be good- it is!
784786
r.tufRepo = repo
787+
warnRolesNearExpiry(repo)
785788
return nil
786789
}
787790

client/helpers.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,30 @@ func applyRootRoleChange(repo *tuf.Repo, c changelist.Change) error {
190190
return nil
191191
}
192192

193-
func nearExpiry(r *data.SignedRoot) bool {
193+
func nearExpiry(r data.SignedCommon) bool {
194194
plus6mo := time.Now().AddDate(0, 6, 0)
195-
return r.Signed.Expires.Before(plus6mo)
195+
return r.Expires.Before(plus6mo)
196+
}
197+
198+
func warnRolesNearExpiry(r *tuf.Repo) error {
199+
//get every role and its respective signed common and call nearExpiry on it
200+
//Root check
201+
if nearExpiry(r.Root.Signed.SignedCommon) {
202+
logrus.Warn("root is nearing expiry, you should re-sign the key")
203+
}
204+
//Targets and delegations check
205+
for role, signedTOrD := range r.Targets {
206+
//signedTOrD is of type *data.SignedTargets
207+
if nearExpiry(signedTOrD.Signed.SignedCommon) {
208+
logrus.Warn(role, " metadata is nearing expiry, you should re-sign the key")
209+
}
210+
}
211+
//Snapshot check
212+
if nearExpiry(r.Snapshot.Signed.SignedCommon) {
213+
logrus.Warn("snapshot is nearing expiry, you should re-sign the key")
214+
}
215+
return nil
216+
//Timestamp is not checked since the user doesn't need to worry about it, we deal with it
196217
}
197218

198219
// Fetches a public key from a remote store, given a gun and role

client/helpers_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package client
22

33
import (
4+
"bytes"
45
"crypto/sha256"
56
"encoding/json"
6-
"testing"
7-
7+
log "github.com/Sirupsen/logrus"
88
"github.com/docker/notary/client/changelist"
99
"github.com/docker/notary/tuf/data"
1010
"github.com/docker/notary/tuf/testutils"
1111
"github.com/stretchr/testify/require"
12+
"testing"
13+
"time"
1214
)
1315

1416
func TestApplyTargetsChange(t *testing.T) {
@@ -968,3 +970,45 @@ func TestChangeTargetMetaFailsIfPrefixError(t *testing.T) {
968970
require.Empty(t, repo.Targets[data.CanonicalTargetsRole].Signed.Targets)
969971
require.Empty(t, repo.Targets["targets/level1"].Signed.Targets)
970972
}
973+
974+
func TestAllNearExpiry(t *testing.T) {
975+
repo, _, err := testutils.EmptyRepo("docker.com/notary")
976+
require.NoError(t, err)
977+
nearexpdate := time.Now().AddDate(0, 1, 0)
978+
repo.Root.Signed.SignedCommon.Expires = nearexpdate
979+
repo.Snapshot.Signed.SignedCommon.Expires = nearexpdate
980+
repo.Targets["targets"].Signed.Expires = nearexpdate
981+
_, err1 := repo.InitTargets("targets/exp")
982+
require.NoError(t, err1)
983+
repo.Targets["targets/exp"].Signed.Expires = nearexpdate
984+
//Reset levels to display warnings through logrus
985+
log.SetLevel(log.WarnLevel)
986+
b := bytes.NewBuffer(nil)
987+
log.SetOutput(b)
988+
warnRolesNearExpiry(repo)
989+
require.Contains(t, b.String(), "targets metadata is nearing expiry, you should re-sign the key", "targets should show near expiry")
990+
require.Contains(t, b.String(), "targets/exp metadata is nearing expiry, you should re-sign the key", b.String(), "targets/exp should show near expiry")
991+
require.Contains(t, b.String(), "root is nearing expiry, you should re-sign the key", "Root should show near expiry")
992+
require.Contains(t, b.String(), "snapshot is nearing expiry, you should re-sign the key", "Snapshot should show near expiry")
993+
}
994+
995+
func TestAllNotNearExpiry(t *testing.T) {
996+
repo, _, err := testutils.EmptyRepo("docker.com/notary")
997+
require.NoError(t, err)
998+
notnearexpdate := time.Now().AddDate(0, 10, 0)
999+
repo.Root.Signed.SignedCommon.Expires = notnearexpdate
1000+
repo.Snapshot.Signed.SignedCommon.Expires = notnearexpdate
1001+
repo.Targets["targets"].Signed.Expires = notnearexpdate
1002+
_, err1 := repo.InitTargets("targets/noexp")
1003+
require.NoError(t, err1)
1004+
repo.Targets["targets/noexp"].Signed.Expires = notnearexpdate
1005+
//Reset levels to display warnings through logrus
1006+
log.SetLevel(log.WarnLevel)
1007+
a := bytes.NewBuffer(nil)
1008+
log.SetOutput(a)
1009+
warnRolesNearExpiry(repo)
1010+
require.NotContains(t, a.String(), "targets metadata is nearing expiry, you should re-sign the key", "targets should not show near expiry")
1011+
require.NotContains(t, a.String(), "targets/noexp metadata is nearing expiry, you should re-sign the key", "targets/noexp should not show near expiry")
1012+
require.NotContains(t, a.String(), "root is nearing expiry, you should re-sign the key", "Root should not show near expiry")
1013+
require.NotContains(t, a.String(), "snapshot is nearing expiry, you should re-sign the key", "Snapshot should not show near expiry")
1014+
}

tuf/testutils/repo.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/docker/notary/passphrase"
1313
"github.com/docker/notary/trustmanager"
1414
"github.com/docker/notary/tuf/data"
15-
"github.com/docker/notary/tuf/utils"
16-
fuzz "github.com/google/gofuzz"
1715
"github.com/stretchr/testify/require"
1816

1917
tuf "github.com/docker/notary/tuf"
@@ -142,23 +140,6 @@ func CopyRepoMetadata(from map[string][]byte) map[string][]byte {
142140
return copied
143141
}
144142

145-
// AddTarget generates a fake target and adds it to a repo.
146-
func AddTarget(role string, r *tuf.Repo) (name string, meta data.FileMeta, content []byte, err error) {
147-
randness := fuzz.Continue{}
148-
content = RandomByteSlice(1024)
149-
name = randness.RandString()
150-
t := data.FileMeta{
151-
Length: int64(len(content)),
152-
Hashes: data.Hashes{
153-
"sha256": utils.DoHash("sha256", content),
154-
"sha512": utils.DoHash("sha512", content),
155-
},
156-
}
157-
files := data.Files{name: t}
158-
_, err = r.AddTargets(role, files)
159-
return
160-
}
161-
162143
// RandomByteSlice generates some random data to be used for testing only
163144
func RandomByteSlice(maxSize int) []byte {
164145
r := rand.New(rand.NewSource(time.Now().UnixNano()))

0 commit comments

Comments
 (0)