Skip to content

Commit

Permalink
perf: save auth requests for push and attach (#1097)
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvia Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia authored Sep 1, 2023
1 parent 2d6d36e commit 31f9b91
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/graph"
"oras.land/oras/internal/registryutil"
)

type attachOptions struct {
Expand Down Expand Up @@ -116,6 +119,11 @@ func runAttach(ctx context.Context, opts attachOptions) error {
if err := opts.EnsureReferenceNotEmpty(); err != nil {
return err
}
if repo, ok := dst.(*remote.Repository); ok {
// add both pull and push scope hints for dst repository
// to save potential push-scope token requests during copy
ctx = registryutil.WithScopeHint(ctx, repo.Reference, auth.ActionPull, auth.ActionPush)
}
subject, err := dst.Resolve(ctx, opts.Reference)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import (
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/fileref"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/contentutil"
"oras.land/oras/internal/registryutil"
)

type pushOptions struct {
Expand Down Expand Up @@ -181,6 +184,12 @@ func runPush(ctx context.Context, opts pushOptions) error {
union := contentutil.MultiReadOnlyTarget(memoryStore, store)
updateDisplayOption(&copyOptions.CopyGraphOptions, union, opts.Verbose)
copy := func(root ocispec.Descriptor) error {
if repo, ok := dst.(*remote.Repository); ok {
// add both pull and push scope hints for dst repository
// to save potential push-scope token requests during copy
ctx = registryutil.WithScopeHint(ctx, repo.Reference, auth.ActionPull, auth.ActionPush)
}

if tag := opts.Reference; tag == "" {
err = oras.CopyGraph(ctx, union, dst, root, copyOptions.CopyGraphOptions)
} else {
Expand Down
29 changes: 29 additions & 0 deletions internal/registryutil/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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.
*/

package registryutil

import (
"context"

"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote/auth"
)

// WithScopeHint adds a hinted scope to the context.
func WithScopeHint(ctx context.Context, ref registry.Reference, actions ...string) context.Context {
scope := auth.ScopeRepository(ref.Repository, actions...)
return auth.AppendScopes(ctx, scope)
}

0 comments on commit 31f9b91

Please sign in to comment.