File: metadata.go

package info (click to toggle)
golang-github-mendersoftware-mender-artifact 3.9.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,136 kB
  • sloc: makefile: 128; sh: 128
file content (522 lines) | stat: -rw-r--r-- 14,178 bytes parent folder | download | duplicates (3)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright 2022 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 artifact

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"github.com/pkg/errors"
)

// WriteValidator is the interface that wraps the io.Writer interface and
// Validate method.
type WriteValidator interface {
	io.Writer
	Validate() error
}

// ErrValidatingData is an error returned by Validate() in case of
// invalid data.
var ErrValidatingData = errors.New("error validating data")

// Info contains the information about the format and the version
// of artifact archive.
type Info struct {
	Format  string `json:"format"`
	Version int    `json:"version"`
}

// Validate performs sanity checks on artifact info.
func (i Info) Validate() error {
	if len(i.Format) == 0 || i.Version == 0 {
		return errors.Wrap(ErrValidatingData, "Artifact Info needs a format type and a version")
	}
	return nil
}

func decode(p []byte, data WriteValidator) error {
	if len(p) == 0 {
		return nil
	}

	dec := json.NewDecoder(bytes.NewReader(p))
	dec.DisallowUnknownFields()
	err := dec.Decode(data)
	if err != nil {
		return err
	}
	return nil
}

func (i *Info) Write(p []byte) (n int, err error) {
	if err := decode(p, i); err != nil {
		return 0, err
	}
	return len(p), nil
}

// UpdateType provides information about the type of update.
// At the moment the only built-in type is "rootfs-image".
type UpdateType struct {
	Type *string `json:"type"`
}

// HeaderInfoer wraps headerInfo version 2 and 3,
// in order to supply the artifact reader with the information it needs.
type HeaderInfoer interface {
	Write(b []byte) (n int, err error)
	GetArtifactName() string
	GetCompatibleDevices() []string
	GetUpdates() []UpdateType
	GetArtifactDepends() *ArtifactDepends
	GetArtifactProvides() *ArtifactProvides
}

// HeaderInfo contains information of number and type of update files
// archived in Mender metadata archive.
type HeaderInfo struct {
	ArtifactName      string       `json:"artifact_name"`
	Updates           []UpdateType `json:"updates"`
	CompatibleDevices []string     `json:"device_types_compatible"`
}

func (h *HeaderInfo) UnmarshalJSON(b []byte) error {
	type Alias HeaderInfo
	buf := &Alias{}
	if err := json.Unmarshal(b, &buf); err != nil {
		return err
	}
	if len(buf.CompatibleDevices) == 0 {
		return ErrCompatibleDevices
	}
	h.ArtifactName = buf.ArtifactName
	h.Updates = buf.Updates
	h.CompatibleDevices = buf.CompatibleDevices
	return nil
}

func NewHeaderInfo(
	artifactName string,
	updates []UpdateType,
	compatibleDevices []string,
) *HeaderInfo {
	return &HeaderInfo{
		ArtifactName:      artifactName,
		Updates:           updates,
		CompatibleDevices: compatibleDevices,
	}
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfo) GetArtifactName() string {
	return hi.ArtifactName
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfo) GetCompatibleDevices() []string {
	return hi.CompatibleDevices
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfo) GetUpdates() []UpdateType {
	return hi.Updates
}

// Validate checks if header-info structure is correct.
func (hi HeaderInfo) Validate() error {
	missingArgs := []string{"Artifact validation failed with missing argument"}
	if len(hi.Updates) == 0 {
		missingArgs = append(missingArgs, "No Payloads added")
	}
	if len(hi.CompatibleDevices) == 0 {
		missingArgs = append(missingArgs, "No compatible devices listed")
	}
	if len(hi.ArtifactName) == 0 {
		missingArgs = append(missingArgs, "No artifact name")
	}
	for _, update := range hi.Updates {
		if update == (UpdateType{}) {
			missingArgs = append(missingArgs, "Empty Payload")
			break
		}
	}
	if len(missingArgs) > 1 {
		if len(missingArgs) > 2 {
			missingArgs[0] = missingArgs[0] + "s" // Add plural.
		}
		missingArgs[0] = missingArgs[0] + ": "
		return errors.New(missingArgs[0] + strings.Join(missingArgs[1:], ", "))
	}
	return nil
}

func (hi *HeaderInfo) Write(p []byte) (n int, err error) {
	if err := decode(p, hi); err != nil {
		return 0, err
	}
	return len(p), nil
}

func (hi *HeaderInfo) GetArtifactDepends() *ArtifactDepends {
	return nil
}

func (hi *HeaderInfo) GetArtifactProvides() *ArtifactProvides {
	return nil
}

type HeaderInfoV3 struct {
	// For historical reasons, "payloads" are often referred to as "updates"
	// in the code, since this was the old name (and still is, in V2).
	// This is the reason why the struct field is still called
	// "Updates".
	Updates []UpdateType `json:"payloads"`
	// Has its own json marshaller tags.
	ArtifactProvides *ArtifactProvides `json:"artifact_provides"`
	// Has its own json marshaller tags.
	ArtifactDepends *ArtifactDepends `json:"artifact_depends"`
}

func NewHeaderInfoV3(updates []UpdateType,
	artifactProvides *ArtifactProvides, artifactDepends *ArtifactDepends) *HeaderInfoV3 {
	return &HeaderInfoV3{
		Updates:          updates,
		ArtifactProvides: artifactProvides,
		ArtifactDepends:  artifactDepends,
	}
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfoV3) GetArtifactName() string {
	if hi.ArtifactProvides == nil {
		return ""
	}
	return hi.ArtifactProvides.ArtifactName
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfoV3) GetCompatibleDevices() []string {
	if hi.ArtifactDepends == nil {
		return nil
	}
	return hi.ArtifactDepends.CompatibleDevices
}

// Satisfy HeaderInfoer interface for the artifact reader.
func (hi *HeaderInfoV3) GetUpdates() []UpdateType {
	return hi.Updates
}

// Validate validates the correctness of the header version3.
func (hi *HeaderInfoV3) Validate() error {
	missingArgs := []string{"Artifact validation failed with missing argument"}
	// Artifact must have an update with them,
	// because the signature of the update is stored in the metadata field.
	if len(hi.Updates) == 0 {
		missingArgs = append(missingArgs, "No Payloads added")
	}
	//////////////////////////////////
	// All required Artifact-provides:
	//////////////////////////////////
	/* Artifact-provides cannot be empty. */
	if hi.ArtifactProvides == nil {
		missingArgs = append(missingArgs, "Empty Artifact provides")
	} else {
		/* Artifact must have a name. */
		if len(hi.ArtifactProvides.ArtifactName) == 0 {
			missingArgs = append(missingArgs, "Artifact name")
		}
		//
		/* Artifact need not have a group */
		//
	}
	///////////////////////////////////////
	// Artifact-depends can be empty, thus:
	///////////////////////////////////////
	/* Artifact must not depend on a name. */
	/* Artifact must not depend on a device. */
	/* Artifact must not depend on an device group. */
	/* Artifact must not depend on a update types supported. */
	/* Artifact must not depend on artifact versions supported. */
	if len(missingArgs) > 1 {
		if len(missingArgs) > 2 {
			missingArgs[0] = missingArgs[0] + "s" // Add plural.
		}
		missingArgs[0] = missingArgs[0] + ": "
		return errors.New(missingArgs[0] + strings.Join(missingArgs[1:], ", "))
	}
	return nil
}

func (hi *HeaderInfoV3) Write(p []byte) (n int, err error) {
	if err := decode(p, hi); err != nil {
		return 0, err
	}
	return len(p), nil
}

type ArtifactDepends struct {
	ArtifactName      []string `json:"artifact_name,omitempty"`
	CompatibleDevices []string `json:"device_type,omitempty"`
	ArtifactGroup     []string `json:"artifact_group,omitempty"`
}

var ErrCompatibleDevices error = errors.New(
	"ArtifactDepends: Required field 'CompatibleDevices' not found",
)

func (a *ArtifactDepends) UnmarshalJSON(b []byte) error {
	type Alias ArtifactDepends // Same fields, no inherited UnmarshalJSON method
	buf := &Alias{}
	if err := json.Unmarshal(b, buf); err != nil {
		return err
	}
	if len(buf.CompatibleDevices) == 0 {
		return ErrCompatibleDevices
	}
	a.ArtifactName = buf.ArtifactName
	a.CompatibleDevices = buf.CompatibleDevices
	a.ArtifactGroup = buf.ArtifactGroup
	return nil
}

type ArtifactProvides struct {
	ArtifactName  string `json:"artifact_name"`
	ArtifactGroup string `json:"artifact_group,omitempty"`
}

// TypeInfo provides information of type of individual updates
// archived in artifacts archive.
type TypeInfo struct {
	Type string `json:"type"`
}

// Validate validates corectness of TypeInfo.
func (ti TypeInfo) Validate() error {
	if len(ti.Type) == 0 {
		return errors.Wrap(ErrValidatingData, "TypeInfo requires a type")
	}
	return nil
}

func (ti *TypeInfo) Write(p []byte) (n int, err error) {
	if err := decode(p, ti); err != nil {
		return 0, err
	}
	return len(p), nil
}

type TypeInfoDepends map[string]interface{}

func (t TypeInfoDepends) Map() map[string]interface{} {
	return map[string]interface{}(t)
}

func NewTypeInfoDepends(m interface{}) (ti TypeInfoDepends, err error) {

	const errMsgInvalidTypeFmt = "Invalid TypeInfo depends type: %T"
	const errMsgInvalidTypeEntFmt = errMsgInvalidTypeFmt + ", with value %v"

	ti = make(map[string]interface{})
	switch val := m.(type) {
	case map[string]interface{}:
		for k, v := range val {
			switch val := v.(type) {

			case string, []string:
				ti[k] = v

			case []interface{}:
				valStr := make([]string, len(val))
				for i, entFace := range v.([]interface{}) {
					entStr, ok := entFace.(string)
					if !ok {
						return nil, fmt.Errorf(
							errMsgInvalidTypeEntFmt,
							v, v)
					}
					valStr[i] = entStr
				}
				ti[k] = valStr

			default:
				return nil, fmt.Errorf(
					errMsgInvalidTypeEntFmt,
					v, v)
			}
		}
		return ti, nil
	case map[string]string:
		m := m.(map[string]string)
		for k, v := range m {
			ti[k] = v
		}
		return ti, nil
	case map[string][]string:
		m := m.(map[string][]string)
		for k, v := range m {
			ti[k] = v
		}
		return ti, nil
	default:
		return nil, fmt.Errorf(errMsgInvalidTypeFmt, m)
	}
}

// UnmarshalJSON attempts to deserialize the json stream into a 'map[string]interface{}',
// where each interface value is required to be either a string, or an array of strings
func (t *TypeInfoDepends) UnmarshalJSON(b []byte) error {
	m := make(map[string]interface{})
	err := json.Unmarshal(b, &m)
	if err != nil {
		return err
	}
	*t, err = NewTypeInfoDepends(m)
	return err
}

type TypeInfoProvides map[string]string

func (t TypeInfoProvides) Map() map[string]string {
	return t
}

func NewTypeInfoProvides(m interface{}) (ti TypeInfoProvides, err error) {

	const errMsgInvalidTypeFmt = "Invalid TypeInfo provides type: %T"
	const errMsgInvalidTypeEntFmt = errMsgInvalidTypeFmt + ", with value %v"

	ti = make(map[string]string)
	switch val := m.(type) {
	case map[string]interface{}:
		for k, v := range val {
			switch val := v.(type) {
			case string:
				ti[k] = val
				continue
			default:
				return nil, fmt.Errorf(errMsgInvalidTypeEntFmt,
					v, v)
			}
		}
		return ti, nil
	case map[string]string:
		m := m.(map[string]string)
		for k, v := range m {
			ti[k] = v
		}
		return ti, nil
	default:
		return nil, fmt.Errorf(errMsgInvalidTypeFmt, m)
	}
}

// UnmarshalJSON attempts to deserialize the json stream into a 'map[string]interface{}',
// where each interface value is required to be either a string, or an array of strings
func (t *TypeInfoProvides) UnmarshalJSON(b []byte) error {
	m := make(map[string]interface{})
	err := json.Unmarshal(b, &m)
	if err != nil {
		return err
	}
	*t, err = NewTypeInfoProvides(m)
	return err
}

// TypeInfoV3 provides information about the type of update contained within the
// headerstructure.
type TypeInfoV3 struct {
	// Rootfs/Delta (Required).
	Type *string `json:"type"`

	ArtifactDepends        TypeInfoDepends  `json:"artifact_depends,omitempty"`
	ArtifactProvides       TypeInfoProvides `json:"artifact_provides,omitempty"`
	ClearsArtifactProvides []string         `json:"clears_artifact_provides,omitempty"`
}

// Validate checks that the required `Type` field is set.
func (ti *TypeInfoV3) Validate() error {
	if ti.Type != nil && *ti.Type == "" {
		return errors.Wrap(ErrValidatingData, "TypeInfoV3: ")
	}
	return nil
}

// Write writes the underlying struct into a json data structure (bytestream).
func (ti *TypeInfoV3) Write(b []byte) (n int, err error) {
	if err := decode(b, ti); err != nil {
		return 0, err
	}
	return len(b), nil
}

func (hi *HeaderInfoV3) GetArtifactDepends() *ArtifactDepends {
	return hi.ArtifactDepends
}

func (hi *HeaderInfoV3) GetArtifactProvides() *ArtifactProvides {
	return hi.ArtifactProvides
}

// Metadata contains artifacts metadata information. The exact metadata fields
// are user-defined and are not specified. The only requirement is that those
// must be stored in a for of JSON.
// The fields which must exist are update-type dependent. In case of
// `rootfs-update` image type, there are no additional fields required.
type Metadata map[string]interface{}

// Validate check corecness of artifacts metadata. Since the exact format is
// not specified validation always succeeds.
func (m Metadata) Validate() error {
	return nil
}

func (m *Metadata) Write(p []byte) (n int, err error) {
	if err := decode(p, m); err != nil {
		return 0, err
	}
	return len(p), nil
}

func (m *Metadata) Map() map[string]interface{} {
	return map[string]interface{}(*m)
}

// Files represents the list of file names that make up the payload for given
// update.
type Files struct {
	FileList []string `json:"files"`
}

// Validate checks format of Files.
func (f Files) Validate() error {
	for _, f := range f.FileList {
		if len(f) == 0 {
			return errors.Wrap(ErrValidatingData, "File in FileList requires a name")
		}
	}
	return nil
}

func (f *Files) Write(p []byte) (n int, err error) {
	if err := decode(p, f); err != nil {
		return 0, err
	}
	return len(p), nil
}