This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: implement StoreWithFallbacks #46
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dc6c2f
implemented fallback store
wangxiaoxuan273 b35654e
added tests
wangxiaoxuan273 d4bad0a
resolved comments
wangxiaoxuan273 e42f60c
refined doc comments
wangxiaoxuan273 00eba81
removed array descriptions
wangxiaoxuan273 3678f50
resolved comments
wangxiaoxuan273 69df6cd
resolved comments
wangxiaoxuan273 e2e7826
refined comments
wangxiaoxuan273 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,51 @@ type Store interface { | |
// Delete removes credentials from the store for the given server address. | ||
Delete(ctx context.Context, serverAddress string) error | ||
} | ||
|
||
// storeWithFallbacks is a store that has multiple fallback stores. | ||
type storeWithFallbacks struct { | ||
stores []Store | ||
} | ||
|
||
// NewStoreWithFallbacks returns a new store based on the given stores. | ||
// - Get() searches the primary and the fallback stores | ||
// for the credentials and returns when it finds the | ||
// credentials in any of the stores. | ||
// - Put() saves the credentials into the primary store. | ||
// - Delete() deletes the credentials from the primary store. | ||
func NewStoreWithFallbacks(primary Store, fallbacks ...Store) Store { | ||
if len(fallbacks) == 0 { | ||
return primary | ||
} | ||
return &storeWithFallbacks{ | ||
stores: append([]Store{primary}, fallbacks...), | ||
} | ||
} | ||
|
||
// Get retrieves credentials from the StoreWithFallbacks for the given server. | ||
// It searches the primary and the fallback stores for the credentials of serverAddress | ||
// and returns when it finds the credentials in any of the stores. | ||
func (sf *storeWithFallbacks) Get(ctx context.Context, serverAddress string) (auth.Credential, error) { | ||
for _, s := range sf.stores { | ||
cred, err := s.Get(ctx, serverAddress) | ||
if err != nil { | ||
return auth.EmptyCredential, err | ||
} | ||
if cred != auth.EmptyCredential { | ||
return cred, nil | ||
} | ||
} | ||
return auth.EmptyCredential, nil | ||
} | ||
|
||
// Put saves credentials into the StoreWithFallbacks. It puts | ||
// the credentials into the primary store. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved. |
||
func (sf *storeWithFallbacks) Put(ctx context.Context, serverAddress string, cred auth.Credential) error { | ||
return sf.stores[0].Put(ctx, serverAddress, cred) | ||
} | ||
|
||
// Delete removes credentials from the StoreWithFallbacks for the given server. | ||
// It deletes the credentials from the primary store. | ||
func (sf *storeWithFallbacks) Delete(ctx context.Context, serverAddress string) error { | ||
return sf.stores[0].Delete(ctx, serverAddress) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
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 credentials | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"oras.land/oras-go/v2/registry/remote/auth" | ||
) | ||
|
||
func TestStoreWithFallbacks(t *testing.T) { | ||
// Initialize a StoreWithFallbacks | ||
primaryStore := &testStore{} | ||
firstFallbackStore := &testStore{} | ||
secondFallbackStore := &testStore{} | ||
secondFallbackStore.Put(context.Background(), "localhost:6666", auth.Credential{RefreshToken: "identity_token"}) | ||
sf := NewStoreWithFallbacks(primaryStore, firstFallbackStore, secondFallbackStore) | ||
// Put an entry into the primary store | ||
err := sf.Put(context.Background(), "localhost:2333", auth.Credential{Username: testUsername, Password: testPassword}) | ||
if err != nil { | ||
t.Fatal("sf.Put() error =", err) | ||
} | ||
// Get an entry stored in the primary store | ||
cred, err := sf.Get(context.Background(), "localhost:2333") | ||
if err != nil { | ||
t.Fatal("sf.Get() error =", err) | ||
} | ||
if !reflect.DeepEqual(cred, auth.Credential{Username: testUsername, Password: testPassword}) { | ||
t.Fatal("incorrect credential from the primary store") | ||
} | ||
// Get an entry stored in the second fallback store | ||
cred, err = sf.Get(context.Background(), "localhost:6666") | ||
if err != nil { | ||
t.Fatal("sf.Get() error =", err) | ||
} | ||
if !reflect.DeepEqual(cred, auth.Credential{RefreshToken: "identity_token"}) { | ||
t.Fatal("incorrect credential from the second backup store") | ||
} | ||
// Delete the entry stored in the primary store | ||
err = sf.Delete(context.Background(), "localhost:2333") | ||
if err != nil { | ||
t.Fatal("sf.Delete() error =", err) | ||
} | ||
// Check if the entry is deleted | ||
cred, err = sf.Get(context.Background(), "localhost:2333") | ||
if err != nil { | ||
t.Fatal("sf.Get() error =", err) | ||
} | ||
if !reflect.DeepEqual(cred, auth.EmptyCredential) { | ||
t.Fatal("incorrect credential after the delete") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if users can see these comments. We may need to move them to
NewStoreWithFallbacks
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved.