Skip to content

Commit

Permalink
config: fix Insecure security constructor (#2810)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt authored May 23, 2024
1 parent 62ebf06 commit 9f854e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,11 @@ func (cfg *Config) NewNode() (host.Host, error) {
return sw
}),
fx.Provide(cfg.newBasicHost),
fx.Provide(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) host.Host {
lifecycle.Append(fx.StartHook(h.Start))
return h
fx.Provide(func(bh *bhost.BasicHost) host.Host {
return bh
}),
fx.Provide(func(h host.Host) peer.ID { return h.ID() }),
fx.Provide(func(h host.Host) crypto.PrivKey { return h.Peerstore().PrivKey(h.ID()) }),
fx.Provide(func(h *swarm.Swarm) peer.ID { return h.LocalPeer() }),
fx.Provide(func(h *swarm.Swarm) crypto.PrivKey { return h.Peerstore().PrivKey(h.LocalPeer()) }),
}
transportOpts, err := cfg.addTransports()
if err != nil {
Expand Down Expand Up @@ -418,6 +417,9 @@ func (cfg *Config) NewNode() (host.Host, error) {

var bh *bhost.BasicHost
fxopts = append(fxopts, fx.Invoke(func(bho *bhost.BasicHost) { bh = bho }))
fxopts = append(fxopts, fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) {
lifecycle.Append(fx.StartHook(h.Start))
}))

var rh *routed.RoutedHost
if cfg.Routing != nil {
Expand All @@ -430,7 +432,12 @@ func (cfg *Config) NewNode() (host.Host, error) {
}

if err := cfg.addAutoNAT(bh); err != nil {
rh.Close()
app.Stop(context.Background())
if cfg.Routing != nil {
rh.Close()
} else {
bh.Close()
}
return nil, err
}

Expand Down Expand Up @@ -505,6 +512,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
return dialer, err

}),
fx.Provide(func(s *swarm.Swarm) peer.ID { return s.LocalPeer() }),
fx.Provide(func() crypto.PrivKey { return autonatPrivKey }),
)
app := fx.New(fxopts...)
Expand Down
15 changes: 15 additions & 0 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,21 @@ func TestAutoNATService(t *testing.T) {
h.Close()
}

func TestInsecureConstructor(t *testing.T) {
h, err := New(
EnableNATService(),
NoSecurity,
)
require.NoError(t, err)
h.Close()

h, err = New(
NoSecurity,
)
require.NoError(t, err)
h.Close()
}

func TestDisableIdentifyAddressDiscovery(t *testing.T) {
h, err := New(DisableIdentifyAddressDiscovery())
require.NoError(t, err)
Expand Down

0 comments on commit 9f854e2

Please sign in to comment.