mirror of
https://github.com/docker/cli.git
synced 2026-10-06 05:58:37 -04:00
The `tlsconfig.SystemCertPool` utility in go-connections was added in [docker/go-connections@55aadc3], at which time Go stdlib didn't support system-pools ([x509.SystemCertPool]) on Windows, so an empty pool was constructed. Support for system pools on Windows originally added in Go 1.8 (through [golang/go@05471e9]), but reverted, and re-implemented in Go 1.18 (through [golang/go@3544082]). Go 1.18 and up now implement this, but, unlike Linux, which uses a pure-Go implementation, certificate validation is handled by the system: > On macOS and Windows, certificate verification is handled by system APIs, > but the package aims to apply consistent validation rules across operating > systems. On macOS and Windows, x509.SystemCertPool returns an empty Pool, with the `systemPool` set to `true` (see [loadSystemRoots]). This must be considered an implementation detail; custom CAs can be appended to this pool, and handled as usual. This patch removes the special handling on Windows, removing the dependency on go-connections for this part. [docker/go-connections@55aadc3]: https://github.com/docker/go-connections/commit/55aadc3cc561684699edcdd0921b9293c3ee6b49 [golang/go@05471e9]: https://github.com/golang/go/commit/05471e9ee64a300bd2dcc4582ee1043c055893bb [golang/go@3544082]: https://github.com/golang/go/commit/3544082f75fd3d2df7af237ed9aef3ddd499ab9c [x509.SystemCertPool]: https://pkg.go.dev/crypto/x509#SystemCertPool [loadSystemRoots]: https://cs.opensource.google/go/go/+/refs/tags/go1.26.1:src/crypto/x509/root_windows.go;l=15-17 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
151 lines
4.3 KiB
Go
151 lines
4.3 KiB
Go
// Package registry contains client primitives to interact with a remote Docker registry.
|
|
package registry
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/containerd/log"
|
|
"github.com/docker/distribution/registry/client/transport"
|
|
"github.com/docker/go-connections/tlsconfig"
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
)
|
|
|
|
// hostCertsDir returns the config directory for a specific host.
|
|
func hostCertsDir(hostnameAndPort string) string {
|
|
if runtime.GOOS == "windows" {
|
|
// Ensure that a directory name is valid; hostnameAndPort may contain
|
|
// a colon (:) if a port is included, and Windows does not allow colons
|
|
// in directory names.
|
|
hostnameAndPort = filepath.FromSlash(strings.ReplaceAll(hostnameAndPort, ":", ""))
|
|
}
|
|
return filepath.Join(CertsDir(), hostnameAndPort)
|
|
}
|
|
|
|
// newTLSConfig constructs a client TLS configuration based on server defaults
|
|
func newTLSConfig(ctx context.Context, hostname string, isSecure bool) (*tls.Config, error) {
|
|
// PreferredServerCipherSuites should have no effect
|
|
tlsConfig := tlsconfig.ServerDefault()
|
|
tlsConfig.InsecureSkipVerify = !isSecure
|
|
|
|
if isSecure {
|
|
hostDir := hostCertsDir(hostname)
|
|
log.G(ctx).Debugf("hostDir: %s", hostDir)
|
|
if err := loadTLSConfig(ctx, hostDir, tlsConfig); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return tlsConfig, nil
|
|
}
|
|
|
|
func hasFile(files []os.DirEntry, name string) bool {
|
|
for _, f := range files {
|
|
if f.Name() == name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// loadTLSConfig reads the directory for TLS certificates including roots and
|
|
// certificate pairs, and updates the provided TLS configuration.
|
|
func loadTLSConfig(ctx context.Context, directory string, tlsConfig *tls.Config) error {
|
|
fs, err := os.ReadDir(directory)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return invalidParam(err)
|
|
}
|
|
|
|
for _, f := range fs {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
switch filepath.Ext(f.Name()) {
|
|
case ".crt":
|
|
if tlsConfig.RootCAs == nil {
|
|
systemPool, err := x509.SystemCertPool()
|
|
if err != nil {
|
|
return invalidParam(fmt.Errorf("unable to get system cert pool: %w", err))
|
|
}
|
|
tlsConfig.RootCAs = systemPool
|
|
}
|
|
fileName := filepath.Join(directory, f.Name())
|
|
log.G(ctx).Debugf("crt: %s", fileName)
|
|
data, err := os.ReadFile(fileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tlsConfig.RootCAs.AppendCertsFromPEM(data)
|
|
case ".cert":
|
|
certName := f.Name()
|
|
keyName := certName[:len(certName)-5] + ".key"
|
|
log.G(ctx).Debugf("cert: %s", filepath.Join(directory, certName))
|
|
if !hasFile(fs, keyName) {
|
|
return invalidParamf("missing key %s for client certificate %s. CA certificates must use the extension .crt", keyName, certName)
|
|
}
|
|
cert, err := tls.LoadX509KeyPair(filepath.Join(directory, certName), filepath.Join(directory, keyName))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
|
|
case ".key":
|
|
keyName := f.Name()
|
|
certName := keyName[:len(keyName)-4] + ".cert"
|
|
log.G(ctx).Debugf("key: %s", filepath.Join(directory, keyName))
|
|
if !hasFile(fs, certName) {
|
|
return invalidParamf("missing client certificate %s for key %s", certName, keyName)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Headers returns request modifiers with a User-Agent and metaHeaders
|
|
func Headers(userAgent string, metaHeaders http.Header) []transport.RequestModifier {
|
|
modifiers := []transport.RequestModifier{}
|
|
if userAgent != "" {
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(http.Header{
|
|
"User-Agent": []string{userAgent},
|
|
}))
|
|
}
|
|
if metaHeaders != nil {
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders))
|
|
}
|
|
return modifiers
|
|
}
|
|
|
|
// newTransport returns a new HTTP transport. If tlsConfig is nil, it uses the
|
|
// default TLS configuration.
|
|
func newTransport(tlsConfig *tls.Config) http.RoundTripper {
|
|
if tlsConfig == nil {
|
|
tlsConfig = tlsconfig.ServerDefault()
|
|
}
|
|
|
|
return otelhttp.NewTransport(
|
|
&http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}).DialContext,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
TLSClientConfig: tlsConfig,
|
|
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
|
|
DisableKeepAlives: true,
|
|
},
|
|
)
|
|
}
|