Skip to content

Commit

Permalink
feat: update log
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Nov 30, 2022
1 parent 86287b5 commit 8988dec
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/notation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"errors"
"net"
"net/http"

notationregistry "github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation/internal/trace"
"github.com/notaryproject/notation/internal/version"
loginauth "github.com/notaryproject/notation/pkg/auth"
"github.com/notaryproject/notation/pkg/configutil"
Expand Down Expand Up @@ -91,6 +93,9 @@ func getAuthClient(opts *SecureFlagOpts, ref registry.Reference) (*auth.Client,
},
Cache: auth.NewCache(),
ClientID: "notation",
Client: &http.Client{
Transport: trace.NewTransport(http.DefaultTransport),
},
}
authClient.SetUserAgent("notation/" + version.GetVersion())

Expand Down
8 changes: 6 additions & 2 deletions cmd/notation/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type signOpts struct {
cmd.LoggingFlagOpts
cmd.SignerFlagOpts
SecureFlagOpts
expiry time.Duration
Expand Down Expand Up @@ -55,15 +56,18 @@ Example - Sign a container image using the image digest
return runSign(cmd, opts)
},
}
opts.LoggingFlagOpts.ApplyFlags(command.Flags())
opts.SignerFlagOpts.ApplyFlags(command.Flags())
opts.SecureFlagOpts.ApplyFlags(command.Flags())
cmd.SetPflagExpiry(command.Flags(), &opts.expiry)
cmd.SetPflagPluginConfig(command.Flags(), &opts.pluginConfig)

return command
}

func runSign(command *cobra.Command, cmdOpts *signOpts) error {
// set log level
ctx, _ := cmdOpts.LoggingFlagOpts.SetLoggerLevel()

// initialize
signer, err := cmd.GetSigner(&cmdOpts.SignerFlagOpts)
if err != nil {
Expand All @@ -79,7 +83,7 @@ func runSign(command *cobra.Command, cmdOpts *signOpts) error {
if err != nil {
return err
}
_, err = notation.Sign(command.Context(), signer, sigRepo, opts)
_, err = notation.Sign(ctx, signer, sigRepo, opts)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

type verifyOpts struct {
cmd.LoggingFlagOpts
SecureFlagOpts
reference string
pluginConfig []string
Expand All @@ -42,12 +43,16 @@ func verifyCommand(opts *verifyOpts) *cobra.Command {
return runVerify(cmd, opts)
},
}
opts.ApplyFlags(command.Flags())
opts.LoggingFlagOpts.ApplyFlags(command.Flags())
opts.SecureFlagOpts.ApplyFlags(command.Flags())
command.Flags().StringArrayVarP(&opts.pluginConfig, "plugin-config", "c", nil, "{key}={value} pairs that are passed as it is to a plugin, if the verification is associated with a verification plugin, refer plugin documentation to set appropriate values")
return command
}

func runVerify(command *cobra.Command, opts *verifyOpts) error {
// set log level
ctx, _ := opts.LoggingFlagOpts.SetLoggerLevel()

// resolve the given reference and set the digest.
ref, err := resolveReference(command, opts)
if err != nil {
Expand Down Expand Up @@ -82,7 +87,7 @@ func runVerify(command *cobra.Command, opts *verifyOpts) error {
}

// core verify process.
_, outcomes, err := notation.Verify(command.Context(), verifier, repo, verifyOpts)
_, outcomes, err := notation.Verify(ctx, verifier, repo, verifyOpts)

// write out.
return ioutil.PrintVerificationResults(os.Stdout, outcomes, err, ref.Reference)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/notaryproject/notation-go v0.12.0-beta.1.0.20221129043056-7ae1f5fd0730
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/veraison/go-cose v1.0.0-rc.2
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e/go.mod h1:chxPXzS
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/distribution/v3 v3.0.0-20220729163034-26163d82560f h1:3NCYdjXycNd/Xn/iICZzmxkiDX1e1cjTHjbMAz+wRVk=
github.com/distribution/distribution/v3 v3.0.0-20220729163034-26163d82560f/go.mod h1:28YO/VJk9/64+sTGNuYaBjWxrXTPrj0C0XmgTIOjxX4=
github.com/docker/docker-credential-helpers v0.7.0 h1:xtCHsjxogADNZcdv1pKUHXryefjlVRqWqIhk/uXJp0A=
Expand All @@ -19,20 +20,21 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/notaryproject/notation-core-go v0.2.0-beta.1.0.20221123104522-9b5de089a023 h1:Z/2hxPJOjWfmgOPTNkGBDp/LVIEtizd9uJNQvjFE0Dc=
github.com/notaryproject/notation-core-go v0.2.0-beta.1.0.20221123104522-9b5de089a023/go.mod h1:n8Gbvl9sKa00KptkKEL5XKUyMTIALe74QipKauE2rj4=
github.com/notaryproject/notation-go v0.12.0-beta.1.0.20221129043056-7ae1f5fd0730 h1:WPzkdjn/fruM07tl4ZsrUNBx9FT2a/hCJwj2Djuamv0=
github.com/notaryproject/notation-go v0.12.0-beta.1.0.20221129043056-7ae1f5fd0730/go.mod h1:2Xy40C9rJip3h9XPC6ei2HEEdUoZJ5KDC6mlX/FD0oQ=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7XWJGmnM7r3wg034=
github.com/opencontainers/image-spec v1.1.0-rc2/go.mod h1:3OVijpioIKYWTqjiG0zfF6wvoJ4fAXGbjdZuI2NgsRQ=
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=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/veraison/go-cose v1.0.0-rc.2 h1:zH3QmP4N5kwpdGauceIT3aJm8iUyV9OqpUOb+7CF7rQ=
Expand All @@ -44,6 +46,7 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
26 changes: 26 additions & 0 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cmd

import (
"context"

"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation/internal/trace"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

Expand All @@ -15,3 +20,24 @@ func (opts *SignerFlagOpts) ApplyFlags(fs *pflag.FlagSet) {
SetPflagKey(fs, &opts.Key)
SetPflagSignatureFormat(fs, &opts.SignatureFormat)
}

// LoggingFlagOpts option struct.
type LoggingFlagOpts struct {
Debug bool
}

// ApplyFlags applies flags to a command flag set.
func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&opts.Debug, "debug", "d", false, "debug mode")
}

// SetLoggerLevel sets up the logger based on common options.
func (opts *LoggingFlagOpts) SetLoggerLevel() (context.Context, log.Logger) {
var logLevel logrus.Level
if opts.Debug {
logLevel = logrus.DebugLevel
} else {
logLevel = logrus.WarnLevel
}
return trace.WithLoggerLevel(context.Background(), logLevel)
}
36 changes: 36 additions & 0 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright The ORAS 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.
Copied and adapted from oras (https://github.com/oras-project/oras)
*/

package trace

import (
"context"

"github.com/notaryproject/notation-go/log"
"github.com/sirupsen/logrus"
)

// WithLoggerLevel returns a context with logrus log entry.
func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context, log.Logger) {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
logger.SetLevel(level)
entry := logger.WithContext(ctx)
return log.WithLogger(ctx, entry), entry
}
73 changes: 73 additions & 0 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright The ORAS 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.
Copied and adapted from oras (https://github.com/oras-project/oras)
*/

package trace

import (
"net/http"
"strings"

"github.com/notaryproject/notation-go/log"
)

// Transport is an http.RoundTripper that keeps track of the in-flight
// request and add hooks to report HTTP tracing events.
type Transport struct {
http.RoundTripper
}

func NewTransport(base http.RoundTripper) *Transport {
return &Transport{base}
}

// RoundTrip calls base roundtrip while keeping track of the current request.
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
ctx := req.Context()
e := log.GetLogger(ctx)

e.Debugf(" Request URL: %q", req.URL)
e.Debugf(" Request method: %q", req.Method)
e.Debugf(" Request headers:")
logHeader(req.Header, e)

resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
e.Errorf("Error in getting response: %w", err)
} else if resp == nil {
e.Errorf("No response obtained for request %s %q", req.Method, req.URL)
} else {
e.Debugf(" Response Status: %q", resp.Status)
e.Debugf(" Response headers:")
logHeader(resp.Header, e)
}
return resp, err
}

// logHeader prints out the provided header keys and values, with auth header
// scrubbed.
func logHeader(header http.Header, e log.Logger) {
if len(header) > 0 {
for k, v := range header {
if strings.EqualFold(k, "Authorization") {
v = []string{"*****"}
}
e.Debugf(" %q: %q", k, strings.Join(v, ", "))
}
} else {
e.Debugf(" Empty header")
}
}

0 comments on commit 8988dec

Please sign in to comment.