-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathverifiertypes.go
194 lines (170 loc) · 6.48 KB
/
verifiertypes.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
// Copyright (c) 2017 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
// Types which feed in and out of the verifier
package types
import (
"github.com/google/go-cmp/cmp"
"github.com/lf-edge/eve/pkg/pillar/base"
)
// Types for verifying the images.
// For now we just verify the sha checksum.
// For defense-in-depth we assume that the ZedManager with the help of
// dom0 has moved the image file to a read-only directory before asking
// for the file to be verified.
// VerifyImageConfig captures the verifications which have been requested.
// The key/index to this is the ImageSha256 which is allocated by the controller or resolver.
type VerifyImageConfig struct {
ImageSha256 string // sha256 of immutable image
Name string
MediaType string // MIME type
FileLocation string // Current location; should be info about file
Size int64 //FileLocation size
RefCount uint
Expired bool // Used in delete handshake
}
// Key returns the pubsub Key
func (config VerifyImageConfig) Key() string {
return config.ImageSha256
}
// LogCreate :
func (config VerifyImageConfig) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.VerifyImageConfigLogType, config.Name,
nilUUID, config.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("refcount-int64", config.RefCount).
AddField("mediatype", config.MediaType).
AddField("expired-bool", config.Expired).
Noticef("VerifyImage config create")
}
// LogModify :
func (config VerifyImageConfig) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.VerifyImageConfigLogType, config.Name,
nilUUID, config.LogKey())
oldConfig, ok := old.(VerifyImageConfig)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of VerifyImageConfig type")
}
if oldConfig.RefCount != config.RefCount ||
oldConfig.Expired != config.Expired {
logObject.CloneAndAddField("refcount-int64", config.RefCount).
AddField("mediatype", config.MediaType).
AddField("expired-bool", config.Expired).
AddField("old-refcount-int64", oldConfig.RefCount).
AddField("old-expired-bool", oldConfig.Expired).
Noticef("VerifyImage config modify")
} else {
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldConfig, config)).
Noticef("VerifyImage config modify other change")
}
}
// LogDelete :
func (config VerifyImageConfig) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.VerifyImageConfigLogType, config.Name,
nilUUID, config.LogKey())
logObject.CloneAndAddField("refcount-int64", config.RefCount).
AddField("mediatype", config.MediaType).
AddField("expired-bool", config.Expired).
Noticef("VerifyImage config delete")
base.DeleteLogObject(logBase, config.LogKey())
}
// LogKey :
func (config VerifyImageConfig) LogKey() string {
return string(base.VerifyImageConfigLogType) + "-" + config.Key()
}
// VerifyImageStatus captures the verifications which have been requested.
// The key/index to this is the ImageSha256
type VerifyImageStatus struct {
ImageSha256 string // sha256 of immutable image
Name string
FileLocation string // Current location
Size int64
MediaType string // MIME type
PendingAdd bool
PendingModify bool
PendingDelete bool
State SwState // DELIVERED; LastErr* set if failed
// ErrorAndTime provides SetErrorNow() and ClearError()
ErrorAndTime
RefCount uint
Expired bool // Used in delete handshake
}
// Key returns the pubsub Key
func (status VerifyImageStatus) Key() string {
return status.ImageSha256
}
// LogCreate :
func (status VerifyImageStatus) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.VerifyImageStatusLogType, status.Name,
nilUUID, status.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("state", status.State.String()).
AddField("mediatype", status.MediaType).
AddField("refcount-int64", status.RefCount).
AddField("expired-bool", status.Expired).
AddField("size-int64", status.Size).
AddField("filelocation", status.FileLocation).
Noticef("VerifyImage status create")
}
// LogModify :
func (status VerifyImageStatus) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.VerifyImageStatusLogType, status.Name,
nilUUID, status.LogKey())
oldStatus, ok := old.(VerifyImageStatus)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of VerifyImageStatus type")
}
if oldStatus.State != status.State ||
oldStatus.RefCount != status.RefCount ||
oldStatus.Expired != status.Expired ||
oldStatus.Size != status.Size ||
oldStatus.FileLocation != status.FileLocation {
logObject.CloneAndAddField("state", status.State.String()).
AddField("mediatype", status.MediaType).
AddField("refcount-int64", status.RefCount).
AddField("expired-bool", status.Expired).
AddField("size-int64", status.Size).
AddField("old-state", oldStatus.State.String()).
AddField("old-refcount-int64", oldStatus.RefCount).
AddField("old-expired-bool", oldStatus.Expired).
AddField("old-size-int64", oldStatus.Size).
AddField("filelocation", status.FileLocation).
AddField("old-filelocation", oldStatus.FileLocation).
Noticef("VerifyImage status modify")
} else {
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldStatus, status)).
Noticef("VerifyImage status modify other change")
}
if status.HasError() {
errAndTime := status.ErrorAndTime
logObject.CloneAndAddField("state", status.State.String()).
AddField("error", errAndTime.Error).
AddField("error-time", errAndTime.ErrorTime).
Noticef("VerifyImage status modify")
}
}
// LogDelete :
func (status VerifyImageStatus) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.VerifyImageStatusLogType, status.Name,
nilUUID, status.LogKey())
logObject.CloneAndAddField("state", status.State.String()).
AddField("mediatype", status.MediaType).
AddField("refcount-int64", status.RefCount).
AddField("expired-bool", status.Expired).
AddField("size-int64", status.Size).
AddField("filelocation", status.FileLocation).
Noticef("VerifyImage status delete")
base.DeleteLogObject(logBase, status.LogKey())
}
// LogKey :
func (status VerifyImageStatus) LogKey() string {
return string(base.VerifyImageStatusLogType) + "-" + status.Key()
}
func (status VerifyImageStatus) Pending() bool {
return status.PendingAdd || status.PendingModify || status.PendingDelete
}