-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathrepair.go
218 lines (184 loc) · 5.97 KB
/
repair.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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 asserts
import (
"fmt"
"strings"
"time"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/strutil"
)
// Repair holds an repair assertion which allows running repair
// code to fixup broken systems. It can be limited by series and models, as well
// as by bases and modes.
type Repair struct {
assertionBase
series []string
architectures []string
models []string
modes []string
bases []string
id int
disabled bool
timestamp time.Time
}
// BrandID returns the brand identifier that signed this assertion.
func (r *Repair) BrandID() string {
return r.HeaderString("brand-id")
}
// RepairID returns the sequential id of the repair. There
// should be a public place to look up details about the repair
// by brand-id and repair-id.
// (e.g. the snapcraft forum).
func (r *Repair) RepairID() int {
return r.id
}
// Sequence implements SequenceMember, it returns the same as RepairID.
func (r *Repair) Sequence() int {
return r.RepairID()
}
// Summary returns the mandatory summary description of the repair.
func (r *Repair) Summary() string {
return r.HeaderString("summary")
}
// Architectures returns the architectures that this assertions applies to.
func (r *Repair) Architectures() []string {
return r.architectures
}
// Series returns the series that this assertion is valid for.
func (r *Repair) Series() []string {
return r.series
}
// Modes returns the modes that this assertion is valid for. It is either a list
// of "run", "recover", or "install", or it is the empty list. The empty list
// is interpreted to mean only "run" mode.
func (r *Repair) Modes() []string {
return r.modes
}
// Bases returns the bases that this assertion is valid for. It is either a list
// of valid base snaps that Ubuntu Core systems can have or it is the empty
// list. The empty list effectively means all Ubuntu Core systems while "core"
// means Ubuntu Core 16, "core18" means Ubuntu Core 18, etc.
func (r *Repair) Bases() []string {
return r.bases
}
// Models returns the models that this assertion is valid for.
// It is a list of "brand-id/model-name" strings.
func (r *Repair) Models() []string {
return r.models
}
// Disabled returns true if the repair has been disabled.
func (r *Repair) Disabled() bool {
return r.disabled
}
// Timestamp returns the time when the repair was issued.
func (r *Repair) Timestamp() time.Time {
return r.timestamp
}
// Implement further consistency checks.
func (r *Repair) checkConsistency(db RODatabase, acck *AccountKey) error {
// Do the cross-checks when this assertion is actually used,
// i.e. in the future repair code
return nil
}
// expected interface is implemented
var _ consistencyChecker = (*Repair)(nil)
func assembleRepair(assert assertionBase) (Assertion, error) {
err := checkAuthorityMatchesBrand(&assert)
if err != nil {
return nil, err
}
repairID, err := checkSequence(assert.headers, "repair-id")
if err != nil {
return nil, err
}
summary, err := checkNotEmptyString(assert.headers, "summary")
if err != nil {
return nil, err
}
if strings.ContainsAny(summary, "\n\r") {
return nil, fmt.Errorf(`"summary" header cannot have newlines`)
}
series, err := checkStringList(assert.headers, "series")
if err != nil {
return nil, err
}
models, err := checkStringList(assert.headers, "models")
if err != nil {
return nil, err
}
architectures, err := checkStringList(assert.headers, "architectures")
if err != nil {
return nil, err
}
modes, err := checkStringList(assert.headers, "modes")
if err != nil {
return nil, err
}
bases, err := checkStringList(assert.headers, "bases")
if err != nil {
return nil, err
}
// validate that all base snap names are valid snap names
for _, b := range bases {
if err := naming.ValidateSnap(b); err != nil {
return nil, fmt.Errorf("invalid snap name %q in \"bases\"", b)
}
}
// verify that modes is a list of only "run" and "recover"
if len(modes) != 0 {
for _, m := range modes {
// note that we could import boot here to use i.e. boot.ModeRun, but
// that is rather a heavy package considering that this package is
// used in many places, so instead just use the values directly,
// they're unlikely to change now
if !strutil.ListContains([]string{"run", "recover"}, m) {
return nil, fmt.Errorf("header \"modes\" contains an invalid element: %q (valid values are run and recover)", m)
}
}
// if modes is non-empty, then bases must be core2X, i.e. core20+
// however, we don't know what future bases could be UC20-like and named
// differently yet, so we just fail on bases that we know as of today
// are _not_ UC20: core and core18
for _, b := range bases {
// fail on uc16 and uc18 base snaps
if b == "core" || b == "core18" || b == "core16" {
return nil, fmt.Errorf("in the presence of a non-empty \"modes\" header, \"bases\" must only contain base snaps supporting recovery modes")
}
}
}
disabled, err := checkOptionalBool(assert.headers, "disabled")
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
return &Repair{
assertionBase: assert,
series: series,
architectures: architectures,
models: models,
modes: modes,
bases: bases,
id: repairID,
disabled: disabled,
timestamp: timestamp,
}, nil
}