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

Commit 2bee3f7

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

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,29 @@ 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) {
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 role metadata")
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 role metadata")
209+
}
210+
}
211+
//Snapshot check
212+
if nearExpiry(r.Snapshot.Signed.SignedCommon) {
213+
logrus.Warn("snapshot is nearing expiry, you should re-sign the role metadata")
214+
}
215+
//do not need to worry about Timestamp, notary signer will re-sign with the timestamp key
196216
}
197217

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

client/helpers_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package client
22

33
import (
4+
"bytes"
45
"crypto/sha256"
56
"encoding/json"
67
"testing"
8+
"time"
79

10+
log "github.com/Sirupsen/logrus"
811
"github.com/docker/notary/client/changelist"
912
"github.com/docker/notary/tuf/data"
1013
"github.com/docker/notary/tuf/testutils"
@@ -968,3 +971,51 @@ func TestChangeTargetMetaFailsIfPrefixError(t *testing.T) {
968971
require.Empty(t, repo.Targets[data.CanonicalTargetsRole].Signed.Targets)
969972
require.Empty(t, repo.Targets["targets/level1"].Signed.Targets)
970973
}
974+
975+
func TestAllNearExpiry(t *testing.T) {
976+
repo, _, err := testutils.EmptyRepo("docker.com/notary")
977+
require.NoError(t, err)
978+
nearexpdate := time.Now().AddDate(0, 1, 0)
979+
repo.Root.Signed.SignedCommon.Expires = nearexpdate
980+
repo.Snapshot.Signed.SignedCommon.Expires = nearexpdate
981+
repo.Targets["targets"].Signed.Expires = nearexpdate
982+
_, err1 := repo.InitTargets("targets/exp")
983+
require.NoError(t, err1)
984+
repo.Targets["targets/exp"].Signed.Expires = nearexpdate
985+
//Reset levels to display warnings through logrus
986+
orgLevel := log.GetLevel()
987+
log.SetLevel(log.WarnLevel)
988+
defer log.SetLevel(orgLevel)
989+
b := bytes.NewBuffer(nil)
990+
log.SetOutput(b)
991+
warnRolesNearExpiry(repo)
992+
require.Contains(t, b.String(), "targets metadata is nearing expiry, you should re-sign the role metadata", "targets should show near expiry")
993+
require.Contains(t, b.String(), "targets/exp metadata is nearing expiry, you should re-sign the role metadata", "targets/exp should show near expiry")
994+
require.Contains(t, b.String(), "root is nearing expiry, you should re-sign the role metadata", "Root should show near expiry")
995+
require.Contains(t, b.String(), "snapshot is nearing expiry, you should re-sign the role metadata", "Snapshot should show near expiry")
996+
require.NotContains(t, b.String(), "timestamp", "there should be no logrus warnings pertaining to timestamp")
997+
}
998+
999+
func TestAllNotNearExpiry(t *testing.T) {
1000+
repo, _, err := testutils.EmptyRepo("docker.com/notary")
1001+
require.NoError(t, err)
1002+
notnearexpdate := time.Now().AddDate(0, 10, 0)
1003+
repo.Root.Signed.SignedCommon.Expires = notnearexpdate
1004+
repo.Snapshot.Signed.SignedCommon.Expires = notnearexpdate
1005+
repo.Targets["targets"].Signed.Expires = notnearexpdate
1006+
_, err1 := repo.InitTargets("targets/noexp")
1007+
require.NoError(t, err1)
1008+
repo.Targets["targets/noexp"].Signed.Expires = notnearexpdate
1009+
//Reset levels to display warnings through logrus
1010+
orgLevel := log.GetLevel()
1011+
log.SetLevel(log.WarnLevel)
1012+
defer log.SetLevel(orgLevel)
1013+
a := bytes.NewBuffer(nil)
1014+
log.SetOutput(a)
1015+
warnRolesNearExpiry(repo)
1016+
require.NotContains(t, a.String(), "targets metadata is nearing expiry, you should re-sign the role metadata", "targets should not show near expiry")
1017+
require.NotContains(t, a.String(), "targets/noexp metadata is nearing expiry, you should re-sign the role metadata", "targets/noexp should not show near expiry")
1018+
require.NotContains(t, a.String(), "root is nearing expiry, you should re-sign the role metadata", "Root should not show near expiry")
1019+
require.NotContains(t, a.String(), "snapshot is nearing expiry, you should re-sign the role metadata", "Snapshot should not show near expiry")
1020+
require.NotContains(t, a.String(), "timestamp", "there should be no logrus warnings pertaining to timestamp")
1021+
}

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)