Skip to content

Commit

Permalink
Merge pull request #5 from bloodorangeio/resolver-with-opts
Browse files Browse the repository at this point in the history
Add new ResolverWithOpts
  • Loading branch information
jdolitsky authored Jun 7, 2021
2 parents 68e99f2 + a024218 commit 933c6d5
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: setup go environment
uses: actions/setup-go@v1
with:
go-version: '1.16.3'
go-version: '1.16.4'
- name: run unit tests
run: make test
- name: run acceptance tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: setup go environment
uses: actions/setup-go@v1
with:
go-version: '1.16.3'
go-version: '1.16.4'
- name: run unit tests
run: make test
- name: run acceptance tests
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ replace (
)

require (
github.com/containerd/containerd v1.5.0-rc.3
github.com/docker/cli v20.10.6+incompatible
github.com/containerd/containerd v1.5.2
github.com/docker/cli v20.10.7+incompatible
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
github.com/docker/docker-credential-helpers v0.6.3 // indirect
Expand All @@ -22,8 +22,7 @@ require (
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
61 changes: 9 additions & 52 deletions go.sum

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions pkg/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,51 @@ var (
ErrNotLoggedIn = errors.New("not logged in")
)

type (
// ResolverOption allows specifying various settings on the resolver.
ResolverOption func(*ResolverSettings)

// ResolverSettings represent all the various settings on a resolver.
ResolverSettings struct {
// Headers are the HTTP request header fields sent by the resolver.
Client *http.Client
// PlainHTTP specifies to use plain http and not https.
PlainHTTP bool
// Client is the http client to used when making registry requests.
Headers http.Header
}
)

// Client provides authentication operations for remotes.
type Client interface {
// Login logs in to a remote server identified by the hostname.
Login(ctx context.Context, hostname, username, secret string, insecure bool) error
// Logout logs out from a remote server identified by the hostname.
Logout(ctx context.Context, hostname string) error
// Resolver returns a new authenticated resolver.
// Deprecated: use ResolverWithOpts
Resolver(ctx context.Context, client *http.Client, plainHTTP bool) (remotes.Resolver, error)
// ResolverWithOpts returns a new authenticated resolver with custom options.
ResolverWithOpts(options ...ResolverOption) (remotes.Resolver, error)
}

// WithResolverClient returns a function that sets the Client setting on resolver.
func WithResolverClient(client *http.Client) ResolverOption {
return func(settings *ResolverSettings) {
settings.Client = client
}
}

// WithResolverPlainHTTP returns a function that sets the PlainHTTP setting to true on resolver.
func WithResolverPlainHTTP() ResolverOption {
return func(settings *ResolverSettings) {
settings.PlainHTTP = true
}
}

// WithResolverHeaders returns a function that sets the Headers setting on resolver.
func WithResolverHeaders(headers http.Header) ResolverOption {
return func(settings *ResolverSettings) {
settings.Headers = headers
}
}
64 changes: 64 additions & 0 deletions pkg/auth/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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 auth

import (
"net/http"
"testing"

"github.com/stretchr/testify/suite"
)

type ClientSuite struct {
suite.Suite
}

func (suite *ClientSuite) TestWithResolverClient() {
settings := &ResolverSettings{}
suite.Nil(settings.Client, "settings.Client is nil by default")

defaultClient := http.DefaultClient
opt := WithResolverClient(defaultClient)
opt(settings)
suite.Equal(defaultClient, settings.Client, "Able to override settings.Client")
}

func (suite *ClientSuite) TestWithResolverPlainHTTP() {
settings := &ResolverSettings{}
suite.Equal(false, settings.PlainHTTP, "settings.PlainHTTP is false by default")

plainHTTP := true
opt := WithResolverPlainHTTP()
opt(settings)
suite.Equal(plainHTTP, settings.PlainHTTP, "Able to override settings.PlainHTTP")
}

func (suite *ClientSuite) TestWithResolverHeaders() {
settings := &ResolverSettings{}
suite.Nil(settings.Headers, "settings.Headers is nil by default")

key := "User-Agent"
value := "oras-go/test"
headers := http.Header{}
headers.Set(key, value)
opt := WithResolverHeaders(headers)
opt(settings)
suite.Equal(settings.Headers.Get(key), value, "Able to override settings.Headers")
}

func TestClientSuite(t *testing.T) {
suite.Run(t, new(ClientSuite))
}
16 changes: 16 additions & 0 deletions pkg/auth/docker/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"github.com/containerd/containerd/remotes/docker"
ctypes "github.com/docker/cli/cli/config/types"
"github.com/docker/docker/registry"
iface "github.com/oras-project/oras-go/pkg/auth"
)

// Resolver returns a new authenticated resolver.
// Deprecated: use ResolverWithOpts
func (c *Client) Resolver(_ context.Context, client *http.Client, plainHTTP bool) (remotes.Resolver, error) {
return docker.NewResolver(docker.ResolverOptions{
Credentials: c.Credential,
Expand All @@ -34,6 +36,20 @@ func (c *Client) Resolver(_ context.Context, client *http.Client, plainHTTP bool
}), nil
}

// ResolverWithOpts returns a new authenticated resolver with custom options.
func (c *Client) ResolverWithOpts(options ...iface.ResolverOption) (remotes.Resolver, error) {
settings := &iface.ResolverSettings{}
for _, option := range options {
option(settings)
}
return docker.NewResolver(docker.ResolverOptions{
Credentials: c.Credential,
Client: settings.Client,
PlainHTTP: settings.PlainHTTP,
Headers: settings.Headers,
}), nil
}

// Credential returns the login credential of the request host.
func (c *Client) Credential(hostname string) (string, string, error) {
hostname = resolveHostname(hostname)
Expand Down

0 comments on commit 933c6d5

Please sign in to comment.