Skip to content
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

fix: error message for trust policy #933

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/notation/policy/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func importCmd() *cobra.Command {
Example - Import trust policy configuration from a file:
notation policy import my_policy.json
`,
Args: cobra.ExactArgs(1),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires 1 argument but received %d.\nUsage: notation policy import <path-to-policy.json>\nPlease specify a trust policy file as the argument", len(args))
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.filePath = args[0]
return runImport(cmd, opts)
Expand Down Expand Up @@ -71,7 +76,7 @@ func runImport(command *cobra.Command, opts importOpts) error {
// optional confirmation
if !opts.force {
if _, err := trustpolicy.LoadDocument(); err == nil {
confirmed, err := cmdutil.AskForConfirmation(os.Stdin, "Existing trust policy configuration found, do you want to overwrite it?", opts.force)
confirmed, err := cmdutil.AskForConfirmation(os.Stdin, "The trust policy configuration already exists, do you want to overwrite it?", opts.force)
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/notation/policy/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@
// get policy file path
policyPath, err := dir.ConfigFS().SysPath(dir.PathTrustPolicy)
if err != nil {
return fmt.Errorf("failed to obtain path of trust policy configuration file: %w", err)
return fmt.Errorf("failed to obtain path of trust policy file: %w", err)

Check warning on line 56 in cmd/notation/policy/show.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/policy/show.go#L56

Added line #L56 was not covered by tests
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
}

// core process
policyJSON, err := os.ReadFile(policyPath)
if err != nil {
return fmt.Errorf("failed to load trust policy configuration, you may import one via `notation policy import <path-to-policy.json>`: %w", err)
if os.IsNotExist(err) {
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("failed to show trust policy because the trust policy file does not exist.\nYou may import one via `notation policy import <path-to-policy.json>`")
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
}
return fmt.Errorf("failed to show trust policy: %w", err)

Check warning on line 65 in cmd/notation/policy/show.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/policy/show.go#L65

Added line #L65 was not covered by tests
}
var doc trustpolicy.Document
if err = json.Unmarshal(policyJSON, &doc); err == nil {
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/suite/command/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("trust policy maintainer", func() {
Host(Opts(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
notation.ExpectFailure().
Exec("policy", "show").
MatchErrKeyWords("failed to load trust policy configuration", "notation policy import")
MatchErrKeyWords("failed to show trust policy", "notation policy import")
})
})

Expand Down Expand Up @@ -60,7 +60,9 @@ var _ = Describe("trust policy maintainer", func() {
It("should fail if no file path is provided", func() {
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
Host(opts, func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
notation.ExpectFailure().
Exec("policy", "import")
Exec("policy", "import").
MatchErrKeyWords("requires 1 argument but received 0")

})
})

Expand Down
Loading