-
Notifications
You must be signed in to change notification settings - Fork 23
/
oci.go
307 lines (266 loc) · 9.91 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
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
// Copyright The Enterprise Contract Contributors
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
// IMPORTANT: The rego functions in this file never return an error. Instead, they return no value
// when an error is encountered. If they did return an error, opa would exit abruptly and it would
// not produce a report of which policy rules succeeded/failed.
package oci
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/types"
log "github.com/sirupsen/logrus"
"github.com/enterprise-contract/ec-cli/internal/utils/oci"
)
const (
ociBlobName = "ec.oci.blob"
ociImageManifestName = "ec.oci.image_manifest"
)
func registerOCIBlob() {
decl := rego.Function{
Name: ociBlobName,
Decl: types.NewFunction(
types.Args(
types.Named("ref", types.S).Description("OCI blob reference"),
),
types.Named("blob", types.S).Description("the OCI blob"),
),
// As per the documentation, enable memoization to ensure function evaluation is
// deterministic. But also mark it as non-deterministic because it does rely on external
// entities, i.e. OCI registry. https://www.openpolicyagent.org/docs/latest/extensions/
Memoize: true,
Nondeterministic: true,
}
rego.RegisterBuiltin1(&decl, ociBlob)
// Due to https://github.com/open-policy-agent/opa/issues/6449, we cannot set a description for
// the custom function through the call above. As a workaround we re-register the function with
// a declaration that does include the description.
ast.RegisterBuiltin(&ast.Builtin{
Name: decl.Name,
Description: "Fetch a blob from an OCI registry.",
Decl: decl.Decl,
Nondeterministic: decl.Nondeterministic,
})
}
func registerOCIImageManifest() {
platform := types.NewObject(
[]*types.StaticProperty{
{Key: "architecture", Value: types.S},
{Key: "os", Value: types.S},
{Key: "os.version", Value: types.S},
{Key: "os.features", Value: types.NewArray([]types.Type{types.S}, nil)},
{Key: "variant", Value: types.S},
{Key: "features", Value: types.NewArray([]types.Type{types.S}, nil)},
},
nil,
)
// annotations represents the map[string]string rego type
annotations := types.NewObject(nil, types.NewDynamicProperty(types.S, types.S))
descriptor := types.NewObject(
[]*types.StaticProperty{
{Key: "mediaType", Value: types.S},
{Key: "size", Value: types.N},
{Key: "digest", Value: types.S},
{Key: "data", Value: types.S},
{Key: "urls", Value: types.NewArray(
[]types.Type{types.S}, nil,
)},
{Key: "annotations", Value: annotations},
{Key: "platform", Value: platform},
{Key: "artifactType", Value: types.S},
},
nil,
)
manifest := types.NewObject(
[]*types.StaticProperty{
// Specifying the properties like this ensure the compiler catches typos when
// evaluating rego functions.
{Key: "schemaVersion", Value: types.N},
{Key: "mediaType", Value: types.S},
{Key: "config", Value: descriptor},
{Key: "layers", Value: types.NewArray(
[]types.Type{descriptor}, nil,
)},
{Key: "annotations", Value: annotations},
{Key: "subject", Value: descriptor},
},
nil,
)
decl := rego.Function{
Name: ociImageManifestName,
Decl: types.NewFunction(
types.Args(
types.Named("ref", types.S).Description("OCI image reference"),
),
types.Named("object", manifest).Description("the Image Manifest object"),
),
// As per the documentation, enable memoization to ensure function evaluation is
// deterministic. But also mark it as non-deterministic because it does rely on external
// entities, i.e. OCI registry. https://www.openpolicyagent.org/docs/latest/extensions/
Memoize: true,
Nondeterministic: true,
}
rego.RegisterBuiltin1(&decl, ociImageManifest)
// Due to https://github.com/open-policy-agent/opa/issues/6449, we cannot set a description for
// the custom function through the call above. As a workaround we re-register the function with
// a declaration that does include the description.
ast.RegisterBuiltin(&ast.Builtin{
Name: decl.Name,
Description: "Fetch an Image Manifest from an OCI registry.",
Decl: decl.Decl,
Nondeterministic: decl.Nondeterministic,
})
}
const maxBytes = 10 * 1024 * 1024 // 10 MB
func ociBlob(bctx rego.BuiltinContext, a *ast.Term) (*ast.Term, error) {
uri, ok := a.Value.(ast.String)
if !ok {
return nil, nil
}
ref, err := name.NewDigest(string(uri))
if err != nil {
log.Errorf("%s new digest: %s", ociBlobName, err)
return nil, nil
}
rawLayer, err := oci.NewClient(bctx.Context).Layer(ref)
if err != nil {
log.Errorf("%s fetch layer: %s", ociBlobName, err)
return nil, nil
}
layer, err := rawLayer.Uncompressed()
if err != nil {
log.Errorf("%s layer uncompressed: %s", ociBlobName, err)
return nil, nil
}
defer layer.Close()
// TODO: Other algorithms are technically supported, e.g. sha512. However, support for those is
// not complete in the go-containerregistry library, e.g. name.NewDigest throws an error if
// sha256 is not used. This is good for now, but may need revisiting later.
hasher := sha256.New()
// Setup some safeguards. First, use LimitReader to avoid an unbounded amount of data from being
// read. Second, use TeeReader so we can compute the digest of the content read.
reader := io.TeeReader(io.LimitReader(layer, maxBytes), hasher)
var blob bytes.Buffer
if _, err := io.Copy(&blob, reader); err != nil {
log.Errorf("%s copy buffer: %s", ociBlobName, err)
return nil, nil
}
sum := fmt.Sprintf("sha256:%x", hasher.Sum(nil))
// io.LimitReader truncates the layer if it exceeds its limit. The condition below catches this
// scenario in order to avoid unexpected behavior caused by partial data being returned.
if sum != ref.DigestStr() {
log.Errorf(
"%s computed digest, %q, not as expected, %q. Content may have been truncated at %d bytes",
ociBlobName, sum, ref.DigestStr(), maxBytes)
return nil, nil
}
return ast.StringTerm(blob.String()), nil
}
func ociImageManifest(bctx rego.BuiltinContext, a *ast.Term) (*ast.Term, error) {
log := log.WithField("rego", ociImageManifestName)
uri, ok := a.Value.(ast.String)
if !ok {
return nil, nil
}
ref, err := name.NewDigest(string(uri))
if err != nil {
log.Errorf("new digest: %s", err)
return nil, nil
}
image, err := oci.NewClient(bctx.Context).Image(ref)
if err != nil {
log.Errorf("fetch image: %s", err)
return nil, nil
}
manifest, err := image.Manifest()
if err != nil {
log.Errorf("fetch manifest: %s", err)
return nil, nil
}
if manifest == nil {
log.Error("manifest is nil")
return nil, nil
}
layers := []*ast.Term{}
for _, layer := range manifest.Layers {
layers = append(layers, newDescriptorTerm(layer))
}
manifestTerms := [][2]*ast.Term{
ast.Item(ast.StringTerm("schemaVersion"), ast.NumberTerm(json.Number(fmt.Sprintf("%d", manifest.SchemaVersion)))),
ast.Item(ast.StringTerm("mediaType"), ast.StringTerm(string(manifest.MediaType))),
ast.Item(ast.StringTerm("config"), newDescriptorTerm(manifest.Config)),
ast.Item(ast.StringTerm("layers"), ast.ArrayTerm(layers...)),
ast.Item(ast.StringTerm("annotations"), newAnnotationsTerm(manifest.Annotations)),
}
if s := manifest.Subject; s != nil {
manifestTerms = append(manifestTerms, ast.Item(ast.StringTerm("subject"), newDescriptorTerm(*s)))
}
return ast.ObjectTerm(manifestTerms...), nil
}
func newPlatformTerm(p v1.Platform) *ast.Term {
osFeatures := []*ast.Term{}
for _, f := range p.OSFeatures {
osFeatures = append(osFeatures, ast.StringTerm(f))
}
features := []*ast.Term{}
for _, f := range p.Features {
features = append(features, ast.StringTerm(f))
}
return ast.ObjectTerm(
ast.Item(ast.StringTerm("architecture"), ast.StringTerm(p.Architecture)),
ast.Item(ast.StringTerm("os"), ast.StringTerm(p.OS)),
ast.Item(ast.StringTerm("os.version"), ast.StringTerm(p.OSVersion)),
ast.Item(ast.StringTerm("os.features"), ast.ArrayTerm(osFeatures...)),
ast.Item(ast.StringTerm("variant"), ast.StringTerm(p.Variant)),
ast.Item(ast.StringTerm("features"), ast.ArrayTerm(features...)),
)
}
func newDescriptorTerm(d v1.Descriptor) *ast.Term {
urls := []*ast.Term{}
for _, url := range d.URLs {
urls = append(urls, ast.StringTerm(url))
}
dTerms := [][2]*ast.Term{
ast.Item(ast.StringTerm("mediaType"), ast.StringTerm(string(d.MediaType))),
ast.Item(ast.StringTerm("size"), ast.NumberTerm(json.Number(fmt.Sprintf("%d", d.Size)))),
ast.Item(ast.StringTerm("digest"), ast.StringTerm(d.Digest.String())),
ast.Item(ast.StringTerm("data"), ast.StringTerm(string(d.Data))),
ast.Item(ast.StringTerm("urls"), ast.ArrayTerm(urls...)),
ast.Item(ast.StringTerm("annotations"), newAnnotationsTerm(d.Annotations)),
ast.Item(ast.StringTerm("artifactType"), ast.StringTerm(d.ArtifactType)),
}
if d.Platform != nil {
dTerms = append(dTerms, ast.Item(ast.StringTerm("platform"), newPlatformTerm(*d.Platform)))
}
return ast.ObjectTerm(dTerms...)
}
func newAnnotationsTerm(annotations map[string]string) *ast.Term {
annotationTerms := [][2]*ast.Term{}
for key, value := range annotations {
annotationTerms = append(annotationTerms, ast.Item(ast.StringTerm(key), ast.StringTerm(value)))
}
return ast.ObjectTerm(annotationTerms...)
}
func init() {
registerOCIBlob()
registerOCIImageManifest()
}