Skip to content

Commit

Permalink
Validate issuer/subject regexp in validate webhook. (#1761)
Browse files Browse the repository at this point in the history
Signed-off-by: Ville Aikas <[email protected]>
  • Loading branch information
vaikas authored Apr 14, 2022
1 parent 0c232da commit 9516998
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/cosigned/v1alpha1/clusterimagepolicy_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func (identity *Identity) Validate(ctx context.Context) *apis.FieldError {
if identity.Issuer == "" && identity.Subject == "" {
errs = errs.Also(apis.ErrMissingOneOf("issuer", "subject"))
}
if identity.Issuer != "" {
errs = errs.Also(ValidateRegex(identity.Issuer).ViaField("issuer"))
}
if identity.Subject != "" {
errs = errs.Also(ValidateRegex(identity.Subject).ViaField("subject"))
}
return errs
}

Expand Down
61 changes: 61 additions & 0 deletions pkg/apis/cosigned/v1alpha1/clusterimagepolicy_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,67 @@ func TestIdentitiesValidation(t *testing.T) {
},
},
},
{
name: "Should fail when issuer has invalid regex",
expectErr: true,
errorString: "invalid value: ****: spec.authorities[0].keyless.identities[0].issuer\nregex is invalid: error parsing regexp: missing argument to repetition operator: `*`",
policy: ClusterImagePolicy{
Spec: ClusterImagePolicySpec{
Images: []ImagePattern{
{
Glob: "globbityglob",
},
},
Authorities: []Authority{
{
Keyless: &KeylessRef{
Identities: []Identity{{Issuer: "****"}},
},
},
},
},
},
},
{
name: "Should fail when subject has invalid regex",
expectErr: true,
errorString: "invalid value: ****: spec.authorities[0].keyless.identities[0].subject\nregex is invalid: error parsing regexp: missing argument to repetition operator: `*`",
policy: ClusterImagePolicy{
Spec: ClusterImagePolicySpec{
Images: []ImagePattern{
{
Glob: "globbityglob",
},
},
Authorities: []Authority{
{
Keyless: &KeylessRef{
Identities: []Identity{{Subject: "****"}},
},
},
},
},
},
},
{
name: "Should pass when subject and issuer have valid regex",
policy: ClusterImagePolicy{
Spec: ClusterImagePolicySpec{
Images: []ImagePattern{
{
Glob: "globbityglob",
},
},
Authorities: []Authority{
{
Keyless: &KeylessRef{
Identities: []Identity{{Subject: ".*subject.*", Issuer: ".*issuer.*"}},
},
},
},
},
},
},
{
name: "Should pass when identities is valid",
expectErr: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 The Sigstore 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.
---
apiVersion: cosigned.sigstore.dev/v1alpha1
kind: ClusterImagePolicy
metadata:
name: image-policy
spec:
images:
- glob: image*
authorities:
- keyless:
identities:
- issuer: ****
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 The Sigstore 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.
---
apiVersion: cosigned.sigstore.dev/v1alpha1
kind: ClusterImagePolicy
metadata:
name: image-policy
spec:
images:
- glob: image*
authorities:
- keyless:
identities:
- subject: ****
4 changes: 4 additions & 0 deletions test/testdata/cosigned/valid/valid-policy-regex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ spec:
- keyless:
identities:
- issuer: "issue-details1"
subject: ".*subject.*"
- keyless:
identities:
- issuer: "issue.*"
- key:
data: |
-----BEGIN PUBLIC KEY-----
Expand Down

0 comments on commit 9516998

Please sign in to comment.