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

Add ec.oci.image_manifest rego function #1357

Merged
merged 1 commit into from
Feb 22, 2024
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
37 changes: 37 additions & 0 deletions acceptance/examples/oci_image_manifest.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package manifest

import rego.v1

# METADATA
# custom:
# short_name: match
deny contains result if {
manifest := ec.oci.image_manifest(input.image.ref)
not manifest_matches(manifest)
Copy link
Member

Choose a reason for hiding this comment

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

We could take the approach of failing, and then since the manifest is in the result message, match it in the snapshot, with a bit of creativity on the digest matching regex bit if we already don't have that

Copy link
Member Author

@lcarva lcarva Feb 22, 2024

Choose a reason for hiding this comment

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

I'm gonna leave this as is for now. I don't want to blindly replace any digest with a token, and plumbing through the different digests seems non-trivial.


result := {
"code": "manifest.match",
"msg": json.marshal(manifest),
}
}

manifest_matches(manifest) if {
manifest.annotations["org.opencontainers.image.base.name"] != ""
manifest.mediaType == "application/vnd.docker.distribution.manifest.v2+json"
manifest.schemaVersion == 2
non_empty_descriptor(manifest.config, "application/vnd.docker.container.image.v1+json")
count(manifest.layers) == 2
non_empty_descriptor(manifest.layers[0], "application/vnd.docker.image.rootfs.diff.tar.gzip")
non_empty_descriptor(manifest.layers[1], "application/vnd.docker.image.rootfs.diff.tar.gzip")
}

non_empty_descriptor(descriptor, media_type) if {
descriptor.annotations == {}
descriptor.artifactType == ""
descriptor.data == ""
startswith(descriptor.digest, "sha256:")
count(descriptor.digest) == count("sha256:") + 64
descriptor.mediaType == media_type
descriptor.size > 0
descriptor.urls == []
}
77 changes: 77 additions & 0 deletions features/__snapshots__/validate_image.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3344,3 +3344,80 @@ time="${TIMESTAMP}" level=error msg="Parsing PURL \"this-is-not-a-valid-purl\" f
Error: success criteria not met

---

[fetch OCI image manifest:stdout - 1]
{
"success": true,
"components": [
{
"name": "Unnamed",
"containerImage": "${REGISTRY}/acceptance/oci-image-manifest@sha256:${REGISTRY_acceptance/oci-image-manifest:latest_DIGEST}",
"source": {},
"successes": [
{
"msg": "Pass",
"metadata": {
"code": "builtin.attestation.signature_check"
}
},
{
"msg": "Pass",
"metadata": {
"code": "builtin.attestation.syntax_check"
}
},
{
"msg": "Pass",
"metadata": {
"code": "builtin.image.signature_check"
}
},
{
"msg": "Pass",
"metadata": {
"code": "manifest.match"
}
}
],
"success": true,
"signatures": [
{
"keyid": "",
"sig": "${IMAGE_SIGNATURE_acceptance/oci-image-manifest}"
}
],
"attestations": [
{
"type": "https://in-toto.io/Statement/v0.1",
"predicateType": "https://slsa.dev/provenance/v0.2",
"predicateBuildType": "https://tekton.dev/attestations/chains/pipelinerun@v2",
"signatures": [
{
"keyid": "",
"sig": "${ATTESTATION_SIGNATURE_acceptance/oci-image-manifest}"
}
]
}
]
}
],
"key": "${known_PUBLIC_KEY_JSON}",
"policy": {
"sources": [
{
"policy": [
"git::https://${GITHOST}/git/oci-image-manifest-policy"
]
}
],
"rekorUrl": "${REKOR}",
"publicKey": "${known_PUBLIC_KEY}"
},
"ec-version": "${EC_VERSION}",
"effective-time": "${TIMESTAMP}"
}
---

[fetch OCI image manifest:stderr - 1]

---
25 changes: 25 additions & 0 deletions features/validate_image.feature
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,31 @@ Feature: evaluate enterprise contract
Then the exit status should be 0
Then the output should match the snapshot

Scenario: fetch OCI image manifest
Given a key pair named "known"
Given an image named "acceptance/oci-image-manifest"
Given a valid image signature of "acceptance/oci-image-manifest" image signed by the "known" key
Given a valid Rekor entry for image signature of "acceptance/oci-image-manifest"
Given a valid attestation of "acceptance/oci-image-manifest" signed by the "known" key
Given a valid Rekor entry for attestation of "acceptance/oci-image-manifest"
Given a git repository named "oci-image-manifest-policy" with
| main.rego | examples/oci_image_manifest.rego |
Given policy configuration named "ec-policy" with specification
"""
{
"sources": [
{
"policy": [
"git::https://${GITHOST}/git/oci-image-manifest-policy"
]
}
]
}
"""
When ec command is run with "validate image --image ${REGISTRY}/acceptance/oci-image-manifest --policy acceptance/ec-policy --public-key ${known_PUBLIC_KEY} --rekor-url ${REKOR} --show-successes"
Then the exit status should be 0
Then the output should match the snapshot

Scenario: tracing and debug logging
Given a key pair named "trace_debug"
And an image named "acceptance/trace-debug"
Expand Down
Loading