Check for IPs in use in CreateHost

This commit is contained in:
Brian Picciano 2024-12-12 20:20:27 +01:00
parent 37d436a428
commit e750bc44a5
5 changed files with 57 additions and 1 deletions

View File

@ -9,6 +9,7 @@ const (
errCodeInitializing = daecommon.ErrorCodeRangeNetwork + iota errCodeInitializing = daecommon.ErrorCodeRangeNetwork + iota
errCodeInvalidConfig errCodeInvalidConfig
errCodeHostNotFound errCodeHostNotFound
errCodeIPInUse
) )
var ( var (
@ -25,4 +26,8 @@ var (
// ErrHostNotFound is returned when performing an operation which expected a // ErrHostNotFound is returned when performing an operation which expected a
// host to exist in the network, but that host wasn't found. // host to exist in the network, but that host wasn't found.
ErrHostNotFound = jsonrpc2.NewError(errCodeHostNotFound, "Host not found") ErrHostNotFound = jsonrpc2.NewError(errCodeHostNotFound, "Host not found")
// ErrIPInUse is returned when performing an operation which was provided an
// IP already in use by another host in the network.
ErrIPInUse = jsonrpc2.NewError(errCodeIPInUse, "IP in use")
) )

View File

@ -81,6 +81,10 @@ type RPC interface {
// CreateHost creates a bootstrap for a new host with the given name and IP // CreateHost creates a bootstrap for a new host with the given name and IP
// address. // address.
//
// Errors:
// - ErrIPInUse - if IP field of CreateHostOpts is given, and already in use
// by another host in the network.
CreateHost( CreateHost(
context.Context, nebula.HostName, CreateHostOpts, context.Context, nebula.HostName, CreateHostOpts,
) ( ) (
@ -889,8 +893,13 @@ func (n *network) CreateHost(
"choosing available IP: %w", err, "choosing available IP: %w", err,
) )
} }
} else {
for _, host := range currBootstrap.Hosts {
if host.IP() == ip {
return JoiningBootstrap{}, ErrIPInUse
}
}
} }
// TODO if the ip is given, check that it's not already in use.
caSigningPrivateKey, err := daecommon.GetNebulaCASigningPrivateKey( caSigningPrivateKey, err := daecommon.GetNebulaCASigningPrivateKey(
ctx, n.secretsStore, ctx, n.secretsStore,

View File

@ -98,6 +98,26 @@ func TestNetwork_GetBootstrap(t *testing.T) {
) )
} }
func TestNetwork_CreateHost(t *testing.T) {
t.Parallel()
// Normal functionality of this method is tested as part of
// `integrationHarness.joinNetwork`. This tests various extra behavior.
t.Run("ErrIPInUse", func(t *testing.T) {
var (
h = newIntegrationHarness(t)
network = h.createNetwork(t, "primus", nil)
hostName = nebula.HostName("secondus")
)
_, err := network.CreateHost(h.ctx, hostName, CreateHostOpts{
IP: network.getBootstrap(t).ThisHost().IP(),
})
assert.ErrorIs(t, err, ErrIPInUse)
})
}
func TestNetwork_SetConfig(t *testing.T) { func TestNetwork_SetConfig(t *testing.T) {
t.Parallel() t.Parallel()

View File

@ -0,0 +1,15 @@
---
type: task
---
# Extended Sub-Command Descriptions
It might be useful to have some kind of extended description for each
sub-command, something that would be displayed only as part of `-h`.
The extended description could provide more context for what the sub-command is
for, and how it interact with other related sub-commands.
This could also be a good place to provide usage examples, although maybe those
should go in yet another section, EXAMPLES, to distinguish them from
DESCRIPTION.

View File

@ -0,0 +1,7 @@
---
type: task
---
# Add `vpn public-addr set`, and `get` Sub-Commands.
`get` should return an error if no public address is set.