2018-12-17 05:27:07 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/connhelper"
|
|
|
|
"github.com/docker/cli/cli/context"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointMeta is a typed wrapper around a context-store generic endpoint describing
|
|
|
|
// a Docker Engine endpoint, without its tls config
|
2018-11-09 09:10:41 -05:00
|
|
|
type EndpointMeta = context.EndpointMetaBase
|
2018-12-17 05:27:07 -05:00
|
|
|
|
|
|
|
// Endpoint is a typed wrapper around a context-store generic endpoint describing
|
|
|
|
// a Docker Engine endpoint, with its tls data
|
|
|
|
type Endpoint struct {
|
|
|
|
EndpointMeta
|
2021-07-28 06:45:09 -04:00
|
|
|
TLSData *context.TLSData
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithTLSData loads TLS materials for the endpoint
|
2019-04-15 06:03:03 -04:00
|
|
|
func WithTLSData(s store.Reader, contextName string, m EndpointMeta) (Endpoint, error) {
|
2018-12-17 05:27:07 -05:00
|
|
|
tlsData, err := context.LoadTLSData(s, contextName, DockerEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return Endpoint{}, err
|
|
|
|
}
|
|
|
|
return Endpoint{
|
2018-11-09 09:10:41 -05:00
|
|
|
EndpointMeta: m,
|
2018-12-17 05:27:07 -05:00
|
|
|
TLSData: tlsData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tlsConfig extracts a context docker endpoint TLS config
|
2023-05-04 11:08:18 -04:00
|
|
|
func (ep *Endpoint) tlsConfig() (*tls.Config, error) {
|
|
|
|
if ep.TLSData == nil && !ep.SkipTLSVerify {
|
2018-12-17 05:27:07 -05:00
|
|
|
// there is no specific tls config
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var tlsOpts []func(*tls.Config)
|
2023-05-04 11:08:18 -04:00
|
|
|
if ep.TLSData != nil && ep.TLSData.CA != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
certPool := x509.NewCertPool()
|
2023-05-04 11:08:18 -04:00
|
|
|
if !certPool.AppendCertsFromPEM(ep.TLSData.CA) {
|
2018-12-17 05:27:07 -05:00
|
|
|
return nil, errors.New("failed to retrieve context tls info: ca.pem seems invalid")
|
|
|
|
}
|
|
|
|
tlsOpts = append(tlsOpts, func(cfg *tls.Config) {
|
|
|
|
cfg.RootCAs = certPool
|
|
|
|
})
|
|
|
|
}
|
2023-05-04 11:08:18 -04:00
|
|
|
if ep.TLSData != nil && ep.TLSData.Key != nil && ep.TLSData.Cert != nil {
|
|
|
|
keyBytes := ep.TLSData.Key
|
2018-12-17 05:27:07 -05:00
|
|
|
pemBlock, _ := pem.Decode(keyBytes)
|
|
|
|
if pemBlock == nil {
|
2021-07-28 10:08:09 -04:00
|
|
|
return nil, errors.New("no valid private key found")
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2022-07-13 06:29:49 -04:00
|
|
|
if x509.IsEncryptedPEMBlock(pemBlock) { //nolint:staticcheck // SA1019: x509.IsEncryptedPEMBlock is deprecated, and insecure by design
|
2021-07-28 10:08:09 -04:00
|
|
|
return nil, errors.New("private key is encrypted - support for encrypted private keys has been removed, see https://docs.docker.com/go/deprecated/")
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2023-05-04 11:08:18 -04:00
|
|
|
x509cert, err := tls.X509KeyPair(ep.TLSData.Cert, keyBytes)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to retrieve context tls info")
|
|
|
|
}
|
|
|
|
tlsOpts = append(tlsOpts, func(cfg *tls.Config) {
|
|
|
|
cfg.Certificates = []tls.Certificate{x509cert}
|
|
|
|
})
|
|
|
|
}
|
2023-05-04 11:08:18 -04:00
|
|
|
if ep.SkipTLSVerify {
|
2018-12-17 05:27:07 -05:00
|
|
|
tlsOpts = append(tlsOpts, func(cfg *tls.Config) {
|
|
|
|
cfg.InsecureSkipVerify = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tlsconfig.ClientDefault(tlsOpts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOpts returns a slice of Client options to configure an API client with this endpoint
|
2023-05-04 11:08:18 -04:00
|
|
|
func (ep *Endpoint) ClientOpts() ([]client.Opt, error) {
|
2019-04-12 18:54:49 -04:00
|
|
|
var result []client.Opt
|
2023-05-04 11:08:18 -04:00
|
|
|
if ep.Host != "" {
|
|
|
|
helper, err := connhelper.GetConnectionHelper(ep.Host)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if helper == nil {
|
2023-05-04 11:08:18 -04:00
|
|
|
tlsConfig, err := ep.tlsConfig()
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result,
|
|
|
|
withHTTPClient(tlsConfig),
|
2023-05-04 11:08:18 -04:00
|
|
|
client.WithHost(ep.Host),
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
httpClient := &http.Client{
|
|
|
|
// No tls
|
|
|
|
// No proxy
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: helper.Dialer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
result = append(result,
|
|
|
|
client.WithHTTPClient(httpClient),
|
|
|
|
client.WithHost(helper.Host),
|
|
|
|
client.WithDialContext(helper.Dialer),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:18:20 -04:00
|
|
|
result = append(result, client.WithVersionFromEnv(), client.WithAPIVersionNegotiation())
|
2018-12-17 05:27:07 -05:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func withHTTPClient(tlsConfig *tls.Config) func(*client.Client) error {
|
|
|
|
return func(c *client.Client) error {
|
|
|
|
if tlsConfig == nil {
|
|
|
|
// Use the default HTTPClient
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
},
|
|
|
|
CheckRedirect: client.CheckRedirect,
|
|
|
|
}
|
|
|
|
return client.WithHTTPClient(httpClient)(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointFromContext parses a context docker endpoint metadata into a typed EndpointMeta structure
|
2019-04-18 09:12:30 -04:00
|
|
|
func EndpointFromContext(metadata store.Metadata) (EndpointMeta, error) {
|
2018-12-17 05:27:07 -05:00
|
|
|
ep, ok := metadata.Endpoints[DockerEndpoint]
|
|
|
|
if !ok {
|
|
|
|
return EndpointMeta{}, errors.New("cannot find docker endpoint in context")
|
|
|
|
}
|
|
|
|
typed, ok := ep.(EndpointMeta)
|
|
|
|
if !ok {
|
|
|
|
return EndpointMeta{}, errors.Errorf("endpoint %q is not of type EndpointMeta", DockerEndpoint)
|
|
|
|
}
|
|
|
|
return typed, nil
|
|
|
|
}
|