From 4d11669cb04ab5dc57c60915753604bf2b3745e7 Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Mon, 25 Sep 2023 16:28:04 +0800 Subject: [PATCH] rename Signed-off-by: Lixia (Sylvia) Lei --- registry/remote/auth/client.go | 4 +- registry/remote/auth/client_test.go | 24 +++++----- registry/remote/auth/scope.go | 28 ++++++------ registry/remote/auth/scope_test.go | 68 ++++++++++++++--------------- registry/remote/registry.go | 2 +- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/registry/remote/auth/client.go b/registry/remote/auth/client.go index aea376ed..a9a49d38 100644 --- a/registry/remote/auth/client.go +++ b/registry/remote/auth/client.go @@ -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) @@ -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 diff --git a/registry/remote/auth/client_test.go b/registry/remote/auth/client_test.go index 74db0dba..de879863 100644 --- a/registry/remote/auth/client_test.go +++ b/registry/remote/auth/client_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/registry/remote/auth/scope.go b/registry/remote/auth/scope.go index ce5b255a..41cb3e2e 100644 --- a/registry/remote/auth/scope.go +++ b/registry/remote/auth/scope.go @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/registry/remote/auth/scope_test.go b/registry/remote/auth/scope_test.go index 44c07f79..be901499 100644 --- a/registry/remote/auth/scope_test.go +++ b/registry/remote/auth/scope_test.go @@ -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) } @@ -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) } } @@ -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) } @@ -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) } @@ -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) } } @@ -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) } @@ -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) } } diff --git a/registry/remote/registry.go b/registry/remote/registry.go index f0c417e4..d1334042 100644 --- a/registry/remote/registry.go +++ b/registry/remote/registry.go @@ -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 {