Skip to content

Commit

Permalink
Add String() to print cosign signature details
Browse files Browse the repository at this point in the history
  • Loading branch information
yawangwang committed Oct 13, 2023
1 parent 56211cd commit a956ebc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions launcher/internal/oci/cosign/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Sig struct {
// Blob represents the opaque data uploaded to OCI registry associated with the layer.
// This contains the Simple Signing Payload as described in https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#tag-based-discovery.
Blob []byte
// SourceRepo represents the location that stores this signature.
SourceRepo string
}

// CosignSigKey is the key of the cosign-generated signature embedded in OCI image manifest.
Expand Down Expand Up @@ -68,3 +70,12 @@ func (s Sig) PublicKey() ([]byte, error) {
func (s Sig) SigningAlgorithm() (oci.SigningAlgorithm, error) {
return "", fmt.Errorf("not implemented")
}

// String returns signature details
func (s Sig) String() string {
sig, err := s.Base64Encoded()
if err != nil {
return fmt.Sprintf("[signature error: %s]", err.Error())
}
return fmt.Sprintf("[signature: %q, sourceRepo: %q]", sig, s.SourceRepo)
}
40 changes: 40 additions & 0 deletions launcher/internal/oci/cosign/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosign
import (
"bytes"
"crypto/rand"
"strings"
"testing"

"github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -130,6 +131,45 @@ func TestWorkflow(t *testing.T) {
}
}

func TestString(t *testing.T) {
testCases := []struct {
name string
sourceRepo string
b64Sig string
wantString string
}{
{
name: "successful signature details",
sourceRepo: "gcr.io/hello_world",
b64Sig: "aGVsbG8gd29ybGQ=", // base64 encoded "hello world"
wantString: `signature: "aGVsbG8gd29ybGQ=", sourceRepo: "gcr.io/hello_world"`,
},
{
name: "erronous signature details",
sourceRepo: "gcr.io/hello_world",
b64Sig: "invalid",
wantString: `signature error: invalid base64 encoded signature`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sig := &Sig{
Layer: v1.Descriptor{
Annotations: map[string]string{
CosignSigKey: tc.b64Sig,
},
},
SourceRepo: tc.sourceRepo,
}
gotString := sig.String()
if !strings.Contains(gotString, tc.wantString) {
t.Errorf("String() failed, got %s, but want %s", gotString, tc.wantString)
}
})
}
}

func randomBase64EncodedString(n int) string {
b := make([]byte, n)
_, err := rand.Read(b)
Expand Down
5 changes: 3 additions & 2 deletions launcher/internal/signaturediscovery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func (c *Client) FetchImageSignatures(ctx context.Context, targetRepository stri
return nil, err
}
sig := &cosign.Sig{
Layer: layer,
Blob: blob,
Layer: layer,
Blob: blob,
SourceRepo: targetRepository,
}
signatures = append(signatures, sig)
}
Expand Down

0 comments on commit a956ebc

Please sign in to comment.