mirror of
https://github.com/containers/gvisor-tap-vsock.git
synced 2026-04-22 16:17:07 +08:00
pkg: Stop using errors.Wrap
Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package sshclient
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -13,7 +14,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
@@ -90,7 +90,7 @@ func CreateBastion(_url *url.URL, passPhrase string, identity string, initial ne
|
||||
if len(identity) > 0 {
|
||||
s, err := PublicKey(identity, []byte(passPhrase))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to parse identity %q", identity)
|
||||
return nil, fmt.Errorf("failed to parse identity %q: %w", identity, err)
|
||||
}
|
||||
authMethods = append(authMethods, ssh.PublicKeys(s))
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func CreateBastion(_url *url.URL, passPhrase string, identity string, initial ne
|
||||
}
|
||||
|
||||
if len(authMethods) == 0 {
|
||||
return nil, errors.New("No available auth methods")
|
||||
return nil, errors.New("no available auth methods")
|
||||
}
|
||||
|
||||
port := _url.Port()
|
||||
@@ -167,7 +167,7 @@ func (bastion *Bastion) reconnect(ctx context.Context, conn net.Conn) error {
|
||||
conn, err = bastion.connect(ctx, bastion)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Connection to bastion host (%s) failed", bastion.Host)
|
||||
return fmt.Errorf("connection to bastion host (%s) failed: %w", bastion.Host, err)
|
||||
}
|
||||
addr := net.JoinHostPort(bastion.Host, bastion.Port)
|
||||
c, chans, reqs, err := ssh.NewClientConn(conn, addr, bastion.Config)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
winio "github.com/Microsoft/go-winio"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -34,7 +33,7 @@ func ListenNpipe(socketURI *url.URL) (net.Listener, error) {
|
||||
|
||||
listener, err := winio.ListenPipe(path, &config)
|
||||
if err != nil {
|
||||
return listener, errors.Wrapf(err, "Error listening on socket: %s", socketURI)
|
||||
return listener, fmt.Errorf("error listening on socket: %s: %w", socketURI, err)
|
||||
}
|
||||
|
||||
logrus.Info("Listening on: " + path)
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
|
||||
"github.com/containers/gvisor-tap-vsock/pkg/fs"
|
||||
"github.com/containers/gvisor-tap-vsock/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -89,7 +88,7 @@ func connectForward(ctx context.Context, bastion *Bastion) (CloseWriteConn, erro
|
||||
return forward.(CloseWriteConn), nil
|
||||
}
|
||||
if retries > 2 {
|
||||
return nil, errors.Wrapf(err, "Couldn't reestablish ssh tunnel on path: %s", bastion.Path)
|
||||
return nil, fmt.Errorf("couldn't reestablish ssh tunnel on path: %s: %w", bastion.Path, err)
|
||||
}
|
||||
// Check if ssh connection is still alive
|
||||
_, _, err = bastion.Client.SendRequest("alive@gvproxy", true, nil)
|
||||
@@ -100,7 +99,7 @@ func connectForward(ctx context.Context, bastion *Bastion) (CloseWriteConn, erro
|
||||
break
|
||||
}
|
||||
if bastionRetries > 2 || !utils.Sleep(ctx, 200*time.Millisecond) {
|
||||
return nil, errors.Wrapf(err, "Couldn't reestablish ssh connection: %s", bastion.Host)
|
||||
return nil, fmt.Errorf("couldn't reestablish ssh connection: %s: %w", bastion.Host, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,7 +124,7 @@ func listenUnix(socketURI *url.URL) (net.Listener, error) {
|
||||
defer fs.Umask(oldmask)
|
||||
listener, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
return listener, errors.Wrapf(err, "Error listening on socket: %s", socketURI.Path)
|
||||
return listener, fmt.Errorf("error listening on socket: %s: %w", socketURI.Path, err)
|
||||
}
|
||||
|
||||
return listener, nil
|
||||
@@ -150,7 +149,7 @@ func setupProxy(ctx context.Context, socketURI *url.URL, dest *url.URL, identity
|
||||
case "":
|
||||
// empty URL = Tunnel Only, no Accept
|
||||
default:
|
||||
return &SSHForward{}, errors.Errorf("URI scheme not supported: %s", socketURI.Scheme)
|
||||
return &SSHForward{}, fmt.Errorf("URI scheme not supported: %s", socketURI.Scheme)
|
||||
}
|
||||
|
||||
connectFunc := func(ctx context.Context, bastion *Bastion) (net.Conn, error) {
|
||||
@@ -187,13 +186,13 @@ func setupProxy(ctx context.Context, socketURI *url.URL, dest *url.URL, identity
|
||||
func acceptConnection(ctx context.Context, listener net.Listener, bastion *Bastion, socketURI *url.URL) error {
|
||||
con, err := listener.Accept()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Error accepting on socket: %s", socketURI.Path)
|
||||
return fmt.Errorf("error accepting on socket: %s: %w", socketURI.Path, err)
|
||||
}
|
||||
|
||||
src, ok := con.(CloseWriteStream)
|
||||
if !ok {
|
||||
con.Close()
|
||||
return errors.Wrapf(err, "Underlying socket does not support half-close %s", socketURI.Path)
|
||||
return fmt.Errorf("underlying socket does not support half-close %s: %w", socketURI.Path, err)
|
||||
}
|
||||
|
||||
var dest CloseWriteStream
|
||||
|
||||
+6
-5
@@ -3,6 +3,7 @@ package tap
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -13,7 +14,6 @@ import (
|
||||
"github.com/containers/gvisor-tap-vsock/pkg/types"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gvisor.dev/gvisor/pkg/buffer"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
@@ -95,7 +95,8 @@ func (e *Switch) Accept(ctx context.Context, rawConn net.Conn, protocol types.Pr
|
||||
e.disconnect(id, conn)
|
||||
}()
|
||||
if err := e.rx(ctx, id, conn); err != nil {
|
||||
log.Error(errors.Wrapf(err, "cannot receive packets from %s, disconnecting", conn.RemoteAddr().String()))
|
||||
err := fmt.Errorf("cannot receive packets from %s, disconnecting: %w", conn.RemoteAddr().String(), err)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -223,7 +224,7 @@ loop:
|
||||
}
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot read size from socket")
|
||||
return fmt.Errorf("cannot read size from socket: %w", err)
|
||||
}
|
||||
e.rxBuf(ctx, id, buf[:n])
|
||||
}
|
||||
@@ -243,14 +244,14 @@ loop:
|
||||
}
|
||||
_, err := io.ReadFull(reader, sizeBuf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot read size from socket")
|
||||
return fmt.Errorf("cannot read size from socket: %w", err)
|
||||
}
|
||||
size := sProtocol.Read(sizeBuf)
|
||||
|
||||
buf := make([]byte, size)
|
||||
_, err = io.ReadFull(reader, buf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot read packet from socket")
|
||||
return fmt.Errorf("cannot read packet from socket: %w", err)
|
||||
}
|
||||
e.rxBuf(ctx, id, buf)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package virtualnetwork
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -8,7 +10,6 @@ import (
|
||||
|
||||
"github.com/containers/gvisor-tap-vsock/pkg/tap"
|
||||
"github.com/containers/gvisor-tap-vsock/pkg/types"
|
||||
"github.com/pkg/errors"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/network/arp"
|
||||
@@ -30,7 +31,7 @@ type VirtualNetwork struct {
|
||||
func New(configuration *types.Configuration) (*VirtualNetwork, error) {
|
||||
_, subnet, err := net.ParseCIDR(configuration.Subnet)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot parse subnet cidr")
|
||||
return nil, fmt.Errorf("cannot parse subnet cidr: %w", err)
|
||||
}
|
||||
|
||||
var endpoint stack.LinkEndpoint
|
||||
@@ -47,7 +48,7 @@ func New(configuration *types.Configuration) (*VirtualNetwork, error) {
|
||||
}
|
||||
tapEndpoint, err := tap.NewLinkEndpoint(configuration.Debug, uint32(mtu), configuration.GatewayMacAddress, configuration.GatewayIP, configuration.GatewayVirtualIPs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot create tap endpoint")
|
||||
return nil, fmt.Errorf("cannot create tap endpoint: %w", err)
|
||||
}
|
||||
networkSwitch := tap.NewSwitch(configuration.Debug, mtu)
|
||||
tapEndpoint.Connect(networkSwitch)
|
||||
@@ -57,11 +58,11 @@ func New(configuration *types.Configuration) (*VirtualNetwork, error) {
|
||||
_ = os.Remove(configuration.CaptureFile)
|
||||
fd, err := os.Create(configuration.CaptureFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot create capture file")
|
||||
return nil, fmt.Errorf("cannot create capture file: %w", err)
|
||||
}
|
||||
endpoint, err = sniffer.NewWithWriter(tapEndpoint, fd, math.MaxUint32)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot create sniffer")
|
||||
return nil, fmt.Errorf("cannot create sniffer: %w", err)
|
||||
}
|
||||
} else {
|
||||
endpoint = tapEndpoint
|
||||
@@ -69,12 +70,12 @@ func New(configuration *types.Configuration) (*VirtualNetwork, error) {
|
||||
|
||||
stack, err := createStack(configuration, endpoint)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot create network stack")
|
||||
return nil, fmt.Errorf("cannot create network stack: %w", err)
|
||||
}
|
||||
|
||||
mux, err := addServices(configuration, stack, ipPool)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot add network services")
|
||||
return nil, fmt.Errorf("cannot add network services: %w", err)
|
||||
}
|
||||
|
||||
return &VirtualNetwork{
|
||||
@@ -129,12 +130,12 @@ func createStack(configuration *types.Configuration, endpoint stack.LinkEndpoint
|
||||
|
||||
_, parsedSubnet, err := net.ParseCIDR(configuration.Subnet)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot parse cidr")
|
||||
return nil, fmt.Errorf("cannot parse cidr: %w", err)
|
||||
}
|
||||
|
||||
subnet, err := tcpip.NewSubnet(tcpip.AddrFromSlice(parsedSubnet.IP), tcpip.MaskFromBytes(parsedSubnet.Mask))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot parse subnet")
|
||||
return nil, fmt.Errorf("cannot parse subnet: %w", err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{
|
||||
|
||||
+2
-3
@@ -1,11 +1,10 @@
|
||||
package e2eutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func CreateSSHKeys(publicKeyFile, privateKeyFile string) (string, error) {
|
||||
@@ -13,7 +12,7 @@ func CreateSSHKeys(publicKeyFile, privateKeyFile string) (string, error) {
|
||||
_ = os.Remove(privateKeyFile)
|
||||
err := exec.Command("ssh-keygen", "-N", "", "-t", "ed25519", "-f", privateKeyFile).Run()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Could not generate ssh keys")
|
||||
return "", fmt.Errorf("could not generate ssh keys: %w", err)
|
||||
}
|
||||
|
||||
return readPublicKey(publicKeyFile)
|
||||
|
||||
Reference in New Issue
Block a user