@@ -39,7 +39,7 @@ func getPassphraseRetriever() notary.PassRetriever {
39
39
40
40
// Attempt to read a role key from a file, and return it as a data.PrivateKey
41
41
// If key is for the Root role, it must be encrypted
42
- func readKey (role data.RoleName , keyFilename string , retriever notary.PassRetriever ) (data.PrivateKey , error ) {
42
+ func readPrivateKey (role data.RoleName , keyFilename string , retriever notary.PassRetriever ) (data.PrivateKey , error ) {
43
43
pemBytes , err := ioutil .ReadFile (keyFilename )
44
44
if err != nil {
45
45
return nil , fmt .Errorf ("Error reading input root key file: %v" , err )
@@ -64,14 +64,40 @@ func readKey(role data.RoleName, keyFilename string, retriever notary.PassRetrie
64
64
return privKey , nil
65
65
}
66
66
67
+ // Attempt to read a role key from a file, and return it as a data.PrivateKey
68
+ func readPublicKey (args []string ) ([]data.PublicKey , error ) {
69
+ pubKeys := []data.PublicKey {}
70
+ if len (args ) > 2 {
71
+ pubKeyPaths := args [2 :]
72
+ for _ , pubKeyPath := range pubKeyPaths {
73
+ // Read public key bytes from PEM file
74
+ pubKeyBytes , err := ioutil .ReadFile (pubKeyPath )
75
+ if err != nil {
76
+ if os .IsNotExist (err ) {
77
+ return nil , fmt .Errorf ("file for public key does not exist: %s" , pubKeyPath )
78
+ }
79
+ return nil , fmt .Errorf ("unable to read public key from file: %s" , pubKeyPath )
80
+ }
81
+
82
+ // Parse PEM bytes into type PublicKey
83
+ pubKey , err := utils .ParsePEMPublicKey (pubKeyBytes )
84
+ if err != nil {
85
+ return nil , fmt .Errorf ("unable to parse valid public key certificate from PEM file %s: %v" , pubKeyPath , err )
86
+ }
87
+ pubKeys = append (pubKeys , pubKey )
88
+ }
89
+ }
90
+ return pubKeys , nil
91
+ }
92
+
67
93
// importRootKey imports the root key from path then adds the key to repo
68
94
// returns key ids
69
95
// https://github.com/theupdateframework/notary/blob/f255ae779066dc28ae4aee196061e58bb38a2b49/cmd/notary/tuf.go#L413
70
96
func importRootKey (rootKey string , nRepo client.Repository , retriever notary.PassRetriever ) ([]string , error ) {
71
97
var rootKeyList []string
72
98
73
99
if rootKey != "" {
74
- privKey , err := readKey (data .CanonicalRootRole , rootKey , retriever )
100
+ privKey , err := readPrivateKey (data .CanonicalRootRole , rootKey , retriever )
75
101
if err != nil {
76
102
return nil , err
77
103
}
@@ -97,25 +123,25 @@ func importRootKey(rootKey string, nRepo client.Repository, retriever notary.Pas
97
123
return []string {}, nil
98
124
}
99
125
100
- // Try to reuse a single targets key across repositories.
126
+ // Try to reuse a single key for the given rolename across repositories.
101
127
// FIXME: Unfortunately, short of forking Notary or sending a PR upstream, there isn't an easy way to prevent it
102
128
// from automagically creating a new, local targets key per TUF metadata repository. We fix this here by undoing
103
129
// more than one new, local targets key, and reusing any existing local targets key, just like the way Notary
104
130
// reuses the root key.
105
- func reuseTargetsKey (r client.Repository ) error {
131
+ func reuseKey (r client.Repository , rolename data. RoleName ) ( string , error ) {
106
132
var (
107
- err error
108
- thisTargetsKeyID , thatTargetsKeyID string
133
+ err error
134
+ thisKeyID , thatKeyID string
109
135
)
110
136
111
- // Get all known targets keys.
112
- targetsKeyList := r .GetCryptoService ().ListKeys (data . CanonicalTargetsRole )
113
- // Try to extract a single targets key we can reuse.
114
- switch len (targetsKeyList ) {
137
+ // Get all known keys for this rolename .
138
+ keyList := r .GetCryptoService ().ListKeys (rolename )
139
+ // Try to extract a single key we can reuse.
140
+ switch len (keyList ) {
115
141
case 0 :
116
- err = fmt .Errorf ("no targets key despite having initialized a repo" )
142
+ err = fmt .Errorf ("no %s key despite having initialized a repo" , rolename )
117
143
case 1 :
118
- log .Debug ("Nothing to do, only one targets key available" )
144
+ log .Debug ("Nothing to do, only one %s key available" , rolename )
119
145
case 2 :
120
146
// First, we publish current changes to repository in order to list roles.
121
147
// FIXME: Find a find better way to list roles w/o publishing changes first.
@@ -132,34 +158,34 @@ func reuseTargetsKey(r client.Repository) error {
132
158
break
133
159
}
134
160
135
- // Get the current targets key.
161
+ // Get the current key for the given rolename .
136
162
// NOTE: We do not delete it, in case the user wants to keep it.
137
163
for _ , roleWithSig := range roleWithSigs {
138
164
role := roleWithSig .Role
139
- if role .Name == data . CanonicalTargetsRole {
165
+ if role .Name == rolename {
140
166
if len (role .KeyIDs ) == 1 {
141
- thisTargetsKeyID = role .KeyIDs [0 ]
142
- log .Debugf ("This targets keyid: %s" , thisTargetsKeyID )
167
+ thisKeyID = role .KeyIDs [0 ]
168
+ log .Debugf ("This %s keyid: %s" , rolename , thisKeyID )
143
169
} else {
144
- return fmt .Errorf ("this targets role has more than 1 key" )
170
+ return thatKeyID , fmt .Errorf ("this %s role has more than 1 key" , rolename )
145
171
}
146
172
}
147
173
}
148
174
149
- // Get and reuse the other targets key.
150
- for _ , keyID := range targetsKeyList {
151
- if keyID != thisTargetsKeyID {
152
- thatTargetsKeyID = keyID
175
+ // Get and reuse the other key for the given rolename .
176
+ for _ , keyID := range keyList {
177
+ if keyID != thisKeyID {
178
+ thatKeyID = keyID
153
179
break
154
180
}
155
181
}
156
- log .Debugf ("That targets keyID: %s" , thatTargetsKeyID )
157
- log .Debugf ("Before rotating targets key from %s to %s" , thisTargetsKeyID , thatTargetsKeyID )
158
- err = r .RotateKey (data . CanonicalTargetsRole , false , []string {thatTargetsKeyID })
159
- log .Debugf ("After targets key rotation" )
182
+ log .Debugf ("That %s keyID: %s" , rolename , thatKeyID )
183
+ log .Debugf ("Before rotating %s key from %s to %s" , rolename , thisKeyID , thatKeyID )
184
+ err = r .RotateKey (rolename , false , []string {thatKeyID })
185
+ log .Debugf ("After %s key rotation" , rolename )
160
186
default :
161
- err = fmt .Errorf ("there are more than 2 targets keys" )
187
+ err = fmt .Errorf ("there are more than 2 %s keys" , rolename )
162
188
}
163
189
164
- return err
190
+ return thatKeyID , err
165
191
}
0 commit comments