From 56f56dd8da3feca56c3d3be0ead67cee9bc4b645 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Wed, 19 Jul 2023 20:06:10 +0800 Subject: [PATCH 1/7] feat: enable trace Signed-off-by: wangxiaoxuan273 --- go.mod | 2 +- go.sum | 4 ++-- internal/cmd/options.go | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 33dbc751d..cbecd7cb3 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/notaryproject/notation-go v1.0.0-rc.6 github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.1.0-rc4 - github.com/oras-project/oras-credentials-go v0.2.0 + github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index f71316420..805dc9fdd 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0= github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= -github.com/oras-project/oras-credentials-go v0.2.0 h1:BvWAXo0e5unWR6Hfxyb0K04mHNHreQz/Zclw6IzCYJo= -github.com/oras-project/oras-credentials-go v0.2.0/go.mod h1:JVdg7a5k7hzTrEeeouwag0aCv7OLrS77r7/6w3gVirU= +github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225 h1:0T0CYijlSdn0ariTr48cRn6YDl445VZf3yqCqJKksqo= +github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 2e925e255..8f5efd31b 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -3,7 +3,9 @@ package cmd import ( "context" + "github.com/notaryproject/notation-go/log" "github.com/notaryproject/notation/internal/trace" + executableTrace "github.com/oras-project/oras-credentials-go/trace" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -43,6 +45,15 @@ func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) { // SetLoggerLevel sets up the logger based on common options. func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context { + logger := log.GetLogger(ctx) + ctx = executableTrace.WithExecutableTrace(ctx, &executableTrace.ExecutableTrace{ + ExecuteStart: func(executableName, action string) { + logger.Info("started executing credential helper program %s with action %s", executableName, action) + }, + ExecuteDone: func(executableName, action string, err error) { + logger.Info("finished executing credential helper program %s with action %s and erro %v", executableName, action, err) + }, + }) if opts.Debug { return trace.WithLoggerLevel(ctx, logrus.DebugLevel) } else if opts.Verbose { From 15a1efa257cfd7c0eb52346ec49530508edd3eee Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Thu, 20 Jul 2023 14:08:07 +0800 Subject: [PATCH 2/7] resolved comments Signed-off-by: wangxiaoxuan273 --- internal/cmd/options.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 8f5efd31b..24b408296 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -5,7 +5,7 @@ import ( "github.com/notaryproject/notation-go/log" "github.com/notaryproject/notation/internal/trace" - executableTrace "github.com/oras-project/oras-credentials-go/trace" + executabletrace "github.com/oras-project/oras-credentials-go/trace" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -45,19 +45,29 @@ func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) { // SetLoggerLevel sets up the logger based on common options. func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context { + if opts.Debug { + return trace.WithLoggerLevel(ctx, logrus.DebugLevel) + } else if opts.Verbose { + return trace.WithLoggerLevel(ctx, logrus.InfoLevel) + } + ctx = withExecutableTrace(ctx) + return ctx +} + +// withExecutableTrace adds tracing for credential helper executables. +func withExecutableTrace(ctx context.Context) context.Context { logger := log.GetLogger(ctx) - ctx = executableTrace.WithExecutableTrace(ctx, &executableTrace.ExecutableTrace{ + ctx = executabletrace.WithExecutableTrace(ctx, &executabletrace.ExecutableTrace{ ExecuteStart: func(executableName, action string) { - logger.Info("started executing credential helper program %s with action %s", executableName, action) + logger.Debugf("started executing credential helper program %s with action %s", executableName, action) }, ExecuteDone: func(executableName, action string, err error) { - logger.Info("finished executing credential helper program %s with action %s and erro %v", executableName, action, err) + if err != nil { + logger.Errorf("finished executing credential helper program %s with action %s and got error %w", executableName, action, err) + } else { + logger.Debugf("finished executing credential helper program %s with action %s", executableName, action) + } }, }) - if opts.Debug { - return trace.WithLoggerLevel(ctx, logrus.DebugLevel) - } else if opts.Verbose { - return trace.WithLoggerLevel(ctx, logrus.InfoLevel) - } return ctx } From 03565ec812f5f932677ff1a0693a4f00af598a82 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Thu, 20 Jul 2023 14:16:06 +0800 Subject: [PATCH 3/7] fixed a bug Signed-off-by: wangxiaoxuan273 --- internal/cmd/options.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 24b408296..176471abd 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -46,12 +46,11 @@ func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) { // SetLoggerLevel sets up the logger based on common options. func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context { if opts.Debug { - return trace.WithLoggerLevel(ctx, logrus.DebugLevel) + ctx = trace.WithLoggerLevel(ctx, logrus.DebugLevel) } else if opts.Verbose { - return trace.WithLoggerLevel(ctx, logrus.InfoLevel) + ctx = trace.WithLoggerLevel(ctx, logrus.InfoLevel) } - ctx = withExecutableTrace(ctx) - return ctx + return withExecutableTrace(ctx) } // withExecutableTrace adds tracing for credential helper executables. From 0220eb3a1b3d6c1f7dd0c0bd50721551f2b328c7 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Thu, 20 Jul 2023 14:49:54 +0800 Subject: [PATCH 4/7] updated oras-credentials-go version Signed-off-by: wangxiaoxuan273 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index cbecd7cb3..24646a843 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/notaryproject/notation-go v1.0.0-rc.6 github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.1.0-rc4 - github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225 + github.com/oras-project/oras-credentials-go v0.3.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 805dc9fdd..868847e5e 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYB github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225 h1:0T0CYijlSdn0ariTr48cRn6YDl445VZf3yqCqJKksqo= github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s= +github.com/oras-project/oras-credentials-go v0.3.0 h1:Bg1d9iAmgo50RlaIy2XI5MQs7qL00DB3R9Q4JRP1VWs= +github.com/oras-project/oras-credentials-go v0.3.0/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= From e8fe38aae532d9512a9f6158dbdb2976544db674 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Thu, 20 Jul 2023 15:13:25 +0800 Subject: [PATCH 5/7] renamed package Signed-off-by: wangxiaoxuan273 --- internal/cmd/options.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 176471abd..8e428f455 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -5,7 +5,7 @@ import ( "github.com/notaryproject/notation-go/log" "github.com/notaryproject/notation/internal/trace" - executabletrace "github.com/oras-project/oras-credentials-go/trace" + credentialstrace "github.com/oras-project/oras-credentials-go/trace" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -56,7 +56,7 @@ func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context // withExecutableTrace adds tracing for credential helper executables. func withExecutableTrace(ctx context.Context) context.Context { logger := log.GetLogger(ctx) - ctx = executabletrace.WithExecutableTrace(ctx, &executabletrace.ExecutableTrace{ + ctx = credentialstrace.WithExecutableTrace(ctx, &credentialstrace.ExecutableTrace{ ExecuteStart: func(executableName, action string) { logger.Debugf("started executing credential helper program %s with action %s", executableName, action) }, From 32fcdaa20476a006b072aa6f3f05105835cc2a1e Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Thu, 20 Jul 2023 16:30:38 +0800 Subject: [PATCH 6/7] resolved comments Signed-off-by: wangxiaoxuan273 --- cmd/notation/cert/list.go | 2 +- cmd/notation/cert/show.go | 2 +- cmd/notation/inspect.go | 2 +- cmd/notation/key.go | 6 +++--- cmd/notation/list.go | 2 +- cmd/notation/login.go | 2 +- cmd/notation/logout.go | 2 +- cmd/notation/sign.go | 2 +- cmd/notation/verify.go | 2 +- go.sum | 2 -- internal/cmd/options.go | 6 ++++-- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/notation/cert/list.go b/cmd/notation/cert/list.go index 6b6723fd9..dc042c50a 100644 --- a/cmd/notation/cert/list.go +++ b/cmd/notation/cert/list.go @@ -52,7 +52,7 @@ Example - List all certificate files from trust store "wabbit-networks" of type func listCerts(ctx context.Context, opts *certListOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) logger := log.GetLogger(ctx) namedStore := opts.namedStore diff --git a/cmd/notation/cert/show.go b/cmd/notation/cert/show.go index 3f3291eb7..f4da746f5 100644 --- a/cmd/notation/cert/show.go +++ b/cmd/notation/cert/show.go @@ -59,7 +59,7 @@ Example - Show details of certificate "cert2.pem" with type "signingAuthority" f func showCerts(ctx context.Context, opts *certShowOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) logger := log.GetLogger(ctx) storeType := opts.storeType diff --git a/cmd/notation/inspect.go b/cmd/notation/inspect.go index 6f678868b..504acb751 100644 --- a/cmd/notation/inspect.go +++ b/cmd/notation/inspect.go @@ -108,7 +108,7 @@ Example - [Experimental] Inspect signatures on an OCI artifact identified by a d func runInspect(command *cobra.Command, opts *inspectOpts) error { // set log level - ctx := opts.LoggingFlagOpts.SetLoggerLevel(command.Context()) + ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context()) if opts.outputFormat != cmd.OutputJSON && opts.outputFormat != cmd.OutputPlaintext { return fmt.Errorf("unrecognized output format %s", opts.outputFormat) diff --git a/cmd/notation/key.go b/cmd/notation/key.go index 9764bdbd0..b0d220256 100644 --- a/cmd/notation/key.go +++ b/cmd/notation/key.go @@ -162,7 +162,7 @@ func keyDeleteCommand(opts *keyDeleteOpts) *cobra.Command { func addKey(ctx context.Context, opts *keyAddOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) pluginConfig, err := cmd.ParseFlagMap(opts.pluginConfig, cmd.PflagPluginConfig.Name) if err != nil { @@ -188,7 +188,7 @@ func addKey(ctx context.Context, opts *keyAddOpts) error { func updateKey(ctx context.Context, opts *keyUpdateOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) logger := log.GetLogger(ctx) if !opts.isDefault { @@ -222,7 +222,7 @@ func listKeys() error { func deleteKeys(ctx context.Context, opts *keyDeleteOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) logger := log.GetLogger(ctx) // core process diff --git a/cmd/notation/list.go b/cmd/notation/list.go index ba7ac1a6b..ec4d7a913 100644 --- a/cmd/notation/list.go +++ b/cmd/notation/list.go @@ -66,7 +66,7 @@ func listCommand(opts *listOpts) *cobra.Command { func runList(ctx context.Context, opts *listOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) // initialize reference := opts.reference diff --git a/cmd/notation/login.go b/cmd/notation/login.go index 7a13cc44a..3f3c763c3 100644 --- a/cmd/notation/login.go +++ b/cmd/notation/login.go @@ -65,7 +65,7 @@ Example - Login using $NOTATION_USERNAME $NOTATION_PASSWORD variables: func runLogin(ctx context.Context, opts *loginOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) // initialize serverAddress := opts.server diff --git a/cmd/notation/logout.go b/cmd/notation/logout.go index fd13b5b80..2a36322d9 100644 --- a/cmd/notation/logout.go +++ b/cmd/notation/logout.go @@ -40,7 +40,7 @@ func logoutCommand(opts *logoutOpts) *cobra.Command { func runLogout(ctx context.Context, opts *logoutOpts) error { // set log level - ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx) + ctx = opts.LoggingFlagOpts.InitializeLogger(ctx) credsStore, err := auth.NewCredentialsStore() if err != nil { return fmt.Errorf("failed to get credentials store: %v", err) diff --git a/cmd/notation/sign.go b/cmd/notation/sign.go index 3b4b4bee2..668cc5890 100644 --- a/cmd/notation/sign.go +++ b/cmd/notation/sign.go @@ -107,7 +107,7 @@ Example - [Experimental] Sign an OCI artifact identified by a tag and referenced func runSign(command *cobra.Command, cmdOpts *signOpts) error { // set log level - ctx := cmdOpts.LoggingFlagOpts.SetLoggerLevel(command.Context()) + ctx := cmdOpts.LoggingFlagOpts.InitializeLogger(command.Context()) // initialize signer, err := cmd.GetSigner(ctx, &cmdOpts.SignerFlagOpts) diff --git a/cmd/notation/verify.go b/cmd/notation/verify.go index 6e17c3b1a..a14c13cdc 100644 --- a/cmd/notation/verify.go +++ b/cmd/notation/verify.go @@ -93,7 +93,7 @@ Example - [Experimental] Verify a signature on an OCI artifact identified by a t func runVerify(command *cobra.Command, opts *verifyOpts) error { // set log level - ctx := opts.LoggingFlagOpts.SetLoggerLevel(command.Context()) + ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context()) // initialize sigVerifier, err := verifier.NewFromConfig() diff --git a/go.sum b/go.sum index 868847e5e..c85ee3eca 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0= github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= -github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225 h1:0T0CYijlSdn0ariTr48cRn6YDl445VZf3yqCqJKksqo= -github.com/oras-project/oras-credentials-go v0.2.1-0.20230714083315-2afb422fe225/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s= github.com/oras-project/oras-credentials-go v0.3.0 h1:Bg1d9iAmgo50RlaIy2XI5MQs7qL00DB3R9Q4JRP1VWs= github.com/oras-project/oras-credentials-go v0.3.0/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 8e428f455..94401a74a 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -43,12 +43,14 @@ func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) { fs.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose mode") } -// SetLoggerLevel sets up the logger based on common options. -func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context { +// InitializeLogger sets up the logger based on common options. +func (opts *LoggingFlagOpts) InitializeLogger(ctx context.Context) context.Context { if opts.Debug { ctx = trace.WithLoggerLevel(ctx, logrus.DebugLevel) } else if opts.Verbose { ctx = trace.WithLoggerLevel(ctx, logrus.InfoLevel) + } else { + return ctx } return withExecutableTrace(ctx) } From 0e8c8d29f54de902594d59f62fb46f30ea5c8003 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Fri, 21 Jul 2023 14:14:24 +0800 Subject: [PATCH 7/7] added an adverb Signed-off-by: wangxiaoxuan273 --- internal/cmd/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 94401a74a..28677f0e2 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -66,7 +66,7 @@ func withExecutableTrace(ctx context.Context) context.Context { if err != nil { logger.Errorf("finished executing credential helper program %s with action %s and got error %w", executableName, action, err) } else { - logger.Debugf("finished executing credential helper program %s with action %s", executableName, action) + logger.Debugf("successfully finished executing credential helper program %s with action %s", executableName, action) } }, })