-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathparsepatchenvelopes.go
More file actions
295 lines (260 loc) · 9.46 KB
/
parsepatchenvelopes.go
File metadata and controls
295 lines (260 loc) · 9.46 KB
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
// Copyright (c) 2023 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package zedagent
import (
"bytes"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"crypto/sha256"
zconfig "github.com/lf-edge/eve-api/go/config"
zcommon "github.com/lf-edge/eve-api/go/evecommon"
"github.com/lf-edge/eve/pkg/pillar/persistcache"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/lf-edge/eve/pkg/pillar/utils/generics"
)
func parsePatchEnvelopes(ctx *getconfigContext, config *zconfig.EdgeDevConfig) {
parsePatchEnvelopesImpl(ctx, config, types.PersistCachePatchEnvelopes)
}
func parsePatchEnvelopesImpl(ctx *getconfigContext, config *zconfig.EdgeDevConfig,
persistCacheFilepath string) {
log.Tracef("Parsing patchEnvelope from configuration")
// Store list of binary blobs which were created before
pc, err := persistcache.New(persistCacheFilepath)
if err != nil {
log.Errorf("Failed to load persistCache %v", err)
return
}
blobsBefore := pc.Objects()
var blobsAfter []string
patchEnvelopes := config.GetPatchEnvelopes()
result := types.PatchEnvelopeInfoList{}
for _, pe := range patchEnvelopes {
peInfo := types.PatchEnvelopeInfo{
AllowedApps: pe.GetAppInstIdsAllowed(),
PatchID: pe.GetUuid(),
Name: pe.GetDisplayName(),
Version: pe.GetVersion(),
State: evePatchEnvelopeActionToState(pe.GetAction()),
}
for _, a := range pe.GetArtifacts() {
err := addBinaryBlobToPatchEnvelope(ctx, &peInfo, a, persistCacheFilepath)
if err != nil {
msg := fmt.Sprintf("Failed to compose binary blob for patch envelope %v", err)
peInfo.Errors = append(peInfo.Errors, msg)
log.Error(msg)
return
}
}
result.Envelopes = append(result.Envelopes, peInfo)
for _, inlineBlob := range peInfo.BinaryBlobs {
blobsAfter = append(blobsAfter, inlineBlob.FileName)
}
for _, cipherBlob := range peInfo.CipherBlobs {
blobsAfter = append(blobsAfter, cipherBlob.EncFileName)
}
}
publishPatchEnvelopes(ctx, result)
// Provide zedrouter with newest version for description.json and then delete files
blobsToDelete, _ := generics.DiffSets(blobsBefore, blobsAfter)
for _, blob := range blobsToDelete {
pc.Delete(blob)
}
}
func publishPatchEnvelopes(ctx *getconfigContext, patchEnvelopes types.PatchEnvelopeInfoList) {
key := patchEnvelopes.Key()
pub := ctx.pubPatchEnvelopeInfo
pub.Publish(key, patchEnvelopes)
log.Tracef("publishPatchEnvelopes(%s) done\n", key)
}
func addBinaryBlobToPatchEnvelope(ctx *getconfigContext, pe *types.PatchEnvelopeInfo, artifact *zconfig.EveBinaryArtifact, persistCacheFilepath string) error {
switch blob := artifact.GetBinaryBlob().(type) {
case *zconfig.EveBinaryArtifact_VolumeRef:
binaryArtifact := blob.VolumeRef
if binaryArtifact == nil {
return fmt.Errorf("ExternalOpaqueBinaryBlob is empty, type indicates it should be present")
}
volumeRef, err := getBinaryBlobVolumeRef(binaryArtifact)
if err != nil {
return err
}
volumeRef.ArtifactMetadata = artifact.GetArtifactMetaData()
volumeRef.EncArtifactMeta, err = getEncArtifactMetadata(ctx, artifact)
if err != nil {
return err
}
pe.VolumeRefs = append(pe.VolumeRefs, *volumeRef)
return nil
case *zconfig.EveBinaryArtifact_Inline:
inlineArtifact := blob.Inline
if inlineArtifact == nil {
return fmt.Errorf("InlineOpaqueBase64data is empty, type indicates it should be present")
}
binaryBlob, err := cacheInlineBase64Artifact(inlineArtifact, persistCacheFilepath)
if err != nil {
return err
}
binaryBlob.ArtifactMetadata = artifact.GetArtifactMetaData()
binaryBlob.EncArtifactMeta, err = getEncArtifactMetadata(ctx, artifact)
if err != nil {
return err
}
pe.BinaryBlobs = append(pe.BinaryBlobs, *binaryBlob)
return nil
case *zconfig.EveBinaryArtifact_EncryptedInline:
encInline := blob.EncryptedInline
if encInline == nil {
return fmt.Errorf("EncryptedInlineOpaqueBase64data is empty, type indicates it should be present")
}
encBlob, err := getEncryptedCipherBlock(ctx, artifact, types.BlobEncrytedTypeInline, encInline, persistCacheFilepath)
if err != nil {
return err
}
pe.CipherBlobs = append(pe.CipherBlobs, *encBlob)
return nil
case *zconfig.EveBinaryArtifact_EncryptedVolumeref:
encVolumeRef := blob.EncryptedVolumeref
if encVolumeRef == nil {
return fmt.Errorf("EncryptedVolumeref is empty, type indicates it should be present")
}
encBlob, err := getEncryptedCipherBlock(ctx, artifact, types.BlobEncrytedTypeVolume, encVolumeRef, persistCacheFilepath)
if err != nil {
return err
}
pe.CipherBlobs = append(pe.CipherBlobs, *encBlob)
return nil
default:
}
return errors.New("Unknown EveBinaryArtifact format")
}
func getEncArtifactMetadata(ctx *getconfigContext,
artifact *zconfig.EveBinaryArtifact) (types.CipherBlockStatus, error) {
data := artifact.GetMetadataCipherData()
if data == nil {
return types.CipherBlockStatus{}, nil
}
if len(data.CipherData) < 16 {
log.Errorf("Failed to get metadata cipher data, cipherData is nil or less than 16 bytes")
return parseCipherBlock(ctx, "None", nil)
}
key := fmt.Sprintf("artifactMeta-%s", hex.EncodeToString(data.CipherData[:16]))
return parseCipherBlock(ctx, key, data)
}
// getEncryptedCipherBlock extracts artifact metadata, either encrypted or not,
// it stores the cypher block data in the EncBinaryArtifact, this data can be
// either encrypted inline blob or encrypted volume reference
// returns path to it to be served by HTTP server
func getEncryptedCipherBlock(ctx *getconfigContext,
artifact *zconfig.EveBinaryArtifact,
enctype types.BlobEncrytedType,
blob interface{},
persistCacheFilepath string) (*types.BinaryCipherBlob, error) {
var cipherData *zcommon.CipherBlock
var typeStr string
encArtifactMeta, err := getEncArtifactMetadata(ctx, artifact)
if err != nil {
return nil, err
}
cipherBlob := types.BinaryCipherBlob{
EncType: enctype,
ArtifactMetaData: artifact.GetArtifactMetaData(),
EncArtifactMeta: encArtifactMeta,
}
switch enctype {
case types.BlobEncrytedTypeInline:
inline, ok := blob.(*zconfig.EncryptedInlineOpaqueBase64Data)
if !ok || inline == nil {
return nil, fmt.Errorf("invalid type for EncryptedInline")
}
cipherData = inline.GetCipherData()
typeStr = "encInline"
case types.BlobEncrytedTypeVolume:
volume, ok := blob.(*zconfig.EncryptedExternalOpaqueBinaryBlob)
if !ok || volume == nil {
return nil, fmt.Errorf("invalid type for EncryptedVolumeref")
}
cipherData = volume.GetCipherData()
typeStr = "encVolume"
}
if cipherData == nil || len(cipherData.CipherData) < 16 {
return nil, fmt.Errorf("BlobEncrytedType %v has incorrect cipher data", enctype)
}
// the key is used for cipher block and also the file name for the URL saved
// we save the cipher block data to the cache file, and read it back in msrv side to decrypt,
// to avoid publishing the cipher block data which can be too big in size
key := fmt.Sprintf("%s-%s", typeStr, hex.EncodeToString(cipherData.CipherData[:16]))
EncBinaryArtifact, err := parseCipherBlock(ctx, key, cipherData)
if err != nil {
return nil, err
}
url, err := saveCipherBlockStatusToFile(EncBinaryArtifact, key, persistCacheFilepath)
if err != nil {
return nil, err
}
cipherBlob.EncURL = url
cipherBlob.EncFileName = key
return &cipherBlob, nil
}
func saveCipherBlockStatusToFile(status types.CipherBlockStatus, fileName, persistCacheFilepath string) (string, error) {
pc, err := persistcache.New(persistCacheFilepath)
if err != nil {
return "", err
}
// Encode the CipherBlockStatus to []byte using gob
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
if err := encoder.Encode(status); err != nil {
return "", fmt.Errorf("failed to gob encode CipherBlockStatus: %v", err)
}
encodedData := buf.Bytes()
// Write the encoded data to a file using pc.Put()
url, err := pc.Put(fileName, encodedData)
if err != nil {
return "", err
}
return url, nil
}
// cacheInlineBinaryArtifact stores inline artifact as file and
// returns path to it to be served by HTTP server
func cacheInlineBase64Artifact(artifact *zconfig.InlineOpaqueBase64Data, persistCacheFilepath string) (*types.BinaryBlobCompleted, error) {
pc, err := persistcache.New(persistCacheFilepath)
if err != nil {
return nil, err
}
metadata := artifact.GetBase64MetaData()
data := artifact.GetBase64Data()
// We want write inline data to a file to serve it from http server
url, err := pc.Put(artifact.GetFileNameToUse(), []byte(data))
if err != nil {
return nil, err
}
shaBytes := sha256.Sum256([]byte(data))
return &types.BinaryBlobCompleted{
FileName: artifact.GetFileNameToUse(),
FileSha: hex.EncodeToString(shaBytes[:]),
FileMetadata: metadata,
URL: url,
Size: int64(len(data)),
}, nil
}
func getBinaryBlobVolumeRef(artifact *zconfig.ExternalOpaqueBinaryBlob) (*types.BinaryBlobVolumeRef, error) {
// Since Volumes will be handled by volumemgr we can only provide
// reference for now. It will be updated once download is completed
// down the processing pipeline
return &types.BinaryBlobVolumeRef{
ImageName: artifact.GetImageName(),
FileName: artifact.GetFileNameToUse(),
FileMetadata: artifact.GetBlobMetaData(),
ImageID: artifact.GetImageId(),
}, nil
}
func evePatchEnvelopeActionToState(action zconfig.EVE_PATCH_ENVELOPE_ACTION) types.PatchEnvelopeState {
switch action {
case zconfig.EVE_PATCH_ENVELOPE_ACTION_STORE:
return types.PatchEnvelopeStateReady
case zconfig.EVE_PATCH_ENVELOPE_ACTION_ACTIVATE:
return types.PatchEnvelopeStateActive
}
return types.PatchEnvelopeStateError
}