Skip to content

Commit

Permalink
Support outputing a certificate without uploading to the tlog (#2506)
Browse files Browse the repository at this point in the history
If you don't upload to the tlog, then rekorbytes is not set. This
change:
* Checks if the signer is actually a certificate
* Outputs the certificate when requested

Signed-off-by: Hayden Blauzvern <[email protected]>

Signed-off-by: Hayden Blauzvern <[email protected]>
  • Loading branch information
haydentherapper authored Dec 5, 2022
1 parent fa8a799 commit 381ba64
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cmd/cosign/cli/sign/sign_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/sigstore/cosign/cmd/cosign/cli/rekor"
internal "github.com/sigstore/cosign/internal/pkg/cosign"
"github.com/sigstore/cosign/pkg/cosign"
"github.com/sigstore/sigstore/pkg/cryptoutils"
signatureoptions "github.com/sigstore/sigstore/pkg/signature/options"
)

Expand Down Expand Up @@ -151,15 +152,23 @@ func SignBlobCmd(ro *options.RootOptions, ko options.KeyOpts, regOpts options.Re
}
}

if outputCertificate != "" && len(rekorBytes) > 0 {
bts := rekorBytes
if b64 {
bts = []byte(base64.StdEncoding.EncodeToString(rekorBytes))
if outputCertificate != "" {
signer, err := sv.Bytes(ctx)
if err != nil {
return nil, fmt.Errorf("error getting signer: %w", err)
}
if err := os.WriteFile(outputCertificate, bts, 0600); err != nil {
return nil, fmt.Errorf("create certificate file: %w", err)
cert, err := cryptoutils.UnmarshalCertificatesFromPEM(signer)
// signer is a certificate
if err == nil && len(cert) == 1 {
bts := signer
if b64 {
bts = []byte(base64.StdEncoding.EncodeToString(signer))
}
if err := os.WriteFile(outputCertificate, bts, 0600); err != nil {
return nil, fmt.Errorf("create certificate file: %w", err)
}
fmt.Printf("Certificate wrote in the file %s\n", outputCertificate)
}
fmt.Printf("Certificate wrote in the file %s\n", outputCertificate)
}

return sig, nil
Expand Down

0 comments on commit 381ba64

Please sign in to comment.