Skip to content

Commit

Permalink
fix: move internal/tree to tree handlers package
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 23, 2025
1 parent 1adc392 commit da16190
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
21 changes: 10 additions & 11 deletions cmd/notation/internal/display/metadata/tree/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation/cmd/notation/internal/display/output"
"github.com/notaryproject/notation/internal/envelope"
"github.com/notaryproject/notation/internal/tree"
"github.com/notaryproject/tspclient-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -41,10 +40,10 @@ type InspectHandler struct {

// rootReferenceNode is the root node with the artifact reference as the
// value.
rootReferenceNode *tree.Node
rootReferenceNode *Node
// notationSignaturesNode is the node for all signatures associated with the
// artifact.
notationSignaturesNode *tree.Node
notationSignaturesNode *Node
}

// NewInspectHandler creates a TreeHandler to inspect signatures and print in tree
Expand All @@ -60,7 +59,7 @@ func NewInspectHandler(printer *output.Printer) *InspectHandler {
//
// mediaType is a no-op for this handler.
func (h *InspectHandler) OnReferenceResolved(reference, _ string) {
h.rootReferenceNode = tree.New(reference)
h.rootReferenceNode = New(reference)
h.notationSignaturesNode = h.rootReferenceNode.Add(registry.ArtifactTypeNotation)
}

Expand All @@ -78,7 +77,7 @@ func (h *InspectHandler) Render() error {
return h.rootReferenceNode.Print(h.printer)
}

func addSignature(node *tree.Node, digest string, sigEnvelope coresignature.Envelope) error {
func addSignature(node *Node, digest string, sigEnvelope coresignature.Envelope) error {
envelopeContent, err := sigEnvelope.Content()
if err != nil {
return err
Expand All @@ -104,7 +103,7 @@ func addSignature(node *tree.Node, digest string, sigEnvelope coresignature.Enve
return nil
}

func addSignedAttributes(node *tree.Node, envelopeContent *coresignature.EnvelopeContent) {
func addSignedAttributes(node *Node, envelopeContent *coresignature.EnvelopeContent) {
signedAttributesNode := node.Add("signed attributes")
signedAttributesNode.AddPair("content type", string(envelopeContent.Payload.ContentType))
signedAttributesNode.AddPair("signing scheme", string(envelopeContent.SignerInfo.SignedAttributes.SigningScheme))
Expand All @@ -117,7 +116,7 @@ func addSignedAttributes(node *tree.Node, envelopeContent *coresignature.Envelop
}
}

func addUserDefinedAttributes(node *tree.Node, annotations map[string]string) {
func addUserDefinedAttributes(node *Node, annotations map[string]string) {
userDefinedAttributesNode := node.Add("user defined attributes")
if len(annotations) == 0 {
userDefinedAttributesNode.Add("(empty)")
Expand All @@ -129,7 +128,7 @@ func addUserDefinedAttributes(node *tree.Node, annotations map[string]string) {
}
}

func addUnsignedAttributes(node *tree.Node, envelopeContent *coresignature.EnvelopeContent) {
func addUnsignedAttributes(node *Node, envelopeContent *coresignature.EnvelopeContent) {
unsignedAttributesNode := node.Add("unsigned attributes")
if signingAgent := envelopeContent.SignerInfo.UnsignedAttributes.SigningAgent; signingAgent != "" {
unsignedAttributesNode.AddPair("signing agent", signingAgent)
Expand All @@ -139,14 +138,14 @@ func addUnsignedAttributes(node *tree.Node, envelopeContent *coresignature.Envel
}
}

func addSignedArtifact(node *tree.Node, signedArtifactDesc ocispec.Descriptor) {
func addSignedArtifact(node *Node, signedArtifactDesc ocispec.Descriptor) {
artifactNode := node.Add("signed artifact")
artifactNode.AddPair("media type", signedArtifactDesc.MediaType)
artifactNode.AddPair("digest", signedArtifactDesc.Digest.String())
artifactNode.AddPair("size", strconv.FormatInt(signedArtifactDesc.Size, 10))
}

func addTimestamp(node *tree.Node, signerInfo coresignature.SignerInfo) {
func addTimestamp(node *Node, signerInfo coresignature.SignerInfo) {
timestampNode := node.Add("timestamp signature")
signedToken, err := tspclient.ParseSignedToken(signerInfo.UnsignedAttributes.TimestampSignature)
if err != nil {
Expand All @@ -167,7 +166,7 @@ func addTimestamp(node *tree.Node, signerInfo coresignature.SignerInfo) {
addCertificates(timestampNode, signedToken.Certificates)
}

func addCertificates(node *tree.Node, certChain []*x509.Certificate) {
func addCertificates(node *Node, certChain []*x509.Certificate) {
certListNode := node.Add("certificates")
for _, cert := range certChain {
hash := sha256.Sum256(cert.Raw)
Expand Down
13 changes: 6 additions & 7 deletions cmd/notation/internal/display/metadata/tree/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ import (
"time"

coresignature "github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation/internal/tree"
)

func TestAddSignedAttributes(t *testing.T) {
t.Run("empty envelopeContent", func(t *testing.T) {
node := tree.New("root")
node := New("root")
ec := &coresignature.EnvelopeContent{}
addSignedAttributes(node, ec)
// No error or panic expected; minimal check or just ensure it doesn't crash.
})

t.Run("with expiry and extented node", func(t *testing.T) {
node := tree.New("root")
node := New("root")
expiryTime := time.Now().Add(time.Hour)
ec := &coresignature.EnvelopeContent{
Payload: coresignature.Payload{
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestAddSignedAttributes(t *testing.T) {

func TestAddUserDefinedAttributes(t *testing.T) {
t.Run("empty map", func(t *testing.T) {
node := tree.New("root")
node := New("root")
addUserDefinedAttributes(node, nil)
if len(node.Children) == 0 {
t.Fatal("expected node to have children")
Expand All @@ -92,7 +91,7 @@ func TestAddUserDefinedAttributes(t *testing.T) {
})

t.Run("non-empty map", func(t *testing.T) {
node := tree.New("root")
node := New("root")
annotations := map[string]string{"key1": "val1", "key2": "val2"}
addUserDefinedAttributes(node, annotations)
udaNode := node.Children[0]
Expand All @@ -107,7 +106,7 @@ func TestAddUserDefinedAttributes(t *testing.T) {

func TestAddTimestamp(t *testing.T) {
t.Run("invalid timestamp signature", func(t *testing.T) {
node := tree.New("root")
node := New("root")
signerInfo := coresignature.SignerInfo{
UnsignedAttributes: coresignature.UnsignedAttributes{
TimestampSignature: []byte("invalid"),
Expand Down Expand Up @@ -141,7 +140,7 @@ func TestAddTimestamp(t *testing.T) {
TimestampSignature: tsaToken,
},
}
node := tree.New("root")
node := New("root")
addTimestamp(node, signerInfo)
if len(node.Children) == 0 {
t.Fatal("expected node to have children")
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit da16190

Please sign in to comment.