...

Source file src/github.com/google/go-containerregistry/pkg/name/registry.go

Documentation: github.com/google/go-containerregistry/pkg/name

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     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 name
    16  
    17  import (
    18  	"net"
    19  	"net/url"
    20  	"path"
    21  	"regexp"
    22  	"strings"
    23  )
    24  
    25  // Detect more complex forms of local references.
    26  var reLocal = regexp.MustCompile(`.*\.local(?:host)?(?::\d{1,5})?$`)
    27  
    28  // Detect the loopback IP (127.0.0.1)
    29  var reLoopback = regexp.MustCompile(regexp.QuoteMeta("127.0.0.1"))
    30  
    31  // Detect the loopback IPV6 (::1)
    32  var reipv6Loopback = regexp.MustCompile(regexp.QuoteMeta("::1"))
    33  
    34  // Registry stores a docker registry name in a structured form.
    35  type Registry struct {
    36  	insecure bool
    37  	registry string
    38  }
    39  
    40  // RegistryStr returns the registry component of the Registry.
    41  func (r Registry) RegistryStr() string {
    42  	return r.registry
    43  }
    44  
    45  // Name returns the name from which the Registry was derived.
    46  func (r Registry) Name() string {
    47  	return r.RegistryStr()
    48  }
    49  
    50  func (r Registry) String() string {
    51  	return r.Name()
    52  }
    53  
    54  // Repo returns a Repository in the Registry with the given name.
    55  func (r Registry) Repo(repo ...string) Repository {
    56  	return Repository{Registry: r, repository: path.Join(repo...)}
    57  }
    58  
    59  // Scope returns the scope required to access the registry.
    60  func (r Registry) Scope(string) string {
    61  	// The only resource under 'registry' is 'catalog'. http://goo.gl/N9cN9Z
    62  	return "registry:catalog:*"
    63  }
    64  
    65  func (r Registry) isRFC1918() bool {
    66  	ipStr := strings.Split(r.Name(), ":")[0]
    67  	ip := net.ParseIP(ipStr)
    68  	if ip == nil {
    69  		return false
    70  	}
    71  	for _, cidr := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
    72  		_, block, _ := net.ParseCIDR(cidr)
    73  		if block.Contains(ip) {
    74  			return true
    75  		}
    76  	}
    77  	return false
    78  }
    79  
    80  // Scheme returns https scheme for all the endpoints except localhost or when explicitly defined.
    81  func (r Registry) Scheme() string {
    82  	if r.insecure {
    83  		return "http"
    84  	}
    85  	if r.isRFC1918() {
    86  		return "http"
    87  	}
    88  	if strings.HasPrefix(r.Name(), "localhost:") {
    89  		return "http"
    90  	}
    91  	if reLocal.MatchString(r.Name()) {
    92  		return "http"
    93  	}
    94  	if reLoopback.MatchString(r.Name()) {
    95  		return "http"
    96  	}
    97  	if reipv6Loopback.MatchString(r.Name()) {
    98  		return "http"
    99  	}
   100  	return "https"
   101  }
   102  
   103  func checkRegistry(name string) error {
   104  	// Per RFC 3986, registries (authorities) are required to be prefixed with "//"
   105  	// url.Host == hostname[:port] == authority
   106  	if url, err := url.Parse("//" + name); err != nil || url.Host != name {
   107  		return newErrBadName("registries must be valid RFC 3986 URI authorities: %s", name)
   108  	}
   109  	return nil
   110  }
   111  
   112  // NewRegistry returns a Registry based on the given name.
   113  // Strict validation requires explicit, valid RFC 3986 URI authorities to be given.
   114  func NewRegistry(name string, opts ...Option) (Registry, error) {
   115  	opt := makeOptions(opts...)
   116  	if opt.strict && len(name) == 0 {
   117  		return Registry{}, newErrBadName("strict validation requires the registry to be explicitly defined")
   118  	}
   119  
   120  	if err := checkRegistry(name); err != nil {
   121  		return Registry{}, err
   122  	}
   123  
   124  	if name == "" {
   125  		name = opt.defaultRegistry
   126  	}
   127  	// Rewrite "docker.io" to "index.docker.io".
   128  	// See: https://github.com/google/go-containerregistry/issues/68
   129  	if name == defaultRegistryAlias {
   130  		name = DefaultRegistry
   131  	}
   132  
   133  	return Registry{registry: name, insecure: opt.insecure}, nil
   134  }
   135  
   136  // NewInsecureRegistry returns an Insecure Registry based on the given name.
   137  //
   138  // Deprecated: Use the Insecure Option with NewRegistry instead.
   139  func NewInsecureRegistry(name string, opts ...Option) (Registry, error) {
   140  	opts = append(opts, Insecure)
   141  	return NewRegistry(name, opts...)
   142  }
   143  

View as plain text