forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
169 lines (148 loc) · 4.27 KB
/
helpers.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2022 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 seed
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/sysdb"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapfile"
"github.com/snapcore/snapd/timings"
)
var trusted = sysdb.Trusted()
func MockTrusted(mockTrusted []asserts.Assertion) (restore func()) {
prevTrusted := trusted
trusted = mockTrusted
return func() {
trusted = prevTrusted
}
}
func newMemAssertionsDB(commitObserve func(verified asserts.Assertion)) (db *asserts.Database, commitTo func(*asserts.Batch) error, err error) {
memDB, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: trusted,
})
if err != nil {
return nil, nil, err
}
commitTo = func(b *asserts.Batch) error {
return b.CommitToAndObserve(memDB, commitObserve, nil)
}
return memDB, commitTo, nil
}
func loadAssertions(assertsDir string, loadedFunc func(*asserts.Ref) error) (*asserts.Batch, error) {
logger.Debugf("loading assertions from %s", assertsDir)
dc, err := ioutil.ReadDir(assertsDir)
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNoAssertions
}
return nil, fmt.Errorf("cannot read assertions dir: %s", err)
}
batch := asserts.NewBatch(nil)
for _, fi := range dc {
fn := filepath.Join(assertsDir, fi.Name())
refs, err := readAsserts(batch, fn)
if err != nil {
return nil, fmt.Errorf("cannot read assertions: %s", err)
}
if loadedFunc != nil {
for _, ref := range refs {
if err := loadedFunc(ref); err != nil {
return nil, err
}
}
}
}
return batch, nil
}
func readAsserts(batch *asserts.Batch, fn string) ([]*asserts.Ref, error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
defer f.Close()
return batch.AddStream(f)
}
func readInfo(snapPath string, si *snap.SideInfo) (*snap.Info, error) {
snapf, err := snapfile.Open(snapPath)
if err != nil {
return nil, err
}
return snap.ReadInfoFromSnapFile(snapf, si)
}
func snapTypeFromModel(modSnap *asserts.ModelSnap) snap.Type {
switch modSnap.SnapType {
case "base":
return snap.TypeBase
case "core":
return snap.TypeOS
case "gadget":
return snap.TypeGadget
case "kernel":
return snap.TypeKernel
case "snapd":
return snap.TypeSnapd
default:
return snap.TypeApp
}
}
func essentialSnapTypesToModelFilter(essentialTypes []snap.Type) func(modSnap *asserts.ModelSnap) bool {
m := make(map[string]bool, len(essentialTypes))
for _, t := range essentialTypes {
switch t {
case snap.TypeBase:
m["base"] = true
case snap.TypeOS:
m["core"] = true
case snap.TypeGadget:
m["gadget"] = true
case snap.TypeKernel:
m["kernel"] = true
case snap.TypeSnapd:
m["snapd"] = true
}
}
return func(modSnap *asserts.ModelSnap) bool {
return m[modSnap.SnapType]
}
}
func findBrand(seed Seed, db asserts.RODatabase) (*asserts.Account, error) {
a, err := db.Find(asserts.AccountType, map[string]string{
"account-id": seed.Model().BrandID(),
})
if err != nil {
return nil, fmt.Errorf("internal error: %v", err)
}
return a.(*asserts.Account), nil
}
type defaultSnapHandler struct{}
func (h defaultSnapHandler) HandleUnassertedSnap(name, path string, _ timings.Measurer) (string, error) {
return path, nil
}
func (h defaultSnapHandler) HandleAndDigestAssertedSnap(name, path string, essType snap.Type, _ *asserts.SnapRevision, _ func(string, uint64) (snap.Revision, error), _ timings.Measurer) (string, string, uint64, error) {
sha3_384, size, err := asserts.SnapFileSHA3_384(path)
if err != nil {
return "", "", 0, err
}
return path, sha3_384, size, err
}