-
Notifications
You must be signed in to change notification settings - Fork 57
/
oci.go
248 lines (210 loc) · 6.26 KB
/
oci.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
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023 The Falco Authors
//
// 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 test
import (
"context"
"encoding/json"
"fmt"
"io"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry/remote"
"github.com/falcosecurity/falcoctl/pkg/oci"
)
// Layer holds config and manifest for an artifact.
type Layer struct {
Config *oci.ArtifactConfig
Manifest *v1.Manifest
}
// RulesfileArtifact holds OCI metadata for a rulesfile artifact.
type RulesfileArtifact struct {
Descriptor *v1.Descriptor
Layer *Layer
Tags []string
}
// PluginArtifact holds OCI metadata for a plugin artifact.
type PluginArtifact struct {
Descriptor *v1.Descriptor
Index *v1.Index
Platforms map[string]*Layer
Tags []string
}
// ManifestFromReader extracts a v1.Manifest from a reader.
func ManifestFromReader(descReader io.Reader) (*v1.Manifest, error) {
var manifest v1.Manifest
descBytes, err := io.ReadAll(descReader)
if err != nil {
return nil, fmt.Errorf("unable to read bytes from descriptor: %w", err)
}
if err = json.Unmarshal(descBytes, &manifest); err != nil {
return nil, fmt.Errorf("unable to unmarshal manifest: %w", err)
}
return &manifest, nil
}
// IndexFromReader extracts a v1.Index from a reader.
func IndexFromReader(descReader io.Reader) (*v1.Index, error) {
var index v1.Index
descBytes, err := io.ReadAll(descReader)
if err != nil {
return nil, fmt.Errorf("unable to read bytes from descriptor: %w", err)
}
if err = json.Unmarshal(descBytes, &index); err != nil {
return nil, fmt.Errorf("unable to unmarshal index: %w", err)
}
return &index, nil
}
// ImageIndexFromReader extracts v1.Index from a reader.
func ImageIndexFromReader(descReader io.Reader) (*v1.Index, error) {
var index v1.Index
descBytes, err := io.ReadAll(descReader)
if err != nil {
return nil, fmt.Errorf("unable to read bytes from descriptor: %w", err)
}
if err = json.Unmarshal(descBytes, &index); err != nil {
return nil, fmt.Errorf("unable to unmarshal manifest: %w", err)
}
return &index, nil
}
// DependenciesFromReader extracts oci.ArtifactConfig from a reader.
func DependenciesFromReader(descReader io.Reader) (*oci.ArtifactConfig, error) {
var dep oci.ArtifactConfig
descBytes, err := io.ReadAll(descReader)
if err != nil {
if err != nil {
return nil, fmt.Errorf("unable to read bytes from descriptor: %w", err)
}
}
if err = json.Unmarshal(descBytes, &dep); err != nil {
return nil, fmt.Errorf("unable to unmarshal dependencies: %w", err)
}
return &dep, nil
}
// NewOrasRegistry creates a registry to interact with a remote OCI registry.
func NewOrasRegistry(host string, plainHTTP bool) (*remote.Registry, error) {
// Create the oras registry.
reg, err := remote.NewRegistry(host)
if err != nil {
return nil, err
}
reg.PlainHTTP = plainHTTP
return reg, nil
}
// FetchRulesfileFromRegistry fetches metadata for a plugin artifact.
func FetchRulesfileFromRegistry(ctx context.Context, ref, tag string, orasRegistry *remote.Registry) (*RulesfileArtifact, error) {
var (
reader io.ReadCloser
descriptor v1.Descriptor
manifest *v1.Manifest
tags []string
)
rf := &RulesfileArtifact{}
// First thing create repository from registry
repo, err := orasRegistry.Repository(ctx, ref)
if err != nil {
return nil, err
}
// Fetch descriptor.
if descriptor, reader, err = repo.FetchReference(ctx, fmt.Sprintf("%s/%s:%s", orasRegistry.Reference.Registry, ref, tag)); err != nil {
return nil, err
}
rf.Descriptor = &descriptor
// Fetch manifest.
if manifest, err = ManifestFromReader(reader); err != nil {
return nil, err
}
// Fetch config.
if reader, err = repo.Fetch(ctx, manifest.Config); err != nil {
return nil, err
}
artifactCfg, err := DependenciesFromReader(reader)
if err != nil {
return nil, err
}
rf.Layer = &Layer{
Config: artifactCfg,
Manifest: manifest,
}
// Fetch tags.
if err = repo.Tags(ctx, "", func(t []string) error {
tags = append(tags, t...)
return nil
}); err != nil {
return nil, err
}
rf.Tags = tags
return rf, nil
}
// FetchPluginFromRegistry fetches metadata for a plugin artifact.
func FetchPluginFromRegistry(ctx context.Context, ref, tag string, orasRegistry *remote.Registry) (*PluginArtifact, error) {
var (
reader io.ReadCloser
descriptor v1.Descriptor
index *v1.Index
tags []string
)
plugin := &PluginArtifact{
Descriptor: nil,
Index: nil,
Platforms: make(map[string]*Layer),
Tags: nil,
}
// First thing create repository from registry
repo, err := orasRegistry.Repository(ctx, ref)
if err != nil {
return nil, err
}
// Fetch descriptor.
if descriptor, reader, err = repo.FetchReference(ctx, fmt.Sprintf("%s/%s:%s", orasRegistry.Reference.Registry, ref, tag)); err != nil {
return nil, err
}
plugin.Descriptor = &descriptor
// Fetch index.
if index, err = IndexFromReader(reader); err != nil {
return nil, err
}
plugin.Index = index
// Fetch Platforms
for _, m := range index.Manifests {
var manifest *v1.Manifest
var cfg *oci.ArtifactConfig
reader, err = repo.Fetch(ctx, m)
if err != nil {
return nil, err
}
if manifest, err = ManifestFromReader(reader); err != nil {
return nil, err
}
// Fetch the config.
if reader, err = repo.Fetch(ctx, manifest.Config); err != nil {
return nil, err
}
if cfg, err = DependenciesFromReader(reader); err != nil {
return nil, err
}
platform := fmt.Sprintf("%s/%s", m.Platform.OS, m.Platform.Architecture)
plugin.Platforms[platform] = &Layer{
Config: cfg,
Manifest: manifest,
}
}
// Fetch tags.
if err = repo.Tags(ctx, "", func(t []string) error {
tags = append(tags, t...)
return nil
}); err != nil {
return nil, err
}
plugin.Tags = tags
return plugin, nil
}