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

Fix comparison in replace option for attestation #1366

Merged
merged 6 commits into from
Feb 3, 2022
Merged

Fix comparison in replace option for attestation #1366

merged 6 commits into from
Feb 3, 2022

Conversation

bburky
Copy link
Contributor

@bburky bburky commented Jan 26, 2022

Summary

Currently the replace option compares r.predicateURI == payloadData["payloadType"] when evaluating each signature:

if r.predicateURI == payloadData["payloadType"] {

This appears to be comparing the payload's predicateType (e.g a URI https://example.com/predicate or cosign.sigstore.dev/attestation/v1 for custom) to the signature's payloadType (which is always(?) application/vnd.in-toto+json). This appears to be a bug unless I'm misunderstanding something.

Fix this by making the replace option replace all attestations which match the current predicate type. I think this is the intended behavior?

The command line output has been updated to clarify the actions taken, clearly listing all the attestations retained and deleted:

$ cosign attest --type https://example.com/predicate --key "$key" --predicate predicate.json --replace "localhost:5000/$image"
Not replacing attestation predicate: https://example.com/aaaa
Replacing attestation predicate: https://example.com/predicate
Replacing attestation predicate: https://example.com/predicate

This feature was initially requested in #923 and implemented in #1039.

Ticket Link

No ticket.

Release Note

* Fixed predicate type comparison in replace option of attestation

@bburky
Copy link
Contributor Author

bburky commented Jan 26, 2022

I think this broke with #1248 which changed the DSSE envelope to always be the in-toto media type.

@dlorenc
Copy link
Member

dlorenc commented Jan 28, 2022

cc @priyawadhwa could you take a look when you get a chance here?

Copy link
Contributor

@priyawadhwa priyawadhwa left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this @bburky! Just left a couple nits. It might also be useful to test the --replace flag so that this type of bug can't be reintroduced.

I think modifying the existing test would probably work:

func TestAttestVerify(t *testing.T) {

You could try attest a second time with the --replace flag and make sure the # of attestations on the image is still 1 (if replacement fails, then there should be an additional attestation, on which the test would fail)

lmk if you run into any issues!

@bburky
Copy link
Contributor Author

bburky commented Jan 29, 2022

@priyawadhwa I can't easily add the test to TestAttestVerify(). The high level command interfaces of VerifyAttestationCommand or download/AttestationCmd only gives you an error result. I cannot easily get any other output from it unless I redirect stdout and parse JSON. I can do that, but I wanted to see if you had a simpler design in mind. Is there a lower level function I can easily invoke to retrieve an array of attestations?

Edit: actually I see now cosign.FetchAttestationsForReference may work

@bburky
Copy link
Contributor Author

bburky commented Jan 29, 2022

I added an e2e test for the the replace option using cosign.FetchAttestationsForReference() to download the attestations.

Somehow this is only failing on random.Image() generated containers. Somehow this causes only the replace option to not upload any new attestations. It silently fails.

I used skopeo to copy real images to a local registry when testing, the replace option works perfectly fine on them. I can reproduce the buggy behavior on the command line with --replace if I upload a random.Image() to a local registry, it's not unique to the unit tests.

I honestly have no idea what's wrong. This bug predates my changes, it occurs with the v1.5.0 release cosign binary on any image from random.Image() when using --replace.

Copy link
Contributor

@priyawadhwa priyawadhwa left a comment

Choose a reason for hiding this comment

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

Thanks for trying to add the test @bburky ! I left a comment for simplifying the test for now, & if you wouldn't mind maybe you could open an issue for the bug you found.

test/e2e_test.go Outdated Show resolved Hide resolved
@dlorenc
Copy link
Member

dlorenc commented Feb 3, 2022

Looks like DCO got dropped somehow, you'll need to add that back.

@bburky
Copy link
Contributor Author

bburky commented Feb 3, 2022

I think everything should be ready now.

Blake Burkhart added 6 commits February 2, 2022 21:22
Currently the replace option compares the following when evaluating each
signature:
  r.predicateURI == payloadData["payloadType"]

This appears to be comparing the payload's predicateType (e.g a URI
"https://example.com/predicate" or "cosign.sigstore.dev/attestation/v1"
for custom to the signature's payloadType (which is always
"application/vnd.in-toto+json").

Fix this by making the replace option replace all attestations which
match the current predicate type.

Signed-off-by: Blake Burkhart <[email protected]>
Signed-off-by: Blake Burkhart <[email protected]>
Signed-off-by: Blake Burkhart <[email protected]>
This reverts commit d1784cd.

Signed-off-by: Blake Burkhart <[email protected]>
Copy link
Contributor

@priyawadhwa priyawadhwa left a comment

Choose a reason for hiding this comment

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

thanks @bburky !

@dlorenc dlorenc merged commit 38c8bf5 into sigstore:main Feb 3, 2022
@github-actions github-actions bot added this to the v1.6.0 milestone Feb 3, 2022
@bburky bburky deleted the attest-replace branch February 4, 2022 03:44
hatmarch pushed a commit to hatmarch/cosign that referenced this pull request Apr 19, 2022
* Fix comparison in replace option for attestation

Currently the replace option compares the following when evaluating each
signature:
  r.predicateURI == payloadData["payloadType"]

This appears to be comparing the payload's predicateType (e.g a URI
"https://example.com/predicate" or "cosign.sigstore.dev/attestation/v1"
for custom to the signature's payloadType (which is always
"application/vnd.in-toto+json").

Fix this by making the replace option replace all attestations which
match the current predicate type.

Signed-off-by: Blake Burkhart <[email protected]>

* refactor attesatation replace

Signed-off-by: Blake Burkhart <[email protected]>

* Add e2e test for replace option in attest

Signed-off-by: Blake Burkhart <[email protected]>

* Cleanup test

Signed-off-by: Blake Burkhart <[email protected]>

* Revert removal of 2nd replace=true attest test

This reverts commit d1784cd.

Signed-off-by: Blake Burkhart <[email protected]>

* Move attestation replace test to TestAttestationReplace

Signed-off-by: Blake Burkhart <[email protected]>

Co-authored-by: Blake Burkhart <[email protected]>
mlieberman85 pushed a commit to mlieberman85/cosign that referenced this pull request May 6, 2022
* Fix comparison in replace option for attestation

Currently the replace option compares the following when evaluating each
signature:
  r.predicateURI == payloadData["payloadType"]

This appears to be comparing the payload's predicateType (e.g a URI
"https://example.com/predicate" or "cosign.sigstore.dev/attestation/v1"
for custom to the signature's payloadType (which is always
"application/vnd.in-toto+json").

Fix this by making the replace option replace all attestations which
match the current predicate type.

Signed-off-by: Blake Burkhart <[email protected]>

* refactor attesatation replace

Signed-off-by: Blake Burkhart <[email protected]>

* Add e2e test for replace option in attest

Signed-off-by: Blake Burkhart <[email protected]>

* Cleanup test

Signed-off-by: Blake Burkhart <[email protected]>

* Revert removal of 2nd replace=true attest test

This reverts commit d1784cd.

Signed-off-by: Blake Burkhart <[email protected]>

* Move attestation replace test to TestAttestationReplace

Signed-off-by: Blake Burkhart <[email protected]>

Co-authored-by: Blake Burkhart <[email protected]>
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.

3 participants