-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathvalidation_set.go
366 lines (302 loc) · 10.1 KB
/
validation_set.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 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 asserts
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/strutil"
)
// Presence represents a presence constraint.
type Presence string
const (
PresenceRequired Presence = "required"
PresenceOptional Presence = "optional"
PresenceInvalid Presence = "invalid"
)
func presencesAsStrings(presences ...Presence) []string {
strs := make([]string, len(presences))
for i, pres := range presences {
strs[i] = string(pres)
}
return strs
}
var validValidationSetSnapPresences = presencesAsStrings(PresenceRequired, PresenceOptional, PresenceInvalid)
func checkOptionalPresence(headers map[string]interface{}, which string, valid []string) (Presence, error) {
presence, err := checkOptionalStringWhat(headers, "presence", which)
if err != nil {
return Presence(""), err
}
if presence != "" && !strutil.ListContains(valid, presence) {
return Presence(""), fmt.Errorf("presence %s must be one of %s", which, strings.Join(valid, "|"))
}
return Presence(presence), nil
}
func checkPresence(headers map[string]interface{}, which string, valid []string) (Presence, error) {
presence, err := checkExistsStringWhat(headers, "presence", which)
if err != nil {
return "", err
}
if presence != "" && !strutil.ListContains(valid, presence) {
return "", fmt.Errorf("presence %s must be one of %s", which, strings.Join(valid, "|"))
}
return Presence(presence), nil
}
// ValidationSetSnap holds the details about a snap constrained by a validation-set assertion.
type ValidationSetSnap struct {
Name string
SnapID string
Presence Presence
Revision int
Components map[string]ValidationSetComponent
}
type ValidationSetComponent struct {
Presence Presence
Revision int
}
// SnapName implements naming.SnapRef.
func (s *ValidationSetSnap) SnapName() string {
return s.Name
}
// ID implements naming.SnapRef.
func (s *ValidationSetSnap) ID() string {
return s.SnapID
}
func checkValidationSetSnap(snap map[string]interface{}) (*ValidationSetSnap, error) {
snapName, err := checkNotEmptyStringWhat(snap, "name", "of snap")
if err != nil {
return nil, err
}
if err := naming.ValidateSnap(snapName); err != nil {
return nil, fmt.Errorf("invalid snap name %q", snapName)
}
what := fmt.Sprintf("of snap %q", snapName)
snapID, err := checkStringMatchesWhat(snap, "id", what, naming.ValidSnapID)
if err != nil {
return nil, err
}
presence, err := checkOptionalPresence(snap, what, validValidationSetSnapPresences)
if err != nil {
return nil, err
}
var snapRevision int
if _, ok := snap["revision"]; ok {
var err error
snapRevision, err = checkSnapRevisionWhat(snap, "revision", what)
if err != nil {
return nil, err
}
}
if snapRevision != 0 && presence == PresenceInvalid {
return nil, fmt.Errorf(`cannot specify revision %s at the same time as stating its presence is invalid`, what)
}
components, err := checkValidationSetComponents(snapName, snap, snapRevision)
if err != nil {
return nil, err
}
return &ValidationSetSnap{
Name: snapName,
SnapID: snapID,
Presence: presence,
Revision: snapRevision,
Components: components,
}, nil
}
func checkValidationSetComponents(snapName string, snap map[string]interface{}, snapRevision int) (map[string]ValidationSetComponent, error) {
mapping, err := checkMapWhat(snap, "components", fmt.Sprintf("of snap %q", snapName))
if err != nil {
return nil, errors.New(`"components" field in "snaps" header must be a map`)
}
if len(mapping) == 0 {
return nil, nil
}
components := make(map[string]ValidationSetComponent, len(mapping))
for name, comp := range mapping {
var parsed map[string]interface{}
switch c := comp.(type) {
case map[string]interface{}:
parsed = c
case string:
parsed = map[string]interface{}{"presence": c}
default:
return nil, errors.New(`each field in "components" map must be either a map or a string`)
}
component, err := checkValidationSetComponent(name, parsed, snapName, snapRevision)
if err != nil {
return nil, err
}
components[name] = component
}
return components, nil
}
func checkValidationSetComponent(compName string, comp map[string]interface{}, snapName string, snapRevision int) (ValidationSetComponent, error) {
if err := naming.ValidateSnap(compName); err != nil {
return ValidationSetComponent{}, fmt.Errorf("invalid component name %q", compName)
}
what := fmt.Sprintf("of component %q", naming.NewComponentRef(snapName, compName))
presence, err := checkPresence(comp, what, validValidationSetSnapPresences)
if err != nil {
return ValidationSetComponent{}, err
}
revision, err := checkOptionalSnapRevisionWhat(comp, "revision", what)
if err != nil {
return ValidationSetComponent{}, err
}
if revision != 0 && presence == PresenceInvalid {
return ValidationSetComponent{}, fmt.Errorf("cannot specify component revision %s at the same time as stating its presence is invalid", what)
}
if snapRevision != 0 && revision == 0 && presence != PresenceInvalid {
return ValidationSetComponent{}, fmt.Errorf("must specify revision %s since its associated snap specifies a revision", what)
}
if snapRevision == 0 && revision != 0 {
return ValidationSetComponent{}, fmt.Errorf("cannot specify revision %s if its associated snap does not specify a revision", what)
}
return ValidationSetComponent{
Presence: presence,
Revision: revision,
}, nil
}
func checkValidationSetSnaps(snapList interface{}) ([]*ValidationSetSnap, error) {
const wrongHeaderType = `"snaps" header must be a list of maps`
entries, ok := snapList.([]interface{})
if !ok {
return nil, errors.New(wrongHeaderType)
}
seen := make(map[string]bool, len(entries))
seenIDs := make(map[string]string, len(entries))
snaps := make([]*ValidationSetSnap, 0, len(entries))
for _, entry := range entries {
snap, ok := entry.(map[string]interface{})
if !ok {
return nil, errors.New(wrongHeaderType)
}
valSetSnap, err := checkValidationSetSnap(snap)
if err != nil {
return nil, err
}
if seen[valSetSnap.Name] {
return nil, fmt.Errorf("cannot list the same snap %q multiple times", valSetSnap.Name)
}
seen[valSetSnap.Name] = true
snapID := valSetSnap.SnapID
if underName := seenIDs[snapID]; underName != "" {
return nil, fmt.Errorf("cannot specify the same snap id %q multiple times, specified for snaps %q and %q", snapID, underName, valSetSnap.Name)
}
seenIDs[snapID] = valSetSnap.Name
if valSetSnap.Presence == "" {
valSetSnap.Presence = PresenceRequired
}
snaps = append(snaps, valSetSnap)
}
return snaps, nil
}
// ValidationSet holds a validation-set assertion, which is a
// statement by an account about a set snaps and possibly revisions
// for which an extrinsic/implied property is valid (e.g. they work
// well together). validation-sets are organized in sequences under a
// name.
type ValidationSet struct {
assertionBase
seq int
snaps []*ValidationSetSnap
timestamp time.Time
}
// SequenceKey returns the sequence key for this validation set.
func (vs *ValidationSet) SequenceKey() string {
return vsSequenceKey(vs.Series(), vs.AccountID(), vs.Name())
}
func vsSequenceKey(series, accountID, name string) string {
return fmt.Sprintf("%s/%s/%s", series, accountID, name)
}
// Series returns the series for which the snap in the set are declared.
func (vs *ValidationSet) Series() string {
return vs.HeaderString("series")
}
// AccountID returns the identifier of the account that signed this assertion.
func (vs *ValidationSet) AccountID() string {
return vs.HeaderString("account-id")
}
// Name returns the name under which the validation-set is organized.
func (vs *ValidationSet) Name() string {
return vs.HeaderString("name")
}
// Sequence returns the sequential number of the validation-set in its
// named sequence.
func (vs *ValidationSet) Sequence() int {
return vs.seq
}
// Snaps returns the constrained snaps by the validation-set.
func (vs *ValidationSet) Snaps() []*ValidationSetSnap {
return vs.snaps
}
// Timestamp returns the time when the validation-set was issued.
func (vs *ValidationSet) Timestamp() time.Time {
return vs.timestamp
}
func checkSequence(headers map[string]interface{}, name string) (int, error) {
seqnum, err := checkInt(headers, name)
if err != nil {
return -1, err
}
if seqnum < 1 {
return -1, fmt.Errorf("%q must be >=1: %v", name, seqnum)
}
return seqnum, nil
}
var (
validValidationSetName = regexp.MustCompile("^[a-z0-9](?:-?[a-z0-9])*$")
)
func assembleValidationSet(assert assertionBase) (Assertion, error) {
authorityID := assert.AuthorityID()
accountID := assert.HeaderString("account-id")
if accountID != authorityID {
return nil, fmt.Errorf("authority-id and account-id must match, validation-set assertions are expected to be signed by the issuer account: %q != %q", authorityID, accountID)
}
_, err := checkStringMatches(assert.headers, "name", validValidationSetName)
if err != nil {
return nil, err
}
seq, err := checkSequence(assert.headers, "sequence")
if err != nil {
return nil, err
}
snapList, ok := assert.headers["snaps"]
if !ok {
return nil, fmt.Errorf(`"snaps" header is mandatory`)
}
snaps, err := checkValidationSetSnaps(snapList)
if err != nil {
return nil, err
}
timestamp, err := checkRFC3339Date(assert.headers, "timestamp")
if err != nil {
return nil, err
}
return &ValidationSet{
assertionBase: assert,
seq: seq,
snaps: snaps,
timestamp: timestamp,
}, nil
}
func IsValidValidationSetName(name string) bool {
return validValidationSetName.MatchString(name)
}