-
Notifications
You must be signed in to change notification settings - Fork 88
feat: blob inspect
command
#1133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f476bf0
fix: complete the basic code
JeyJeyGao db2858e
fix: update
JeyJeyGao 98d34b6
fix: add envelope media type
JeyJeyGao 4bbbb75
fix: complete test and docs
JeyJeyGao 5985391
fix: restore code
JeyJeyGao 8603956
fix: update comments
JeyJeyGao 25ce72a
fix: resolve comments
JeyJeyGao 98d63b8
fix: refactor to have inspect_helper.go
JeyJeyGao e3a963d
fix: update
JeyJeyGao 85b140c
Merge remote-tracking branch 'upstream/main' into feat/blob_inspect
JeyJeyGao 9b28159
fix: resolve comments
JeyJeyGao 48cc0c5
fix: update
JeyJeyGao 6586b9f
fix: update
JeyJeyGao 304b17f
fix: update comments
JeyJeyGao 71e8b81
Merge remote-tracking branch 'upstream/main' into feat/blob_inspect
JeyJeyGao 5be5665
fix: e2e
JeyJeyGao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright The Notary Project Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package blob | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/notaryproject/notation-core-go/signature" | ||
"github.com/notaryproject/notation/cmd/notation/internal/display" | ||
"github.com/notaryproject/notation/cmd/notation/internal/option" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type inspectOpts struct { | ||
option.Common | ||
option.Format | ||
sigPath string | ||
} | ||
|
||
func inspectCommand() *cobra.Command { | ||
opts := &inspectOpts{} | ||
command := &cobra.Command{ | ||
Use: "inspect [flags] <signature_path>", | ||
Short: "Inspect a signature associated with a blob", | ||
Long: `Inspect a signature associated with a blob. | ||
|
||
Example - Inspect a signature: | ||
notation blob inspect blob.cose.sig | ||
|
||
Example - Inspect a signature and output as JSON: | ||
notation blob inspect -o json blob.cose.sig | ||
`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("missing signature path: use `notation blob inspect --help` to see what parameters are required") | ||
} | ||
opts.sigPath = args[0] | ||
return nil | ||
}, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
opts.Common.Parse(cmd) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runInspect(opts) | ||
}, | ||
} | ||
|
||
opts.Format.ApplyFlags(command.Flags(), option.FormatTypeText, option.FormatTypeJSON) | ||
return command | ||
} | ||
|
||
func runInspect(opts *inspectOpts) error { | ||
// initialize display handler | ||
displayHandler, err := display.NewBlobInspectHandler(opts.Printer, opts.Format) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// parse signature file | ||
signatureMediaType, err := parseSignatureMediaType(opts.sigPath) | ||
if err != nil { | ||
return err | ||
} | ||
envelopeBytes, err := os.ReadFile(opts.sigPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read signature file: %w", err) | ||
} | ||
envelope, err := signature.ParseEnvelope(signatureMediaType, envelopeBytes) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse signature: %w", err) | ||
} | ||
if err := displayHandler.OnEnvelopeParsed(opts.sigPath, signatureMediaType, envelope); err != nil { | ||
return err | ||
} | ||
|
||
return displayHandler.Render() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JeyJeyGao marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
cmd/notation/internal/display/metadata/json/blob_inspect.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright The Notary Project Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package json | ||
|
||
import ( | ||
coresignature "github.com/notaryproject/notation-core-go/signature" | ||
"github.com/notaryproject/notation/cmd/notation/internal/display/output" | ||
) | ||
|
||
// BlobInspectHandler is a handler for inspecting metadata information and | ||
// rendering it in JSON format. It implements the metadata.BlobInspectHandler | ||
// interface. | ||
type BlobInspectHandler struct { | ||
printer *output.Printer | ||
signature *signature | ||
} | ||
|
||
// NewBlobInspectHandler creates a BlobInspectHandler to inspect signature and | ||
// print in JSON format. | ||
func NewBlobInspectHandler(printer *output.Printer) *BlobInspectHandler { | ||
return &BlobInspectHandler{ | ||
printer: printer, | ||
} | ||
} | ||
|
||
// OnEnvelopeParsed sets the parsed envelope for the handler. | ||
func (h *BlobInspectHandler) OnEnvelopeParsed(nodeName, signatureMediaType string, envelope coresignature.Envelope) error { | ||
// blob signature does not have a digest | ||
sig, err := newSignature("", signatureMediaType, envelope) | ||
if err != nil { | ||
return err | ||
} | ||
h.signature = sig | ||
return nil | ||
} | ||
|
||
// Render prints out the metadata information in JSON format. | ||
func (h *BlobInspectHandler) Render() error { | ||
return output.PrintPrettyJSON(h.printer, h.signature) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.