forked from notaryproject/notation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ect#593) This PR implements what's described in [policy.md](https://github.com/notaryproject/notation/blob/18efc3de233ef5eb00e2f32009475a2b8a3eae47/specs/commandline/policy.md). Resolves notaryproject#548 Signed-off-by: Billy Zha <[email protected]>
- Loading branch information
Showing
7 changed files
with
365 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
package policy | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func Cmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "policy [command]", | ||
Short: "[Preview] Manage trust policy configuration", | ||
Long: "[Preview] Manage trust policy configuration for signature verification.", | ||
} | ||
|
||
command.AddCommand( | ||
showCmd(), | ||
importCmd(), | ||
) | ||
|
||
return command | ||
} |
This file contains 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,83 @@ | ||
package policy | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/notaryproject/notation-go/dir" | ||
"github.com/notaryproject/notation-go/verifier/trustpolicy" | ||
"github.com/notaryproject/notation/cmd/notation/internal/cmdutil" | ||
"github.com/notaryproject/notation/internal/osutil" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type importOpts struct { | ||
filePath string | ||
force bool | ||
} | ||
|
||
func importCmd() *cobra.Command { | ||
var opts importOpts | ||
command := &cobra.Command{ | ||
Use: "import [flags] <file_path>", | ||
Short: "[Preview] Import trust policy configuration from a JSON file", | ||
Long: `[Preview] Import trust policy configuration from a JSON file. | ||
** This command is in preview and under development. ** | ||
Example - Import trust policy configuration from a file: | ||
notation policy import my_policy.json | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.filePath = args[0] | ||
return runImport(cmd, opts) | ||
}, | ||
} | ||
command.Flags().BoolVar(&opts.force, "force", false, "override the existing trust policy configuration, never prompt") | ||
return command | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return err | ||
} | ||
if !confirmed { | ||
return nil | ||
} | ||
} | ||
} else { | ||
fmt.Fprintf(os.Stderr, "Warning: existing trust policy configuration file will be overwritten") | ||
} | ||
|
||
// read configuration | ||
policyJSON, err := os.ReadFile(opts.filePath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read trust policy file: %w", err) | ||
} | ||
|
||
// parse and validate | ||
var doc trustpolicy.Document | ||
if err = json.Unmarshal(policyJSON, &doc); err != nil { | ||
return fmt.Errorf("failed to parse trust policy configuration: %w", err) | ||
} | ||
if err = doc.Validate(); err != nil { | ||
return fmt.Errorf("failed to validate trust policy: %w", err) | ||
} | ||
|
||
// write | ||
policyPath, err := dir.ConfigFS().SysPath(dir.PathTrustPolicy) | ||
if err != nil { | ||
return fmt.Errorf("failed to obtain path of trust policy file: %w", err) | ||
} | ||
if err = osutil.WriteFile(policyPath, policyJSON); err != nil { | ||
return fmt.Errorf("failed to write trust policy file: %w", err) | ||
} | ||
_, err = fmt.Fprintln(os.Stdout, "Trust policy configuration imported successfully.") | ||
return err | ||
} |
This file contains 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,64 @@ | ||
package policy | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/notaryproject/notation-go/dir" | ||
"github.com/notaryproject/notation-go/verifier/trustpolicy" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type showOpts struct { | ||
} | ||
|
||
func showCmd() *cobra.Command { | ||
var opts showOpts | ||
command := &cobra.Command{ | ||
Use: "show [flags]", | ||
Short: "[Preview] Show trust policy configuration", | ||
Long: `[Preview] Show trust policy configuration. | ||
** This command is in preview and under development. ** | ||
Example - Show current trust policy configuration: | ||
notation policy show | ||
Example - Save current trust policy configuration to a file: | ||
notation policy show > my_policy.json | ||
`, | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runShow(cmd, opts) | ||
}, | ||
} | ||
return command | ||
} | ||
|
||
func runShow(command *cobra.Command, opts showOpts) error { | ||
// 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) | ||
} | ||
|
||
// 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) | ||
} | ||
var doc trustpolicy.Document | ||
if err = json.Unmarshal(policyJSON, &doc); err == nil { | ||
err = doc.Validate() | ||
} | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) | ||
fmt.Fprintf(os.Stderr, "Existing trust policy configuration is invalid, you may update or create a new one via `notation policy import <path-to-policy.json>`\n") | ||
// not returning to show the invalid policy configuration | ||
} | ||
|
||
// show policy content | ||
_, err = os.Stdout.Write(policyJSON) | ||
return err | ||
} |
This file contains 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
Oops, something went wrong.