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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2025 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"
"github.com/snapcore/snapd/asserts"
. "gopkg.in/check.v1"
)
type requestMessageSuite struct{}
var _ = Suite(&requestMessageSuite{})
const (
requestMessageExample = `type: request-message
authority-id: store
account-id: account-id-1
message-id: someId
message-kind: confdb
devices:
- 03961d5d-26e5.generic-classic.generic
- 66:c5:d7:14:84:f8.rpi5.acme-corp
- some.legacy.serial.model.brand
assumes:
- snapd2.70
valid-since: 2025-01-08T13:31:20+00:00
valid-until: 2025-01-15T13:31:20+00:00
timestamp: 2025-01-08T13:31:20+00:00
sign-key-sha3-384: t9yuKGLyiezBq_PXMJZsGdkTukmL7MgrgqXAlxxiZF4TYryOjZcy48nnjDmEHQDp
body-length: 112
%s
AXNpZw==`
requestBodyExample = `{
"action": "get",
"account": "account-id-1",
"view": "network/read-proxy",
"keys": [ "https", "ftp" ]
}`
)
func (s *requestMessageSuite) TestDecodeOK(c *C) {
encoded := fmt.Sprintf(requestMessageExample, requestBodyExample)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a, NotNil)
c.Check(a.Type(), Equals, asserts.RequestMessageType)
req := a.(*asserts.RequestMessage)
c.Check(req.AuthorityID(), Equals, "store")
c.Check(req.AccountID(), Equals, "account-id-1")
c.Check(req.ID(), Equals, "someId")
c.Check(req.SeqNum(), Equals, 0)
c.Check(req.Kind(), Equals, "confdb")
expectedDevices := []asserts.DeviceID{
{"03961d5d-26e5", "generic-classic", "generic"},
{"66:c5:d7:14:84:f8", "rpi5", "acme-corp"},
{"some.legacy.serial", "model", "brand"},
}
c.Check(req.Devices(), DeepEquals, expectedDevices)
c.Check(req.ValidSince().Format(time.RFC3339), Equals, "2025-01-08T13:31:20Z")
c.Check(req.ValidUntil().Format(time.RFC3339), Equals, "2025-01-15T13:31:20Z")
c.Check(req.Assumes(), DeepEquals, []string{"snapd2.70"})
c.Check(string(req.Body()), Equals, requestBodyExample)
}
func (s *requestMessageSuite) TestDecodeSequencedOK(c *C) {
encoded := fmt.Sprintf(requestMessageExample, requestBodyExample)
encoded = strings.Replace(encoded, "message-id: someId", "message-id: someId-4", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
req := a.(*asserts.RequestMessage)
c.Check(req.ID(), Equals, "someId")
c.Check(req.SeqNum(), Equals, 4)
}
func (s *requestMessageSuite) TestDecodeNoAssumesOK(c *C) {
encoded := fmt.Sprintf(requestMessageExample, requestBodyExample)
encoded = strings.Replace(encoded, "assumes:\n - snapd2.70\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
req := a.(*asserts.RequestMessage)
c.Check(req.Assumes(), HasLen, 0)
}
func (s *requestMessageSuite) TestDecodeInvalid(c *C) {
encoded := fmt.Sprintf(requestMessageExample, requestBodyExample)
const errPrefix = "assertion request-message: "
devices := `devices:
- 03961d5d-26e5.generic-classic.generic
- 66:c5:d7:14:84:f8.rpi5.acme-corp
- some.legacy.serial.model.brand
`
invalidTests := []struct{ original, invalid, expectedErr string }{
{"account-id: account-id-1\n", "", `"account-id" header is mandatory`},
{"account-id: account-id-1\n", "account-id: \n", `"account-id" header should not be empty`},
{"account-id: account-id-1\n", "account-id: @9\n", `invalid account id: @9`},
{"message-id: someId\n", "", `"message-id" header is mandatory`},
{"message-id: someId\n", "message-id: \n", `"message-id" header should not be empty`},
{"message-id: someId\n", "message-id: s#ome&Id\n", "invalid message-id: s#ome&Id"},
{"message-id: someId\n", "message-id: someId-\n", "invalid message-id: someId-"},
{"message-id: someId\n", "message-id: someId-abc\n", "invalid message-id: someId-abc"},
{"message-id: someId\n", "message-id: someId-0\n", "invalid message-id: sequence number must be greater than 0"},
{"message-kind: confdb\n", "", `"message-kind" header is mandatory`},
{"message-kind: confdb\n", "message-kind: \n", `"message-kind" header should not be empty`},
{"message-kind: confdb\n", "message-kind: 23#s\n", `"message-kind" header contains invalid characters: "23#s"`},
{devices, "", `"devices" header must not be empty`},
{devices, "devices: \n", `"devices" header must be a list of strings`},
{devices, "devices:\n - ab\n", `cannot parse device at position 1: invalid device id format: expected 3 parts separated by '.', got 1: ab`},
{devices, "devices:\n - c.b.a#3\n", `cannot parse device at position 1: invalid brand-id "a#3" in device id "c.b.a#3"`},
{devices, "devices:\n - y.x3#4.abc\n", `cannot parse device at position 1: invalid model "x3#4" in device id "y.x3#4.abc"`},
{"assumes:\n - snapd2.70\n", "assumes: \n", `"assumes" header must be a list of strings`},
{"assumes:\n - snapd2.70\n", "assumes:\n - x3#4\n", `assumes: invalid features: x3#4`},
{"valid-since: 2025-01-08T13:31:20+00:00\n", "", `"valid-since" header is mandatory`},
{"valid-until: 2025-01-15T13:31:20+00:00\n", "", `"valid-until" header is mandatory`},
{"valid-until: 2025-01-15T13:31:20+00:00\n", "valid-until: 2024-01-15T13:31:20+00:00\n", `'valid-until' time cannot be before 'valid-since' time`},
{"timestamp: 2025-01-08T13:31:20+00:00\n", "", `"timestamp" header is mandatory`},
{"body-length: 112\n\n" + requestBodyExample, "body-length: 0", `body must not be empty`},
}
for i, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Assert(err, ErrorMatches, errPrefix+test.expectedErr, Commentf("test %d/%d failed", i+1, len(invalidTests)))
}
}
type responseMessageSuite struct{}
var _ = Suite(&responseMessageSuite{})
const (
responseMessageExample = `type: response-message
account-id: account-id-1
message-id: someId
device: 03961d5d-26e5.generic-classic.generic
status: success
timestamp: 2025-01-08T13:31:20+00:00
sign-key-sha3-384: t9yuKGLyiezBq_PXMJZsGdkTukmL7MgrgqXAlxxiZF4TYryOjZcy48nnjDmEHQDp
body-length: 96
%s
AXNpZw==`
responseBodyExample = `{
"values": {
"https": "proxy.example.com:8080",
"ftp": "proxy.example.com:8080"
}
}`
)
func (s *responseMessageSuite) TestDecodeOK(c *C) {
encoded := fmt.Sprintf(responseMessageExample, responseBodyExample)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a, NotNil)
c.Check(a.Type(), Equals, asserts.ResponseMessageType)
resp := a.(*asserts.ResponseMessage)
c.Check(resp.AccountID(), Equals, "account-id-1")
c.Check(resp.ID(), Equals, "someId")
c.Check(resp.SeqNum(), Equals, 0)
c.Check(resp.Status(), Equals, asserts.MessageStatus("success"))
expectedDevice := asserts.DeviceID{
Serial: "03961d5d-26e5",
Model: "generic-classic",
BrandID: "generic",
}
c.Check(resp.Device(), DeepEquals, expectedDevice)
c.Check(string(resp.Body()), Equals, responseBodyExample)
}
func (s *requestMessageSuite) TestDeviceIDString(c *C) {
device := asserts.DeviceID{
Serial: "serial-1",
Model: "model-a",
BrandID: "test-brand",
}
c.Check(device.String(), Equals, "serial-1.model-a.test-brand")
}
func (s *responseMessageSuite) TestDecodeSequencedOK(c *C) {
encoded := fmt.Sprintf(responseMessageExample, responseBodyExample)
encoded = strings.Replace(encoded, "message-id: someId", "message-id: someId-4", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
resp := a.(*asserts.ResponseMessage)
c.Check(resp.ID(), Equals, "someId")
c.Check(resp.SeqNum(), Equals, 4)
}
func (s *responseMessageSuite) TestDecodeInvalid(c *C) {
encoded := fmt.Sprintf(responseMessageExample, responseBodyExample)
const errPrefix = "assertion response-message: "
device := "device: 03961d5d-26e5.generic-classic.generic\n"
invalidTests := []struct{ original, invalid, expectedErr string }{
{"account-id: account-id-1\n", "", `"account-id" header is mandatory`},
{"account-id: account-id-1\n", "account-id: \n", `"account-id" header should not be empty`},
{"account-id: account-id-1\n", "account-id: @9\n", `invalid account id: @9`},
{"message-id: someId\n", "", `"message-id" header is mandatory`},
{"message-id: someId\n", "message-id: \n", `"message-id" header should not be empty`},
{"message-id: someId\n", "message-id: s#ome&Id\n", `invalid message-id: s#ome&Id`},
{"message-id: someId\n", "message-id: someId-\n", `invalid message-id: someId-`},
{"message-id: someId\n", "message-id: someId-abc\n", `invalid message-id: someId-abc`},
{device, "", `"device" header is mandatory`},
{device, "device: \n", `"device" header should not be empty`},
{device, "device: ab\n", `invalid device id format: expected 3 parts separated by '.', got 1: ab`},
{device, "device: c.b.a#3\n", `invalid brand-id "a#3" in device id "c.b.a#3"`},
{device, "device: y.x3#4.abc\n", `invalid model "x3#4" in device id "y.x3#4.abc"`},
{"status: success\n", "", `expected "status" to be one of \[success, error, unauthorized, rejected\] but was ""`},
{"status: success\n", "status: \n", `expected "status" to be one of \[success, error, unauthorized, rejected\] but was ""`},
{"status: success\n", "status: invalid\n", `expected "status" to be one of \[success, error, unauthorized, rejected\] but was "invalid"`},
{"timestamp: 2025-01-08T13:31:20+00:00\n", "", `"timestamp" header is mandatory`},
}
for i, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Assert(err, ErrorMatches, errPrefix+test.expectedErr, Commentf("test %d/%d failed", i+1, len(invalidTests)))
}
}
|