Skip to content

Commit

Permalink
Merge pull request #94 from thaJeztah/dry_errors
Browse files Browse the repository at this point in the history
Lowercase all errors, and remove use of github.com/pkg/errors
  • Loading branch information
thaJeztah committed Nov 10, 2023
2 parents 34b5cd0 + 23efdb1 commit 3518605
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 46 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
14 changes: 7 additions & 7 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: %w", 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<empty>", rawPort)
return nil, fmt.Errorf("no port specified: %s<empty>", 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)
}
}

Expand All @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions nat/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// 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)
Expand All @@ -27,7 +27,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
}
16 changes: 8 additions & 8 deletions nat/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions proxy/network_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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))
}
}
2 changes: 1 addition & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
2 changes: 1 addition & 1 deletion sockets/sockets_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const maxUnixSocketPathSize = len(syscall.RawSockaddrUnix{}.Path)

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
Expand Down
2 changes: 1 addition & 1 deletion sockets/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
*/
Expand Down
2 changes: 1 addition & 1 deletion sockets/unix_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down
27 changes: 12 additions & 15 deletions tlsconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -125,10 +124,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
}
Expand All @@ -144,7 +143,7 @@ func adjustMinVersion(options Options, config *tls.Config) error {
// legacy PEM encryption (as specified in RFC 1423), as it is insecure by
// design (see https://go-review.googlesource.com/c/go/+/264159).
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.
Expand All @@ -161,7 +160,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})
}
Expand All @@ -177,26 +176,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
Expand All @@ -216,7 +213,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

Expand All @@ -234,9 +231,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 != "" {
Expand Down

0 comments on commit 3518605

Please sign in to comment.