-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathsystem_user_test.go
308 lines (266 loc) · 13.6 KB
/
system_user_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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 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 (
"fmt"
"strings"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
)
var (
_ = Suite(&systemUserSuite{})
)
type systemUserSuite struct {
until time.Time
untilLine string
since time.Time
sinceLine string
userPresenceLine string
formatLine string
modelsLine string
systemUserStr string
}
const systemUserExample = "type: system-user\n" +
"FORMATLINE\n" +
"authority-id: canonical\n" +
"brand-id: canonical\n" +
"email: [email protected]\n" +
"series:\n" +
" - 16\n" +
"MODELSLINE\n" +
"name: Nice Guy\n" +
"username: guy\n" +
"password: $6$salt$hash\n" +
"ssh-keys:\n" +
" - ssh-rsa AAAABcdefg\n" +
"SINCELINE\n" +
"UNTILLINE\n" +
"USERVALIDFOR\n" +
"body-length: 0\n" +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij" +
"\n\n" +
"AXNpZw=="
func (s *systemUserSuite) SetUpTest(c *C) {
s.since = time.Now().Truncate(time.Second)
s.sinceLine = fmt.Sprintf("since: %s\n", s.since.Format(time.RFC3339))
s.until = time.Now().AddDate(0, 1, 0).Truncate(time.Second)
s.untilLine = fmt.Sprintf("until: %s\n", s.until.Format(time.RFC3339))
s.modelsLine = "models:\n - frobinator\n"
s.formatLine = "format: 0\n"
s.userPresenceLine = "user-presence: \n"
s.systemUserStr = strings.Replace(systemUserExample, "UNTILLINE\n", s.untilLine, 1)
s.systemUserStr = strings.Replace(s.systemUserStr, "SINCELINE\n", s.sinceLine, 1)
s.systemUserStr = strings.Replace(s.systemUserStr, "MODELSLINE\n", s.modelsLine, 1)
s.systemUserStr = strings.Replace(s.systemUserStr, "FORMATLINE\n", s.formatLine, 1)
s.systemUserStr = strings.Replace(s.systemUserStr, "USERVALIDFOR\n", s.userPresenceLine, 1)
}
func (s *systemUserSuite) TestDecodeOK(c *C) {
a, err := asserts.Decode([]byte(s.systemUserStr))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.SystemUserType)
systemUser := a.(*asserts.SystemUser)
c.Check(systemUser.BrandID(), Equals, "canonical")
c.Check(systemUser.Email(), Equals, "[email protected]")
c.Check(systemUser.Series(), DeepEquals, []string{"16"})
c.Check(systemUser.Models(), DeepEquals, []string{"frobinator"})
c.Check(systemUser.Name(), Equals, "Nice Guy")
c.Check(systemUser.Username(), Equals, "guy")
c.Check(systemUser.Password(), Equals, "$6$salt$hash")
c.Check(systemUser.SSHKeys(), DeepEquals, []string{"ssh-rsa AAAABcdefg"})
c.Check(systemUser.Since().Equal(s.since), Equals, true)
c.Check(systemUser.Until().Equal(s.until), Equals, true)
c.Check(systemUser.UserExpiration().IsZero(), Equals, true)
}
func (s *systemUserSuite) TestDecodePasswd(c *C) {
validTests := []struct{ original, valid string }{
{"password: $6$salt$hash\n", "password: $6$rounds=9999$salt$hash\n"},
{"password: $6$salt$hash\n", ""},
}
for _, test := range validTests {
valid := strings.Replace(s.systemUserStr, test.original, test.valid, 1)
_, err := asserts.Decode([]byte(valid))
c.Check(err, IsNil)
}
}
func (s *systemUserSuite) TestDecodeForcePasswdChange(c *C) {
old := "password: $6$salt$hash\n"
new := "password: $6$salt$hash\nforce-password-change: true\n"
valid := strings.Replace(s.systemUserStr, old, new, 1)
a, err := asserts.Decode([]byte(valid))
c.Check(err, IsNil)
systemUser := a.(*asserts.SystemUser)
c.Check(systemUser.ForcePasswordChange(), Equals, true)
}
func (s *systemUserSuite) TestValidAt(c *C) {
a, err := asserts.Decode([]byte(s.systemUserStr))
c.Assert(err, IsNil)
su := a.(*asserts.SystemUser)
c.Check(su.ValidAt(su.Since()), Equals, true)
c.Check(su.ValidAt(su.Since().AddDate(0, 0, -1)), Equals, false)
c.Check(su.ValidAt(su.Since().AddDate(0, 0, 1)), Equals, true)
c.Check(su.ValidAt(su.Until()), Equals, false)
c.Check(su.ValidAt(su.Until().AddDate(0, -1, 0)), Equals, true)
c.Check(su.ValidAt(su.Until().AddDate(0, 1, 0)), Equals, false)
}
func (s *systemUserSuite) TestValidAtRevoked(c *C) {
// With since == until, i.e. system-user has been revoked.
revoked := strings.Replace(s.systemUserStr, s.sinceLine, fmt.Sprintf("since: %s\n", s.until.Format(time.RFC3339)), 1)
a, err := asserts.Decode([]byte(revoked))
c.Assert(err, IsNil)
su := a.(*asserts.SystemUser)
c.Check(su.ValidAt(su.Since()), Equals, false)
c.Check(su.ValidAt(su.Since().AddDate(0, 0, -1)), Equals, false)
c.Check(su.ValidAt(su.Since().AddDate(0, 0, 1)), Equals, false)
c.Check(su.ValidAt(su.Until()), Equals, false)
c.Check(su.ValidAt(su.Until().AddDate(0, -1, 0)), Equals, false)
c.Check(su.ValidAt(su.Until().AddDate(0, 1, 0)), Equals, false)
}
const (
systemUserErrPrefix = "assertion system-user: "
)
func (s *systemUserSuite) TestDecodeInvalid(c *C) {
invalidTests := []struct{ original, invalid, expectedErr string }{
{"brand-id: canonical\n", "", `"brand-id" header is mandatory`},
{"brand-id: canonical\n", "brand-id: \n", `"brand-id" header should not be empty`},
{"email: [email protected]\n", "", `"email" header is mandatory`},
{"email: [email protected]\n", "email: \n", `"email" header should not be empty`},
{"email: [email protected]\n", "email: <alice!example.com>\n", `"email" header must be a RFC 5322 compliant email address: mail: missing @ in addr-spec`},
{"email: [email protected]\n", "email: no-mail\n", `"email" header must be a RFC 5322 compliant email address:.*`},
{"series:\n - 16\n", "series: \n", `"series" header must be a list of strings`},
{"series:\n - 16\n", "series: something\n", `"series" header must be a list of strings`},
{"models:\n - frobinator\n", "models: \n", `"models" header must be a list of strings`},
{"models:\n - frobinator\n", "models: something\n", `"models" header must be a list of strings`},
{"ssh-keys:\n - ssh-rsa AAAABcdefg\n", "ssh-keys: \n", `"ssh-keys" header must be a list of strings`},
{"ssh-keys:\n - ssh-rsa AAAABcdefg\n", "ssh-keys: something\n", `"ssh-keys" header must be a list of strings`},
{"name: Nice Guy\n", "name:\n - foo\n", `"name" header must be a string`},
{"username: guy\n", "username:\n - foo\n", `"username" header must be a string`},
{"username: guy\n", "username: bäää\n", `"username" header contains invalid characters: "bäää"`},
{"username: guy\n", "", `"username" header is mandatory`},
{"password: $6$salt$hash\n", "password:\n - foo\n", `"password" header must be a string`},
{"password: $6$salt$hash\n", "password: cleartext\n", `"password" header invalid: hashed password must be of the form "\$integer-id\$salt\$hash", see crypt\(3\)`},
{"password: $6$salt$hash\n", "password: $ni!$salt$hash\n", `"password" header must start with "\$integer-id\$", got "ni!"`},
{"password: $6$salt$hash\n", "password: $3$salt$hash\n", `"password" header only supports \$id\$ values of 6 \(sha512crypt\) or higher`},
{"password: $6$salt$hash\n", "password: $7$invalid-salt$hash\n", `"password" header has invalid chars in salt "invalid-salt"`},
{"password: $6$salt$hash\n", "password: $8$salt$invalid-hash\n", `"password" header has invalid chars in hash "invalid-hash"`},
{"password: $6$salt$hash\n", "password: $8$rounds=9999$hash\n", `"password" header invalid: missing hash field`},
{"password: $6$salt$hash\n", "password: $8$rounds=xxx$salt$hash\n", `"password" header has invalid number of rounds:.*`},
{"password: $6$salt$hash\n", "password: $8$rounds=1$salt$hash\n", `"password" header rounds parameter out of bounds: 1`},
{"password: $6$salt$hash\n", "password: $8$rounds=1999999999$salt$hash\n", `"password" header rounds parameter out of bounds: 1999999999`},
{"password: $6$salt$hash\n", "force-password-change: true\n", `cannot use "force-password-change" with an empty "password"`},
{"password: $6$salt$hash\n", "password: $6$salt$hash\nforce-password-change: xxx\n", `"force-password-change" header must be 'true' or 'false'`},
{s.sinceLine, "since: \n", `"since" header should not be empty`},
{s.sinceLine, "since: 12:30\n", `"since" header is not a RFC3339 date: .*`},
{s.untilLine, "until: \n", `"until" header should not be empty`},
{s.untilLine, "until: 12:30\n", `"until" header is not a RFC3339 date: .*`},
{s.untilLine, "until: 1002-11-01T22:08:41+00:00\n", `'until' time cannot be before 'since' time`},
{s.modelsLine, s.modelsLine + "serials: \n", `"serials" header must be a list of strings`},
{s.modelsLine, s.modelsLine + "serials: something\n", `"serials" header must be a list of strings`},
{s.modelsLine, s.modelsLine + "serials:\n - 7c7f435d-ed28-4281-bd77-e271e0846904\n", `the "serials" header is only supported for format 1 or greater`},
{s.userPresenceLine, "user-presence: until-expiration\n", `the "user-presence" header is only supported for format 2 or greater`},
}
for _, test := range invalidTests {
invalid := strings.Replace(s.systemUserStr, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, systemUserErrPrefix+test.expectedErr)
}
}
func (s *systemUserSuite) TestUntilNoModels(c *C) {
// no models is good for <1y
su := strings.Replace(s.systemUserStr, s.modelsLine, "", -1)
_, err := asserts.Decode([]byte(su))
c.Check(err, IsNil)
// but invalid for more than one year
oneYearPlusOne := time.Now().AddDate(1, 0, 1).Truncate(time.Second)
su = strings.Replace(su, s.untilLine, fmt.Sprintf("until: %s\n", oneYearPlusOne.Format(time.RFC3339)), -1)
_, err = asserts.Decode([]byte(su))
c.Check(err, ErrorMatches, systemUserErrPrefix+"'until' time cannot be more than 365 days in the future when no models are specified")
}
func (s *systemUserSuite) TestUntilWithModels(c *C) {
// with models it can be valid forever
oneYearPlusOne := time.Now().AddDate(10, 0, 1).Truncate(time.Second)
su := strings.Replace(s.systemUserStr, s.untilLine, fmt.Sprintf("until: %s\n", oneYearPlusOne.Format(time.RFC3339)), -1)
_, err := asserts.Decode([]byte(su))
c.Check(err, IsNil)
}
// The following tests deal with "format: 1" which adds support for
// tying system-user assertions to device serials.
var serialsLine = "serials:\n - 7c7f435d-ed28-4281-bd77-e271e0846904\n"
func (s *systemUserSuite) TestDecodeInvalidFormat1Serials(c *C) {
s.systemUserStr = strings.Replace(s.systemUserStr, s.formatLine, "format: 1\n", 1)
serialWithMultipleModels := "models:\n - m1\n - m2\n" + serialsLine
invalidTests := []struct{ original, invalid, expectedErr string }{
{s.modelsLine, serialWithMultipleModels, `in the presence of the "serials" header "models" must specify exactly one model`},
}
for _, test := range invalidTests {
invalid := strings.Replace(s.systemUserStr, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, systemUserErrPrefix+test.expectedErr)
}
}
func (s *systemUserSuite) TestDecodeOKFormat1Serials(c *C) {
s.systemUserStr = strings.Replace(s.systemUserStr, s.formatLine, "format: 1\n", 1)
s.systemUserStr = strings.Replace(s.systemUserStr, s.modelsLine, s.modelsLine+serialsLine, 1)
a, err := asserts.Decode([]byte(s.systemUserStr))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.SystemUserType)
systemUser := a.(*asserts.SystemUser)
// just for double checking, already covered by "format: 0" tests
c.Check(systemUser.BrandID(), Equals, "canonical")
// new in "format: 1"
c.Check(systemUser.Serials(), DeepEquals, []string{"7c7f435d-ed28-4281-bd77-e271e0846904"})
}
func (s *systemUserSuite) TestDecodeInvalidFormat2UserPresence(c *C) {
s.systemUserStr = strings.Replace(s.systemUserStr, s.formatLine, "format: 2\n", 1)
invalidTests := []struct{ original, invalid, expectedErr string }{
{s.userPresenceLine, "user-presence: tomorrow\n", `invalid "user-presence" header, only explicit valid value is "until-expiration": "tomorrow"`},
{s.userPresenceLine, "user-presence: 0\n", `invalid "user-presence" header, only explicit valid value is "until-expiration": "0"`},
}
for _, test := range invalidTests {
invalid := strings.Replace(s.systemUserStr, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, systemUserErrPrefix+test.expectedErr)
}
}
func (s *systemUserSuite) TestDecodeOKFormat2UserPresence(c *C) {
s.systemUserStr = strings.Replace(s.systemUserStr, s.formatLine, "format: 2\n", 1)
s.systemUserStr = strings.Replace(s.systemUserStr, s.userPresenceLine, "user-presence: until-expiration\n", 1)
a, err := asserts.Decode([]byte(s.systemUserStr))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.SystemUserType)
systemUser := a.(*asserts.SystemUser)
// new in "format: 2"
c.Check(systemUser.UserExpiration().Equal(systemUser.Until()), Equals, true)
}
func (s *systemUserSuite) TestSuggestedFormat(c *C) {
fmtnum, err := asserts.SuggestFormat(asserts.SystemUserType, nil, nil)
c.Assert(err, IsNil)
c.Check(fmtnum, Equals, 0)
headers := map[string]interface{}{
"serials": []interface{}{"serialserial"},
}
fmtnum, err = asserts.SuggestFormat(asserts.SystemUserType, headers, nil)
c.Assert(err, IsNil)
c.Check(fmtnum, Equals, 1)
headers = map[string]interface{}{
"user-presence": "until-expiration",
}
fmtnum, err = asserts.SuggestFormat(asserts.SystemUserType, headers, nil)
c.Assert(err, IsNil)
c.Check(fmtnum, Equals, 2)
}