From d7211f21e66598f25fb935d5f68344f7d44a2035 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 27 Jul 2021 17:04:35 +0200 Subject: [PATCH] Lowercase all errors, and remove use of github.com/pkg/errors Signed-off-by: Sebastiaan van Stijn --- go.mod | 5 +---- go.sum | 4 ---- nat/nat.go | 14 +++++++------- nat/parse.go | 6 +++--- nat/parse_test.go | 16 ++++++++-------- proxy/network_proxy_test.go | 4 ++-- proxy/proxy.go | 2 +- sockets/sockets_unix.go | 2 +- sockets/unix_socket.go | 2 +- sockets/unix_socket_test.go | 2 +- tlsconfig/config.go | 27 ++++++++++++--------------- 11 files changed, 37 insertions(+), 47 deletions(-) diff --git a/go.mod b/go.mod index 70ec3128f..038271ea5 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module github.com/docker/go-connections go 1.13 -require ( - github.com/Microsoft/go-winio v0.4.14 - github.com/pkg/errors v0.9.1 -) +require github.com/Microsoft/go-winio v0.4.14 diff --git a/go.sum b/go.sum index 2bcfb4ffb..b888be749 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,8 @@ github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/nat/nat.go b/nat/nat.go index 296c96a63..d1618120c 100644 --- a/nat/nat.go +++ b/nat/nat.go @@ -177,27 +177,27 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) { // Strip [] from IPV6 addresses rawIP, _, err := net.SplitHostPort(ip + ":") if err != nil { - return nil, fmt.Errorf("Invalid ip address %v: %s", ip, err) + return nil, fmt.Errorf("invalid IP address %v: %s", ip, err) } ip = rawIP } if ip != "" && net.ParseIP(ip) == nil { - return nil, fmt.Errorf("Invalid ip address: %s", ip) + return nil, fmt.Errorf("invalid IP address: %s", ip) } if containerPort == "" { - return nil, fmt.Errorf("No port specified: %s", rawPort) + return nil, fmt.Errorf("no port specified: %s", rawPort) } startPort, endPort, err := ParsePortRange(containerPort) if err != nil { - return nil, fmt.Errorf("Invalid containerPort: %s", containerPort) + return nil, fmt.Errorf("invalid containerPort: %s", containerPort) } var startHostPort, endHostPort uint64 = 0, 0 if len(hostPort) > 0 { startHostPort, endHostPort, err = ParsePortRange(hostPort) if err != nil { - return nil, fmt.Errorf("Invalid hostPort: %s", hostPort) + return nil, fmt.Errorf("invalid hostPort: %s", hostPort) } } @@ -206,12 +206,12 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) { // In this case, use the host port range as the dynamic // host port range to allocate into. if endPort != startPort { - return nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort) + return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort) } } if !validateProto(strings.ToLower(proto)) { - return nil, fmt.Errorf("Invalid proto: %s", proto) + return nil, fmt.Errorf("invalid proto: %s", proto) } ports := []PortMapping{} diff --git a/nat/parse.go b/nat/parse.go index 892adf8c6..e91ee5dcc 100644 --- a/nat/parse.go +++ b/nat/parse.go @@ -17,7 +17,7 @@ func PartParser(template, data string) (map[string]string, error) { out = make(map[string]string, len(templateParts)) ) if len(parts) != len(templateParts) { - return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template) + return nil, fmt.Errorf("invalid format to parse %s - should match template %s", data, template) } for i, t := range templateParts { @@ -33,7 +33,7 @@ func PartParser(template, data string) (map[string]string, error) { // ParsePortRange parses and validates the specified string as a port-range (8000-9000) func ParsePortRange(ports string) (uint64, uint64, error) { if ports == "" { - return 0, 0, fmt.Errorf("Empty string specified for ports.") + return 0, 0, fmt.Errorf("empty string specified for ports") } if !strings.Contains(ports, "-") { start, err := strconv.ParseUint(ports, 10, 16) @@ -51,7 +51,7 @@ func ParsePortRange(ports string) (uint64, uint64, error) { return 0, 0, err } if end < start { - return 0, 0, fmt.Errorf("Invalid range specified for the Port: %s", ports) + return 0, 0, fmt.Errorf("invalid range specified for port: %s", ports) } return start, end, nil } diff --git a/nat/parse_test.go b/nat/parse_test.go index 2ac204a05..cb46995b4 100644 --- a/nat/parse_test.go +++ b/nat/parse_test.go @@ -12,8 +12,8 @@ func TestParsePortRange(t *testing.T) { } func TestParsePortRangeEmpty(t *testing.T) { - if _, _, err := ParsePortRange(""); err == nil || err.Error() != "Empty string specified for ports." { - t.Fatalf("Expected error 'Empty string specified for ports.', got %v", err) + if _, _, err := ParsePortRange(""); err == nil || err.Error() != "empty string specified for ports" { + t.Fatalf("Expected error 'empty string specified for ports', got %v", err) } } @@ -28,27 +28,27 @@ func TestParsePortRangeWithNoRange(t *testing.T) { } func TestParsePortRangeIncorrectRange(t *testing.T) { - if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") { - t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) + if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "invalid range specified for port") { + t.Fatalf("Expecting error 'invalid range specified for port' but received %s.", err) } } func TestParsePortRangeIncorrectEndRange(t *testing.T) { if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { - t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) + t.Fatalf("Expecting error 'invalid syntax' but received %s.", err) } if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { - t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) + t.Fatalf("Expecting error 'invalid syntax' but received %s.", err) } } func TestParsePortRangeIncorrectStartRange(t *testing.T) { if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { - t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) + t.Fatalf("Expecting error 'invalid syntax' but received %s.", err) } if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { - t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) + t.Fatalf("Expecting error 'invalid syntax' but received %s.", err) } } diff --git a/proxy/network_proxy_test.go b/proxy/network_proxy_test.go index 2c90de03c..d16e8a8ec 100644 --- a/proxy/network_proxy_test.go +++ b/proxy/network_proxy_test.go @@ -107,7 +107,7 @@ func testProxyAt(t *testing.T, proto string, proxy Proxy, addr string) { t.Fatal(err) } if !bytes.Equal(testBuf, recvBuf) { - t.Fatal(fmt.Errorf("Expected [%v] but got [%v]", testBuf, recvBuf)) + t.Fatal(fmt.Errorf("expected [%v] but got [%v]", testBuf, recvBuf)) } } @@ -215,6 +215,6 @@ func TestUDPWriteError(t *testing.T) { t.Fatal(err) } if !bytes.Equal(testBuf, recvBuf) { - t.Fatal(fmt.Errorf("Expected [%v] but got [%v]", testBuf, recvBuf)) + t.Fatal(fmt.Errorf("expected [%v] but got [%v]", testBuf, recvBuf)) } } diff --git a/proxy/proxy.go b/proxy/proxy.go index 537aebb7c..92cd8bb76 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -31,6 +31,6 @@ func NewProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) { case *net.TCPAddr: return NewTCPProxy(frontendAddr.(*net.TCPAddr), backendAddr.(*net.TCPAddr)) default: - panic(fmt.Errorf("Unsupported protocol")) + panic(fmt.Errorf("unsupported protocol")) } } diff --git a/sockets/sockets_unix.go b/sockets/sockets_unix.go index 10d763426..3139f4ff9 100644 --- a/sockets/sockets_unix.go +++ b/sockets/sockets_unix.go @@ -18,7 +18,7 @@ const ( func configureUnixTransport(tr *http.Transport, proto, addr string) error { if len(addr) > maxUnixSocketPathSize { - return fmt.Errorf("Unix socket path %q is too long", addr) + return fmt.Errorf("unix socket path %q is too long", addr) } // No need for compression in local communications. tr.DisableCompression = true diff --git a/sockets/unix_socket.go b/sockets/unix_socket.go index bd3336de4..3ef01d4f9 100644 --- a/sockets/unix_socket.go +++ b/sockets/unix_socket.go @@ -42,7 +42,7 @@ For example: if _, err := conn.Read(buf); err != nil { panic(err) } else if string(buf) != echoStr { - panic(fmt.Errorf("Msg may lost")) + panic(fmt.Errorf("msg may lost")) } } */ diff --git a/sockets/unix_socket_test.go b/sockets/unix_socket_test.go index 8957efd32..2fb6073fa 100644 --- a/sockets/unix_socket_test.go +++ b/sockets/unix_socket_test.go @@ -31,7 +31,7 @@ func runTest(t *testing.T, path string, l net.Listener, echoStr string) { if _, err := conn.Read(buf); err != nil { t.Fatal(err) } else if string(buf) != echoStr { - t.Fatal(fmt.Errorf("Msg may lost")) + t.Fatal(fmt.Errorf("msg may lost")) } } diff --git a/tlsconfig/config.go b/tlsconfig/config.go index 992968373..6e110525c 100644 --- a/tlsconfig/config.go +++ b/tlsconfig/config.go @@ -9,11 +9,10 @@ import ( "crypto/tls" "crypto/x509" "encoding/pem" + "errors" "fmt" "io/ioutil" "os" - - "github.com/pkg/errors" ) // Options represents the information needed to create client and server TLS configurations. @@ -120,10 +119,10 @@ func isValidMinVersion(version uint16) bool { func adjustMinVersion(options Options, config *tls.Config) error { if options.MinVersion > 0 { if !isValidMinVersion(options.MinVersion) { - return fmt.Errorf("Invalid minimum TLS version: %x", options.MinVersion) + return fmt.Errorf("invalid minimum TLS version: %x", options.MinVersion) } if options.MinVersion < config.MinVersion { - return fmt.Errorf("Requested minimum TLS version is too low. Should be at-least: %x", config.MinVersion) + return fmt.Errorf("requested minimum TLS version is too low. Should be at-least: %x", config.MinVersion) } config.MinVersion = options.MinVersion } @@ -134,7 +133,7 @@ func adjustMinVersion(options Options, config *tls.Config) error { // IsErrEncryptedKey returns true if the 'err' is an error of incorrect // password when trying to decrypt a TLS private key func IsErrEncryptedKey(err error) bool { - return errors.Cause(err) == x509.IncorrectPasswordError + return errors.Is(err, x509.IncorrectPasswordError) } // getPrivateKey returns the private key in 'keyBytes', in PEM-encoded format. @@ -151,7 +150,7 @@ func getPrivateKey(keyBytes []byte, passphrase string) ([]byte, error) { if x509.IsEncryptedPEMBlock(pemBlock) { //nolint:staticcheck // Ignore SA1019 (IsEncryptedPEMBlock is deprecated) keyBytes, err = x509.DecryptPEMBlock(pemBlock, []byte(passphrase)) //nolint:staticcheck // Ignore SA1019 (DecryptPEMBlock is deprecated) if err != nil { - return nil, errors.Wrap(err, "private key is encrypted, but could not decrypt it") + return nil, fmt.Errorf("private key is encrypted, but could not decrypt it: %w", err) } keyBytes = pem.EncodeToMemory(&pem.Block{Type: pemBlock.Type, Bytes: keyBytes}) } @@ -167,26 +166,24 @@ func getCert(options Options) ([]tls.Certificate, error) { return nil, nil } - errMessage := "Could not load X509 key pair" - cert, err := ioutil.ReadFile(options.CertFile) if err != nil { - return nil, errors.Wrap(err, errMessage) + return nil, err } prKeyBytes, err := ioutil.ReadFile(options.KeyFile) if err != nil { - return nil, errors.Wrap(err, errMessage) + return nil, err } prKeyBytes, err = getPrivateKey(prKeyBytes, options.Passphrase) if err != nil { - return nil, errors.Wrap(err, errMessage) + return nil, err } tlsCert, err := tls.X509KeyPair(cert, prKeyBytes) if err != nil { - return nil, errors.Wrap(err, errMessage) + return nil, err } return []tls.Certificate{tlsCert}, nil @@ -206,7 +203,7 @@ func Client(options Options) (*tls.Config, error) { tlsCerts, err := getCert(options) if err != nil { - return nil, err + return nil, fmt.Errorf("could not load X509 key pair: %w", err) } tlsConfig.Certificates = tlsCerts @@ -224,9 +221,9 @@ func Server(options Options) (*tls.Config, error) { tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile) if err != nil { if os.IsNotExist(err) { - return nil, fmt.Errorf("Could not load X509 key pair (cert: %q, key: %q): %v", options.CertFile, options.KeyFile, err) + return nil, fmt.Errorf("could not load X509 key pair (cert: %q, key: %q): %v", options.CertFile, options.KeyFile, err) } - return nil, fmt.Errorf("Error reading X509 key pair (cert: %q, key: %q): %v. Make sure the key is not encrypted.", options.CertFile, options.KeyFile, err) + return nil, fmt.Errorf("error reading X509 key pair - make sure the key is not encrypted (cert: %q, key: %q): %v", options.CertFile, options.KeyFile, err) } tlsConfig.Certificates = []tls.Certificate{tlsCert} if options.ClientAuth >= tls.VerifyClientCertIfGiven && options.CAFile != "" {