Skip to content

Commit 9f854e2

Browse files
authored
config: fix Insecure security constructor (#2810)
1 parent 62ebf06 commit 9f854e2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

config/config.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,11 @@ func (cfg *Config) NewNode() (host.Host, error) {
372372
return sw
373373
}),
374374
fx.Provide(cfg.newBasicHost),
375-
fx.Provide(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) host.Host {
376-
lifecycle.Append(fx.StartHook(h.Start))
377-
return h
375+
fx.Provide(func(bh *bhost.BasicHost) host.Host {
376+
return bh
378377
}),
379-
fx.Provide(func(h host.Host) peer.ID { return h.ID() }),
380-
fx.Provide(func(h host.Host) crypto.PrivKey { return h.Peerstore().PrivKey(h.ID()) }),
378+
fx.Provide(func(h *swarm.Swarm) peer.ID { return h.LocalPeer() }),
379+
fx.Provide(func(h *swarm.Swarm) crypto.PrivKey { return h.Peerstore().PrivKey(h.LocalPeer()) }),
381380
}
382381
transportOpts, err := cfg.addTransports()
383382
if err != nil {
@@ -418,6 +417,9 @@ func (cfg *Config) NewNode() (host.Host, error) {
418417

419418
var bh *bhost.BasicHost
420419
fxopts = append(fxopts, fx.Invoke(func(bho *bhost.BasicHost) { bh = bho }))
420+
fxopts = append(fxopts, fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) {
421+
lifecycle.Append(fx.StartHook(h.Start))
422+
}))
421423

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

432434
if err := cfg.addAutoNAT(bh); err != nil {
433-
rh.Close()
435+
app.Stop(context.Background())
436+
if cfg.Routing != nil {
437+
rh.Close()
438+
} else {
439+
bh.Close()
440+
}
434441
return nil, err
435442
}
436443

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

507514
}),
515+
fx.Provide(func(s *swarm.Swarm) peer.ID { return s.LocalPeer() }),
508516
fx.Provide(func() crypto.PrivKey { return autonatPrivKey }),
509517
)
510518
app := fx.New(fxopts...)

libp2p_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,21 @@ func TestAutoNATService(t *testing.T) {
382382
h.Close()
383383
}
384384

385+
func TestInsecureConstructor(t *testing.T) {
386+
h, err := New(
387+
EnableNATService(),
388+
NoSecurity,
389+
)
390+
require.NoError(t, err)
391+
h.Close()
392+
393+
h, err = New(
394+
NoSecurity,
395+
)
396+
require.NoError(t, err)
397+
h.Close()
398+
}
399+
385400
func TestDisableIdentifyAddressDiscovery(t *testing.T) {
386401
h, err := New(DisableIdentifyAddressDiscovery())
387402
require.NoError(t, err)

0 commit comments

Comments
 (0)