-
Notifications
You must be signed in to change notification settings - Fork 77
/
init.go
311 lines (282 loc) · 9.17 KB
/
init.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package app
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/peterbourgon/ff/v3/ffcli"
pkeys "github.com/sigstore/root-signing/pkg/keys"
prepo "github.com/sigstore/root-signing/pkg/repo"
cjson "github.com/tent/canonical-json-go"
"github.com/theupdateframework/go-tuf"
"github.com/theupdateframework/go-tuf/data"
)
// Threshold for root and targets signers.
var DefaultThreshold = 3
// Time to role expiration represented as a list of ints corresponding to
// (years, months, days).
var RoleExpiration = map[string][]int{
"root": {0, 6, 0},
"targets": {0, 6, 0},
"snapshot": {0, 0, 21},
"timestamp": {0, 0, 14},
}
func getExpiration(role string) time.Time {
// Default expiration for any delegated role is the targets expiration.
times, ok := RoleExpiration[role]
if !ok {
times = RoleExpiration["targets"]
fmt.Fprintf(os.Stderr, "Explicit expiration not found, using default targets expiration in %d years, %d months, %d days\n",
times[0], times[1], times[2])
}
return time.Now().AddDate(times[0], times[1], times[2]).UTC().Round(time.Second)
}
func Init() *ffcli.Command {
var (
flagset = flag.NewFlagSet("tuf init", flag.ExitOnError)
repository = flagset.String("repository", "", "path to initialize the staged repository")
threshold = flagset.Int("threshold", DefaultThreshold, "default root and targets signer threshold")
previous = flagset.String("previous", "", "path to the previous repository")
snapshot = flagset.String("snapshot", "", "reference to an online snapshot signer")
timestamp = flagset.String("timestamp", "", "reference to an online timestamp signer")
targets = flagset.String("target-meta", "", "path to a target configuration file")
)
return &ffcli.Command{
Name: "init",
ShortUsage: "tuf init initializes a new TUF repository",
ShortHelp: "tuf init initializes a new TUF repository",
LongHelp: `tuf init initializes a new TUF repository to the
specified repository directory. It will create unpopulated directories
keys/, staged/, and staged/targets under the repository with threshold 3
and a 4 month expiration.
EXAMPLES
# initialize repository at ceremony/YYYY-MM-DD
tuf init -repository ceremony/YYYY-MM-DD`,
FlagSet: flagset,
Exec: func(ctx context.Context, args []string) error {
if *repository == "" {
return flag.ErrHelp
}
if *snapshot == "" {
return flag.ErrHelp
}
if *timestamp == "" {
return flag.ErrHelp
}
if *targets == "" {
return flag.ErrHelp
}
targetsConfigStr, err := os.ReadFile(*targets)
if err != nil {
return err
}
targetsConfig, err := prepo.SigstoreTargetMetaFromString(targetsConfigStr)
if err != nil {
return err
}
return InitCmd(ctx, *repository, *previous, *threshold, targetsConfig, *snapshot, *timestamp)
},
}
}
// InitCmd creates a new staged root.json and targets.json in the specified directory. It populates the top-level
// roles with signers and adds targets to top-level targets.
// * directory: Directory to write newly staged metadata. Must contain a keys/ subdirectory with root/targets signers.
// * previous: Optional previous repository to chain the root from.
// * threshold: The root and targets threshold.
// * targetsConfig: A map of target file names and custom metadata to add to top-level targets.
// Target file names are expected to be in the working directory.
// * snapshotRef: A reference (KMS, file, URL) to a snapshot signer.
// * timestampRef: A reference (KMS, file, URL) to a timestamp signer.
// The root and targets metadata will be initialized with a 6 month expiration.
// Revoked keys will be automatically calculated given the previous root and the signers in directory.
// Signature placeholders for each key will be added to the root.json and targets.json file.
func InitCmd(ctx context.Context, directory, previous string, threshold int, targetsConfig map[string]json.RawMessage, snapshotRef string, timestampRef string) error {
// TODO: Validate directory is a good path.
store := tuf.FileSystemStore(directory, nil)
repo, err := tuf.NewRepoIndent(store, "", "\t", "sha512", "sha256")
if err != nil {
return err
}
curRootVersion, err := repo.RootVersion()
if err != nil {
return err
}
if previous == "" {
// Only initialize if no previous specified.
if err := repo.Init(false); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "TUF repository initialized at ", directory)
}
// Add the keys we just provisioned to root and targets and revoke any removed ones.
root, err := prepo.GetRootFromStore(store)
if err != nil {
return err
}
keys, err := getKeysFromDir(directory + "/keys")
if err != nil {
return err
}
var allRootKeys []*data.PublicKey
for _, role := range []string{"root", "targets"} {
currentKeyMap := map[string]bool{}
for _, tufKey := range keys {
currentKeyMap[tufKey.IDs()[0]] = true
if err := repo.AddVerificationKeyWithExpiration(role, tufKey, getExpiration(role)); err != nil {
return err
}
}
if role == "root" {
// This retrieves all the new root keys, but before we revoke any.
allRootKeys, err = repo.RootKeys()
if err != nil {
return err
}
}
// Revoke any keys that we've rotated out
oldKeys, ok := root.Roles[role]
if ok {
for _, oldKeyID := range oldKeys.KeyIDs {
if _, ok := currentKeyMap[oldKeyID]; !ok {
if err := repo.RevokeKey(role, oldKeyID); err != nil {
return err
}
}
}
}
if err := repo.SetThreshold(role, threshold); err != nil {
return err
}
}
// Revoke old root keys used for snapshot and timestamp and roles and add new keys.
for role, keyRef := range map[string]string{"snapshot": snapshotRef, "timestamp": timestampRef} {
signerKey, err := pkeys.GetSigningKey(ctx, keyRef)
if err != nil {
return err
}
// Add key. The expiration will adjust in the snapshot/timestamp step.
if err := repo.AddVerificationKeyWithExpiration(role, signerKey.Key, getExpiration(role)); err != nil {
return err
}
if err := repo.SetThreshold(role, 1); err != nil {
return err
}
}
// Add targets (copy them into the repository and add them to the targets.json)
for tt, custom := range targetsConfig {
from, err := os.Open(tt)
if err != nil {
return err
}
defer from.Close()
base := filepath.Base(tt)
to, err := os.Create(filepath.Join(directory, "staged", "targets", base))
if err != nil {
return err
}
defer to.Close()
if _, err := io.Copy(to, from); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Created target file at ", to.Name())
if err := repo.AddTargetWithExpiresToPreferredRole(base, custom, getExpiration("targets"), "targets"); err != nil {
return fmt.Errorf("error adding targets %w", err)
}
}
if err := repo.SetThreshold("targets", threshold); err != nil {
return err
}
// Add blank signatures to root and targets
t, err := prepo.GetTargetsFromStore(store)
if err != nil {
return err
}
if err := setMetaWithSigKeyIDs(store, "targets.json", t, allRootKeys); err != nil {
return err
}
// We manually increment the root version in case adding the verification keys did not
// increment the root version (because of no change).
root, err = prepo.GetRootFromStore(store)
if err != nil {
return err
}
root.Version = curRootVersion + 1
root.Expires = getExpiration("root")
return setMetaWithSigKeyIDs(store, "root.json", root, keys)
}
func setSignedMeta(store tuf.LocalStore, role string, s *data.Signed) error {
b, err := jsonMarshal(s)
if err != nil {
return err
}
return store.SetMeta(role, b)
}
func setMetaWithSigKeyIDs(store tuf.LocalStore, role string, meta interface{}, keys []*data.PublicKey) error {
signed, err := jsonMarshal(meta)
if err != nil {
return err
}
// Add empty sigs
emptySigs := make([]data.Signature, 0, 1)
for _, key := range keys {
for _, id := range key.IDs() {
emptySigs = append(emptySigs, data.Signature{
KeyID: id,
})
}
}
return setSignedMeta(store, role, &data.Signed{Signatures: emptySigs, Signed: signed})
}
func ClearEmptySignatures(store tuf.LocalStore, role string) error {
signedMeta, err := prepo.GetSignedMeta(store, role)
if err != nil {
return err
}
sigs := make([]data.Signature, 0, 1)
for _, signature := range signedMeta.Signatures {
if len(signature.Signature) == 0 {
// Skip placeholder signatures.
continue
}
sigs = append(sigs, signature)
}
return setSignedMeta(store, role, &data.Signed{Signatures: sigs, Signed: signedMeta.Signed})
}
func jsonMarshal(v interface{}) ([]byte, error) {
b, err := cjson.Marshal(v)
if err != nil {
return nil, err
}
var out bytes.Buffer
if err := json.Indent(&out, b, "", "\t"); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func getKeysFromDir(dir string) ([]*data.PublicKey, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var tufKeys []*data.PublicKey
for _, file := range files {
if file.IsDir() {
key, err := pkeys.SigningKeyFromDir(filepath.Join(dir, file.Name()))
if err != nil {
return nil, err
}
tufKey, err := pkeys.ToTufKey(*key)
if err != nil {
return nil, err
}
tufKeys = append(tufKeys, tufKey)
}
}
return tufKeys, err
}