-
Notifications
You must be signed in to change notification settings - Fork 15
/
parse.go
144 lines (125 loc) · 3.98 KB
/
parse.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
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package policy
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/sigstore/policy-controller/pkg/apis/glob"
"github.com/sigstore/policy-controller/pkg/apis/policy/v1alpha1"
"github.com/sigstore/policy-controller/pkg/apis/policy/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
"sigs.k8s.io/yaml"
)
// Parse decodes a provided YAML document containing zero or more objects into
// a collection of unstructured.Unstructured objects.
func Parse(_ context.Context, document string) ([]*unstructured.Unstructured, error) {
docs := strings.Split(document, "\n---\n")
objs := make([]*unstructured.Unstructured, 0, len(docs))
for i, doc := range docs {
doc = strings.TrimSpace(doc)
if doc == "" {
continue
}
var obj unstructured.Unstructured
if err := yaml.Unmarshal([]byte(doc), &obj); err != nil {
return nil, fmt.Errorf("decoding object[%d]: %w", i, err)
}
if obj.GetAPIVersion() == "" {
return nil, apis.ErrMissingField("apiVersion").ViaIndex(i)
}
if obj.GetName() == "" {
return nil, apis.ErrMissingField("metadata.name").ViaIndex(i)
}
objs = append(objs, &obj)
}
return objs, nil
}
// ParseClusterImagePolicies returns ClusterImagePolicy objects found in the
// policy document.
func ParseClusterImagePolicies(ctx context.Context, document string) (cips []*v1beta1.ClusterImagePolicy, warns error, err error) {
if warns, err = Validate(ctx, document); err != nil {
return nil, warns, err
}
cips, err = parseClusterImagePolicies(ctx, document)
if err != nil {
return nil, warns, err
}
return cips, warns, nil
}
// UnsafeParseClusterImagePolicies returns ClusterImagePolicy objects found in the
// policy document, without validating if the policy is valid.
func UnsafeParseClusterImagePolicies(ctx context.Context, document string) (cips []*v1beta1.ClusterImagePolicy, err error) {
return parseClusterImagePolicies(ctx, document)
}
// CoversImage parses the given policy document, and checks if the target image
// matches any of the image globs included in the policy.
func CoversImage(ctx context.Context, document, target string) (bool, error) {
cips, _, err := ParseClusterImagePolicies(ctx, document)
if err != nil {
return false, err
}
if len(cips) != 1 {
return false, fmt.Errorf("document must contain exactly one ClusterImagePolicy (%d found)", len(cips))
}
for _, image := range cips[0].Spec.Images {
ok, err := glob.Match(image.Glob, target)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
func parseClusterImagePolicies(ctx context.Context, document string) (cips []*v1beta1.ClusterImagePolicy, err error) {
ol, err := Parse(ctx, document)
if err != nil {
return nil, err
}
cips = make([]*v1beta1.ClusterImagePolicy, 0)
for _, obj := range ol {
gv, err := schema.ParseGroupVersion(obj.GetAPIVersion())
if err != nil {
// Practically unstructured.Unstructured won't let this happen.
return nil, fmt.Errorf("error parsing apiVersion of: %w", err)
}
cip := &v1beta1.ClusterImagePolicy{}
switch gv.WithKind(obj.GetKind()) {
case v1alpha1.SchemeGroupVersion.WithKind("ClusterImagePolicy"):
v1a1 := &v1alpha1.ClusterImagePolicy{}
if err := convert(obj, v1a1); err != nil {
return nil, err
}
if err := v1a1.ConvertTo(ctx, cip); err != nil {
return nil, err
}
case v1beta1.SchemeGroupVersion.WithKind("ClusterImagePolicy"):
// This is allowed, but we should convert things.
if err := convert(obj, cip); err != nil {
return nil, err
}
default:
continue
}
cips = append(cips, cip)
}
return cips, nil
}
func convert(from interface{}, to runtime.Object) error {
bs, err := json.Marshal(from)
if err != nil {
return fmt.Errorf("Marshal() = %w", err)
}
if err := json.Unmarshal(bs, to); err != nil {
return fmt.Errorf("Unmarshal() = %w", err)
}
return nil
}