...

Source file src/oras.land/oras-go/pkg/content/registry.go

Documentation: oras.land/oras-go/pkg/content

     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  package content
    16  
    17  import (
    18  	"context"
    19  	"crypto/tls"
    20  	"fmt"
    21  	"net/http"
    22  	"os"
    23  
    24  	auth "oras.land/oras-go/pkg/auth/docker"
    25  
    26  	"github.com/containerd/containerd/remotes"
    27  	"github.com/containerd/containerd/remotes/docker"
    28  )
    29  
    30  // RegistryOptions provide configuration options to a Registry
    31  type RegistryOptions struct {
    32  	Configs   []string
    33  	Username  string
    34  	Password  string
    35  	Insecure  bool
    36  	PlainHTTP bool
    37  }
    38  
    39  // Registry provides content from a spec-compliant registry. Create an use a new one for each
    40  // registry with unique configuration of RegistryOptions.
    41  type Registry struct {
    42  	remotes.Resolver
    43  }
    44  
    45  // NewRegistry creates a new Registry store
    46  func NewRegistry(opts RegistryOptions) (*Registry, error) {
    47  	return &Registry{
    48  		Resolver: newResolver(opts.Username, opts.Password, opts.Insecure, opts.PlainHTTP, opts.Configs...),
    49  	}, nil
    50  }
    51  
    52  func newResolver(username, password string, insecure bool, plainHTTP bool, configs ...string) remotes.Resolver {
    53  
    54  	opts := docker.ResolverOptions{
    55  		PlainHTTP: plainHTTP,
    56  	}
    57  
    58  	client := http.DefaultClient
    59  	if insecure {
    60  		client.Transport = &http.Transport{
    61  			TLSClientConfig: &tls.Config{
    62  				InsecureSkipVerify: true,
    63  			},
    64  		}
    65  	}
    66  	opts.Client = client
    67  
    68  	if username != "" || password != "" {
    69  		opts.Credentials = func(hostName string) (string, string, error) {
    70  			return username, password, nil
    71  		}
    72  		return docker.NewResolver(opts)
    73  	}
    74  	cli, err := auth.NewClient(configs...)
    75  	if err != nil {
    76  		fmt.Fprintf(os.Stderr, "WARNING: Error loading auth file: %v\n", err)
    77  	}
    78  	resolver, err := cli.Resolver(context.Background(), client, plainHTTP)
    79  	if err != nil {
    80  		fmt.Fprintf(os.Stderr, "WARNING: Error loading resolver: %v\n", err)
    81  		resolver = docker.NewResolver(opts)
    82  	}
    83  	return resolver
    84  }
    85  

View as plain text