Skip to content

Commit

Permalink
update: added log to notation key and certificate commands (notarypro…
Browse files Browse the repository at this point in the history
…ject#478)

This PR adds log to notation key and certificate commands and updated
the corresponding spec in key.md and certificate.md.

This PR is a part of resolving
notaryproject/roadmap#71.

Signed-off-by: Patrick Zheng <patrickzheng@microsoft.com>
Signed-off-by: vaninrao10 <111005862+vaninrao10@users.noreply.github.com>
  • Loading branch information
Two-Hearts authored and vaninrao10 committed Jan 3, 2023
1 parent b2f8c4c commit e3f5b12
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/notation/cert/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func certAddCommand(opts *certAddOpts) *cobra.Command {
opts.path = args
return nil
},
Long: `Manage certificates in trust store
Long: `Add certificates to the trust store
Example - Add a certificate to the "ca" type of a named store "acme-rockets":
notation cert add --type ca --store acme-rockets acme-rockets.crt
Expand Down
7 changes: 3 additions & 4 deletions cmd/notation/cert/generateTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (

var (
keyDefaultFlag = &pflag.Flag{
Name: "default",
Shorthand: "d",
Usage: "mark as default signing key",
Name: "default",
Usage: "mark as default signing key",
}
setKeyDefaultFlag = func(fs *pflag.FlagSet, p *bool) {
fs.BoolVarP(p, keyDefaultFlag.Name, keyDefaultFlag.Shorthand, false, keyDefaultFlag.Usage)
Expand All @@ -51,7 +50,7 @@ func certGenerateTestCommand(opts *certGenerateTestOpts) *cobra.Command {
opts.name = args[0]
return nil
},
Long: `Manage certificates in trust store
Long: `Generate a test RSA key and a corresponding self-signed certificate
Example - Generate a test RSA key and a corresponding self-signed certificate named "wabbit-networks.io":
notation cert generate-test "wabbit-networks.io"
Expand Down
25 changes: 19 additions & 6 deletions cmd/notation/cert/list.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package cert

import (
"context"
"fmt"

"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/log"
notationgoTruststore "github.com/notaryproject/notation-go/verifier/truststore"
"github.com/notaryproject/notation/cmd/notation/internal/truststore"
"github.com/notaryproject/notation/internal/cmd"
"github.com/spf13/cobra"
)

type certListOpts struct {
cmd.LoggingFlagOpts
storeType string
namedStore string
}
Expand All @@ -22,7 +26,7 @@ func certListCommand(opts *certListOpts) *cobra.Command {
Use: "list",
Aliases: []string{"ls"},
Short: "List certificates in the trust store.",
Long: `List certificates in trust store
Long: `List certificates in the trust store
Example - List all certificate files stored in the trust store
notation cert ls
Expand All @@ -37,15 +41,20 @@ Example - List all certificate files from trust store "wabbit-networks" of type
notation cert ls --type signingAuthority --store "wabbit-networks"
`,
RunE: func(cmd *cobra.Command, args []string) error {
return listCerts(opts)
return listCerts(cmd.Context(), opts)
},
}
opts.LoggingFlagOpts.ApplyFlags(command.Flags())
command.Flags().StringVarP(&opts.storeType, "type", "t", "", "specify trust store type, options: ca, signingAuthority")
command.Flags().StringVarP(&opts.namedStore, "store", "s", "", "specify named store")
return command
}

func listCerts(opts *certListOpts) error {
func listCerts(ctx context.Context, opts *certListOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
logger := log.GetLogger(ctx)

namedStore := opts.namedStore
storeType := opts.storeType
configFS := dir.ConfigFS()
Expand All @@ -58,6 +67,7 @@ func listCerts(opts *certListOpts) error {
return err
}
if err := truststore.CheckNonErrNotExistError(truststore.ListCerts(path, 2)); err != nil {
logger.Debugln("Failed to complete list at path:", path)
return fmt.Errorf("failed to list all certificates stored in the trust store, with error: %s", err.Error())
}

Expand All @@ -72,7 +82,8 @@ func listCerts(opts *certListOpts) error {
return err
}
if err := truststore.CheckNonErrNotExistError(truststore.ListCerts(path, 0)); err != nil {
return fmt.Errorf("failed to list certificates stored in the named store %s of type %s, with error: %s", namedStore, storeType, err.Error())
logger.Debugln("Failed to complete list at path:", path)
return fmt.Errorf("failed to list all certificates stored in the named store %s of type %s, with error: %s", namedStore, storeType, err.Error())
}

return nil
Expand All @@ -86,7 +97,8 @@ func listCerts(opts *certListOpts) error {
return err
}
if err := truststore.CheckNonErrNotExistError(truststore.ListCerts(path, 1)); err != nil {
return fmt.Errorf("failed to list certificates stored of type %s, with error: %s", storeType, err.Error())
logger.Debugln("Failed to complete list at path:", path)
return fmt.Errorf("failed to list all certificates stored of type %s, with error: %s", storeType, err.Error())
}
} else {
// List all certificates under named store namedStore, display empty if
Expand All @@ -97,7 +109,8 @@ func listCerts(opts *certListOpts) error {
return err
}
if err := truststore.CheckNonErrNotExistError(truststore.ListCerts(path, 0)); err != nil {
return fmt.Errorf("failed to list certificates stored in the named store %s, with error: %s", namedStore, err.Error())
logger.Debugln("Failed to complete list at path:", path)
return fmt.Errorf("failed to list all certificates stored in the named store %s, with error: %s", namedStore, err.Error())
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions cmd/notation/cert/show.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package cert

import (
"context"
"errors"
"fmt"

corex509 "github.com/notaryproject/notation-core-go/x509"
"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation/cmd/notation/internal/truststore"
"github.com/notaryproject/notation/internal/cmd"
"github.com/spf13/cobra"
)

type certShowOpts struct {
cmd.LoggingFlagOpts
storeType string
namedStore string
cert string
Expand All @@ -33,7 +37,7 @@ func certShowCommand(opts *certShowOpts) *cobra.Command {
opts.cert = args[0]
return nil
},
Long: `Show details of a certain certificate file
Long: `Show certificate details of given trust store name, trust store type, and certificate file name. If the certificate file contains multiple certificates, then all certificates are displayed
Example - Show details of certificate "cert1.pem" with type "ca" from trust store "acme-rockets":
notation cert show --type ca --store acme-rockets cert1.pem
Expand All @@ -42,15 +46,20 @@ Example - Show details of certificate "cert2.pem" with type "signingAuthority" f
notation cert show --type signingAuthority --store wabbit-networks cert2.pem
`,
RunE: func(cmd *cobra.Command, args []string) error {
return showCerts(opts)
return showCerts(cmd.Context(), opts)
},
}
opts.LoggingFlagOpts.ApplyFlags(command.Flags())
command.Flags().StringVarP(&opts.storeType, "type", "t", "", "specify trust store type, options: ca, signingAuthority")
command.Flags().StringVarP(&opts.namedStore, "store", "s", "", "specify named store")
return command
}

func showCerts(opts *certShowOpts) error {
func showCerts(ctx context.Context, opts *certShowOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
logger := log.GetLogger(ctx)

storeType := opts.storeType
if storeType == "" {
return errors.New("store type cannot be empty")
Expand All @@ -71,6 +80,7 @@ func showCerts(opts *certShowOpts) error {
if err != nil {
return fmt.Errorf("failed to show details of certificate %s, with error: %s", cert, err.Error())
}
logger.Debugln("Showing details of certificate:", path)
certs, err := corex509.ReadCertificateFile(path)
if err != nil {
return fmt.Errorf("failed to show details of certificate %s, with error: %s", cert, err.Error())
Expand Down
46 changes: 35 additions & 11 deletions cmd/notation/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/notaryproject/notation-go/config"
"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/ioutil"
Expand All @@ -19,16 +20,16 @@ import (

var (
keyDefaultFlag = &pflag.Flag{
Name: "default",
Shorthand: "d",
Usage: "mark as default",
Name: "default",
Usage: "mark as default",
}
setKeyDefaultFlag = func(fs *pflag.FlagSet, p *bool) {
fs.BoolVarP(p, keyDefaultFlag.Name, keyDefaultFlag.Shorthand, false, keyDefaultFlag.Usage)
}
)

type keyAddOpts struct {
cmd.LoggingFlagOpts
name string
plugin string
id string
Expand All @@ -37,11 +38,13 @@ type keyAddOpts struct {
}

type keyUpdateOpts struct {
cmd.LoggingFlagOpts
name string
isDefault bool
}

type keyDeleteOpts struct {
cmd.LoggingFlagOpts
names []string
}

Expand Down Expand Up @@ -84,9 +87,10 @@ func keyAddCommand(opts *keyAddOpts) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return addKey(cmd, opts)
return addKey(cmd.Context(), opts)
},
}
opts.LoggingFlagOpts.ApplyFlags(command.Flags())
command.Flags().StringVarP(&opts.plugin, "plugin", "p", "", "signing plugin name")
command.MarkFlagRequired("plugin")

Expand Down Expand Up @@ -114,10 +118,11 @@ func keyUpdateCommand(opts *keyUpdateOpts) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return updateKey(opts)
return updateKey(cmd.Context(), opts)
},
}

opts.LoggingFlagOpts.ApplyFlags(command.Flags())
setKeyDefaultFlag(command.Flags(), &opts.isDefault)

return command
Expand All @@ -139,7 +144,7 @@ func keyDeleteCommand(opts *keyDeleteOpts) *cobra.Command {
opts = &keyDeleteOpts{}
}

return &cobra.Command{
command := &cobra.Command{
Use: "delete [flags] <key_name>...",
Short: "Delete key from signing key list",
Args: func(cmd *cobra.Command, args []string) error {
Expand All @@ -150,12 +155,19 @@ func keyDeleteCommand(opts *keyDeleteOpts) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return deleteKeys(opts)
return deleteKeys(cmd.Context(), opts)
},
}
opts.LoggingFlagOpts.ApplyFlags(command.Flags())

return command
}

func addKey(command *cobra.Command, opts *keyAddOpts) error {
func addKey(ctx context.Context, opts *keyAddOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
logger := log.GetLogger(ctx)

signingKeys, err := configutil.LoadSigningkeysOnce()
if err != nil {
return err
Expand All @@ -167,7 +179,8 @@ func addKey(command *cobra.Command, opts *keyAddOpts) error {
}
pluginName := opts.plugin
if pluginName != "" {
key, err = addExternalKey(command.Context(), opts, pluginName, name)
logger.Debugf("Adding key with name %v and plugin name %v", name, pluginName)
key, err = addExternalKey(ctx, opts, pluginName, name)
if err != nil {
return err
}
Expand All @@ -186,6 +199,7 @@ func addKey(command *cobra.Command, opts *keyAddOpts) error {
}

// write out
logger.Debugf("Added key with name %s - {%+v}", key.Name, key.ExternalKey)
if isDefault {
fmt.Printf("%s: marked as default\n", key.Name)
} else {
Expand Down Expand Up @@ -232,7 +246,11 @@ func addKeyCore(signingKeys *config.SigningKeys, key config.KeySuite, markDefaul
return nil
}

func updateKey(opts *keyUpdateOpts) error {
func updateKey(ctx context.Context, opts *keyUpdateOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
logger := log.GetLogger(ctx)

// initialize
name := opts.name
// core process
Expand All @@ -244,6 +262,7 @@ func updateKey(opts *keyUpdateOpts) error {
return errors.New(name + ": not found")
}
if !opts.isDefault {
logger.Warn("--default flag is not set, command did not take effect")
return nil
}
if signingKeys.Default != name {
Expand All @@ -269,7 +288,11 @@ func listKeys() error {
return ioutil.PrintKeyMap(os.Stdout, signingKeys.Default, signingKeys.Keys)
}

func deleteKeys(opts *keyDeleteOpts) error {
func deleteKeys(ctx context.Context, opts *keyDeleteOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
logger := log.GetLogger(ctx)

// core process
signingKeys, err := configutil.LoadSigningkeysOnce()
if err != nil {
Expand All @@ -281,6 +304,7 @@ func deleteKeys(opts *keyDeleteOpts) error {
for _, name := range opts.names {
idx := slices.Index(signingKeys.Keys, name)
if idx < 0 {
logger.Warnf("Key %s not found, command did not take effect", name)
return errors.New(name + ": not found")
}
signingKeys.Keys = slices.Delete(signingKeys.Keys, idx)
Expand Down
12 changes: 8 additions & 4 deletions specs/commandline/certificate.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,27 @@ Aliases:
list, ls
Flags:
-d, --debug debug mode
-h, --help help for list
-s, --store string specify named store
-t, --type string specify trust store type, options: ca, signingAuthority
-v, --verbose verbose mode
```

### notation certificate show

```text
Show certificate details given trust store type, named store, and certificate file name. If the certificate file contains multiple certificates, then all certificates are displayed.
Show certificate details of given trust store name, trust store type, and certificate file name. If the certificate file contains multiple certificates, then all certificates are displayed.
Usage:
notation certificate show --type <type> --store <name> [flags] <cert_fileName>
Flags:
-d, --debug debug mode
-h, --help help for show
-s, --store string specify named store
-t, --type string specify trust store type, options: ca, signingAuthority
-v, --verbose verbose mode
```

### notation certificate delete
Expand Down Expand Up @@ -124,9 +128,9 @@ Usage:
notation certificate generate-test [flags] <common_name>
Flags:
-b, --bits int RSA key bits (default 2048)
-d, --default mark as default signing key
-h, --help help for generate-test
-b, --bits int RSA key bits (default 2048)
--default mark as default signing key
-h, --help help for generate-test
```

## Usage
Expand Down
Loading

0 comments on commit e3f5b12

Please sign in to comment.