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

cosign verify-manifest now supports Jobs and CRD Types that fit within PodSpec, PodSpecTemplate, or JobSpecTemplate #697

Merged
merged 1 commit into from
Sep 16, 2021

Conversation

n3wscott
Copy link
Contributor

Summary

Rather than using the known Kubernetes types, to extract images from inbound manifests using cosign verify-manifest, we can use a simple duck type to union the types into just the fields we need and extract the image fields directly.

As a result of this change, we can now verify the manifests of things like Knative Serving.

Ticket Link

Fixes #679

Release Note

verify-manifest now supports verifying Jobs, and any other type that implements the PodSpec, PodSpecTemplate, or JobSpecTemplate shapes.

@dekkagaijin dekkagaijin self-requested a review September 16, 2021 22:08
@mattmoor
Copy link
Member

We've got knative.dev/pkg for cosigned, so you could just use duckv1.Pod and duckv1.WithPod.

@mattmoor
Copy link
Member

See:

corev1.SchemeGroupVersion.WithKind("Pod"): &duckv1.Pod{},
appsv1.SchemeGroupVersion.WithKind("ReplicaSet"): &duckv1.WithPod{},
appsv1.SchemeGroupVersion.WithKind("Deployment"): &duckv1.WithPod{},
appsv1.SchemeGroupVersion.WithKind("StatefulSet"): &duckv1.WithPod{},
appsv1.SchemeGroupVersion.WithKind("DaemonSet"): &duckv1.WithPod{},
batchv1.SchemeGroupVersion.WithKind("Job"): &duckv1.WithPod{},

@n3wscott
Copy link
Contributor Author

Ok awesome, let me switch to those.

@n3wscott
Copy link
Contributor Author

n3wscott commented Sep 16, 2021

Na, duckv1.Pod/WithPod does not help here, we are only interested in the image. I am specifically union-ing the duck types for this extraction. Plus we would still have to special case the CronJob shape.

@mattmoor
Copy link
Member

honestly parsing it twice feels less gross than your crazy struct beast 🤣

@n3wscott
Copy link
Contributor Author

n3wscott commented Sep 16, 2021

It would be parsing three times, or using the scheme. This is very simple:

type imageContainer struct {
	Spec struct {
		Containers []struct {
			Image string
		}
		InitContainers []struct {
			Image string
		}
		Template struct {
			Spec struct {
				Containers []struct {
					Image string
				}
				InitContainers []struct {
					Image string
				}
			}
		}
		JobTemplate struct {
			Spec struct {
				Template struct {
					Spec struct {
						Containers []struct {
							Image string
						}
						InitContainers []struct {
							Image string
						}
					}
				}
			}
		}
	}
}

I could break it apart if you like, I like the idea of the decoder getting to throw away the majority of the keys that come in.

}

func getImagesFromYamlManifest(manifest []byte) ([]string, error) {
dec := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(manifest), 4096)
Copy link
Member

Choose a reason for hiding this comment

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

I'm sort of puzzled by how this works without the annotations 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the marshaler assumes camelCase for the keys, the json keys are only needed when you want to control a behavior or key name (like you want init_containers vs the default initContainers).

// imageContainer is the union type that match PodSpec, PodSpecTemplate, and
// JobSpecTemplate; but filtering all keys except for `Image`.
type imageContainer struct {
Spec struct {
Copy link
Member

Choose a reason for hiding this comment

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

I'd be curious what this did if you just inlined both of duckv1.Pod and duckv1.WithPod 🤔 😅 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did try, and you can't because both duckv1.Pod and duckv1.WithPod define the common metadata and only one wins. The decoder gets real confused 🤣 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could create my own union type and break the duckv1.Pod components up and make a new parent wrapper like I do here, but this usage just is not complicated enough to bother

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I wouldn't worry about it.

@mattmoor
Copy link
Member

I don't really care, it just seems super verbose, and I was hoping for better. We can always iterate later.

Do you want to add a ksvc test, since it should work? Is there one already maybe?

@mattmoor
Copy link
Member

Also, I'd be curious if this worked with CronJob, which isn't PodSpecable 🙄

@dekkagaijin
Copy link
Member

I was hoping for better

Very poor first showing from the new Scott. Really makes you miss the old Scott

@n3wscott
Copy link
Contributor Author

Also, I'd be curious if this worked with CronJob, which isn't PodSpecable 🙄

and I test for cron jobs: https://github.com/sigstore/cosign/pull/697/files#diff-4756f111371f1063a1bc2510070a8b51ebe6d2dd2c45b28cc0c785ff1eeacd34R205-R230

I don't really care, it just seems super verbose, and I was hoping for better. We can always iterate later.

Do you want to add a ksvc test, since it should work? Is there one already maybe?

I test with a made up type: https://github.com/sigstore/cosign/pull/697/files#diff-4756f111371f1063a1bc2510070a8b51ebe6d2dd2c45b28cc0c785ff1eeacd34R121-R131

😎

@n3wscott
Copy link
Contributor Author

I was hoping for better

Very poor first showing from the new Scott. Really makes you miss the old Scott

ok I can do better. One sec.

@n3wscott
Copy link
Contributor Author

/hold

…n PodSpec, PodSpecTemplate, or JobSpecTemplate

Signed-off-by: Scott Nichols <[email protected]>
@n3wscott
Copy link
Contributor Author

/unhold

@n3wscott
Copy link
Contributor Author

tell me you have used prow without telling me you used prow...

🤣

Copy link
Member

@dekkagaijin dekkagaijin left a comment

Choose a reason for hiding this comment

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

Welcome to the club!

@dlorenc dlorenc merged commit c87e911 into sigstore:main Sep 16, 2021
@cpanato cpanato added this to the v1.3.0 milestone Sep 17, 2021
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.

Support verifying arbitrary PodSpec-like k8s Objects
5 participants