forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
600 lines (513 loc) · 16.3 KB
/
api.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/snapshotstate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/sandbox"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/strutil"
)
var api = []*Command{
rootCmd,
sysInfoCmd,
loginCmd,
logoutCmd,
appIconCmd,
findCmd,
snapsCmd,
snapCmd,
snapFileCmd,
snapDownloadCmd,
snapConfCmd,
interfacesCmd,
assertsCmd,
assertsFindManyCmd,
stateChangeCmd,
stateChangesCmd,
createUserCmd,
buyCmd,
readyToBuyCmd,
snapctlCmd,
usersCmd,
sectionsCmd,
aliasesCmd,
appsCmd,
logsCmd,
warningsCmd,
debugPprofCmd,
debugCmd,
snapshotCmd,
snapshotExportCmd,
connectionsCmd,
modelCmd,
cohortsCmd,
serialModelCmd,
systemsCmd,
systemsActionCmd,
validationSetsListCmd,
validationSetsCmd,
routineConsoleConfStartCmd,
systemRecoveryKeysCmd,
}
var (
// see daemon.go:canAccess for details how the access is controlled
snapCmd = &Command{
Path: "/v2/snaps/{name}",
UserOK: true,
PolkitOK: "io.snapcraft.snapd.manage",
GET: getSnapInfo,
POST: postSnap,
}
)
// UserFromRequest extracts user information from request and return the respective user in state, if valid
// It requires the state to be locked
func UserFromRequest(st *state.State, req *http.Request) (*auth.UserState, error) {
// extract macaroons data from request
header := req.Header.Get("Authorization")
if header == "" {
return nil, auth.ErrInvalidAuth
}
authorizationData := strings.SplitN(header, " ", 2)
if len(authorizationData) != 2 || authorizationData[0] != "Macaroon" {
return nil, fmt.Errorf("authorization header misses Macaroon prefix")
}
var macaroon string
var discharges []string
for _, field := range strutil.CommaSeparatedList(authorizationData[1]) {
if strings.HasPrefix(field, `root="`) {
macaroon = strings.TrimSuffix(field[6:], `"`)
}
if strings.HasPrefix(field, `discharge="`) {
discharges = append(discharges, strings.TrimSuffix(field[11:], `"`))
}
}
if macaroon == "" {
return nil, fmt.Errorf("invalid authorization header")
}
user, err := auth.CheckMacaroon(st, macaroon, discharges)
return user, err
}
var muxVars = mux.Vars
func getSnapInfo(c *Command, r *http.Request, user *auth.UserState) Response {
vars := muxVars(r)
name := vars["name"]
about, err := localSnapInfo(c.d.overlord.State(), name)
if err != nil {
if err == errNoSnap {
return SnapNotFound(name, err)
}
return InternalError("%v", err)
}
route := c.d.router.Get(c.Path)
if route == nil {
return InternalError("cannot find route for %q snap", name)
}
url, err := route.URL("name", name)
if err != nil {
return InternalError("cannot build URL for %q snap: %v", name, err)
}
sd := servicestate.NewStatusDecorator(progress.Null)
result := webify(mapLocal(about, sd), url.String())
return SyncResponse(result, nil)
}
func webify(result *client.Snap, resource string) *client.Snap {
if result.Icon == "" || strings.HasPrefix(result.Icon, "http") {
return result
}
result.Icon = ""
route := appIconCmd.d.router.Get(appIconCmd.Path)
if route != nil {
url, err := route.URL("name", result.Name)
if err == nil {
result.Icon = url.String()
}
}
return result
}
func getStore(c *Command) snapstate.StoreService {
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
return snapstate.Store(st, nil)
}
// licenseData holds details about the snap license, and may be
// marshaled back as an error when the license agreement is pending,
// and is expected as input to accept (or not) that license
// agreement. As such, its field names are part of the API.
type licenseData struct {
Intro string `json:"intro"`
License string `json:"license"`
Agreed bool `json:"agreed"`
}
func (*licenseData) Error() string {
return "license agreement required"
}
type snapRevisionOptions struct {
Channel string `json:"channel"`
Revision snap.Revision `json:"revision"`
CohortKey string `json:"cohort-key"`
LeaveCohort bool `json:"leave-cohort"`
}
func (ropt *snapRevisionOptions) validate() error {
if ropt.CohortKey != "" {
if ropt.LeaveCohort {
return fmt.Errorf("cannot specify both cohort-key and leave-cohort")
}
if !ropt.Revision.Unset() {
return fmt.Errorf("cannot specify both cohort-key and revision")
}
}
if ropt.Channel != "" {
_, err := channel.Parse(ropt.Channel, "-")
if err != nil {
return err
}
}
return nil
}
type snapInstruction struct {
progress.NullMeter
Action string `json:"action"`
Amend bool `json:"amend"`
snapRevisionOptions
DevMode bool `json:"devmode"`
JailMode bool `json:"jailmode"`
Classic bool `json:"classic"`
IgnoreValidation bool `json:"ignore-validation"`
IgnoreRunning bool `json:"ignore-running"`
Unaliased bool `json:"unaliased"`
Purge bool `json:"purge,omitempty"`
// dropping support temporarely until flag confusion is sorted,
// this isn't supported by client atm anyway
LeaveOld bool `json:"temp-dropped-leave-old"`
License *licenseData `json:"license"`
Snaps []string `json:"snaps"`
Users []string `json:"users"`
// The fields below should not be unmarshalled into. Do not export them.
userID int
ctx context.Context
}
func (inst *snapInstruction) revnoOpts() *snapstate.RevisionOptions {
return &snapstate.RevisionOptions{
Channel: inst.Channel,
Revision: inst.Revision,
CohortKey: inst.CohortKey,
LeaveCohort: inst.LeaveCohort,
}
}
func (inst *snapInstruction) modeFlags() (snapstate.Flags, error) {
return modeFlags(inst.DevMode, inst.JailMode, inst.Classic)
}
func (inst *snapInstruction) installFlags() (snapstate.Flags, error) {
flags, err := inst.modeFlags()
if err != nil {
return snapstate.Flags{}, err
}
if inst.Unaliased {
flags.Unaliased = true
}
if inst.IgnoreRunning {
flags.IgnoreRunning = true
}
return flags, nil
}
func (inst *snapInstruction) validate() error {
if inst.CohortKey != "" {
if inst.Action != "install" && inst.Action != "refresh" && inst.Action != "switch" {
return fmt.Errorf("cohort-key can only be specified for install, refresh, or switch")
}
}
if inst.LeaveCohort {
if inst.Action != "refresh" && inst.Action != "switch" {
return fmt.Errorf("leave-cohort can only be specified for refresh or switch")
}
}
if inst.Action == "install" {
for _, snapName := range inst.Snaps {
// FIXME: alternatively we could simply mutate *inst
// and s/ubuntu-core/core/ ?
if snapName == "ubuntu-core" {
return fmt.Errorf(`cannot install "ubuntu-core", please use "core" instead`)
}
}
}
return inst.snapRevisionOptions.validate()
}
type snapInstructionResult struct {
Summary string
Affected []string
Tasksets []*state.TaskSet
Result map[string]interface{}
}
var (
snapstateInstall = snapstate.Install
snapstateInstallPath = snapstate.InstallPath
snapstateRefreshCandidates = snapstate.RefreshCandidates
snapstateTryPath = snapstate.TryPath
snapstateUpdate = snapstate.Update
snapstateUpdateMany = snapstate.UpdateMany
snapstateInstallMany = snapstate.InstallMany
snapstateRemoveMany = snapstate.RemoveMany
snapstateRevert = snapstate.Revert
snapstateRevertToRevision = snapstate.RevertToRevision
snapstateSwitch = snapstate.Switch
snapshotList = snapshotstate.List
snapshotCheck = snapshotstate.Check
snapshotForget = snapshotstate.Forget
snapshotRestore = snapshotstate.Restore
snapshotSave = snapshotstate.Save
snapshotExport = snapshotstate.Export
snapshotImport = snapshotstate.Import
assertstateRefreshSnapDeclarations = assertstate.RefreshSnapDeclarations
)
func ensureStateSoonImpl(st *state.State) {
st.EnsureBefore(0)
}
var ensureStateSoon = ensureStateSoonImpl
var errDevJailModeConflict = errors.New("cannot use devmode and jailmode flags together")
var errClassicDevmodeConflict = errors.New("cannot use classic and devmode flags together")
var errNoJailMode = errors.New("this system cannot honour the jailmode flag")
func modeFlags(devMode, jailMode, classic bool) (snapstate.Flags, error) {
flags := snapstate.Flags{}
devModeOS := sandbox.ForceDevMode()
switch {
case jailMode && devModeOS:
return flags, errNoJailMode
case jailMode && devMode:
return flags, errDevJailModeConflict
case devMode && classic:
return flags, errClassicDevmodeConflict
}
// NOTE: jailmode and classic are allowed together. In that setting,
// jailmode overrides classic and the app gets regular (non-classic)
// confinement.
flags.JailMode = jailMode
flags.Classic = classic
flags.DevMode = devMode
return flags, nil
}
func snapInstall(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if len(inst.Snaps[0]) == 0 {
return "", nil, fmt.Errorf(i18n.G("cannot install snap with empty name"))
}
flags, err := inst.installFlags()
if err != nil {
return "", nil, err
}
var ckey string
if inst.CohortKey == "" {
logger.Noticef("Installing snap %q revision %s", inst.Snaps[0], inst.Revision)
} else {
ckey = strutil.ElliptLeft(inst.CohortKey, 10)
logger.Noticef("Installing snap %q from cohort %q", inst.Snaps[0], ckey)
}
tset, err := snapstateInstall(inst.ctx, st, inst.Snaps[0], inst.revnoOpts(), inst.userID, flags)
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Install %q snap"), inst.Snaps[0])
if inst.Channel != "stable" && inst.Channel != "" {
msg += fmt.Sprintf(" from %q channel", inst.Channel)
}
if inst.CohortKey != "" {
msg += fmt.Sprintf(" from %q cohort", ckey)
}
return msg, []*state.TaskSet{tset}, nil
}
func snapUpdate(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
// TODO: bail if revision is given (and != current?), *or* behave as with install --revision?
flags, err := inst.modeFlags()
if err != nil {
return "", nil, err
}
if inst.IgnoreValidation {
flags.IgnoreValidation = true
}
if inst.IgnoreRunning {
flags.IgnoreRunning = true
}
if inst.Amend {
flags.Amend = true
}
// we need refreshed snap-declarations to enforce refresh-control as best as we can
if err = assertstateRefreshSnapDeclarations(st, inst.userID); err != nil {
return "", nil, err
}
ts, err := snapstateUpdate(st, inst.Snaps[0], inst.revnoOpts(), inst.userID, flags)
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Refresh %q snap"), inst.Snaps[0])
if inst.Channel != "stable" && inst.Channel != "" {
msg = fmt.Sprintf(i18n.G("Refresh %q snap from %q channel"), inst.Snaps[0], inst.Channel)
}
return msg, []*state.TaskSet{ts}, nil
}
func snapRemove(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
ts, err := snapstate.Remove(st, inst.Snaps[0], inst.Revision, &snapstate.RemoveFlags{Purge: inst.Purge})
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Remove %q snap"), inst.Snaps[0])
return msg, []*state.TaskSet{ts}, nil
}
func snapRevert(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
var ts *state.TaskSet
flags, err := inst.modeFlags()
if err != nil {
return "", nil, err
}
if inst.Revision.Unset() {
ts, err = snapstateRevert(st, inst.Snaps[0], flags)
} else {
ts, err = snapstateRevertToRevision(st, inst.Snaps[0], inst.Revision, flags)
}
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Revert %q snap"), inst.Snaps[0])
return msg, []*state.TaskSet{ts}, nil
}
func snapEnable(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("enable takes no revision")
}
ts, err := snapstate.Enable(st, inst.Snaps[0])
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Enable %q snap"), inst.Snaps[0])
return msg, []*state.TaskSet{ts}, nil
}
func snapDisable(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("disable takes no revision")
}
ts, err := snapstate.Disable(st, inst.Snaps[0])
if err != nil {
return "", nil, err
}
msg := fmt.Sprintf(i18n.G("Disable %q snap"), inst.Snaps[0])
return msg, []*state.TaskSet{ts}, nil
}
func snapSwitch(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("switch takes no revision")
}
ts, err := snapstateSwitch(st, inst.Snaps[0], inst.revnoOpts())
if err != nil {
return "", nil, err
}
var msg string
switch {
case inst.LeaveCohort && inst.Channel != "":
msg = fmt.Sprintf(i18n.G("Switch %q snap to channel %q and away from cohort"), inst.Snaps[0], inst.Channel)
case inst.LeaveCohort:
msg = fmt.Sprintf(i18n.G("Switch %q snap away from cohort"), inst.Snaps[0])
case inst.CohortKey == "" && inst.Channel != "":
msg = fmt.Sprintf(i18n.G("Switch %q snap to channel %q"), inst.Snaps[0], inst.Channel)
case inst.CohortKey != "" && inst.Channel == "":
msg = fmt.Sprintf(i18n.G("Switch %q snap to cohort %q"), inst.Snaps[0], strutil.ElliptLeft(inst.CohortKey, 10))
default:
msg = fmt.Sprintf(i18n.G("Switch %q snap to channel %q and cohort %q"), inst.Snaps[0], inst.Channel, strutil.ElliptLeft(inst.CohortKey, 10))
}
return msg, []*state.TaskSet{ts}, nil
}
type snapActionFunc func(*snapInstruction, *state.State) (string, []*state.TaskSet, error)
var snapInstructionDispTable = map[string]snapActionFunc{
"install": snapInstall,
"refresh": snapUpdate,
"remove": snapRemove,
"revert": snapRevert,
"enable": snapEnable,
"disable": snapDisable,
"switch": snapSwitch,
}
func (inst *snapInstruction) dispatch() snapActionFunc {
if len(inst.Snaps) != 1 {
logger.Panicf("dispatch only handles single-snap ops; got %d", len(inst.Snaps))
}
return snapInstructionDispTable[inst.Action]
}
func (inst *snapInstruction) errToResponse(err error) Response {
if len(inst.Snaps) == 0 {
return errToResponse(err, nil, BadRequest, "cannot %s: %v", inst.Action)
}
return errToResponse(err, inst.Snaps, BadRequest, "cannot %s %s: %v", inst.Action, strutil.Quoted(inst.Snaps))
}
func postSnap(c *Command, r *http.Request, user *auth.UserState) Response {
route := c.d.router.Get(stateChangeCmd.Path)
if route == nil {
return InternalError("cannot find route for change")
}
decoder := json.NewDecoder(r.Body)
var inst snapInstruction
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into snap instruction: %v", err)
}
inst.ctx = r.Context()
state := c.d.overlord.State()
state.Lock()
defer state.Unlock()
if user != nil {
inst.userID = user.ID
}
vars := muxVars(r)
inst.Snaps = []string{vars["name"]}
if err := inst.validate(); err != nil {
return BadRequest("%s", err)
}
impl := inst.dispatch()
if impl == nil {
return BadRequest("unknown action %s", inst.Action)
}
msg, tsets, err := impl(&inst, state)
if err != nil {
return inst.errToResponse(err)
}
chg := newChange(state, inst.Action+"-snap", msg, tsets, inst.Snaps)
ensureStateSoon(state)
return AsyncResponse(nil, &Meta{Change: chg.ID()})
}
func newChange(st *state.State, kind, summary string, tsets []*state.TaskSet, snapNames []string) *state.Change {
chg := st.NewChange(kind, summary)
for _, ts := range tsets {
chg.AddAll(ts)
}
if snapNames != nil {
chg.Set("snap-names", snapNames)
}
return chg
}