-
Notifications
You must be signed in to change notification settings - Fork 562
/
validation_set.go
275 lines (227 loc) · 7.21 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
// -*- 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 (
"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 checkPresence(snap map[string]interface{}, which string, valid []string) (Presence, error) {
presence, err := checkOptionalStringWhat(snap, "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
}
// ValidationSetSnap holds the details about a snap constrained by a validation-set assertion.
type ValidationSetSnap struct {
Name string
SnapID string
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) {
name, err := checkNotEmptyStringWhat(snap, "name", "of snap")
if err != nil {
return nil, err
}
if err := naming.ValidateSnap(name); err != nil {
return nil, fmt.Errorf("invalid snap name %q", name)
}
what := fmt.Sprintf("of snap %q", name)
snapID, err := checkStringMatchesWhat(snap, "id", what, naming.ValidSnapID)
if err != nil {
return nil, err
}
presence, err := checkPresence(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)
}
return &ValidationSetSnap{
Name: name,
SnapID: snapID,
Presence: presence,
Revision: snapRevision,
}, 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, fmt.Errorf(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, fmt.Errorf(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)
}