Skip to content

Commit a0f6556

Browse files
authored
fix(credentials): preserve canceled inherited helper lookups (#1467)
Signed-off-by: Liang Hu <lh3057@columbia.edu>
1 parent 21b1f7b commit a0f6556

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎registry/remote/credentials/store.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ type inheritedHelperStore struct {
295295
func (s inheritedHelperStore) Get(ctx context.Context, serverAddress string) (Credential, error) {
296296
cred, err := s.Store.Get(ctx, serverAddress)
297297
if err != nil {
298+
if ctx.Err() != nil {
299+
return EmptyCredential, err
300+
}
298301
return EmptyCredential, nil
299302
}
300303
return cred, nil

‎registry/remote/credentials/store_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func (t *testStore) Delete(ctx context.Context, serverAddress string) error {
5252

5353
type badStore struct{}
5454

55+
type contextErrorStore struct{ Store }
56+
57+
func (contextErrorStore) Get(ctx context.Context, _ string) (Credential, error) {
58+
return EmptyCredential, ctx.Err()
59+
}
60+
5561
var errBadStore = errors.New("bad store!")
5662

5763
// Get retrieves credentials from the store for the given server address.
@@ -1179,6 +1185,37 @@ func Test_inheritedHelperStore_Get_helperError(t *testing.T) {
11791185
}
11801186
}
11811187

1188+
func Test_inheritedHelperStore_Get_success(t *testing.T) {
1189+
address := "example.com/team/app"
1190+
want := Credential{Username: "username", Password: "password"}
1191+
store := inheritedHelperStore{&testStore{storage: map[string]Credential{address: want}}}
1192+
got, err := store.Get(context.Background(), address)
1193+
if err != nil {
1194+
t.Fatal("inheritedHelperStore.Get() error =", err)
1195+
}
1196+
if got != want {
1197+
t.Errorf("inheritedHelperStore.Get() = %v, want %v", got, want)
1198+
}
1199+
}
1200+
1201+
func Test_inheritedHelperStore_Get_contextError(t *testing.T) {
1202+
store := inheritedHelperStore{contextErrorStore{}}
1203+
canceled, cancel := context.WithCancel(context.Background())
1204+
cancel()
1205+
deadline, stop := context.WithTimeout(context.Background(), 0)
1206+
defer stop()
1207+
1208+
for _, ctx := range []context.Context{canceled, deadline} {
1209+
got, err := store.Get(ctx, "example.com/team/app")
1210+
if !errors.Is(err, ctx.Err()) {
1211+
t.Errorf("inheritedHelperStore.Get() error = %v, want %v", err, ctx.Err())
1212+
}
1213+
if got != EmptyCredential {
1214+
t.Errorf("inheritedHelperStore.Get() = %v, want %v", got, EmptyCredential)
1215+
}
1216+
}
1217+
}
1218+
11821219
func Test_inheritedHelperStore_Put_helperError(t *testing.T) {
11831220
store := inheritedHelperStore{&nativeStore{&testExecuter{}}}
11841221
if err := store.Put(context.Background(), "localhost:500/unknown", Credential{}); err == nil {

0 commit comments

Comments
 (0)