Skip to content

Commit 4a33a14

Browse files
[Delegations prereq 3] Add roles helpers (#193)
* [Delegations prereq] Add roles helpers Splitting up #175 * Remove unused validMetadata
1 parent 5573c9c commit 4a33a14

File tree

5 files changed

+101
-35
lines changed

5 files changed

+101
-35
lines changed

internal/roles/roles.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package roles
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
var TopLevelRoles = map[string]struct{}{
9+
"root": {},
10+
"targets": {},
11+
"snapshot": {},
12+
"timestamp": {},
13+
}
14+
15+
func IsTopLevelRole(name string) bool {
16+
_, ok := TopLevelRoles[name]
17+
return ok
18+
}
19+
20+
func IsDelegatedTargetsRole(name string) bool {
21+
return !IsTopLevelRole(name)
22+
}
23+
24+
func IsTopLevelManifest(name string) bool {
25+
return IsTopLevelRole(strings.TrimSuffix(name, ".json"))
26+
}
27+
28+
func IsDelegatedTargetsManifest(name string) bool {
29+
return !IsTopLevelManifest(name)
30+
}
31+
32+
func IsVersionedManifest(name string) bool {
33+
parts := strings.Split(name, ".")
34+
// Versioned manifests have the form "x.role.json"
35+
if len(parts) < 3 {
36+
return false
37+
}
38+
39+
_, err := strconv.Atoi(parts[0])
40+
return err == nil
41+
}

internal/roles/roles_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package roles
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsTopLevelRole(t *testing.T) {
10+
assert.True(t, IsTopLevelRole("root"))
11+
assert.True(t, IsTopLevelRole("targets"))
12+
assert.True(t, IsTopLevelRole("timestamp"))
13+
assert.True(t, IsTopLevelRole("snapshot"))
14+
assert.False(t, IsTopLevelRole("bins"))
15+
}
16+
17+
func TestIsDelegatedTargetsRole(t *testing.T) {
18+
assert.False(t, IsDelegatedTargetsRole("root"))
19+
assert.False(t, IsDelegatedTargetsRole("targets"))
20+
assert.False(t, IsDelegatedTargetsRole("timestamp"))
21+
assert.False(t, IsDelegatedTargetsRole("snapshot"))
22+
assert.True(t, IsDelegatedTargetsRole("deleg"))
23+
}
24+
25+
func TestIsTopLevelManifest(t *testing.T) {
26+
assert.True(t, IsTopLevelManifest("root.json"))
27+
assert.True(t, IsTopLevelManifest("targets.json"))
28+
assert.True(t, IsTopLevelManifest("timestamp.json"))
29+
assert.True(t, IsTopLevelManifest("snapshot.json"))
30+
assert.False(t, IsTopLevelManifest("bins.json"))
31+
}
32+
33+
func TestIsDelegatedTargetsManifest(t *testing.T) {
34+
assert.False(t, IsDelegatedTargetsManifest("root.json"))
35+
assert.False(t, IsDelegatedTargetsManifest("targets.json"))
36+
assert.False(t, IsDelegatedTargetsManifest("timestamp.json"))
37+
assert.False(t, IsDelegatedTargetsManifest("snapshot.json"))
38+
assert.True(t, IsDelegatedTargetsManifest("bins.json"))
39+
}
40+
41+
func TestIsVersionedManifest(t *testing.T) {
42+
assert.False(t, IsVersionedManifest("a.b"))
43+
assert.False(t, IsVersionedManifest("a.b.c"))
44+
assert.False(t, IsVersionedManifest("a.b.json"))
45+
assert.False(t, IsVersionedManifest("1.a"))
46+
assert.True(t, IsVersionedManifest("1.a.json"))
47+
assert.True(t, IsVersionedManifest("2.a.json"))
48+
}

repo.go

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

1313
"github.com/secure-systems-lab/go-securesystemslib/cjson"
1414
"github.com/theupdateframework/go-tuf/data"
15+
"github.com/theupdateframework/go-tuf/internal/roles"
1516
"github.com/theupdateframework/go-tuf/internal/signer"
1617
"github.com/theupdateframework/go-tuf/pkg/keys"
1718
"github.com/theupdateframework/go-tuf/sign"
@@ -199,7 +200,7 @@ func (r *Repo) GetThreshold(keyRole string) (int, error) {
199200
}
200201

201202
func (r *Repo) SetThreshold(keyRole string, t int) error {
202-
if !validMetadata(keyRole + ".json") {
203+
if !roles.IsTopLevelRole(keyRole) {
203204
// Delegations are not currently supported, so return an error if this is not a
204205
// top-level metadata file.
205206
return ErrInvalidRole{keyRole}
@@ -319,7 +320,7 @@ func (r *Repo) timestamp() (*data.Timestamp, error) {
319320
}
320321

321322
func (r *Repo) ChangePassphrase(keyRole string) error {
322-
if !verify.ValidRole(keyRole) {
323+
if !roles.IsTopLevelRole(keyRole) {
323324
return ErrInvalidRole{keyRole}
324325
}
325326

@@ -352,7 +353,7 @@ func (r *Repo) AddPrivateKey(role string, signer keys.Signer) error {
352353
}
353354

354355
func (r *Repo) AddPrivateKeyWithExpires(keyRole string, signer keys.Signer, expires time.Time) error {
355-
if !verify.ValidRole(keyRole) {
356+
if !roles.IsTopLevelRole(keyRole) {
356357
return ErrInvalidRole{keyRole}
357358
}
358359

@@ -451,7 +452,7 @@ func (r *Repo) RevokeKey(role, id string) error {
451452
}
452453

453454
func (r *Repo) RevokeKeyWithExpires(keyRole, id string, expires time.Time) error {
454-
if !verify.ValidRole(keyRole) {
455+
if !roles.IsTopLevelRole(keyRole) {
455456
return ErrInvalidRole{keyRole}
456457
}
457458

@@ -555,7 +556,7 @@ func (r *Repo) setMeta(roleFilename string, meta interface{}) error {
555556

556557
func (r *Repo) Sign(roleFilename string) error {
557558
role := strings.TrimSuffix(roleFilename, ".json")
558-
if !verify.ValidRole(role) {
559+
if !roles.IsTopLevelRole(role) {
559560
return ErrInvalidRole{role}
560561
}
561562

@@ -591,7 +592,7 @@ func (r *Repo) Sign(roleFilename string) error {
591592
// The name must be a valid metadata file name, like root.json.
592593
func (r *Repo) AddOrUpdateSignature(roleFilename string, signature data.Signature) error {
593594
role := strings.TrimSuffix(roleFilename, ".json")
594-
if !verify.ValidRole(role) {
595+
if !roles.IsTopLevelRole(role) {
595596
return ErrInvalidRole{role}
596597
}
597598

@@ -695,15 +696,6 @@ func (r *Repo) SignedMeta(roleFilename string) (*data.Signed, error) {
695696
return s, nil
696697
}
697698

698-
func validMetadata(roleFilename string) bool {
699-
for _, m := range topLevelMetadata {
700-
if m == roleFilename {
701-
return true
702-
}
703-
}
704-
return false
705-
}
706-
707699
func (r *Repo) AddTarget(path string, custom json.RawMessage) error {
708700
return r.AddTargets([]string{path}, custom)
709701
}

verify/db.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package verify
22

33
import (
44
"github.com/theupdateframework/go-tuf/data"
5+
"github.com/theupdateframework/go-tuf/internal/roles"
56
"github.com/theupdateframework/go-tuf/pkg/keys"
67
)
78

@@ -44,7 +45,7 @@ func NewDelegationsVerifier(d *data.Delegations) (DelegationsVerifier, error) {
4445
verifiers: make(map[string]keys.Verifier, len(d.Keys)),
4546
}
4647
for _, r := range d.Roles {
47-
if _, ok := topLevelRoles[r.Name]; ok {
48+
if _, ok := roles.TopLevelRoles[r.Name]; ok {
4849
return DelegationsVerifier{}, ErrInvalidDelegatedRole
4950
}
5051
role := &data.Role{Threshold: r.Threshold, KeyIDs: r.KeyIDs}
@@ -72,25 +73,8 @@ func (db *DB) AddKey(id string, k *data.PublicKey) error {
7273
return nil
7374
}
7475

75-
var topLevelRoles = map[string]struct{}{
76-
"root": {},
77-
"targets": {},
78-
"snapshot": {},
79-
"timestamp": {},
80-
}
81-
82-
// ValidRole checks if a role is a top level role.
83-
func ValidRole(name string) bool {
84-
return isTopLevelRole(name)
85-
}
86-
87-
func isTopLevelRole(name string) bool {
88-
_, ok := topLevelRoles[name]
89-
return ok
90-
}
91-
9276
func (db *DB) AddRole(name string, r *data.Role) error {
93-
if !isTopLevelRole(name) {
77+
if !roles.IsTopLevelRole(name) {
9478
return ErrInvalidRole
9579
}
9680
return db.addRole(name, r)

verify/verify.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/secure-systems-lab/go-securesystemslib/cjson"
99
"github.com/theupdateframework/go-tuf/data"
10+
"github.com/theupdateframework/go-tuf/internal/roles"
1011
)
1112

1213
type signedMeta struct {
@@ -25,7 +26,7 @@ func (db *DB) VerifyIgnoreExpiredCheck(s *data.Signed, role string, minVersion i
2526
return err
2627
}
2728

28-
if isTopLevelRole(role) {
29+
if roles.IsTopLevelRole(role) {
2930
// Top-level roles can only sign metadata of the same type (e.g. snapshot
3031
// metadata must be signed by the snapshot role).
3132
if !strings.EqualFold(sm.Type, role) {

0 commit comments

Comments
 (0)