Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia committed Sep 25, 2023
1 parent a0b8680 commit 4d11669
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
4 changes: 2 additions & 2 deletions registry/remote/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
}
case SchemeBearer:
// merge per-host scopes with generic scopes
scopes := GetScopesPerHost(ctx, host)
scopes := GetScopesForHost(ctx, host)
if moreScopes := GetScopes(ctx); len(moreScopes) > 0 {
scopes = append(scopes, moreScopes...)
scopes = CleanScopes(scopes)
Expand Down Expand Up @@ -228,7 +228,7 @@ func (c *Client) Do(originalReq *http.Request) (*http.Response, error) {
case SchemeBearer:
resp.Body.Close()

scopes := GetScopesPerHost(ctx, host)
scopes := GetScopesForHost(ctx, host)
cleanScopeLen := len(scopes)
if moreScopes := GetScopes(ctx); len(moreScopes) > 0 {
// merge per-host scopes with generic scopes
Expand Down
24 changes: 12 additions & 12 deletions registry/remote/auth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ func TestClient_Do_Bearer_AccessToken_Cached_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scope1)
ctx = WithScopesPerHost(ctx, uri2.Host, scope2)
ctx = WithScopesForHost(ctx, uri1.Host, scope1)
ctx = WithScopesForHost(ctx, uri2.Host, scope2)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down Expand Up @@ -1066,8 +1066,8 @@ func TestClient_Do_Bearer_Auth_Cached_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesPerHost(ctx, uri2.Host, scopes2...)
ctx = WithScopesForHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesForHost(ctx, uri2.Host, scopes2...)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down Expand Up @@ -1716,8 +1716,8 @@ func TestClient_Do_Bearer_OAuth2_Password_Cached_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesPerHost(ctx, uri2.Host, scopes2...)
ctx = WithScopesForHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesForHost(ctx, uri2.Host, scopes2...)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down Expand Up @@ -2330,8 +2330,8 @@ func TestClient_Do_Bearer_OAuth2_RefreshToken_Cached_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesPerHost(ctx, uri2.Host, scopes2...)
ctx = WithScopesForHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesForHost(ctx, uri2.Host, scopes2...)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down Expand Up @@ -2779,8 +2779,8 @@ func TestClient_Do_Token_Expire_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesPerHost(ctx, uri2.Host, scopes2...)
ctx = WithScopesForHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesForHost(ctx, uri2.Host, scopes2...)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down Expand Up @@ -3212,8 +3212,8 @@ func TestClient_Do_Scope_Hint_Mismatch_PerHost(t *testing.T) {
}

ctx := context.Background()
ctx = WithScopesPerHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesPerHost(ctx, uri2.Host, scopes2...)
ctx = WithScopesForHost(ctx, uri1.Host, scopes1...)
ctx = WithScopesForHost(ctx, uri2.Host, scopes2...)
// first request to server 1
req1, err := http.NewRequestWithContext(ctx, http.MethodGet, ts1.URL, nil)
if err != nil {
Expand Down
28 changes: 14 additions & 14 deletions registry/remote/auth/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func AppendScopeHints(ctx context.Context, ref registry.Reference, actions ...st
return ctx
}
scope := ScopeRepository(ref.Repository, actions...)
return AppendScopesPerHost(ctx, ref.Host(), scope)
return AppendScopesForHost(ctx, ref.Host(), scope)
}

// scopesContextKey is the context key for scopes.
Expand Down Expand Up @@ -123,10 +123,10 @@ func GetScopes(ctx context.Context) []string {
return nil
}

// scopesPerHostContextKey is the context key for per-host scopes.
type scopesPerHostContextKey string
// scopesForHostContextKey is the context key for per-host scopes.
type scopesForHostContextKey string

// WithScopesPerHost returns a context with per-host scopes added.
// WithScopesForHost returns a context with per-host scopes added.
// Scopes are de-duplicated.
// Scopes are used as hints for the auth client to fetch bearer tokens with
// larger scopes.
Expand All @@ -136,7 +136,7 @@ type scopesPerHostContextKey string
// `repository:hello-world:pull`, and the auth client will fetch a token for
// that challenge. Later, the POST request will return a challenge for scope
// `repository:hello-world:push`, and the auth client will fetch a token for
// that challenge again. By invoking WithScopesPerHost with the scope
// that challenge again. By invoking WithScopesForHost with the scope
// `repository:hello-world:pull,push`, the auth client with cache is hinted to
// fetch a token via a single token fetch request for all the HEAD, POST, PUT
// requests.
Expand All @@ -145,26 +145,26 @@ type scopesPerHostContextKey string
// context for the given host.
//
// Reference: https://docs.docker.com/registry/spec/auth/scope/
func WithScopesPerHost(ctx context.Context, host string, scopes ...string) context.Context {
func WithScopesForHost(ctx context.Context, host string, scopes ...string) context.Context {
scopes = CleanScopes(scopes)
return context.WithValue(ctx, scopesPerHostContextKey(host), scopes)
return context.WithValue(ctx, scopesForHostContextKey(host), scopes)
}

// AppendScopesPerHost appends additional scopes to the existing scopes
// AppendScopesForHost appends additional scopes to the existing scopes
// in the context for the given host and returns a new context.
// The resulted scopes are de-duplicated.
// The append operation does modify the existing scope in the context passed in.
func AppendScopesPerHost(ctx context.Context, host string, scopes ...string) context.Context {
func AppendScopesForHost(ctx context.Context, host string, scopes ...string) context.Context {
if len(scopes) == 0 {
return ctx
}
oldScopes := GetScopesPerHost(ctx, host)
return WithScopesPerHost(ctx, host, append(oldScopes, scopes...)...)
oldScopes := GetScopesForHost(ctx, host)
return WithScopesForHost(ctx, host, append(oldScopes, scopes...)...)
}

// GetScopesPerHost returns the scopes in the context for the given host.
func GetScopesPerHost(ctx context.Context, host string) []string {
if scopes, ok := ctx.Value(scopesPerHostContextKey(host)).([]string); ok {
// GetScopesForHost returns the scopes in the context for the given host.
func GetScopesForHost(ctx context.Context, host string) []string {
if scopes, ok := ctx.Value(scopesForHostContextKey(host)).([]string); ok {
return slices.Clone(scopes)
}
return nil
Expand Down
68 changes: 34 additions & 34 deletions registry/remote/auth/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func TestWithScopeHints(t *testing.T) {
}
ctx = AppendScopeHints(ctx, ref1, ActionPull)
ctx = AppendScopeHints(ctx, ref2, ActionPush)
if got := GetScopesPerHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
if got := GetScopesForHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want2)
}

Expand All @@ -151,20 +151,20 @@ func TestWithScopeHints(t *testing.T) {
}
ctx = AppendScopeHints(ctx, ref1, scopes1...)
ctx = AppendScopeHints(ctx, ref2, scopes2...)
if got := GetScopesPerHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
if got := GetScopesForHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want2)
}

// append empty scopes
ctx = AppendScopeHints(ctx, ref1)
ctx = AppendScopeHints(ctx, ref2)
if got := GetScopesPerHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
if got := GetScopesForHost(ctx, ref1.Host()); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, ref2.Host()); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopeHints()) = %v, want %v", got, want2)
}
}
Expand Down Expand Up @@ -262,12 +262,12 @@ func TestWithScopesPerHost(t *testing.T) {
want2 := []string{
"repository:foo:push",
}
ctx = WithScopesPerHost(ctx, reg1, want1...)
ctx = WithScopesPerHost(ctx, reg2, want2...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = WithScopesForHost(ctx, reg1, want1...)
ctx = WithScopesForHost(ctx, reg2, want2...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want2)
}

Expand All @@ -278,12 +278,12 @@ func TestWithScopesPerHost(t *testing.T) {
want2 = []string{
"repository:bar:pull",
}
ctx = WithScopesPerHost(ctx, reg1, want1...)
ctx = WithScopesPerHost(ctx, reg2, want2...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = WithScopesForHost(ctx, reg1, want1...)
ctx = WithScopesForHost(ctx, reg2, want2...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want2)
}

Expand All @@ -308,23 +308,23 @@ func TestWithScopesPerHost(t *testing.T) {
"repository:goodbye-world:pull,push",
"repository:nginx:delete",
}
ctx = WithScopesPerHost(ctx, reg1, scopes1...)
ctx = WithScopesPerHost(ctx, reg2, scopes2...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = WithScopesForHost(ctx, reg1, scopes1...)
ctx = WithScopesForHost(ctx, reg2, scopes2...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want2)
}

// clean scopes
var want []string
ctx = WithScopesPerHost(ctx, reg1, want...)
ctx = WithScopesPerHost(ctx, reg2, want...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want) {
ctx = WithScopesForHost(ctx, reg1, want...)
ctx = WithScopesForHost(ctx, reg2, want...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want) {
t.Errorf("GetScopesPerRegistry(WithScopesPerRegistry()) = %v, want %v", got, want)
}
}
Expand All @@ -341,12 +341,12 @@ func TestAppendScopesPerHost(t *testing.T) {
want2 := []string{
"repository:foo:push",
}
ctx = AppendScopesPerHost(ctx, reg1, want1...)
ctx = AppendScopesPerHost(ctx, reg2, want2...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = AppendScopesForHost(ctx, reg1, want1...)
ctx = AppendScopesForHost(ctx, reg2, want2...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want2)
}

Expand All @@ -373,22 +373,22 @@ func TestAppendScopesPerHost(t *testing.T) {
"repository:goodbye-world:pull,push",
"repository:nginx:delete",
}
ctx = AppendScopesPerHost(ctx, reg1, scopes1...)
ctx = AppendScopesPerHost(ctx, reg2, scopes2...)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = AppendScopesForHost(ctx, reg1, scopes1...)
ctx = AppendScopesForHost(ctx, reg2, scopes2...)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want2)
}

// append empty scopes
ctx = AppendScopesPerHost(ctx, reg1)
ctx = AppendScopesPerHost(ctx, reg2)
if got := GetScopesPerHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
ctx = AppendScopesForHost(ctx, reg1)
ctx = AppendScopesForHost(ctx, reg2)
if got := GetScopesForHost(ctx, reg1); !reflect.DeepEqual(got, want1) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want1)
}
if got := GetScopesPerHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
if got := GetScopesForHost(ctx, reg2); !reflect.DeepEqual(got, want2) {
t.Errorf("GetScopesPerRegistry(AppendScopesPerRegistry()) = %v, want %v", got, want2)
}
}
Expand Down
2 changes: 1 addition & 1 deletion registry/remote/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (r *Registry) Ping(ctx context.Context) error {
//
// Reference: https://docs.docker.com/registry/spec/api/#catalog
func (r *Registry) Repositories(ctx context.Context, last string, fn func(repos []string) error) error {
ctx = auth.AppendScopesPerHost(ctx, r.Reference.Host(), auth.ScopeRegistryCatalog)
ctx = auth.AppendScopesForHost(ctx, r.Reference.Host(), auth.ScopeRegistryCatalog)
url := buildRegistryCatalogURL(r.PlainHTTP, r.Reference)
var err error
for err == nil {
Expand Down

0 comments on commit 4d11669

Please sign in to comment.