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

1417 policy validations #1548

Merged
merged 12 commits into from
Mar 10, 2022
Merged

Conversation

kkavitha
Copy link
Contributor

@kkavitha kkavitha commented Mar 3, 2022

Summary

Create a validation webhook for policy CRD and their tests
Fixes: #1417

cc: @hectorj2f @mattmoor @vaikas

Kavitha Krishnan and others added 3 commits March 3, 2022 15:46
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 3, 2022

Codecov Report

Merging #1548 (524dfc4) into main (4b2c3c0) will increase coverage by 0.63%.
The diff coverage is 51.32%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1548      +/-   ##
==========================================
+ Coverage   26.49%   27.12%   +0.63%     
==========================================
  Files         126      136      +10     
  Lines        7214     7686     +472     
==========================================
+ Hits         1911     2085     +174     
- Misses       5093     5374     +281     
- Partials      210      227      +17     
Impacted Files Coverage Δ
cmd/cosign/policy_webhook/main.go 0.00% <0.00%> (ø)
cmd/cosign/webhook/main.go 0.00% <ø> (ø)
...cosigned/v1alpha1/clusterimagepolicy_validation.go 82.85% <82.85%> (ø)
pkg/cosign/kubernetes/webhook/validation.go 74.02% <0.00%> (-7.80%) ⬇️
...s/cosigned/v1alpha1/clusterimagepolicy_defaults.go 0.00% <0.00%> (ø)
...kg/apis/cosigned/v1alpha1/zz_generated.deepcopy.go 0.00% <0.00%> (ø)
pkg/reconciler/clusterimagepolicy/controller.go 81.57% <0.00%> (ø)
pkg/apis/config/image_policies.go 71.42% <0.00%> (ø)
pkg/apis/config/store.go 100.00% <0.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b2c3c0...524dfc4. Read the comment docs.

@vaikas
Copy link
Contributor

vaikas commented Mar 4, 2022

I'm just curious why the test run needed to be approved since it says first time contributor, but we got the other PR merged yesterday, so I don't understand why it blocked you again? 🤔

@kkavitha
Copy link
Contributor Author

kkavitha commented Mar 4, 2022

It didn't need approval to run when I was the author. Looks like Adam has to contribute a PR under his name to get the permission. The merged PR was under my name.

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
creationTimestamp: null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think we need this, or L34 below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, we don't need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, will remove it!

set -ex
echo '::group:: invalid policy: both glob and regex'

cat > policy.yaml <<EOF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we might want to create something like:
./test/testadata/success
./test/testdata/failure

directories and then create files there with names like:
./test/testdata/failure/both-glob-and-regex.yaml
./test/testdata/failure/key-with-multiple-properties.yaml

And then the both-glob-and-regex.yaml would look like:

# Invalid policy: both glob and regex
apiVersion: cosigned.sigstore.dev/v1alpha1
kind: ClusterImagePolicy
metadata:
  name: image-policy
spec:
  images:
  - glob: image**
    regex: image.*
    authorities:
    - key:
        data: "---somedata---"

Then we could have a for loop for both directories and for failures they should fail, and for successes they
should be created. For success case, then we could also delete after created, so that we don't have to deal
with conflicting names.

Just thinking that having those files might make it easier then to add new tests in the future as well as have some example configurations that could also serve as documentation. They could also be used for testing the validations for the fields. Just a thought.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with the proposed filenames under testdata, I'd suggest to add a cosigned prefix to know where they are been used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I will push them to new files.

Copy link
Contributor

@hectorj2f hectorj2f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kkavitha I checked the code, the e2e_test_cosigned.sh are failing to reject invalid pods because the cluster image policy has not been installed. We'll need to install so the tests are working again,


set -ex

echo '::group:: invalid policy: both glob and regex'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesomesauce! So, if we don't hand code these files, but use a for loop here:

for i in `ls ./test/testdata/cosigned/invalid/`
do
  if kubectl create -f ./test/testdata/cosigned/invalid/$i ; then
  then
    echo $i failed when it should not have
    exit 1
  else $i rejected as expected
done

Or something like that. Then if new files are added to .../invalid/ directory, then we don't have to modify this file at all.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that sounds good. Will make the change!

Adam Shamblin and others added 4 commits March 7, 2022 14:28
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

set -ex

echo '::group:: Invalid policy tests:'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is missing its ::endgroup::

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@hectorj2f
Copy link
Contributor

@kkavitha In addition to #1548 (comment), I saw the ValidationWebhookConfiguration is not set for the validating.clusterimagepolicy.sigstore.dev the rules are not defined, so the webhook won't be called for the ClusterImagePolicy.

Kavitha Krishnan and others added 4 commits March 8, 2022 17:28
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Copy link
Contributor

@vaikas vaikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

identities:
- issuer: "issue-details1"
- key:
data: "---some-key---"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this:
#1581
to track tighter validations.
But let's get this in, it's a great starting point!

@vaikas vaikas merged commit c341168 into sigstore:main Mar 10, 2022
@github-actions github-actions bot added this to the v1.7.0 milestone Mar 10, 2022
mlieberman85 pushed a commit to mlieberman85/cosign that referenced this pull request May 6, 2022
* Register webhook and add configuration

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Add validation tests for policy crd

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Update clusterrole;Add ko local for webhook

Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Add e2e tests for policy creation

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>
Signed-off-by: Adam Shamblin <ashamblin@vmware.com>

* Move testdata to a separate folder; Remove current timestamp proeprty

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Move crd tests to a different step

Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Refactor policy crd test script

Signed-off-by: Adam Shamblin <ashamblin@vmware.com>
Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Separate policy webhooks from cosigned webhooks

Signed-off-by: Adam Shamblin <ashamblin@vmware.com>

* Fix lint

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

* Remove unused var

Signed-off-by: Adam Shamblin <ashamblin@vmware.com>

* Add policy webhook secrets

Signed-off-by: Kavitha Krishnan <krishnanka@vmware.com>

Co-authored-by: Adam Shamblin <ashamblin@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: create image policy types and CRDs
5 participants