Skip to content

Commit ee7c481

Browse files
authoredDec 5, 2022
Merge pull request #399 from hectorj2f/source_secrets
feat: accept source without setting any oci repository
2 parents 4445003 + 9dc37b7 commit ee7c481

File tree

7 files changed

+101
-40
lines changed

7 files changed

+101
-40
lines changed
 

‎pkg/apis/policy/v1alpha1/clusterimagepolicy_validation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ func (keyless *KeylessRef) Validate(ctx context.Context) *apis.FieldError {
221221

222222
func (source *Source) Validate(ctx context.Context) *apis.FieldError {
223223
var errs *apis.FieldError
224-
if source.OCI == "" {
225-
errs = errs.Also(apis.ErrMissingField("oci"))
226-
} else if err := common.ValidateOCI(source.OCI); err != nil {
227-
errs = errs.Also(apis.ErrInvalidValue(source.OCI, "oci", err.Error()))
224+
if source.OCI != "" {
225+
if err := common.ValidateOCI(source.OCI); err != nil {
226+
errs = errs.Also(apis.ErrInvalidValue(source.OCI, "oci", err.Error()))
227+
}
228228
}
229229

230230
if len(source.SignaturePullSecrets) > 0 {

‎pkg/apis/policy/v1alpha1/clusterimagepolicy_validation_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -752,20 +752,6 @@ func TestAuthoritiesValidation(t *testing.T) {
752752
},
753753
},
754754
},
755-
}, {
756-
name: "Should fail when source oci is empty",
757-
errorString: "missing field(s): spec.authorities[0].source[0].oci",
758-
policy: ClusterImagePolicy{
759-
Spec: ClusterImagePolicySpec{
760-
Images: []ImagePattern{{Glob: "gcr.io/*"}},
761-
Authorities: []Authority{
762-
{
763-
Key: &KeyRef{KMS: "kms://key/path"},
764-
Sources: []Source{{OCI: ""}},
765-
},
766-
},
767-
},
768-
},
769755
}, {
770756
name: "Should pass with multiple source oci is present",
771757
policy: ClusterImagePolicy{
@@ -909,6 +895,19 @@ func TestAuthoritiesValidation(t *testing.T) {
909895
},
910896
},
911897
},
898+
}, {
899+
name: "Should pass when source oci is empty",
900+
policy: ClusterImagePolicy{
901+
Spec: ClusterImagePolicySpec{
902+
Images: []ImagePattern{{Glob: "gcr.io/*"}},
903+
Authorities: []Authority{
904+
{
905+
Key: &KeyRef{KMS: "kms://key/path"},
906+
Sources: []Source{{OCI: ""}},
907+
},
908+
},
909+
},
910+
},
912911
},
913912
}
914913

‎pkg/apis/policy/v1beta1/clusterimagepolicy_validation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ func (keyless *KeylessRef) Validate(ctx context.Context) *apis.FieldError {
222222

223223
func (source *Source) Validate(ctx context.Context) *apis.FieldError {
224224
var errs *apis.FieldError
225-
if source.OCI == "" {
226-
errs = errs.Also(apis.ErrMissingField("oci"))
227-
} else if err := common.ValidateOCI(source.OCI); err != nil {
228-
errs = errs.Also(apis.ErrInvalidValue(source.OCI, "oci", err.Error()))
225+
if source.OCI != "" {
226+
if err := common.ValidateOCI(source.OCI); err != nil {
227+
errs = errs.Also(apis.ErrInvalidValue(source.OCI, "oci", err.Error()))
228+
}
229229
}
230230

231231
if len(source.SignaturePullSecrets) > 0 {

‎pkg/apis/policy/v1beta1/clusterimagepolicy_validation_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -704,20 +704,6 @@ func TestAuthoritiesValidation(t *testing.T) {
704704
},
705705
},
706706
},
707-
}, {
708-
name: "Should fail when source oci is empty",
709-
errorString: "missing field(s): spec.authorities[0].source[0].oci",
710-
policy: ClusterImagePolicy{
711-
Spec: ClusterImagePolicySpec{
712-
Images: []ImagePattern{{Glob: "*"}},
713-
Authorities: []Authority{
714-
{
715-
Key: &KeyRef{KMS: "kms://key/path"},
716-
Sources: []Source{{OCI: ""}},
717-
},
718-
},
719-
},
720-
},
721707
}, {
722708
name: "Should pass with multiple source oci is present",
723709
policy: ClusterImagePolicy{
@@ -942,6 +928,19 @@ func TestAuthoritiesValidation(t *testing.T) {
942928
},
943929
},
944930
},
931+
}, {
932+
name: "Should pass when source oci is empty",
933+
policy: ClusterImagePolicy{
934+
Spec: ClusterImagePolicySpec{
935+
Images: []ImagePattern{{Glob: "*"}},
936+
Authorities: []Authority{
937+
{
938+
Key: &KeyRef{KMS: "kms://key/path"},
939+
Sources: []Source{{OCI: ""}},
940+
},
941+
},
942+
},
943+
},
945944
},
946945
}
947946

‎pkg/reconciler/clusterimagepolicy/clusterimagepolicy_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ RCTfQ5s1kD+hGMSE1rH7s46hmXEeyhnlRnaGF8eMU/SBJE/2NKPnxE7WzQ==
9393

9494
replaceCIPKeySourcePatch = `[{"op":"replace","path":"/data/test-cip","value":"{\"uid\":\"test-uid\",\"resourceVersion\":\"0123456789\",\"images\":[{\"glob\":\"ghcr.io/example/*\"}],\"authorities\":[{\"name\":\"authority-0\",\"key\":{\"data\":\"-----BEGIN PUBLIC KEY-----\\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExB6+H6054/W1SJgs5JR6AJr6J35J\\nRCTfQ5s1kD+hGMSE1rH7s46hmXEeyhnlRnaGF8eMU/SBJE/2NKPnxE7WzQ==\\n-----END PUBLIC KEY-----\",\"hashAlgorithm\":\"sha256\"},\"source\":[{\"oci\":\"example.com/alternative/signature\",\"signaturePullSecrets\":[{\"name\":\"signaturePullSecretName\"}]}]}],\"mode\":\"enforce\"}"}]`
9595

96+
replaceCIPKeySourceWithoutOCIPatch = `[{"op":"replace","path":"/data/test-cip","value":"{\"uid\":\"test-uid\",\"resourceVersion\":\"0123456789\",\"images\":[{\"glob\":\"ghcr.io/example/*\"}],\"authorities\":[{\"name\":\"authority-0\",\"key\":{\"data\":\"-----BEGIN PUBLIC KEY-----\\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExB6+H6054/W1SJgs5JR6AJr6J35J\\nRCTfQ5s1kD+hGMSE1rH7s46hmXEeyhnlRnaGF8eMU/SBJE/2NKPnxE7WzQ==\\n-----END PUBLIC KEY-----\",\"hashAlgorithm\":\"sha256\"},\"source\":[{\"signaturePullSecrets\":[{\"name\":\"signaturePullSecretName\"}]}]}],\"mode\":\"enforce\"}"}]`
97+
9698
resourceVersion = "0123456789"
9799
uid = "test-uid"
98100
)
@@ -605,6 +607,35 @@ func TestReconcile(t *testing.T) {
605607
WantPatches: []clientgotesting.PatchActionImpl{
606608
makePatch(replaceCIPKeySourcePatch),
607609
},
610+
}, {
611+
Name: "Key with data, source, no oci but signature pull secrets",
612+
Key: testKey,
613+
614+
SkipNamespaceValidation: true, // Cluster scoped
615+
Objects: []runtime.Object{
616+
NewClusterImagePolicy(cipName,
617+
WithUID(uid),
618+
WithResourceVersion(resourceVersion),
619+
WithFinalizer,
620+
WithImagePattern(v1alpha1.ImagePattern{
621+
Glob: glob,
622+
}),
623+
WithAuthority(v1alpha1.Authority{
624+
Key: &v1alpha1.KeyRef{
625+
Data: validPublicKeyData,
626+
},
627+
Sources: []v1alpha1.Source{{
628+
SignaturePullSecrets: []corev1.LocalObjectReference{
629+
{Name: "signaturePullSecretName"},
630+
},
631+
}},
632+
}),
633+
),
634+
makeConfigMap(),
635+
},
636+
WantPatches: []clientgotesting.PatchActionImpl{
637+
makePatch(replaceCIPKeySourceWithoutOCIPatch),
638+
},
608639
}, {
609640
Name: "ClusterImagePolicy with glob and KMS key, for unsupported KMS provider",
610641
Key: cipKMSName,

‎pkg/webhook/clusterimagepolicy/clusterimagepolicy_types.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ func (a *Authority) UnmarshalJSON(data []byte) error {
193193
// Determine additional RemoteOpts
194194
if len(rawAuthority.Sources) > 0 {
195195
for _, source := range rawAuthority.Sources {
196-
if targetRepoOverride, err := name.NewRepository(source.OCI); err != nil {
197-
return fmt.Errorf("failed to determine source: %w", err)
198-
} else if (targetRepoOverride != name.Repository{}) {
199-
rawAuthority.RemoteOpts = append(rawAuthority.RemoteOpts, ociremote.WithTargetRepository(targetRepoOverride))
196+
if source.OCI != "" {
197+
if targetRepoOverride, err := name.NewRepository(source.OCI); err != nil {
198+
return fmt.Errorf("failed to determine source: %w", err)
199+
} else if (targetRepoOverride != name.Repository{}) {
200+
rawAuthority.RemoteOpts = append(rawAuthority.RemoteOpts, ociremote.WithTargetRepository(targetRepoOverride))
201+
}
200202
}
201203
}
202204
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2022 The Sigstore Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
---
15+
apiVersion: policy.sigstore.dev/v1alpha1
16+
kind: ClusterImagePolicy
17+
metadata:
18+
name: image-policy
19+
spec:
20+
images:
21+
- glob: images.*
22+
authorities:
23+
- keyless:
24+
ca-cert:
25+
secretRef:
26+
name: ca-cert-secret
27+
namespace: some-namespacemak
28+
source:
29+
- signaturePullSecrets:
30+
- name: examplePullSecret

0 commit comments

Comments
 (0)
Please sign in to comment.