From f930993e3ad6ba4e9fe9f066e35a27d4d7dda590 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 24 Sep 2020 10:37:42 +0300 Subject: [PATCH] [#43] pkg/network: Do not panic at multiaddr to net.Addr conversion Signed-off-by: Alex Vanin --- pkg/network/address.go | 8 ++++---- pkg/network/address_test.go | 4 +++- pkg/services/object/head/remote.go | 5 ++++- pkg/services/object/put/remote.go | 5 ++++- pkg/services/object/range/remote.go | 5 ++++- pkg/services/object/rangehash/remote.go | 5 ++++- pkg/services/object/search/remote.go | 5 ++++- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/network/address.go b/pkg/network/address.go index d1a430e1e..03ec744fc 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -23,13 +23,13 @@ func (a Address) String() string { } // NetAddr returns network endpoint address in string format. -func (a Address) NetAddr() string { +func (a Address) NetAddr() (string, error) { ip, err := manet.ToNetAddr(a.ma) if err != nil { - panic(errors.Wrap(err, "could not get net addr")) + return "", errors.Wrap(err, "could not get net addr") } - return ip.String() + return ip.String(), nil } // AddressFromString restores address from a string representation. @@ -47,5 +47,5 @@ func AddressFromString(s string) (*Address, error) { // IsLocalAddress returns true if network endpoint from local address // source is equal to network endpoint of passed address. func IsLocalAddress(src LocalAddressSource, addr *Address) bool { - return src.LocalAddress().NetAddr() == addr.NetAddr() + return src.LocalAddress().ma.Equal(addr.ma) } diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index e53b7bfb5..ed2ea0d87 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -24,5 +24,7 @@ func TestAddress_NetAddr(t *testing.T) { addr, err := AddressFromString(ma.String()) require.NoError(t, err) - require.Equal(t, ip+":"+port, addr.NetAddr()) + netAddr, err := addr.NetAddr() + require.NoError(t, err) + require.Equal(t, ip+":"+port, netAddr) } diff --git a/pkg/services/object/head/remote.go b/pkg/services/object/head/remote.go index e922cc8cd..d53aff501 100644 --- a/pkg/services/object/head/remote.go +++ b/pkg/services/object/head/remote.go @@ -22,7 +22,10 @@ func (h *remoteHeader) head(ctx context.Context, prm *Prm, handler func(*object. return errors.Wrapf(err, "(%T) could not receive private key", h) } - addr := h.node.NetAddr() + addr, err := h.node.NetAddr() + if err != nil { + return err + } c, err := client.New(key, client.WithAddress(addr), diff --git a/pkg/services/object/put/remote.go b/pkg/services/object/put/remote.go index b3752cc46..69b401d8e 100644 --- a/pkg/services/object/put/remote.go +++ b/pkg/services/object/put/remote.go @@ -38,7 +38,10 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) { return nil, errors.Wrapf(err, "(%T) could not receive private key", t) } - addr := t.addr.NetAddr() + addr, err := t.addr.NetAddr() + if err != nil { + return nil, err + } c, err := client.New(key, client.WithAddress(addr), diff --git a/pkg/services/object/range/remote.go b/pkg/services/object/range/remote.go index 473c6d56a..1d40315fc 100644 --- a/pkg/services/object/range/remote.go +++ b/pkg/services/object/range/remote.go @@ -32,7 +32,10 @@ func (r *remoteRangeWriter) WriteTo(w io.Writer) (int64, error) { return 0, errors.Wrapf(err, "(%T) could not receive private key", r) } - addr := r.node.NetAddr() + addr, err := r.node.NetAddr() + if err != nil { + return 0, err + } c, err := client.New(key, client.WithAddress(addr), diff --git a/pkg/services/object/rangehash/remote.go b/pkg/services/object/rangehash/remote.go index 2fffcb56d..1dbcaaf08 100644 --- a/pkg/services/object/rangehash/remote.go +++ b/pkg/services/object/rangehash/remote.go @@ -23,7 +23,10 @@ func (h *remoteHasher) hashRange(ctx context.Context, prm *Prm, handler func([][ return errors.Wrapf(err, "(%T) could not receive private key", h) } - addr := h.node.NetAddr() + addr, err := h.node.NetAddr() + if err != nil { + return err + } c, err := client.New(key, client.WithAddress(addr), diff --git a/pkg/services/object/search/remote.go b/pkg/services/object/search/remote.go index f3e4f87f5..f96d50593 100644 --- a/pkg/services/object/search/remote.go +++ b/pkg/services/object/search/remote.go @@ -24,7 +24,10 @@ func (s *remoteStream) stream(ctx context.Context, ch chan<- []*object.ID) error return errors.Wrapf(err, "(%T) could not receive private key", s) } - addr := s.addr.NetAddr() + addr, err := s.addr.NetAddr() + if err != nil { + return err + } c, err := client.New(key, client.WithAddress(addr),