/
image.go
316 lines (269 loc) · 9.04 KB
/
image.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
309
310
311
312
313
314
315
316
// Copyright 2023 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"context"
"io"
"path"
"time"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/mendersoftware/go-lib-micro/identity"
"github.com/mendersoftware/go-lib-micro/mongo/doc"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
const (
ArtifactFileSuffix = ".mender"
)
var (
StorageKeyImageProvidesIdxKey = "meta_artifact.provides_idx.key"
StorageKeyImageProvidesIdxValue = "meta_artifact.provides_idx.value"
)
type ProvidesIdx map[string]string
func ImagePathFromContext(ctx context.Context, id string) string {
imgPath := id
if idty := identity.FromContext(ctx); idty != nil {
imgPath = path.Join(idty.Tenant, id)
}
return imgPath
}
// Information provided by the user
type ImageMeta struct {
// Image description
Description string `json:"description,omitempty" valid:"length(1|4096),optional"`
}
// Creates new, empty ImageMeta
func NewImageMeta() *ImageMeta {
return &ImageMeta{}
}
// Validate checks structure according to valid tags.
func (s ImageMeta) Validate() error {
return validation.ValidateStruct(&s,
validation.Field(&s.Description, lengthLessThan4096),
)
}
// Structure with artifact version information
type ArtifactInfo struct {
// Mender artifact format - the only possible value is "mender"
//Format string `json:"format" valid:"string,equal("mender"),required"`
Format string `json:"format" valid:"required"`
// Mender artifact format version
//Version uint `json:"version" valid:"uint,equal(1),required"`
Version uint `json:"version" valid:"required"`
}
func (ai ArtifactInfo) Validate() error {
return validation.ValidateStruct(&ai,
validation.Field(&ai.Format, validation.Required),
validation.Field(&ai.Version, validation.In(uint(1), uint(2), uint(3))),
)
}
// Information provided by the Mender Artifact header
type ArtifactMeta struct {
// artifact_name from artifact file
Name string `json:"name" bson:"name" valid:"length(1|4096),required"`
// Compatible device types for the application
//nolint:lll
DeviceTypesCompatible []string `json:"device_types_compatible" bson:"device_types_compatible" valid:"length(1|4096),required"`
// Artifact version info
Info *ArtifactInfo `json:"info"`
// Flag that indicates if artifact is signed or not
Signed bool `json:"signed" bson:"signed"`
// List of updates
Updates []Update `json:"updates" valid:"-"`
// Provides is a map of artifact_provides used
// for checking artifact (version 3) dependencies.
//nolint: lll
Provides map[string]string `json:"artifact_provides,omitempty" bson:"provides,omitempty" valid:"-"`
// ProvidesIdx is special representation of provides
// which makes possible to index and query using provides.
ProvidesIdx ProvidesIdx `json:"-" bson:"provides_idx,omitempty"`
// Depends is a map[string]interface{} (JSON) of artifact_depends used
// for checking/validate against artifact (version 3) provides.
Depends map[string]interface{} `json:"artifact_depends,omitempty" bson:"depends" valid:"-"`
// ClearsProvides is a list of strings (JSON) of clears_artifact_provides used
// for clearing already-installed artifact (version 3) provides.
//nolint:lll
ClearsProvides []string `json:"clears_artifact_provides,omitempty" bson:"clears_provides,omitempty" valid:"-"`
}
// MarshalBSON transparently creates depends_idx field on bson.Marshal
func (am ArtifactMeta) MarshalBSON() ([]byte, error) {
if err := am.Validate(); err != nil {
return nil, err
}
dependsIdx, err := doc.UnwindMap(am.Depends)
if err != nil {
return nil, err
}
doc := doc.DocumentFromStruct(am, bson.E{
Key: "depends_idx", Value: dependsIdx,
})
return bson.Marshal(doc)
}
// MarshalBSONValue transparently creates depends_idx field on bson.MarshalValue
// which is called if ArtifactMeta is marshaled as an embedded document.
func (am ArtifactMeta) MarshalBSONValue() (bsontype.Type, []byte, error) {
if err := am.Validate(); err != nil {
return bson.TypeNull, nil, err
}
dependsIdx, err := doc.UnwindMap(am.Depends)
if err != nil {
return bson.TypeNull, nil, err
}
doc := doc.DocumentFromStruct(am, bson.E{
Key: "depends_idx", Value: dependsIdx,
})
return bson.MarshalValue(doc)
}
// Validate checks structure according to valid tags.
func (am *ArtifactMeta) Validate() error {
if am.Depends == nil {
am.Depends = make(map[string]interface{})
}
am.Depends["device_type"] = am.DeviceTypesCompatible
return validation.ValidateStruct(am,
validation.Field(&am.Name, validation.Required, lengthIn1To4096),
validation.Field(&am.DeviceTypesCompatible,
validation.Required,
lengthIn0To200,
validation.Each(lengthIn1To4096),
),
validation.Field(&am.Info),
)
}
func NewArtifactMeta() *ArtifactMeta {
return &ArtifactMeta{}
}
// Image YOCTO image with user application
type Image struct {
// Image ID
Id string `json:"id" bson:"_id" valid:"uuidv4,required"`
// User provided field set
*ImageMeta `bson:"meta"`
// Field set provided with yocto image
*ArtifactMeta `bson:"meta_artifact"`
// Artifact total size
Size int64 `json:"size" bson:"size" valid:"-"`
// Last modification time, including image upload time
Modified *time.Time `json:"modified" valid:"-"`
}
func (img Image) MarshalBSON() (b []byte, err error) {
return bson.Marshal(doc.DocumentFromStruct(img))
}
func (img Image) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(doc.DocumentFromStruct(img))
}
// Validate checks structure according to valid tags.
func (s Image) Validate() error {
return validation.ValidateStruct(&s,
validation.Field(&s.Id, validation.Required, is.UUID),
validation.Field(&s.ImageMeta),
validation.Field(&s.ArtifactMeta),
)
}
// NewImage creates new software image object.
func NewImage(
id string,
metaConstructor *ImageMeta,
metaArtifactConstructor *ArtifactMeta,
artifactSize int64) *Image {
now := time.Now()
return &Image{
ImageMeta: metaConstructor,
ArtifactMeta: metaArtifactConstructor,
Modified: &now,
Id: id,
Size: artifactSize,
}
}
// SetModified set last modification time for the image.
func (s *Image) SetModified(time time.Time) {
s.Modified = &time
}
type ReadCounter interface {
io.Reader
// Count returns the number of bytes read.
Count() int64
}
// MultipartUploadMsg is a structure with fields extracted from the multipart/form-data form
// send in the artifact upload request
type MultipartUploadMsg struct {
// user metadata constructor
MetaConstructor *ImageMeta
// ArtifactID contains the artifact ID
ArtifactID string
// reader pointing to the beginning of the artifact data
ArtifactReader io.Reader
}
// MultipartGenerateImageMsg is a structure with fields extracted from the multipart/form-data
// form sent in the artifact generation request
type MultipartGenerateImageMsg struct {
Name string `json:"name"`
Description string `json:"description"`
DeviceTypesCompatible []string `json:"device_types_compatible"`
Type string `json:"type"`
Args string `json:"args"`
ArtifactID string `json:"artifact_id"`
GetArtifactURI string `json:"get_artifact_uri"`
DeleteArtifactURI string `json:"delete_artifact_uri"`
TenantID string `json:"tenant_id"`
Token string `json:"token"`
FileReader io.Reader `json:"-"`
}
func (msg MultipartGenerateImageMsg) Validate() error {
if err := validation.ValidateStruct(&msg,
validation.Field(&msg.Name, validation.Required),
validation.Field(&msg.DeviceTypesCompatible, validation.Required),
validation.Field(&msg.Type, validation.Required),
); err != nil {
return err
}
// Somehow FileReader is not covered by "required" rule.
if msg.FileReader == nil {
return errors.New("missing 'file' section")
}
return nil
}
type provideInternal struct {
Key string
Value string
}
func (p ProvidesIdx) MarshalBSONValue() (bsontype.Type, []byte, error) {
attrs := make([]provideInternal, len(p))
i := 0
for k, v := range p {
attrs[i].Key = k
attrs[i].Value = v
i++
}
return bson.MarshalValue(attrs)
}
func (p *ProvidesIdx) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
raw := bson.Raw(b)
elems, err := raw.Elements()
if err != nil {
return err
}
*p = make(ProvidesIdx, len(elems))
var tmp provideInternal
for _, elem := range elems {
err = elem.Value().Unmarshal(&tmp)
if err != nil {
return err
}
(*p)[tmp.Key] = tmp.Value
}
return nil
}