Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate issuer/subject regexp in validate webhook. #1761

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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