Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
refined
Browse files Browse the repository at this point in the history
Signed-off-by: wangxiaoxuan273 <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Apr 13, 2023
1 parent 22bc26e commit 924fcc9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
32 changes: 22 additions & 10 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@ package credentials

import (
"context"
"errors"
"fmt"

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

// Login provides the login functionality with the given credentials.
func Login(ctx context.Context, store Store, registry remote.Registry, cred auth.Credential) error {
registry.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.DefaultCache,
Credential: auth.StaticCredential(registry.Reference.Registry, cred),
// Login provides the login functionality with the given credentials. A local client is used
// to keep the original client of the registry intact.
func Login(ctx context.Context, store Store, reg *remote.Registry, cred auth.Credential) error {
// create a clone of the original registry for login purpose
regClone := *reg
// we use the original client if applicable, otherwise use a default client
var authClient auth.Client
if reg.Client == nil {
authClient = *auth.DefaultClient
authClient.Cache = nil // no cache
} else if client, ok := reg.Client.(*auth.Client); ok {
authClient = *client
} else {
return errors.New("client type not supported")
}
if err := registry.Ping(ctx); err != nil {
return fmt.Errorf("unable to login to the registry %s: %w", registry.Reference.Registry, err)
regClone.Client = &authClient
// update credentials with the client
authClient.Credential = auth.StaticCredential(reg.Reference.Registry, cred)
// login and store credential
if err := regClone.Ping(ctx); err != nil {
return fmt.Errorf("unable to login to the registry %s: %w", regClone.Reference.Registry, err)
}
hostname := mapHostname(registry.Reference.Registry)
hostname := mapHostname(regClone.Reference.Registry)
if err := store.Put(ctx, hostname, cred); err != nil {
return fmt.Errorf("unable to store the credential for %s: %w", hostname, err)
}
Expand Down
36 changes: 18 additions & 18 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (t *testStore) Put(ctx context.Context, serverAddress string, cred auth.Cre
}

func (t *testStore) Delete(ctx context.Context, serverAddress string) error {
delete(t.storage, serverAddress)
return nil
}

Expand All @@ -61,48 +62,46 @@ func TestLogin(t *testing.T) {
}))
defer ts.Close()
uri, _ := url.Parse(ts.URL)

reg, err := remote.NewRegistry(uri.Host)
if err != nil {
t.Fatalf("cannot create test registry: %v", err)
}
reg.PlainHTTP = true
// create a test store
ns := &testStore{}
tests := []struct {
name string
ctx context.Context
registry string
registry *remote.Registry
cred auth.Credential
wantErr bool
}{
{
name: "login succeeds",
ctx: context.Background(),
registry: reg,
cred: auth.Credential{Username: testUsername, Password: testPassword},
wantErr: false,
},
{
name: "login fails (incorrect password)",
ctx: context.Background(),
registry: uri.Host,
registry: reg,
cred: auth.Credential{Username: testUsername, Password: "whatever"},
wantErr: true,
},
{
name: "login fails (nil context makes remote.Ping fails)",
ctx: nil,
registry: uri.Host,
registry: reg,
cred: auth.Credential{Username: testUsername, Password: testPassword},
wantErr: true,
},
{
name: "login succeeds",
ctx: context.Background(),
registry: uri.Host,
cred: auth.Credential{Username: testUsername, Password: testPassword},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// create test registry
reg, err := remote.NewRegistry(tt.registry)
if err != nil {
t.Fatalf("cannot create test registry: %v", err)
}
reg.PlainHTTP = true
// login to test registry
err = Login(tt.ctx, ns, *reg, tt.cred)
err := Login(tt.ctx, ns, reg, tt.cred)
if (err != nil) != tt.wantErr {
t.Fatalf("Login() error = %v, wantErr %v", err, tt.wantErr)
}
Expand All @@ -112,6 +111,7 @@ func TestLogin(t *testing.T) {
if got := ns.storage[reg.Reference.Registry]; !reflect.DeepEqual(got, tt.cred) {
t.Fatalf("Stored credential = %v, want %v", got, tt.cred)
}
ns.Delete(tt.ctx, reg.Reference.Registry)
})
}
}
Expand Down

0 comments on commit 924fcc9

Please sign in to comment.