forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remodel.go
470 lines (388 loc) · 12.1 KB
/
remodel.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 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 devicestate
import (
"fmt"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/overlord/storecontext"
)
/*
This is the central logic to setup and mediate the access to the to-be
device state and dedicated store during remodeling and drive the
re-registration, leveraging the snapstate.DeviceContext/DeviceCtx and
storecontext.DeviceBackend mechanisms and also registrationContext.
Different context implementations will be used depending on the kind
of remodel, and those will play the roles/implement as needed
snapstate.DeviceContext, storecontext.DeviceBackend and
registrationContext:
* same brand/model, brand store => updateRemodel
this is just a contextual carrier for the new model
* same brand/model different brand store => storeSwitchRemodel this
mediates access to device state kept on the remodel change, it also
creates a store that uses that and refers to the new brand store
* different brand/model, maybe different brand store => reregRemodel
similar to storeSwitchRemodel case after a first phase that performs
re-registration where the context plays registrationContext's role
(NOT IMPLEMENTED YET)
*/
// RemodelKind designates a kind of remodeling.
type RemodelKind int
const (
// same brand/model, brand store
UpdateRemodel RemodelKind = iota
// same brand/model, different brand store
StoreSwitchRemodel
// different brand/model, maybe different brand store
ReregRemodel
)
func (k RemodelKind) String() string {
switch k {
case UpdateRemodel:
return "revision update remodel"
case StoreSwitchRemodel:
return "store switch remodel"
case ReregRemodel:
return "re-registration remodel"
}
panic(fmt.Sprintf("internal error: unknown remodel kind: %d", k))
}
// ClassifyRemodel returns what kind of remodeling is going from oldModel to newModel.
func ClassifyRemodel(oldModel, newModel *asserts.Model) RemodelKind {
if oldModel.BrandID() != newModel.BrandID() {
return ReregRemodel
}
if oldModel.Model() != newModel.Model() {
return ReregRemodel
}
if oldModel.Store() != newModel.Store() {
return StoreSwitchRemodel
}
return UpdateRemodel
}
type remodelCtxKey struct {
chgID string
}
func cachedRemodelCtx(chg *state.Change) (remodelContext, bool) {
key := remodelCtxKey{chg.ID()}
remodCtx, ok := chg.State().Cached(key).(remodelContext)
return remodCtx, ok
}
func cleanupRemodelCtx(chg *state.Change) {
chg.State().Cache(remodelCtxKey{chg.ID()}, nil)
}
// A remodelContext mediates the correct and isolated device state
// access and evolution during a remodel.
// All remodelContexts are at least a DeviceContext.
type remodelContext interface {
Init(chg *state.Change)
Finish() error
snapstate.DeviceContext
Kind() RemodelKind
// initialDevice takes the current/initial device state
// when setting up the remodel context
initialDevice(device *auth.DeviceState) error
// associate associates the remodel context with the change
// and caches it
associate(chg *state.Change)
}
// remodelCtx returns a remodeling context for the given transition.
// It constructs and caches a dedicated store as needed as well.
func remodelCtx(st *state.State, oldModel, newModel *asserts.Model) (remodelContext, error) {
var remodCtx remodelContext
devMgr := deviceMgr(st)
switch kind := ClassifyRemodel(oldModel, newModel); kind {
case UpdateRemodel:
// simple context for the simple case
groundCtx := groundDeviceContext{
model: newModel,
systemMode: devMgr.SystemMode(SysAny),
}
remodCtx = &updateRemodelContext{baseRemodelContext{groundCtx, oldModel}}
case StoreSwitchRemodel:
remodCtx = newNewStoreRemodelContext(st, devMgr, newModel, oldModel)
case ReregRemodel:
remodCtx = &reregRemodelContext{
newStoreRemodelContext: newNewStoreRemodelContext(st, devMgr, newModel, oldModel),
}
default:
return nil, fmt.Errorf("unsupported remodel: %s", kind)
}
device, err := devMgr.device()
if err != nil {
return nil, err
}
if err := remodCtx.initialDevice(device); err != nil {
return nil, err
}
return remodCtx, nil
}
// remodelCtxFromTask returns a possibly cached remodeling context associated
// with the task via its change, if task is nil or the task change
// is not a remodeling it will return ErrNoState.
func remodelCtxFromTask(t *state.Task) (remodelContext, error) {
if t == nil {
return nil, state.ErrNoState
}
chg := t.Change()
if chg == nil {
return nil, state.ErrNoState
}
var encNewModel string
if err := chg.Get("new-model", &encNewModel); err != nil {
return nil, err
}
// shortcut, cached?
if remodCtx, ok := cachedRemodelCtx(chg); ok {
return remodCtx, nil
}
st := t.State()
oldModel, err := findModel(st)
if err != nil {
return nil, fmt.Errorf("internal error: cannot find old model during remodel: %v", err)
}
newModelA, err := asserts.Decode([]byte(encNewModel))
if err != nil {
return nil, err
}
newModel, ok := newModelA.(*asserts.Model)
if !ok {
return nil, fmt.Errorf("internal error: cannot use a remodel new-model, wrong type")
}
remodCtx, err := remodelCtx(st, oldModel, newModel)
if err != nil {
return nil, err
}
remodCtx.associate(chg)
return remodCtx, nil
}
type baseRemodelContext struct {
groundDeviceContext
oldModel *asserts.Model
}
func (rc *baseRemodelContext) ForRemodeling() bool {
return true
}
func (rc *baseRemodelContext) GroundContext() snapstate.DeviceContext {
return &groundDeviceContext{
model: rc.oldModel,
systemMode: rc.systemMode,
}
}
func (rc *baseRemodelContext) initialDevice(*auth.DeviceState) error {
// do nothing
return nil
}
func (rc *baseRemodelContext) cacheViaChange(chg *state.Change, remodCtx remodelContext) {
chg.State().Cache(remodelCtxKey{chg.ID()}, remodCtx)
}
func (rc *baseRemodelContext) init(chg *state.Change) {
chg.Set("new-model", string(asserts.Encode(rc.model)))
}
func (rc *baseRemodelContext) SystemMode() string {
return rc.systemMode
}
// updateRemodelContext: model assertion revision-only update remodel
// (no change to brand/model or store)
type updateRemodelContext struct {
baseRemodelContext
}
func (rc *updateRemodelContext) Kind() RemodelKind {
return UpdateRemodel
}
func (rc *updateRemodelContext) associate(chg *state.Change) {
rc.cacheViaChange(chg, rc)
}
func (rc *updateRemodelContext) Init(chg *state.Change) {
rc.init(chg)
rc.associate(chg)
}
func (rc *updateRemodelContext) Store() snapstate.StoreService {
return nil
}
func (rc *updateRemodelContext) Finish() error {
// nothing more to do
return nil
}
// newStoreRemodelContext: remodel needing a new store session
// (for change of store (or brand/model))
type newStoreRemodelContext struct {
baseRemodelContext
// device state storage before this is associate with a change
deviceState *auth.DeviceState
// the associated change
remodelChange *state.Change
store snapstate.StoreService
st *state.State
deviceMgr *DeviceManager
}
func newNewStoreRemodelContext(st *state.State, devMgr *DeviceManager, newModel, oldModel *asserts.Model) *newStoreRemodelContext {
rc := &newStoreRemodelContext{}
groundCtx := groundDeviceContext{
model: newModel,
systemMode: devMgr.SystemMode(SysAny),
}
rc.baseRemodelContext = baseRemodelContext{groundCtx, oldModel}
rc.st = st
rc.deviceMgr = devMgr
rc.store = devMgr.newStore(rc.deviceBackend())
return rc
}
func (rc *newStoreRemodelContext) Kind() RemodelKind {
return StoreSwitchRemodel
}
func (rc *newStoreRemodelContext) associate(chg *state.Change) {
rc.remodelChange = chg
rc.cacheViaChange(chg, rc)
}
func (rc *newStoreRemodelContext) initialDevice(device *auth.DeviceState) error {
device1 := *device
// we will need a new one, it might embed the store as well
device1.SessionMacaroon = ""
rc.deviceState = &device1
return nil
}
func (rc *newStoreRemodelContext) init(chg *state.Change) {
rc.baseRemodelContext.init(chg)
chg.Set("device", rc.deviceState)
rc.deviceState = nil
}
func (rc *newStoreRemodelContext) Init(chg *state.Change) {
rc.init(chg)
rc.associate(chg)
}
func (rc *newStoreRemodelContext) Store() snapstate.StoreService {
return rc.store
}
func (rc *newStoreRemodelContext) device() (*auth.DeviceState, error) {
var err error
var device auth.DeviceState
if rc.remodelChange == nil {
// no remodelChange yet
device = *rc.deviceState
} else {
err = rc.remodelChange.Get("device", &device)
}
return &device, err
}
func (rc *newStoreRemodelContext) setCtxDevice(device *auth.DeviceState) {
if rc.remodelChange == nil {
// no remodelChange yet
rc.deviceState = device
} else {
rc.remodelChange.Set("device", device)
}
}
func (rc *newStoreRemodelContext) Finish() error {
// expose the device state of the remodel with the new session
// to the rest of the system
remodelDevice, err := rc.device()
if err != nil {
return err
}
return rc.deviceMgr.setDevice(remodelDevice)
}
func (rc *newStoreRemodelContext) deviceBackend() storecontext.DeviceBackend {
return &remodelDeviceBackend{rc}
}
type remodelDeviceBackend struct {
*newStoreRemodelContext
}
func (b remodelDeviceBackend) Device() (*auth.DeviceState, error) {
return b.device()
}
func (b remodelDeviceBackend) SetDevice(device *auth.DeviceState) error {
b.setCtxDevice(device)
return nil
}
func (b remodelDeviceBackend) Model() (*asserts.Model, error) {
return b.model, nil
}
func (b remodelDeviceBackend) Serial() (*asserts.Serial, error) {
// this the shared logic, also correct for the rereg case
// we should lookup the serial with the remodeling device state
device, err := b.device()
if err != nil {
return nil, err
}
return findSerial(b.st, device)
}
// reregRemodelContext: remodel for a change of brand/model
type reregRemodelContext struct {
*newStoreRemodelContext
origModel *asserts.Model
origSerial *asserts.Serial
}
func (rc *reregRemodelContext) Kind() RemodelKind {
return ReregRemodel
}
func (rc *reregRemodelContext) associate(chg *state.Change) {
rc.remodelChange = chg
rc.cacheViaChange(chg, rc)
}
func (rc *reregRemodelContext) initialDevice(device *auth.DeviceState) error {
origModel, err := findModel(rc.st)
if err != nil {
return err
}
origSerial, err := findSerial(rc.st, nil)
if err != nil {
return fmt.Errorf("cannot find current serial before proceeding with re-registration: %v", err)
}
rc.origModel = origModel
rc.origSerial = origSerial
// starting almost from scratch with only device-key
rc.deviceState = &auth.DeviceState{
Brand: rc.model.BrandID(),
Model: rc.model.Model(),
KeyID: device.KeyID,
}
return nil
}
func (rc *reregRemodelContext) Init(chg *state.Change) {
rc.init(chg)
rc.associate(chg)
}
// reregRemodelContext impl of registrationContext
func (rc *reregRemodelContext) Device() (*auth.DeviceState, error) {
return rc.device()
}
func (rc *reregRemodelContext) GadgetForSerialRequestConfig() string {
return rc.origModel.Gadget()
}
func (rc *reregRemodelContext) SerialRequestExtraHeaders() map[string]interface{} {
return map[string]interface{}{
"original-brand-id": rc.origSerial.BrandID(),
"original-model": rc.origSerial.Model(),
"original-serial": rc.origSerial.Serial(),
}
}
func (rc *reregRemodelContext) SerialRequestAncillaryAssertions() []asserts.Assertion {
return []asserts.Assertion{rc.model, rc.origSerial}
}
func (rc *reregRemodelContext) FinishRegistration(serial *asserts.Serial) error {
device, err := rc.device()
if err != nil {
return err
}
device.Serial = serial.Serial()
rc.setCtxDevice(device)
return nil
}