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

feat: add --debug & --verbose flags & http request/response debug log #457

Merged
merged 17 commits into from
Dec 5, 2022
Merged
3 changes: 1 addition & 2 deletions cmd/notation/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ func listCommand(opts *listOpts) *cobra.Command {
func runList(command *cobra.Command, opts *listOpts) error {
// initialize
reference := opts.reference
remoteRepo, err := getSignatureRepositoryClient(&opts.SecureFlagOpts, reference)
sigRepo, err := getSignatureRepositoryClient(&opts.SecureFlagOpts, reference)
if err != nil {
return err
}
sigRepo := notationRegistry.NewRepository(remoteRepo)

// core process
manifestDesc, err := getManifestDescriptorFromReference(command.Context(), &opts.SecureFlagOpts, reference)
Expand Down
5 changes: 2 additions & 3 deletions cmd/notation/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"

notationRegistry "github.com/notaryproject/notation-go/registry"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry"
)
Expand All @@ -22,9 +21,9 @@ func getManifestDescriptorFromReference(ctx context.Context, opts *SecureFlagOpt
if err != nil {
return ocispec.Descriptor{}, err
}
repository, err := getRepositoryClient(opts, ref)
repo, err := getRepositoryClient(opts, ref)
if err != nil {
return ocispec.Descriptor{}, err
}
return notationRegistry.NewRepository(repository).Resolve(ctx, ref.ReferenceOrDefault())
return repo.Resolve(ctx, ref.ReferenceOrDefault())
}
52 changes: 33 additions & 19 deletions cmd/notation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,62 @@ import (
"net"
"net/http"

notationRegistry "github.com/notaryproject/notation-go/registry"
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
"github.com/notaryproject/notation/internal/trace"
"github.com/notaryproject/notation/internal/version"
loginauth "github.com/notaryproject/notation/pkg/auth"

JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
"github.com/notaryproject/notation/pkg/configutil"
"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
)

func getSignatureRepositoryClient(opts *SecureFlagOpts, reference string) (*remote.Repository, error) {
type repository struct {
notationRegistry.Repository
remoteRepo *remote.Repository
}

func (r *repository) SetHttpDebugLog(debug bool) {
if !debug {
return
}
if authClient, ok := r.remoteRepo.Client.(*auth.Client); ok {
if authClient.Client == nil {
authClient.Client = http.DefaultClient
}
if authClient.Client.Transport == nil {
authClient.Client.Transport = http.DefaultTransport
}
authClient.Client.Transport = trace.NewTransport(authClient.Client.Transport)
}
}

func NewNotationRepository(remoteRepo *remote.Repository) *repository {
return &repository{
Repository: notationRegistry.NewRepository(remoteRepo),
remoteRepo: remoteRepo,
}
}

func getSignatureRepositoryClient(opts *SecureFlagOpts, reference string) (*repository, error) {
ref, err := registry.ParseReference(reference)
if err != nil {
return nil, err
}
return getRepositoryClient(opts, ref)
}

func getRepositoryClient(opts *SecureFlagOpts, ref registry.Reference) (*remote.Repository, error) {
func getRepositoryClient(opts *SecureFlagOpts, ref registry.Reference) (*repository, error) {
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
authClient, plainHTTP, err := getAuthClient(opts, ref)
if err != nil {
return nil, err
}
return &remote.Repository{
return NewNotationRepository(&remote.Repository{
Client: authClient,
Reference: ref,
PlainHTTP: plainHTTP,
}, nil
}

func setHttpDebugLog(repo *remote.Repository, debug bool) {
if !debug {
return
}
if authClient, ok := repo.Client.(*auth.Client); ok {
if authClient.Client == nil {
authClient.Client = http.DefaultClient
}
if authClient.Client.Transport == nil {
authClient.Client.Transport = http.DefaultTransport
}
authClient.Client.Transport = trace.NewTransport(authClient.Client.Transport)
}
}), nil
}

func getRegistryClient(opts *SecureFlagOpts, serverAddress string) (*remote.Registry, error) {
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
8 changes: 3 additions & 5 deletions cmd/notation/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/notaryproject/notation-go"
notationRegistry "github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/envelope"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -83,13 +82,12 @@ func runSign(command *cobra.Command, cmdOpts *signOpts) error {
if err != nil {
return err
}
remoteRepo, err := getSignatureRepositoryClient(&cmdOpts.SecureFlagOpts, cmdOpts.reference)
sigRepo, err := getSignatureRepositoryClient(&cmdOpts.SecureFlagOpts, cmdOpts.reference)
if err != nil {
return err
}
setHttpDebugLog(remoteRepo, cmdOpts.Debug)
repo := notationRegistry.NewRepository(remoteRepo)
_, err = notation.Sign(ctx, signer, repo, opts)
sigRepo.SetHttpDebugLog(cmdOpts.Debug)
_, err = notation.Sign(ctx, signer, sigRepo, opts)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/notaryproject/notation-go"
notationRegistry "github.com/notaryproject/notation-go/registry"
"github.com/notaryproject/notation-go/verifier"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/ioutil"
Expand Down Expand Up @@ -72,12 +71,11 @@ func runVerify(command *cobra.Command, opts *verifyOpts) error {
if err != nil {
return err
}
remoteRepo, err := getRepositoryClient(&opts.SecureFlagOpts, ref)
repo, err := getRepositoryClient(&opts.SecureFlagOpts, ref)
if err != nil {
return err
}
setHttpDebugLog(remoteRepo, opts.Debug)
repo := notationRegistry.NewRepository(remoteRepo)
repo.SetHttpDebugLog(opts.Debug)
// set up verification plugin config.
configs, err := cmd.ParseFlagPluginConfig(opts.pluginConfig)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context,
logger := logrus.New()
logger.SetFormatter(&formatter)
logger.SetLevel(level)
entry := logger.WithContext(ctx)

// save logger to context
return log.WithLogger(ctx, entry), entry
return log.WithLogger(ctx, logger), logger
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 4 additions & 7 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ 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
Expand All @@ -40,9 +38,8 @@ 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:")
e.Debugf("> Request: %q %q", req.Method, req.URL)
e.Debugf("> Request headers:")
logHeader(req.Header, e)

resp, err = t.RoundTripper.RoundTrip(req)
Expand All @@ -51,8 +48,8 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
} 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:")
e.Debugf("< Response status: %q", resp.Status)
e.Debugf("< Response headers:")
logHeader(resp.Header, e)
}
return resp, err
Expand Down