2019-01-21 13:52:46 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-04-09 08:07:30 +00:00
|
|
|
"time"
|
2019-01-21 13:52:46 +00:00
|
|
|
|
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
|
|
|
"github.com/pkg/errors"
|
2023-07-14 17:59:45 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-01-21 13:52:46 +00:00
|
|
|
)
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// Opt is a configuration option to initialize a [Client].
|
2019-04-09 23:11:53 +00:00
|
|
|
type Opt func(*Client) error
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// FromEnv configures the client with values from environment variables. It
|
|
|
|
// is the equivalent of using the [WithTLSClientConfigFromEnv], [WithHostFromEnv],
|
|
|
|
// and [WithVersionFromEnv] options.
|
2019-01-21 13:52:46 +00:00
|
|
|
//
|
2022-03-07 10:29:38 +00:00
|
|
|
// FromEnv uses the following environment variables:
|
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// - DOCKER_HOST ([EnvOverrideHost]) to set the URL to the docker server.
|
|
|
|
// - DOCKER_API_VERSION ([EnvOverrideAPIVersion]) to set the version of the
|
|
|
|
// API to use, leave empty for latest.
|
|
|
|
// - DOCKER_CERT_PATH ([EnvOverrideCertPath]) to specify the directory from
|
|
|
|
// which to load the TLS certificates ("ca.pem", "cert.pem", "key.pem').
|
|
|
|
// - DOCKER_TLS_VERIFY ([EnvTLSVerify]) to enable or disable TLS verification
|
|
|
|
// (off by default).
|
2019-01-21 13:52:46 +00:00
|
|
|
func FromEnv(c *Client) error {
|
2021-03-30 13:15:42 +00:00
|
|
|
ops := []Opt{
|
|
|
|
WithTLSClientConfigFromEnv(),
|
|
|
|
WithHostFromEnv(),
|
|
|
|
WithVersionFromEnv(),
|
2019-01-21 13:52:46 +00:00
|
|
|
}
|
2021-03-30 13:15:42 +00:00
|
|
|
for _, op := range ops {
|
|
|
|
if err := op(c); err != nil {
|
2019-03-19 21:12:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-01-21 13:52:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialContext applies the dialer to the client transport. This can be
|
2023-07-15 15:22:06 +00:00
|
|
|
// used to set the Timeout and KeepAlive settings of the client. It returns
|
|
|
|
// an error if the client does not have a [http.Transport] configured.
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
|
|
|
transport.DialContext = dialContext
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Errorf("cannot apply dialer to transport: %T", c.client.Transport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHost overrides the client host with the specified one.
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithHost(host string) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
hostURL, err := ParseHostURL(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.host = host
|
|
|
|
c.proto = hostURL.Scheme
|
|
|
|
c.addr = hostURL.Host
|
|
|
|
c.basePath = hostURL.Path
|
|
|
|
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
|
|
|
return sockets.ConfigureTransport(transport, c.proto, c.addr)
|
|
|
|
}
|
|
|
|
return errors.Errorf("cannot apply host to transport: %T", c.client.Transport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:15:42 +00:00
|
|
|
// WithHostFromEnv overrides the client host with the host specified in the
|
2023-07-15 15:22:06 +00:00
|
|
|
// DOCKER_HOST ([EnvOverrideHost]) environment variable. If DOCKER_HOST is not set,
|
2022-03-07 10:29:38 +00:00
|
|
|
// or set to an empty value, the host is not modified.
|
2021-03-30 13:15:42 +00:00
|
|
|
func WithHostFromEnv() Opt {
|
|
|
|
return func(c *Client) error {
|
2022-03-07 10:50:22 +00:00
|
|
|
if host := os.Getenv(EnvOverrideHost); host != "" {
|
2021-03-30 13:15:42 +00:00
|
|
|
return WithHost(host)(c)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// WithHTTPClient overrides the client's HTTP client with the specified one.
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithHTTPClient(client *http.Client) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
if client != nil {
|
|
|
|
c.client = client
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// WithTimeout configures the time limit for requests made by the HTTP client.
|
2019-04-09 08:07:30 +00:00
|
|
|
func WithTimeout(timeout time.Duration) Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.client.Timeout = timeout
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 20:42:46 +00:00
|
|
|
// WithUserAgent configures the User-Agent header to use for HTTP requests.
|
|
|
|
// It overrides any User-Agent set in headers. When set to an empty string,
|
|
|
|
// the User-Agent header is removed, and no header is sent.
|
|
|
|
func WithUserAgent(ua string) Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.userAgent = &ua
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// WithHTTPHeaders appends custom HTTP headers to the client's default headers.
|
|
|
|
// It does not allow for built-in headers (such as "User-Agent", if set) to
|
|
|
|
// be overridden. Also see [WithUserAgent].
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithHTTPHeaders(headers map[string]string) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
c.customHTTPHeaders = headers
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// WithScheme overrides the client scheme with the specified one.
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithScheme(scheme string) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
c.scheme = scheme
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// WithTLSClientConfig applies a TLS config to the client transport.
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithTLSClientConfig(cacertPath, certPath, keyPath string) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
2023-07-15 16:06:53 +00:00
|
|
|
transport, ok := c.client.Transport.(*http.Transport)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("cannot apply tls config to transport: %T", c.client.Transport)
|
|
|
|
}
|
2023-07-15 16:03:34 +00:00
|
|
|
config, err := tlsconfig.Client(tlsconfig.Options{
|
2019-01-21 13:52:46 +00:00
|
|
|
CAFile: cacertPath,
|
|
|
|
CertFile: certPath,
|
|
|
|
KeyFile: keyPath,
|
|
|
|
ExclusiveRootPools: true,
|
2023-07-15 16:03:34 +00:00
|
|
|
})
|
2019-01-21 13:52:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create tls config")
|
|
|
|
}
|
2023-07-15 16:06:53 +00:00
|
|
|
transport.TLSClientConfig = config
|
|
|
|
return nil
|
2019-01-21 13:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:15:42 +00:00
|
|
|
// WithTLSClientConfigFromEnv configures the client's TLS settings with the
|
2023-07-15 15:22:06 +00:00
|
|
|
// settings in the DOCKER_CERT_PATH ([EnvOverrideCertPath]) and DOCKER_TLS_VERIFY
|
|
|
|
// ([EnvTLSVerify]) environment variables. If DOCKER_CERT_PATH is not set or empty,
|
|
|
|
// TLS configuration is not modified.
|
2021-03-30 13:15:42 +00:00
|
|
|
//
|
2022-03-07 10:29:38 +00:00
|
|
|
// WithTLSClientConfigFromEnv uses the following environment variables:
|
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// - DOCKER_CERT_PATH ([EnvOverrideCertPath]) to specify the directory from
|
|
|
|
// which to load the TLS certificates ("ca.pem", "cert.pem", "key.pem").
|
|
|
|
// - DOCKER_TLS_VERIFY ([EnvTLSVerify]) to enable or disable TLS verification
|
|
|
|
// (off by default).
|
2021-03-30 13:15:42 +00:00
|
|
|
func WithTLSClientConfigFromEnv() Opt {
|
|
|
|
return func(c *Client) error {
|
2022-03-07 10:50:22 +00:00
|
|
|
dockerCertPath := os.Getenv(EnvOverrideCertPath)
|
2021-03-30 13:15:42 +00:00
|
|
|
if dockerCertPath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-15 16:03:34 +00:00
|
|
|
tlsc, err := tlsconfig.Client(tlsconfig.Options{
|
2021-03-30 13:15:42 +00:00
|
|
|
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
|
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
|
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
2022-03-07 10:50:22 +00:00
|
|
|
InsecureSkipVerify: os.Getenv(EnvTLSVerify) == "",
|
2023-07-15 16:03:34 +00:00
|
|
|
})
|
2021-03-30 13:15:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client = &http.Client{
|
|
|
|
Transport: &http.Transport{TLSClientConfig: tlsc},
|
|
|
|
CheckRedirect: CheckRedirect,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 22:54:16 +00:00
|
|
|
// WithVersion overrides the client version with the specified one. If an empty
|
2023-07-15 15:22:06 +00:00
|
|
|
// version is provided, the value is ignored to allow version negotiation
|
|
|
|
// (see [WithAPIVersionNegotiation]).
|
2019-04-09 23:11:53 +00:00
|
|
|
func WithVersion(version string) Opt {
|
2019-01-21 13:52:46 +00:00
|
|
|
return func(c *Client) error {
|
2019-04-09 22:54:16 +00:00
|
|
|
if version != "" {
|
|
|
|
c.version = version
|
|
|
|
c.manualOverride = true
|
|
|
|
}
|
2019-01-21 13:52:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:14:07 +00:00
|
|
|
|
2021-03-30 13:15:42 +00:00
|
|
|
// WithVersionFromEnv overrides the client version with the version specified in
|
2023-07-15 15:22:06 +00:00
|
|
|
// the DOCKER_API_VERSION ([EnvOverrideAPIVersion]) environment variable.
|
|
|
|
// If DOCKER_API_VERSION is not set, or set to an empty value, the version
|
|
|
|
// is not modified.
|
2021-03-30 13:15:42 +00:00
|
|
|
func WithVersionFromEnv() Opt {
|
|
|
|
return func(c *Client) error {
|
2022-03-07 10:50:22 +00:00
|
|
|
return WithVersion(os.Getenv(EnvOverrideAPIVersion))(c)
|
2021-03-30 13:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:14:07 +00:00
|
|
|
// WithAPIVersionNegotiation enables automatic API version negotiation for the client.
|
|
|
|
// With this option enabled, the client automatically negotiates the API version
|
|
|
|
// to use when making requests. API version negotiation is performed on the first
|
2023-07-15 15:22:06 +00:00
|
|
|
// request; subsequent requests do not re-negotiate.
|
2019-04-08 12:14:07 +00:00
|
|
|
func WithAPIVersionNegotiation() Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.negotiateVersion = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 17:59:45 +00:00
|
|
|
|
|
|
|
// WithTraceProvider sets the trace provider for the client.
|
|
|
|
// If this is not set then the global trace provider will be used.
|
|
|
|
func WithTraceProvider(provider trace.TracerProvider) Opt {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.tp = provider
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|