-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathconfdb_test.go
435 lines (388 loc) · 13.6 KB
/
confdb_test.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2024 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_test
import (
"path/filepath"
"strings"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
)
type confdbSuite struct {
ts time.Time
tsLine string
}
var _ = Suite(&confdbSuite{})
func (s *confdbSuite) SetUpSuite(c *C) {
s.ts = time.Now().Truncate(time.Second).UTC()
s.tsLine = "timestamp: " + s.ts.Format(time.RFC3339) + "\n"
}
const (
confdbExample = `type: confdb
authority-id: brand-id1
account-id: brand-id1
name: my-network
summary: confdb description
views:
wifi-setup:
rules:
-
request: ssids
storage: wifi.ssids
-
request: ssid
storage: wifi.ssid
access: read-write
-
request: password
storage: wifi.psk
access: write
-
request: status
storage: wifi.status
access: read
-
request: private.{key}
storage: wifi.{key}
` + "TSLINE" +
"sign-key-sha3-384: jv8_jihiizjvco9m55ppdqsdwuvuhfdibjus-3vw7f_idjix7ffn5qmxb21zquij\n" +
"body-length: 115" +
"\n\n" +
schema +
"\n\n" +
"AXNpZw=="
)
const schema = `{
"storage": {
"schema": {
"wifi": {
"type": "map",
"values": "any"
}
}
}
}`
func (s *confdbSuite) TestDecodeOK(c *C) {
encoded := strings.Replace(confdbExample, "TSLINE", s.tsLine, 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a, NotNil)
c.Check(a.Type(), Equals, asserts.ConfdbType)
ar := a.(*asserts.Confdb)
c.Check(ar.AuthorityID(), Equals, "brand-id1")
c.Check(ar.AccountID(), Equals, "brand-id1")
c.Check(ar.Name(), Equals, "my-network")
confdb := ar.Confdb()
c.Assert(confdb, NotNil)
c.Check(confdb.View("wifi-setup"), NotNil)
}
func (s *confdbSuite) TestDecodeInvalid(c *C) {
const validationSetErrPrefix = "assertion confdb: "
encoded := strings.Replace(confdbExample, "TSLINE", s.tsLine, 1)
viewsStanza := encoded[strings.Index(encoded, "views:") : strings.Index(encoded, "timestamp:")+1]
body := encoded[strings.Index(encoded, "body-length:"):strings.Index(encoded, "\n\nAXN")]
invalidTests := []struct{ original, invalid, expectedErr string }{
{"account-id: brand-id1\n", "", `"account-id" header is mandatory`},
{"account-id: brand-id1\n", "account-id: \n", `"account-id" header should not be empty`},
{"account-id: brand-id1\n", "account-id: random\n", `authority-id and account-id must match, confdb assertions are expected to be signed by the issuer account: "brand-id1" != "random"`},
{"name: my-network\n", "", `"name" header is mandatory`},
{"name: my-network\n", "name: \n", `"name" header should not be empty`},
{"name: my-network\n", "name: my/network\n", `"name" primary key header cannot contain '/'`},
{"name: my-network\n", "name: my+network\n", `"name" header contains invalid characters: "my\+network"`},
{s.tsLine, "", `"timestamp" header is mandatory`},
{viewsStanza, "views: foo\n", `"views" header must be a map`},
{viewsStanza, "", `"views" stanza is mandatory`},
{"read-write", "update", `cannot define view "wifi-setup": cannot create view rule:.*`},
{body, "body-length: 0", `body must contain JSON`},
{body, "body-length: 8\n\n - foo\n", `invalid JSON in body: invalid character ' ' in numeric literal`},
{body, "body-length: 2\n\n{}", `body must contain a "storage" stanza`},
{body, "body-length: 19\n\n{\n \"storage\": {}\n}", `invalid schema: cannot parse top level schema: must have a "schema" constraint`},
{body, "body-length: 4\n\nnull", `body must contain a "storage" stanza`},
{body, "body-length: 54\n\n{\n\t\"storage\": {\n\t\t\"schema\": {\n\t\t\t\"foo\": \"any\"\n\t\t}\n\t}\n}", `JSON in body must be indented with 2 spaces and sort object entries by key`},
{body, `body-length: 79
{
"storage": {
"schema": {
"c": "any",
"a": "any"
}
}
}`, `JSON in body must be indented with 2 spaces and sort object entries by key`},
}
for i, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, validationSetErrPrefix+test.expectedErr, Commentf("test %d/%d failed", i+1, len(invalidTests)))
}
}
func (s *confdbSuite) TestAssembleAndSignChecksSchemaFormatOK(c *C) {
headers := map[string]interface{}{
"authority-id": "brand-id1",
"account-id": "brand-id1",
"name": "my-network",
"views": map[string]interface{}{
"foo": map[string]interface{}{
"rules": []interface{}{
map[string]interface{}{"request": "wifi", "storage": "wifi"},
},
},
},
"body-length": "60",
"timestamp": s.ts.Format(time.RFC3339),
}
schema := `{
"storage": {
"schema": {
"wifi": {
"type": "map",
"values": "any"
}
}
}
}`
acct1, err := asserts.AssembleAndSignInTest(asserts.ConfdbType, headers, []byte(schema), testPrivKey0)
c.Assert(err, IsNil)
c.Assert(string(acct1.Body()), Equals, schema)
}
func (s *confdbSuite) TestAssembleAndSignChecksSchemaFormatFail(c *C) {
headers := map[string]interface{}{
"authority-id": "brand-id1",
"account-id": "brand-id1",
"name": "my-network",
"views": map[string]interface{}{
"foo": map[string]interface{}{
"rules": []interface{}{
map[string]interface{}{"request": "wifi", "storage": "wifi"},
},
},
},
"body-length": "60",
"timestamp": s.ts.Format(time.RFC3339),
}
schema := `{ "storage": { "schema": { "foo": "any" } } }`
_, err := asserts.AssembleAndSignInTest(asserts.ConfdbType, headers, []byte(schema), testPrivKey0)
c.Assert(err, ErrorMatches, `assertion confdb: JSON in body must be indented with 2 spaces and sort object entries by key`)
}
type confdbCtrlSuite struct {
db *asserts.Database
}
var _ = Suite(&confdbCtrlSuite{})
const (
confdbControlExample = `type: confdb-control
brand-id: generic
model: generic-classic
serial: 03961d5d-26e5-443f-838d-6db046126bea
groups:
-
operators:
- john
authentications:
- operator-key
views:
- canonical/network/control-device
- canonical/network/observe-device
-
operators:
- john
authentications:
- store
views:
- canonical/network/control-interfaces
-
operators:
- jane
authentications:
- store
- operator-key
views:
- canonical/network/observe-interfaces
-
operators:
- alice
- bob
authentications:
- store
- operator-key
views:
- canonical/network/observe-device
- canonical/network/control-interfaces
sign-key-sha3-384: t9yuKGLyiezBq_PXMJZsGdkTukmL7MgrgqXAlxxiZF4TYryOjZcy48nnjDmEHQDp
AXNpZw==`
)
func (s *confdbCtrlSuite) SetUpTest(c *C) {
topDir := filepath.Join(c.MkDir(), "asserts-db")
bs, err := asserts.OpenFSBackstore(topDir)
c.Assert(err, IsNil)
cfg := &asserts.DatabaseConfig{
Backstore: bs,
Trusted: []asserts.Assertion{
asserts.BootstrapAccountForTest("canonical"),
asserts.BootstrapAccountKeyForTest("canonical", testPrivKey0.PublicKey()),
},
}
db, err := asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
s.db = db
}
func (s *confdbCtrlSuite) addSerial(c *C) {
pubKey := testPrivKey0.PublicKey()
encodedPubKey, err := asserts.EncodePublicKey(pubKey)
c.Assert(err, IsNil)
serial, err := asserts.AssembleAndSignInTest(asserts.SerialType, map[string]interface{}{
"authority-id": "canonical",
"brand-id": "canonical",
"model": "pc",
"serial": "42",
"device-key": string(encodedPubKey),
"device-key-sha3-384": pubKey.ID(),
"timestamp": time.Now().Format(time.RFC3339),
}, nil, testPrivKey0)
c.Assert(err, IsNil)
err = s.db.Add(serial)
c.Assert(err, IsNil)
}
func (s *confdbCtrlSuite) TestDecodeOK(c *C) {
encoded := confdbControlExample
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Assert(a, NotNil)
c.Assert(a.Type(), Equals, asserts.ConfdbControlType)
cc := a.(*asserts.ConfdbControl)
c.Assert(cc.BrandID(), Equals, "generic")
c.Assert(cc.Model(), Equals, "generic-classic")
c.Assert(cc.Serial(), Equals, "03961d5d-26e5-443f-838d-6db046126bea")
c.Assert(cc.AuthorityID(), Equals, "")
ctrl := cc.Control()
delegated, _ := ctrl.IsDelegated("john", "canonical/network/control-device", []string{"operator-key"})
c.Check(delegated, Equals, true)
delegated, _ = ctrl.IsDelegated("john", "canonical/network/observe-device", []string{"operator-key"})
c.Check(delegated, Equals, true)
delegated, _ = ctrl.IsDelegated("john", "canonical/network/control-interfaces", []string{"store"})
c.Check(delegated, Equals, true)
delegated, _ = ctrl.IsDelegated("jane", "canonical/network/observe-interfaces", []string{"store", "operator-key"})
c.Check(delegated, Equals, true)
}
func (s *confdbCtrlSuite) TestDecodeInvalid(c *C) {
encoded := confdbControlExample
const validationSetErrPrefix = "assertion confdb-control: "
invalidTests := []struct{ original, invalid, expectedErr string }{
{"brand-id: generic\n", "", `"brand-id" header is mandatory`},
{"brand-id: generic\n", "brand-id: \n", `"brand-id" header should not be empty`},
{"brand-id: generic\n", "brand-id: 456#\n", `"brand-id" header contains invalid characters: "456#"`},
{"model: generic-classic\n", "", `"model" header is mandatory`},
{"model: generic-classic\n", "model: \n", `"model" header should not be empty`},
{"model: generic-classic\n", "model: #\n", `"model" header contains invalid characters: "#"`},
{"serial: 03961d5d-26e5-443f-838d-6db046126bea\n", "", `"serial" header is mandatory`},
{"serial: 03961d5d-26e5-443f-838d-6db046126bea\n", "serial: \n", `"serial" header should not be empty`},
{"groups:", "groups: foo\nviews:", `"groups" header must be a list`},
{"groups:", "views:", `"groups" stanza is mandatory`},
{"groups:", "groups:\n - bar", `cannot parse group at position 1: must be a map`},
{" operators:\n - jane\n", "", `cannot parse group at position 3: "operators" must be provided`},
{
" operators:\n - jane\n",
" operators: abcd\n", `cannot parse group at position 3: "operators" field must be a list of strings`,
},
{
" - jane",
" - @op",
`cannot parse group at position 3: invalid operator ID: @op`,
},
{
" authentications:\n - store",
" authentications: abcd",
`cannot parse group at position 2: "authentications" field must be a list of strings`,
},
{
" authentications:\n - store",
" foo: bar",
`cannot parse group at position 2: "authentications" must be provided`,
},
{
" views:\n - canonical/network/control-interfaces",
" views: abcd",
`cannot parse group at position 2: "views" field must be a list of strings`,
},
{
" views:\n - canonical/network/control-interfaces",
" foo: bar",
`cannot parse group at position 2: "views" must be provided`,
},
{
" - operator-key",
" - foo-bar",
"cannot parse group at position 1: cannot delegate: invalid authentication method: foo-bar",
},
{
"canonical/network/control-interfaces",
"canonical",
`cannot parse group at position 2: cannot delegate: view "canonical" must be in the format account/confdb/view`,
},
}
for i, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Assert(err, ErrorMatches, validationSetErrPrefix+test.expectedErr, Commentf("test %d/%d failed", i+1, len(invalidTests)))
}
}
func (s *confdbCtrlSuite) TestPrerequisites(c *C) {
a, err := asserts.Decode([]byte(confdbControlExample))
c.Assert(err, IsNil)
prereqs := a.Prerequisites()
c.Assert(prereqs, HasLen, 1)
c.Check(prereqs[0], DeepEquals, &asserts.Ref{
Type: asserts.SerialType,
PrimaryKey: []string{"generic", "generic-classic", "03961d5d-26e5-443f-838d-6db046126bea"},
})
}
func (s *confdbCtrlSuite) TestAckAssertionNoSerial(c *C) {
headers := map[string]interface{}{
"brand-id": "canonical", "model": "pc", "serial": "42", "groups": []interface{}{},
}
a, err := asserts.AssembleAndSignInTest(asserts.ConfdbControlType, headers, nil, testPrivKey0)
c.Assert(err, IsNil)
err = s.db.Add(a)
c.Assert(
err,
ErrorMatches,
`cannot check no-authority assertion type "confdb-control": cannot find matching device serial assertion: .* not found`,
)
}
func (s *confdbCtrlSuite) TestAckAssertionKeysMismatch(c *C) {
s.addSerial(c)
headers := map[string]interface{}{
"brand-id": "canonical", "model": "pc", "serial": "42", "groups": []interface{}{},
}
a, err := asserts.AssembleAndSignInTest(asserts.ConfdbControlType, headers, nil, testPrivKey2)
c.Assert(err, IsNil)
err = s.db.Add(a)
c.Assert(
err,
ErrorMatches,
`cannot check no-authority assertion type "confdb-control": confdb-control's signing key doesn't match the device key`,
)
}
func (s *confdbCtrlSuite) TestAckAssertionOK(c *C) {
s.addSerial(c)
headers := map[string]interface{}{
"brand-id": "canonical", "model": "pc", "serial": "42", "groups": []interface{}{},
}
a, err := asserts.AssembleAndSignInTest(asserts.ConfdbControlType, headers, nil, testPrivKey0)
c.Assert(err, IsNil)
err = s.db.Add(a)
c.Assert(err, IsNil)
}