/
repository.go
99 lines (80 loc) · 2.18 KB
/
repository.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
package notary
import (
"os"
"path"
"github.com/pkg/errors"
"github.com/theupdateframework/notary/client"
"github.com/theupdateframework/notary/client/changelist"
"github.com/theupdateframework/notary/storage"
"github.com/theupdateframework/notary/trustpinning"
"github.com/theupdateframework/notary/tuf/data"
"github.com/theupdateframework/notary/tuf/signed"
)
type RemoteRepositoryFactory struct {
}
func (r *RemoteRepositoryFactory) GetRepository(url string, gun data.GUN, remoteStore storage.RemoteStore, cryptoService signed.CryptoService) (Repository, error) {
changeListDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
}
changeList, err := changelist.NewFileChangelist(changeListDir)
if err != nil {
return nil, err
}
repo, err := client.NewRepository(
gun,
url,
remoteStore,
storage.NewMemoryStore(nil),
trustpinning.TrustPinConfig{},
cryptoService,
changeList,
)
if err != nil {
return nil, err
}
return &RemoteRepository{repo: repo}, nil
}
type RemoteRepository struct {
repo client.Repository
}
func (r *RemoteRepository) PublishTarget(target *client.Target) error {
roles, err := r.getRoles(target)
if err != nil {
return err
}
err = r.repo.AddTarget(target, roles...)
if err != nil {
return err
}
return r.repo.Publish()
}
func (r *RemoteRepository) getRoles(target *client.Target) ([]data.RoleName, error) {
delegationRoles, err := r.repo.GetDelegationRoles()
if err != nil {
return nil, err
}
if len(delegationRoles) == 0 {
return []data.RoleName{data.CanonicalTargetsRole}, nil
}
keys := r.repo.GetCryptoService().ListAllKeys()
if len(keys) != 1 {
return nil, errors.Errorf("expected exactly one signing key but got %d", len(keys))
}
var roles []data.RoleName
for _, delegationRole := range delegationRoles {
if path.Dir(delegationRole.Name.String()) != data.CanonicalTargetsRole.String() || !delegationRole.CheckPaths(target.Name) {
continue
}
for _, keyID := range delegationRole.KeyIDs {
if _, ok := keys[keyID]; ok {
roles = append(roles, delegationRole.Name)
break
}
}
}
if len(roles) == 0 {
return []data.RoleName{}, errors.New("no delegation roles found")
}
return roles, nil
}