-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_handlers.go
565 lines (476 loc) · 14.8 KB
/
config_handlers.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"sort"
"code.google.com/p/go.crypto/openpgp"
"github.com/gorilla/mux"
"golang.org/x/net/context"
)
// Lots of DRY fail here, can probably clear this by wrapping requests
// that result in a repo update
// This build a function to enumerate the distributions
func httpConfigHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
switch r.Method {
case "GET":
return handleWithReadLock(doHTTPConfigGetHandler, ctx, w, r)
case "PUT":
return handleWithWriteLock(doHTTPConfigPutHandler, ctx, w, r)
default:
return sendResponse(w, http.StatusMethodNotAllowed, nil)
}
}
// This build a function to view the config of a distribution
func doHTTPConfigGetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
vars := mux.Vars(r)
name := vars["name"]
rel, err := state.Archive.GetDist(name)
switch {
case err == nil:
return sendOKResponse(w, rel.Config())
case os.IsNotExist(err):
return sendResponse(w, http.StatusNotFound, nil)
default:
return &appError{Error: err}
}
}
// This build a function to update the config of a distribution
func doHTTPConfigPutHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
if !AuthorisedAdmin(ctx, w, r) {
return sendResponse(w, http.StatusUnauthorized, nil)
}
// We use this to determine if the fields in the
// update were included or left out
type configUpdate struct {
PruneRules *string
VerifyChanges *bool
AcceptLoneDebs *bool
PoolPattern *string
VerifyDebs *bool
AutoTrimLength *int
AutoTrim *bool
VerifyChangesSufficient *bool
}
vars := mux.Vars(r)
name := vars["name"]
rel, err := state.Archive.GetDist(name)
switch {
case err == nil:
case os.IsNotExist(err):
return sendResponse(w, http.StatusNotFound, nil)
default:
return &appError{Error: err}
}
cfg := rel.Config()
decoder := json.NewDecoder(r.Body)
var d configUpdate
err = decoder.Decode(&d)
if err != nil {
return sendResponse(w, http.StatusBadRequest, nil)
}
var acts []ReleaseLogAction
if d.PruneRules != nil && *d.PruneRules != cfg.PruneRules {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("PruneRules changed from %v to %v", cfg.PruneRules, *d.PruneRules),
})
cfg.PruneRules = *d.PruneRules
}
if d.VerifyChanges != nil && *d.VerifyChanges != cfg.VerifyChanges {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("VerifyChanges changed from %v to %v", cfg.VerifyChanges, *d.VerifyChanges),
})
cfg.VerifyChanges = *d.VerifyChanges
}
if d.AcceptLoneDebs != nil && *d.AcceptLoneDebs != cfg.AcceptLoneDebs {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("AcceptLoneDebs changed from %v to %v", cfg.AcceptLoneDebs, *d.AcceptLoneDebs),
})
cfg.AcceptLoneDebs = *d.AcceptLoneDebs
}
if d.PoolPattern != nil && *d.PoolPattern != cfg.PoolPattern {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("PoolPattern changed from %v to %v", cfg.PoolPattern, *d.PoolPattern),
})
cfg.PoolPattern = *d.PoolPattern
}
if d.VerifyDebs != nil && *d.VerifyDebs != cfg.VerifyDebs {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("VerifyDebs changed from %v to %v", cfg.VerifyDebs, *d.VerifyDebs),
})
cfg.VerifyDebs = *d.VerifyDebs
}
if d.AutoTrimLength != nil && *d.AutoTrimLength != cfg.AutoTrimLength {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("AutoTrimLength changed from %v to %v", cfg.AutoTrimLength, *d.AutoTrimLength),
})
cfg.AutoTrimLength = *d.AutoTrimLength
}
if d.AutoTrim != nil && *d.AutoTrim != cfg.AutoTrim {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("AutoTrim changed from %v to %v", cfg.AutoTrim, *d.AutoTrim),
})
cfg.AutoTrim = *d.AutoTrim
}
if d.VerifyChangesSufficient != nil && *d.VerifyChangesSufficient != cfg.VerifyChangesSufficient {
acts = append(acts, ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("VerifyChangesSufficient changed from %v to %v", cfg.VerifyChangesSufficient, *d.VerifyChangesSufficient),
})
cfg.VerifyChangesSufficient = *d.VerifyChangesSufficient
}
if len(acts) == 0 {
// No actions, do nothing
return doHTTPConfigGetHandler(ctx, w, r)
}
n := rel.NewChild()
n.Actions = acts
newcfgid, err := state.Archive.AddReleaseConfig(*cfg)
if err != nil {
return &appError{
Error: errors.New("failed to add new release config, " + err.Error()),
}
}
n.ConfigID = newcfgid
n.updateReleasefiles()
newrelid, err := state.Archive.AddRelease(n)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
err = state.Archive.SetDist(name, newrelid)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
err = state.Archive.ReifyRelease(newrelid)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
return doHTTPConfigGetHandler(ctx, w, r)
}
// This build a function to enumerate the distributions
func httpConfigSigningKeyHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
switch r.Method {
case "GET":
return handleWithReadLock(doHTTPConfigSigningKeyGetHandler, ctx, w, r)
case "PUT", "POST":
return handleWithWriteLock(doHTTPConfigSigningKeyPutHandler, ctx, w, r)
case "DELETE":
return handleWithWriteLock(doHTTPConfigSigningKeyDeleteHandler, ctx, w, r)
default:
return sendResponse(w, http.StatusMethodNotAllowed, nil)
}
}
func doHTTPConfigSigningKeyGetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
vars := mux.Vars(r)
name := vars["name"]
rel, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
id, err := rel.SignerKey()
if err != nil {
return &appError{
Error: err,
}
}
if id == nil {
return sendResponse(w, http.StatusBadRequest, "No signing key set")
}
key := id.PrimaryKey
if key == nil {
return sendResponse(w, http.StatusBadRequest, "No signing key set")
}
return sendOKResponse(w, key.KeyIdShortString())
}
func doHTTPConfigSigningKeyPutHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
if !AuthorisedAdmin(ctx, w, r) {
return sendResponse(w, http.StatusUnauthorized, nil)
}
vars := mux.Vars(r)
name := vars["name"]
p, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
rel := p.NewChild()
id, err := state.Archive.CopyToStore(r.Body)
if err != nil {
return &appError{
Error: errors.New("Failed to copy key to store, " + err.Error()),
}
}
rdr, err := state.Archive.Open(id)
_, err = openpgp.ReadArmoredKeyRing(rdr)
if err != nil {
return sendResponse(w, http.StatusBadRequest, "failed to parse data as key, "+err.Error())
}
cfg := rel.Config()
if cfg.SigningKeyID.String() == id.String() {
return doHTTPConfigSigningKeyGetHandler(ctx, w, r)
}
rel.Actions = []ReleaseLogAction{
ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("Signing key updated from %v to %v", cfg.SigningKeyID.String(), id.String()),
},
}
cfg.SigningKeyID = id
newcfgid, err := state.Archive.AddReleaseConfig(*cfg)
if err != nil {
return &appError{
Error: errors.New("failed to add new release config, " + err.Error()),
}
}
rel.ConfigID = newcfgid
rel.updateReleasefiles()
newrelid, err := state.Archive.AddRelease(rel)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
err = state.Archive.SetDist(name, newrelid)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
err = state.Archive.ReifyRelease(newrelid)
if err != nil {
return &appError{
Error: errors.New("failed to update key, " + err.Error()),
}
}
return doHTTPConfigSigningKeyGetHandler(ctx, w, r)
}
func doHTTPConfigSigningKeyDeleteHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
if !AuthorisedAdmin(ctx, w, r) {
return sendResponse(w, http.StatusUnauthorized, nil)
}
vars := mux.Vars(r)
name := vars["name"]
p, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
rel := p.NewChild()
cfg := rel.Config()
if cfg.SigningKeyID == nil {
return doHTTPConfigSigningKeyGetHandler(ctx, w, r)
}
rel.Actions = []ReleaseLogAction{
ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("Signing key removed"),
},
}
cfg.SigningKeyID = nil
newcfgid, err := state.Archive.AddReleaseConfig(*cfg)
if err != nil {
return &appError{Error: fmt.Errorf("failed to parse add new release config, %v", err)}
}
rel.ConfigID = newcfgid
rel.updateReleasefiles()
newrelid, err := state.Archive.AddRelease(rel)
if err != nil {
return &appError{Error: fmt.Errorf("failed to parse add new release, %v", err)}
}
err = state.Archive.SetDist(name, newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to parse add update release tag, %v", err)}
}
err = state.Archive.ReifyRelease(newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
return sendOKResponse(w, "DELETED")
}
// This build a function to enumerate the distributions
func httpConfigPublicKeysHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
switch r.Method {
case "GET":
return handleWithReadLock(doHTTPConfigPublicKeysGetHandler, ctx, w, r)
case "POST":
return handleWithWriteLock(doHTTPConfigPublicKeysPostHandler, ctx, w, r)
case "DELETE":
return handleWithWriteLock(doHTTPConfigPublicKeysDeleteHandler, ctx, w, r)
default:
return sendResponse(w, http.StatusMethodNotAllowed, nil)
}
}
// For managing public keys in a config
func doHTTPConfigPublicKeysGetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
vars := mux.Vars(r)
name := vars["name"]
reqid := vars["id"]
rel, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
ids, err := rel.PubRing()
if err != nil {
return &appError{Error: fmt.Errorf("Error retrieving signing keys, %v", err)}
}
if reqid == "" {
var keyids []string
for _, id := range ids {
key := id.PrimaryKey
if key != nil {
keyids = append(keyids, key.KeyIdString())
}
}
return sendOKResponse(w, keyids)
}
for _, id := range ids {
key := id.PrimaryKey
if key.KeyIdString() == reqid {
// This is a bit boring, should output more
return sendOKResponse(w, key.KeyIdShortString())
}
}
return sendResponse(w, http.StatusNotFound, nil)
}
func doHTTPConfigPublicKeysPostHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
if !AuthorisedAdmin(ctx, w, r) {
return sendResponse(w, http.StatusUnauthorized, nil)
}
vars := mux.Vars(r)
name := vars["name"]
p, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
rel := p.NewChild()
id, err := state.Archive.CopyToStore(r.Body)
if err != nil {
return &appError{Error: fmt.Errorf("failed to copy key to store, %v", err)}
}
rdr, err := state.Archive.Open(id)
defer rdr.Close()
kr, err := openpgp.ReadArmoredKeyRing(rdr)
if err != nil {
return sendResponse(w, http.StatusBadRequest, "failed to parse data as key, "+err.Error())
}
if len(kr) != 1 {
return sendResponse(w, http.StatusBadRequest, "upload 1 key at a time")
}
key := kr[0]
known, err := rel.PubRing()
if err != nil {
return &appError{Error: fmt.Errorf("while reading keyring, %v", err)}
}
for _, k := range known {
if key.PrimaryKey.KeyIdString() == k.PrimaryKey.KeyIdString() {
return doHTTPConfigPublicKeysGetHandler(ctx, w, r)
}
}
c := rel.Config()
c.PublicKeyIDs = append(c.PublicKeyIDs, id)
sort.Sort(ByID(c.PublicKeyIDs))
newcfgid, err := state.Archive.AddReleaseConfig(*c)
if err != nil {
return &appError{Error: fmt.Errorf("failed to add new release config , %v", err)}
}
rel.Actions = []ReleaseLogAction{
ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("Added Public key %s", id.String()),
},
}
rel.ConfigID = newcfgid
newrelid, err := state.Archive.AddRelease(rel)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
err = state.Archive.SetDist(name, newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
err = state.Archive.ReifyRelease(newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
return doHTTPConfigPublicKeysGetHandler(ctx, w, r)
}
func doHTTPConfigPublicKeysDeleteHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *appError {
if !AuthorisedAdmin(ctx, w, r) {
return sendResponse(w, http.StatusUnauthorized, nil)
}
vars := mux.Vars(r)
name := vars["name"]
id := vars["id"]
p, err := state.Archive.GetDist(name)
if err != nil {
return sendResponse(w, http.StatusNotFound, nil)
}
rel := p.NewChild()
c := rel.Config()
found := false
finalKeys := []StoreID{}
for _, k := range c.PublicKeyIDs {
rdr, err := rel.store.Open(k)
if err != nil {
log.Printf("reading key failed, %v", err)
continue
}
kr, err := openpgp.ReadArmoredKeyRing(rdr)
if err != nil {
log.Printf("reading keyring from store failed, %v", err)
continue
}
if len(kr) != 1 {
log.Printf("reading keyring from store failed, len was %v", len(kr))
continue
}
key := kr[0]
if key.PrimaryKey.KeyIdString() == id {
found = true
} else {
finalKeys = append(finalKeys, k)
}
}
if !found {
return sendResponse(w, http.StatusNotFound, nil)
}
rel.Actions = []ReleaseLogAction{
ReleaseLogAction{
Type: ActionCONFIGCHANGE,
Description: fmt.Sprintf("Deleted Public key %s", id),
},
}
c.PublicKeyIDs = finalKeys
newcfgid, err := state.Archive.AddReleaseConfig(*c)
if err != nil {
return &appError{Error: fmt.Errorf("failed to add new release config, %v", err)}
}
rel.ConfigID = newcfgid
newrelid, err := state.Archive.AddRelease(rel)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
err = state.Archive.SetDist(name, newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
err = state.Archive.ReifyRelease(newrelid)
if err != nil {
return &appError{Error: fmt.Errorf("failed to update key, %v", err)}
}
return sendOKResponse(w, "DELETED")
}