-
Notifications
You must be signed in to change notification settings - Fork 18
/
state.go
284 lines (254 loc) · 8.52 KB
/
state.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
package genericocireg
import (
"archive/tar"
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"github.com/go-test/deep"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/logging"
"github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/contexts/oci/artdesc"
"github.com/open-component-model/ocm/pkg/contexts/oci/repositories/ctf/format"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/genericocireg/componentmapping"
ocmlog "github.com/open-component-model/ocm/pkg/logging"
"github.com/open-component-model/ocm/pkg/utils"
)
func NewState(mode accessobj.AccessMode, name, version string, access oci.ManifestAccess, compat ...bool) (accessobj.State, error) {
return accessobj.NewState(mode, NewStateAccess(access, compat...), NewStateHandler(name, version))
}
// StateAccess handles the component descriptor persistence in an OCI Manifest.
type StateAccess struct {
access oci.ManifestAccess
layerMedia string
compat bool
}
var _ accessobj.StateAccess = (*StateAccess)(nil)
func NewStateAccess(access oci.ManifestAccess, compat ...bool) accessobj.StateAccess {
return &StateAccess{
compat: utils.Optional(compat...),
access: access,
}
}
func (s *StateAccess) Get() (blobaccess.BlobAccess, error) {
mediaType := s.access.GetDescriptor().Config.MediaType
switch mediaType {
case componentmapping.ComponentDescriptorConfigMimeType,
componentmapping.LegacyComponentDescriptorConfigMimeType,
componentmapping.Legacy2ComponentDescriptorConfigMimeType:
return s.get()
case "":
return nil, errors.ErrNotFound(cpi.KIND_COMPONENTVERSION)
default:
return nil, errors.Newf("artifact is no component: %s", mediaType)
}
}
func (s *StateAccess) get() (blobaccess.BlobAccess, error) {
var config ComponentDescriptorConfig
data, err := blobaccess.BlobData(s.access.GetConfigBlob())
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &config)
if err != nil {
return nil, err
}
if config.ComponentDescriptorLayer == nil || config.ComponentDescriptorLayer.Digest == "" {
return nil, errors.ErrInvalid("component descriptor config")
}
switch config.ComponentDescriptorLayer.MediaType {
case componentmapping.ComponentDescriptorJSONMimeType,
componentmapping.LegacyComponentDescriptorJSONMimeType,
componentmapping.ComponentDescriptorYAMLMimeType,
componentmapping.LegacyComponentDescriptorYAMLMimeType:
s.layerMedia = ""
return s.access.GetBlob(config.ComponentDescriptorLayer.Digest)
case componentmapping.ComponentDescriptorTarMimeType,
componentmapping.LegacyComponentDescriptorTarMimeType,
componentmapping.Legacy2ComponentDescriptorTarMimeType:
d, err := s.access.GetBlob(config.ComponentDescriptorLayer.Digest)
if err != nil {
return nil, err
}
r, err := d.Reader()
if err != nil {
return nil, err
}
defer r.Close()
data, err := s.readComponentDescriptorFromTar(r)
if err != nil {
return nil, err
}
s.layerMedia = config.ComponentDescriptorLayer.MediaType
return blobaccess.ForData(componentmapping.ComponentDescriptorYAMLMimeType, data), nil
default:
return nil, errors.ErrInvalid("config mediatype", config.ComponentDescriptorLayer.MediaType)
}
}
// readComponentDescriptorFromTar reads the component descriptor from a tar.
// The component is expected to be inside the tar at "/component-descriptor.yaml".
func (s *StateAccess) readComponentDescriptorFromTar(r io.Reader) ([]byte, error) {
tr := tar.NewReader(r)
for {
header, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
return nil, errors.New("no component descriptor found in tar")
}
return nil, fmt.Errorf("unable to read tar: %w", err)
}
if strings.TrimLeft(header.Name, "/") != compdesc.ComponentDescriptorFileName {
continue
}
var data bytes.Buffer
//nolint:gosec // We don't know what size limit we could set, the tar
// archive can be an image layer and that can even reach the gigabyte range.
// For now, we acknowledge the risk.
//
// We checked other softwares and tried to figure out how they manage this,
// but it's handled the same way.
if _, err := io.Copy(&data, tr); err != nil {
return nil, fmt.Errorf("erro while reading component descriptor file from tar: %w", err)
}
return data.Bytes(), err
}
}
func (s StateAccess) Digest() digest.Digest {
blob, err := s.access.GetConfigBlob()
if err != nil {
return ""
}
return blob.Digest()
}
func (s *StateAccess) Put(data []byte) error {
desc := s.access.GetDescriptor()
mediaType := desc.Config.MediaType
if mediaType == "" {
if s.compat {
mediaType = componentmapping.LegacyComponentDescriptorConfigMimeType
} else {
mediaType = componentmapping.ComponentDescriptorConfigMimeType
}
desc.Config.MediaType = mediaType
}
arch, err := s.writeComponentDescriptorTar(data)
if err != nil {
return err
}
config := ComponentDescriptorConfig{
ComponentDescriptorLayer: artdesc.DefaultBlobDescriptor(arch),
}
configdata, err := json.Marshal(&config)
if err != nil {
return err
}
err = s.access.AddBlob(arch)
if err != nil {
return err
}
s.layerMedia = arch.MimeType()
configblob := blobaccess.ForData(mediaType, configdata)
err = s.access.AddBlob(configblob)
if err != nil {
return err
}
desc.Config = *artdesc.DefaultBlobDescriptor(configblob)
if len(desc.Layers) < 2 {
desc.Layers = []ociv1.Descriptor{*artdesc.DefaultBlobDescriptor(arch)}
} else {
desc.Layers[0] = *artdesc.DefaultBlobDescriptor(arch)
}
return nil
}
// writeComponentDescriptorTar writes the component descriptor into a tar.
// The component is expected to be inside the tar at "/component-descriptor.yaml".
func (s *StateAccess) writeComponentDescriptorTar(data []byte) (cpi.BlobAccess, error) {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
err := tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: componentmapping.ComponentDescriptorFileName,
Size: int64(len(data)),
ModTime: format.ModTime,
})
media := s.layerMedia
if media == "" {
if s.compat {
media = componentmapping.LegacyComponentDescriptorTarMimeType
} else {
media = componentmapping.ComponentDescriptorTarMimeType
}
}
if err != nil {
return nil, errors.Newf("unable to add component descriptor header: %s", err)
}
if _, err := io.Copy(tw, bytes.NewBuffer(data)); err != nil {
return nil, errors.Newf("unable to write component-descriptor to tar: %s", err)
}
if err := tw.Close(); err != nil {
return nil, errors.Newf("unable to close tar writer: %s", err)
}
return blobaccess.ForData(media, buf.Bytes()), nil
}
// ComponentDescriptorConfig is a Component-Descriptor OCI configuration that is used to store the reference to the
// (pseudo-)layer used to store the Component-Descriptor in.
type ComponentDescriptorConfig struct {
ComponentDescriptorLayer *ociv1.Descriptor `json:"componentDescriptorLayer,omitempty"`
}
////////////////////////////////////////////////////////////////////////////////
// StateHandler handles the encoding of a component descriptor.
type StateHandler struct {
name string
version string
}
var _ accessobj.StateHandler = (*StateHandler)(nil)
func NewStateHandler(name, version string) accessobj.StateHandler {
return &StateHandler{
name: name,
version: version,
}
}
func (i StateHandler) Initial() interface{} {
return compdesc.New(i.name, i.version)
}
// Encode always provides a yaml representation.
func (i StateHandler) Encode(d interface{}) ([]byte, error) {
desc, ok := d.(*compdesc.ComponentDescriptor)
if !ok {
return nil, fmt.Errorf("failed to assert type %t to *compdesc.ComponentDescriptor", d)
}
desc.Name = i.name
desc.Version = i.version
return compdesc.Encode(desc)
}
// Decode always accepts a yaml representation, and therefore json, also.
func (i StateHandler) Decode(data []byte) (interface{}, error) {
return compdesc.Decode(data)
}
func (i StateHandler) Equivalent(a, b interface{}) bool {
if l := Logger(ocmlog.Context(), TAG_CDDIFF); l.Enabled(logging.DebugLevel) {
diff := deep.Equal(a, b)
if len(diff) > 0 {
l.Debug("component descriptor has been changed", "diff", diff)
return false
}
return true
}
ea, err := i.Encode(a)
if err == nil {
eb, err := i.Encode(b)
if err == nil {
return bytes.Equal(ea, eb)
}
}
return reflect.DeepEqual(a, b)
}