File: assertion.go

package info (click to toggle)
snapd 2.74.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,428 kB
  • sloc: sh: 16,966; ansic: 16,788; python: 11,332; makefile: 1,897; exp: 190; awk: 58; xml: 22
file content (163 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download
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
// -*- 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 assemblestate

import (
	"errors"
	"fmt"
	"io"
	"sort"
	"strconv"
	"strings"

	"github.com/snapcore/snapd/asserts"
)

// AssertionDevices converts the data returned by an assembly session into the
// data structure used by the "devices" block of a cluster assertion.
func AssertionDevices(ids []Identity, routes Routes) ([]any, error) {
	addresses, err := addressesFromRoutes(routes)
	if err != nil {
		return nil, err
	}

	serials := make(map[DeviceToken]*asserts.Serial, len(ids))
	for _, identity := range ids {
		serial, err := serialFromBundle(identity.SerialBundle)
		if err != nil {
			return nil, fmt.Errorf("cannot parse serial bundle for device %q: %w", identity.RDT, err)
		}

		if _, ok := serials[identity.RDT]; ok {
			return nil, fmt.Errorf("duplicate device token found in identities: %q", identity.RDT)
		}

		serials[identity.RDT] = serial
	}

	// sort identities based on brand, model, then serial so that numeric id
	// assignment is consistent, even across multiple assemble sessions.
	sort.Slice(ids, func(i, j int) bool {
		left := serials[ids[i].RDT]
		right := serials[ids[j].RDT]

		if left.BrandID() != right.BrandID() {
			return left.BrandID() < right.BrandID()
		}
		if left.Model() != right.Model() {
			return left.Model() < right.Model()
		}

		return left.Serial() < right.Serial()
	})

	devices := make([]any, 0, len(ids))
	for i, identity := range ids {
		addrs := addresses[identity.RDT]
		if len(addrs) == 0 {
			return nil, fmt.Errorf("no addresses available for device %q", identity.RDT)
		}

		serial := serials[identity.RDT]
		devices = append(devices, map[string]any{
			"id":        strconv.Itoa(i + 1),
			"device":    serial.DeviceID().String(),
			"addresses": addrs,
		})
	}

	return devices, nil
}

func serialFromBundle(bundle string) (*asserts.Serial, error) {
	decoder := asserts.NewDecoder(strings.NewReader(bundle))
	for {
		assertion, err := decoder.Decode()
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, fmt.Errorf("cannot decode serial bundle: %w", err)
		}

		if assertion.Type() == asserts.SerialType {
			serial, ok := assertion.(*asserts.Serial)
			if !ok {
				return nil, fmt.Errorf("internal error: serial assertion has unexpected type %T", assertion)
			}
			return serial, nil
		}
	}

	return nil, errors.New("serial assertion not found in bundle")
}

func addressesFromRoutes(routes Routes) (map[DeviceToken][]any, error) {
	if len(routes.Routes)%3 != 0 {
		return nil, errors.New("routes array length must be multiple of 3")
	}

	addressSets := make(map[DeviceToken]map[string]bool)

	// TODO:GOVERSION: we repeat this iteration and validation construct a lot,
	// real iterators would be a good fit here
	for i := 0; i < len(routes.Routes); i += 3 {
		src := routes.Routes[i]
		dest := routes.Routes[i+1]
		addrIdx := routes.Routes[i+2]

		if src < 0 || src >= len(routes.Devices) {
			return nil, fmt.Errorf("invalid source device index %d in routes", src)
		}
		if dest < 0 || dest >= len(routes.Devices) {
			return nil, fmt.Errorf("invalid destination device index %d in routes", dest)
		}
		if addrIdx < 0 || addrIdx >= len(routes.Addresses) {
			return nil, fmt.Errorf("invalid address index %d in routes", addrIdx)
		}

		destRDT := routes.Devices[dest]
		addr := routes.Addresses[addrIdx]

		if addressSets[destRDT] == nil {
			addressSets[destRDT] = make(map[string]bool)
		}

		addressSets[destRDT][addr] = true
	}

	addresses := make(map[DeviceToken][]any, len(addressSets))
	for rdt, set := range addressSets {
		sorted := make([]string, 0, len(set))
		for addr := range set {
			sorted = append(sorted, addr)
		}
		sort.Strings(sorted)

		addrs := make([]any, 0, len(sorted))
		for _, addr := range sorted {
			addrs = append(addrs, addr)
		}

		addresses[rdt] = addrs
	}

	return addresses, nil
}