-
Notifications
You must be signed in to change notification settings - Fork 18
/
info.go
216 lines (202 loc) · 5.35 KB
/
info.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package ociutils
import (
"archive/tar"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/opencontainers/go-digest"
"sigs.k8s.io/yaml"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/compression"
"github.com/open-component-model/ocm/pkg/contexts/oci/artdesc"
"github.com/open-component-model/ocm/pkg/contexts/oci/cpi"
"github.com/open-component-model/ocm/pkg/mime"
)
type BlobInfo struct {
Error string `json:"error,omitempty"`
Unparsed string `json:"unparsed,omitempty"`
Content json.RawMessage `json:"content,omitempty"`
Type string `json:"type,omitempty"`
Digest digest.Digest `json:"digest,omitempty"`
Size int64 `json:"size,omitempty"`
Info interface{} `json:"info,omitempty"`
}
type ArtifactInfo struct {
Digest digest.Digest `json:"digest"`
Type string `json:"type"`
Descriptor interface{} `json:"descriptor"`
Config *BlobInfo `json:"config,omitempty"`
Layers []*BlobInfo `json:"layers,omitempty"`
Manifests []*BlobInfo `json:"manifests,omitempty"`
}
func GetArtifactInfo(art cpi.ArtifactAccess, layerFiles bool) *ArtifactInfo {
if art.IsManifest() {
return GetManifestInfo(art.ManifestAccess(), layerFiles)
}
if art.IsIndex() {
return GetIndexInfo(art.IndexAccess(), layerFiles)
}
return &ArtifactInfo{Type: "unspecific"}
}
func GetManifestInfo(m cpi.ManifestAccess, layerFiles bool) *ArtifactInfo {
info := &ArtifactInfo{
Type: artdesc.MediaTypeImageManifest,
Descriptor: m.GetDescriptor(),
}
b, err := m.Blob()
if err == nil {
info.Digest = b.Digest()
}
man := m.GetDescriptor()
cfg := &BlobInfo{
Content: nil,
Type: man.Config.MediaType,
Digest: man.Config.Digest,
Size: man.Config.Size,
}
info.Config = cfg
config, err := accessio.BlobData(m.GetBlob(man.Config.Digest))
if err != nil {
cfg.Error = "error getting config blob: " + err.Error()
} else {
cfg.Content = json.RawMessage(config)
}
h := getHandler(man.Config.MediaType)
if h != nil {
pr, buf := common.NewBufferedPrinter()
h.Description(pr, m, config)
cfg.Info = buf.String()
}
for _, l := range man.Layers {
blobinfo := &BlobInfo{
Type: l.MediaType,
Digest: l.Digest,
Size: l.Size,
}
blob, err := m.GetBlob(l.Digest)
if err != nil {
blobinfo.Error = "error getting blob: " + err.Error()
} else {
blobinfo.Info = GetLayerInfo(blob, layerFiles)
}
info.Layers = append(info.Layers, blobinfo)
}
return info
}
type LayerInfo struct {
Description string `json:"description,omitempty"`
Error string `json:"error,omitempty"`
Unparsed string `json:"unparsed,omitempty"`
Content interface{} `json:"content,omitempty"`
}
func GetLayerInfo(blob accessio.BlobAccess, layerFiles bool) *LayerInfo {
info := &LayerInfo{}
if mime.IsJSON(blob.MimeType()) {
info.Description = "json document"
data, err := blob.Get()
if err != nil {
info.Error = "cannot read blob: " + err.Error()
return info
}
var j interface{}
err = json.Unmarshal(data, &j)
if err != nil {
if len(data) < 10000 {
info.Unparsed = string(data)
}
info.Error = "invalid json: " + err.Error()
return info
}
info.Content = j
return info
}
if mime.IsYAML(blob.MimeType()) {
info.Description = "yaml document"
data, err := blob.Get()
if err != nil {
info.Error = "cannot read blob: " + err.Error()
return info
}
var j interface{}
err = yaml.Unmarshal(data, &j)
if err != nil {
if len(data) < 10000 {
info.Unparsed = string(data)
}
info.Error = "invalid yaml: " + err.Error()
return info
}
info.Content = j
return info
}
if !layerFiles {
return nil
}
reader, err := blob.Reader()
if err != nil {
info.Error = "cannot read blob: " + err.Error()
return info
}
defer reader.Close()
reader, _, err = compression.AutoDecompress(reader)
if err != nil {
info.Error = "cannot decompress blob: " + err.Error()
return info
}
var files []string
tr := tar.NewReader(reader)
for {
header, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
info.Content = files
return info
}
if len(files) == 0 {
info.Description = "no tar"
return info
}
info.Error = fmt.Sprintf("tar error: %s", err)
return info
}
if len(files) == 0 {
info.Description = "tar file"
}
switch header.Typeflag {
case tar.TypeDir:
files = append(files, fmt.Sprintf("dir: %s\n", header.Name))
case tar.TypeReg:
files = append(files, fmt.Sprintf("file: %s\n", header.Name))
}
}
}
func GetIndexInfo(i cpi.IndexAccess, layerFiles bool) *ArtifactInfo {
info := &ArtifactInfo{
Type: artdesc.MediaTypeImageIndex,
Descriptor: i.GetDescriptor(),
}
b, err := i.Blob()
if err == nil {
info.Digest = b.Digest()
}
for _, l := range i.GetDescriptor().Manifests {
blobinfo := &BlobInfo{
Type: l.MediaType,
Digest: l.Digest,
Size: l.Size,
}
a, err := i.GetArtifact(l.Digest)
if err != nil {
blobinfo.Error = fmt.Sprintf("cannot get artifact: %s\n", err)
} else {
blobinfo.Info = GetArtifactInfo(a, layerFiles)
}
info.Layers = append(info.Layers, blobinfo)
}
return info
}