...

Source file src/oras.land/oras-go/pkg/auth/docker/resolver.go

Documentation: oras.land/oras-go/pkg/auth/docker

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package docker
    17  
    18  import (
    19  	"context"
    20  	"net/http"
    21  
    22  	"github.com/containerd/containerd/remotes"
    23  	"github.com/containerd/containerd/remotes/docker"
    24  	ctypes "github.com/docker/cli/cli/config/types"
    25  	"github.com/docker/docker/registry"
    26  
    27  	iface "oras.land/oras-go/pkg/auth"
    28  )
    29  
    30  // Resolver returns a new authenticated resolver.
    31  // Deprecated: use ResolverWithOpts
    32  func (c *Client) Resolver(_ context.Context, client *http.Client, plainHTTP bool) (remotes.Resolver, error) {
    33  	return docker.NewResolver(docker.ResolverOptions{
    34  		Credentials: c.Credential,
    35  		Client:      client,
    36  		PlainHTTP:   plainHTTP,
    37  	}), nil
    38  }
    39  
    40  // ResolverWithOpts returns a new authenticated resolver with custom options.
    41  func (c *Client) ResolverWithOpts(options ...iface.ResolverOption) (remotes.Resolver, error) {
    42  	settings := &iface.ResolverSettings{}
    43  	for _, option := range options {
    44  		option(settings)
    45  	}
    46  	return docker.NewResolver(docker.ResolverOptions{
    47  		Credentials: c.Credential,
    48  		Client:      settings.Client,
    49  		PlainHTTP:   settings.PlainHTTP,
    50  		Headers:     settings.Headers,
    51  	}), nil
    52  }
    53  
    54  // Credential returns the login credential of the request host.
    55  func (c *Client) Credential(hostname string) (string, string, error) {
    56  	hostname = resolveHostname(hostname)
    57  	var (
    58  		auth ctypes.AuthConfig
    59  		err  error
    60  	)
    61  	for _, cfg := range c.configs {
    62  		auth, err = cfg.GetAuthConfig(hostname)
    63  		if err != nil {
    64  			// fall back to next config
    65  			continue
    66  		}
    67  		if auth.IdentityToken != "" {
    68  			return "", auth.IdentityToken, nil
    69  		}
    70  		if auth.Username == "" && auth.Password == "" {
    71  			// fall back to next config
    72  			continue
    73  		}
    74  		return auth.Username, auth.Password, nil
    75  	}
    76  	return "", "", err
    77  }
    78  
    79  // resolveHostname resolves Docker specific hostnames
    80  func resolveHostname(hostname string) string {
    81  	switch hostname {
    82  	case registry.IndexHostname, registry.IndexName, registry.DefaultV2Registry.Host:
    83  		return registry.IndexServer
    84  	}
    85  	return hostname
    86  }
    87  

View as plain text