Skip to content

Commit

Permalink
fix: complete refactor for blob inspect
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 6, 2025
1 parent f0860e9 commit f9e1a73
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 84 deletions.
91 changes: 7 additions & 84 deletions cmd/notation/blob/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/notaryproject/notation-core-go/signature/cose"
"github.com/notaryproject/notation-core-go/signature/jws"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/envelope"
"github.com/notaryproject/notation/internal/ioutil"
"github.com/notaryproject/notation/internal/tree"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -84,93 +80,20 @@ func runInspect(opts *inspectOpts) error {
// displayed as UserDefinedAttributes
sig.SignedArtifact.Annotations = nil

return printOutput(opts.sigPath, sig, opts.outputFormat)
}

func printOutput(sigPath string, signature *envelope.SignatureInfo, outputFormat string) error {
if outputFormat == cmd.OutputJSON {
return ioutil.PrintObjectAsJSON(signature)
switch opts.outputFormat {
case cmd.OutputJSON:
return ioutil.PrintObjectAsJSON(sig)
case cmd.OutputPlaintext:
treeNode := sig.ToTreeNode(opts.sigPath)
treeNode.Print()

Check warning on line 88 in cmd/notation/blob/inspect.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/blob/inspect.go#L81-L88

Added lines #L81 - L88 were not covered by tests
}

sigNode := tree.New(sigPath)
sigNode.AddPair("signature algorithm", signature.SignatureAlgorithm)
sigNode.AddPair("signature envelope type", signature.MediaType)

signedAttributesNode := sigNode.Add("signed attributes")
addMapToTree(signedAttributesNode, signature.SignedAttributes)

userDefinedAttributesNode := sigNode.Add("user defined attributes")
addStringMapToTree(userDefinedAttributesNode, signature.UserDefinedAttributes)

unsignedAttributesNode := sigNode.Add("unsigned attributes")
for k, v := range signature.UnsignedAttributes {
switch value := v.(type) {
case string:
unsignedAttributesNode.AddPair(k, value)
case envelope.TimestampInfo:
timestampNode := unsignedAttributesNode.Add("timestamp signature")
if value.Error != "" {
timestampNode.AddPair("error", value.Error)
break
}
timestampNode.AddPair("timestamp", value.Timestamp)
addCertificatesToTree(timestampNode, "certificates", value.Certificates)
}
}

addCertificatesToTree(sigNode, "certificates", signature.Certificates)

artifactNode := sigNode.Add("signed artifact")
artifactNode.AddPair("media type", signature.SignedArtifact.MediaType)
artifactNode.AddPair("digest", signature.SignedArtifact.Digest.String())
artifactNode.AddPair("size", strconv.FormatInt(signature.SignedArtifact.Size, 10))

sigNode.Print()
return nil

Check warning on line 90 in cmd/notation/blob/inspect.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/blob/inspect.go#L90

Added line #L90 was not covered by tests
}

func addMapToTree(node *tree.Node, m map[string]any) {
if len(m) > 0 {
for k, v := range m {
node.AddPair(k, v)
}
} else {
node.Add("(empty)")
}
}

func addStringMapToTree(node *tree.Node, m map[string]string) {
if len(m) > 0 {
for k, v := range m {
node.AddPair(k, v)
}
} else {
node.Add("(empty)")
}
}

func addCertificatesToTree(node *tree.Node, name string, certs []envelope.CertificateInfo) {
certListNode := node.Add(name)
for _, cert := range certs {
certNode := certListNode.AddPair("SHA256 fingerprint", cert.SHA256Fingerprint)
certNode.AddPair("issued to", cert.IssuedTo)
certNode.AddPair("issued by", cert.IssuedBy)
certNode.AddPair("expiry", cert.Expiry)
}
}

func parseEnvelopeMediaType(filename string) (string, error) {
parts := strings.Split(filename, ".")
if len(parts) < 3 {
return "", fmt.Errorf("invalid signature filename: %s", filename)
}
mediaType := strings.ToLower(parts[len(parts)-2])
switch mediaType {
case "jws":
return jws.MediaTypeEnvelope, nil
case "cose":
return cose.MediaTypeEnvelope, nil
default:
return "", fmt.Errorf("unsupported signature format: %s", mediaType)
}
return envelope.GetEnvelopeMediaType(strings.ToLower(parts[len(parts)-2]))

Check warning on line 98 in cmd/notation/blob/inspect.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/blob/inspect.go#L93-L98

Added lines #L93 - L98 were not covered by tests
}
58 changes: 58 additions & 0 deletions internal/envelope/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"crypto/x509"
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/notaryproject/notation-plugin-framework-go/plugin"
"github.com/notaryproject/notation/internal/ioutil"
"github.com/notaryproject/notation/internal/tree"

"github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation-go/plugin/proto"
Expand Down Expand Up @@ -163,3 +165,59 @@ func parseTimestamp(signerInfo signature.SignerInfo) TimestampInfo {
Certificates: certificates,
}

Check warning on line 166 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L142-L166

Added lines #L142 - L166 were not covered by tests
}

func (s *SignatureInfo) ToTreeNode(sigName string) *tree.Node {
sigNode := tree.New(sigName)
sigNode.AddPair("signature algorithm", s.SignatureAlgorithm)
sigNode.AddPair("signature envelope type", s.MediaType)

signedAttributesNode := sigNode.Add("signed attributes")
addMapToTree(signedAttributesNode, s.SignedAttributes)

userDefinedAttributesNode := sigNode.Add("user defined attributes")
addMapToTree(userDefinedAttributesNode, s.UserDefinedAttributes)

unsignedAttributesNode := sigNode.Add("unsigned attributes")
for k, v := range s.UnsignedAttributes {
switch value := v.(type) {
case string:
unsignedAttributesNode.AddPair(k, value)
case TimestampInfo:
timestampNode := unsignedAttributesNode.Add("timestamp signature")
if value.Error != "" {
timestampNode.AddPair("error", value.Error)
break

Check warning on line 189 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L169-L189

Added lines #L169 - L189 were not covered by tests
}
timestampNode.AddPair("timestamp", value.Timestamp)
addCertificatesToTree(timestampNode, "certificates", value.Certificates)

Check warning on line 192 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L191-L192

Added lines #L191 - L192 were not covered by tests
}
}

addCertificatesToTree(sigNode, "certificates", s.Certificates)

artifactNode := sigNode.Add("signed artifact")
artifactNode.AddPair("media type", s.SignedArtifact.MediaType)
artifactNode.AddPair("digest", s.SignedArtifact.Digest.String())
artifactNode.AddPair("size", strconv.FormatInt(s.SignedArtifact.Size, 10))
return sigNode

Check warning on line 202 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L196-L202

Added lines #L196 - L202 were not covered by tests
}

func addMapToTree[T any](node *tree.Node, m map[string]T) {
if len(m) > 0 {
for k, v := range m {
node.AddPair(k, v)
}
} else {
node.Add("(empty)")
}

Check warning on line 212 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L205-L212

Added lines #L205 - L212 were not covered by tests
}

func addCertificatesToTree(node *tree.Node, name string, certs []CertificateInfo) {
certListNode := node.Add(name)
for _, cert := range certs {
certNode := certListNode.AddPair("SHA256 fingerprint", cert.SHA256Fingerprint)
certNode.AddPair("issued to", cert.IssuedTo)
certNode.AddPair("issued by", cert.IssuedBy)
certNode.AddPair("expiry", cert.Expiry)
}

Check warning on line 222 in internal/envelope/signature.go

View check run for this annotation

Codecov / codecov/patch

internal/envelope/signature.go#L215-L222

Added lines #L215 - L222 were not covered by tests
}

0 comments on commit f9e1a73

Please sign in to comment.