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

continued sign refacc #1098

Merged
merged 10 commits into from
Nov 30, 2021
Merged

continued sign refacc #1098

merged 10 commits into from
Nov 30, 2021

Conversation

dekkagaijin
Copy link
Member

@dekkagaijin dekkagaijin commented Nov 24, 2021

This continues to refactor signing operations re: #844 and #931

TODO:

  • move over fulcio operations as well
  • perform all signing operations via this new library
  • perform all attestation operations via this new library
  • for efficient reuse, expose the rekor client in tlog upload operations
NONE

@dekkagaijin dekkagaijin force-pushed the sign-refacc branch 7 times, most recently from ab9569d to 153228d Compare November 25, 2021 03:36
@dekkagaijin dekkagaijin marked this pull request as ready for review November 25, 2021 04:15
@dekkagaijin dekkagaijin changed the title WIP: continued sign refacc continued sign refacc Nov 25, 2021
@dekkagaijin dekkagaijin requested a review from mattmoor November 25, 2021 04:24
Comment on lines 39 to 48
type SigningResults struct {
SignedPayload []byte
Signature []byte
Pub crypto.PublicKey
Cert, Chain []byte
Bundle *oci.Bundle

OCISignature oci.Signature
SignedEntity oci.SignedEntity
}
Copy link
Member

Choose a reason for hiding this comment

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

The SignedEntity is what's passed in, and virtually all of the stuff on top is accessible from the oci.Signature, so I think the result should just be oci.Signature.

Copy link
Member

Choose a reason for hiding this comment

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

So it seems like this result struct is built up across multiple implementations that must be composed a particular way to work, which isn't really the goal of composition. 🤔

Given the pieces I'm seeing I think the interface should just be:

type Signer interface {
	Sign(ctx context.Context, payload []byte) (oci.Signature, error)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

roger. The only thing not captured by oci.Signature was the signature's public key

Comment on lines 151 to 156
// Attach the signature to the entity.
newSE, err := mutate.AttachSignatureToEntity(results.SignedEntity, results.OCISignature, mutate.WithDupeDetector(sa.DD))
if err != nil {
return nil, err
}
results.SignedEntity = newSE
Copy link
Member

Choose a reason for hiding this comment

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

Don't conflate signing with mutating the signed entity to carry that signature. The signer should just sign, let the caller worry about this.

Copy link
Member Author

Choose a reason for hiding this comment

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

roger

return &SigningResults{
Signature: sig,
SignedPayload: req.SignaturePayload,
SignedEntity: req.SignedEntity,
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be the only use of req.SignedEntity. What are our expectations of the Payload?

Copy link
Member Author

@dekkagaijin dekkagaijin Nov 29, 2021

Choose a reason for hiding this comment

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

I was thinking a prior step might want to modify the req.SignedEntity, which is immutable, so I wanted to return the actual Entity in the results.

Our expectations for payload are to be able to upload it to Rekor, but we can get that from the oci.Signature

Comment on lines 388 to 393
if err != nil {
return nil, errors.Wrap(err, "getting key from Fulcio")
Copy link
Member

Choose a reason for hiding this comment

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

This saves ~3 LoC and is less clear, I'd revert.

If you want to save 2 LoC without losing clarity, you can fold the call into the if:

if k, err = fulcio.NewSigner(...); err != nil {
     return nil, errors.Wrap(err, "getting key from Fulcio")
}

Copy link
Member

Choose a reason for hiding this comment

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

Also, I would expect that ultimately the bonus logic in fulcioverifier just becomes yet-another composition of the Signer interface.

Copy link
Member Author

Choose a reason for hiding this comment

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

right, already have a TODO for it

@@ -74,7 +74,7 @@ func WriteSignedImageIndexImages(ref name.Reference, sii oci.SignedImageIndex, o
func WriteSignatures(repo name.Repository, se oci.SignedEntity, opts ...Option) error {
o := makeOptions(repo, opts...)

// Access the signature list to publish
// Access the signature list to published
Copy link
Member

Choose a reason for hiding this comment

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

this is an incorrect correction

Copy link
Member Author

Choose a reason for hiding this comment

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

ACK

@dekkagaijin dekkagaijin force-pushed the sign-refacc branch 3 times, most recently from e47eec1 to 4c135b6 Compare November 29, 2021 23:46
Jake Sanders added 6 commits November 29, 2021 15:54
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Copy link
Member

@mattmoor mattmoor left a comment

Choose a reason for hiding this comment

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

I like the interface a lot more. Left a few comments inline, but at a high level:

  1. Can we shuffle the package structure around so that implementations generally live in their own packages with private types and public ctors in terms of the interface?
  2. As I was building up oci I was trying to ensure fairly high coverage as I went, and it should now be fairly high. Can we try to do the same here?

)

type Signer interface {
Sign(ctx context.Context, payload io.Reader) (oci.Signature, crypto.PublicKey, error)
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the public key available from the oci.Signature?

Copy link
Member Author

@dekkagaijin dekkagaijin Nov 30, 2021

Choose a reason for hiding this comment

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

nope ¯\(ツ)

It's not stored in the registry, so that makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

What are callers consuming it to do today?

Copy link
Member

Choose a reason for hiding this comment

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

bump this comment?

signatureoptions "github.com/sigstore/sigstore/pkg/signature/options"
)

type Signer interface {
Copy link
Member

Choose a reason for hiding this comment

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

This is where you really want good comments documenting the expectations.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the interface a lot more. Left a few comments inline, but at a high level:

  1. Can we shuffle the package structure around so that implementations generally live in their own packages with private types and public ctors in terms of the interface?

yed

  1. As I was building up oci I was trying to ensure fairly high coverage as I went, and it should now be fairly high. Can we try to do the same here?

Will do, but I'd like to have the package structure stable before I go implementing all the accoutrements for testing

Jake Sanders added 3 commits November 29, 2021 16:50
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
Signed-off-by: Jake Sanders <jsand@google.com>
// the resulting `Cert` and `Chain`, which are added to the returned `oci.Signature`
type FulcioSignerWrapper struct {
Inner Signer
type SignerWrapper struct {
Copy link
Member

Choose a reason for hiding this comment

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

This should be private now.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -77,3 +78,12 @@ func (fs *FulcioSignerWrapper) Sign(ctx context.Context, payload io.Reader) (oci

return newSig, pub, nil
}

// NewSigner returns a *SignerWrapper which signs and uploads the given payload to Fulcio.
func NewSigner(inner cosign.Signer, cert, chain []byte) *SignerWrapper {
Copy link
Member

Choose a reason for hiding this comment

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

return the interface

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -77,3 +78,12 @@ func (fs *FulcioSignerWrapper) Sign(ctx context.Context, payload io.Reader) (oci

return newSig, pub, nil
}

// NewSigner returns a *SignerWrapper which signs and uploads the given payload to Fulcio.
Copy link
Member

Choose a reason for hiding this comment

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

This comment could use work :)

Copy link
Member Author

@dekkagaijin dekkagaijin Nov 30, 2021

Choose a reason for hiding this comment

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

your FACE could use work

sig, pub, err := fs.Inner.Sign(ctx, payload)
// Sign implements `cosign.Signer`
func (fs *SignerWrapper) Sign(ctx context.Context, payload io.Reader) (oci.Signature, crypto.PublicKey, error) {
sig, pub, err := fs.inner.Sign(ctx, payload)
Copy link
Member

Choose a reason for hiding this comment

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

I don't really grok why this is wrapping any Signer instead of having a specific Signer implementation embedded here. Can this truly be composed with other Signer implementations arbitrarily? What other compositions make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

not quite arbitrarily but we made the Signer abstraction for a reason. I think noting the requirements for our public instance in comments would be a legitimate way of documenting them (vs. using the type system)

@@ -212,16 +214,9 @@ func signDigest(ctx context.Context, digest name.Digest, payload []byte, ko KeyO
s = &icos.PayloadSigner{
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 move the payload signer as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

roger

Signed-off-by: Jake Sanders <jsand@google.com>
return nil
var s icos.Signer
s = ipayload.NewSigner(sv, nil, nil)
s = ifulcio.NewSigner(s, sv.Cert, sv.Chain)
Copy link
Member

Choose a reason for hiding this comment

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

A few things about this line don't sit right with me:

  1. Shouldn't it be predicated on some condition?
  2. I still don't like the fact that the fulcio Signer wraps another random Signer where we have zero visibility into what it does. I think the reality here is that the payload Signer is doing the fulcio signing, and the fulcio Signer is just decorating the resulting oci.Signature to match. I think this is a fragile composition, and I think I'd rather see payload's Signer be a hidden implementation detail of a fulcio Signer than allow arbitrary composition here.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes :p

"TODO:"
"* move over fulcio operations as well"

Copy link
Member

@mattmoor mattmoor left a comment

Choose a reason for hiding this comment

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

Let's land this and keep going :)

@dekkagaijin dekkagaijin merged commit 304c2b2 into sigstore:main Nov 30, 2021
@github-actions github-actions bot added this to the v1.4.0 milestone Nov 30, 2021
@dekkagaijin dekkagaijin deleted the sign-refacc branch December 3, 2021 00:57
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.

2 participants