2024-11-09 22:17:10 +00:00
|
|
|
//go:generate mockery --name Network --inpackage --filename network_mock.go
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// Package network implements the Network type, which manages the daemon's
|
|
|
|
// membership in a single network.
|
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon/children"
|
|
|
|
"isle/daemon/daecommon"
|
|
|
|
"isle/garage"
|
|
|
|
"isle/jsonutil"
|
|
|
|
"isle/nebula"
|
|
|
|
"isle/secrets"
|
|
|
|
"isle/toolkit"
|
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-11-05 10:59:45 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
2024-09-09 14:34:00 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GarageClientParams contains all the data needed to instantiate garage
|
|
|
|
// clients.
|
|
|
|
type GarageClientParams struct {
|
2024-11-08 16:46:44 +00:00
|
|
|
Node garage.RemoteNode
|
2024-09-09 14:34:00 +00:00
|
|
|
GlobalBucketS3APICredentials garage.S3APICredentials
|
|
|
|
|
|
|
|
// RPCSecret may be empty, if the secret is not available on the host.
|
|
|
|
RPCSecret string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalBucketS3APIClient returns an S3 client pre-configured with access to
|
|
|
|
// the global bucket.
|
2024-10-07 19:12:47 +00:00
|
|
|
func (p GarageClientParams) GlobalBucketS3APIClient() *garage.S3APIClient {
|
2024-09-09 14:34:00 +00:00
|
|
|
var (
|
2024-11-08 16:46:44 +00:00
|
|
|
addr = p.Node.S3APIAddr()
|
2024-09-09 14:34:00 +00:00
|
|
|
creds = p.GlobalBucketS3APICredentials
|
|
|
|
)
|
|
|
|
return garage.NewS3APIClient(addr, creds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateHostOpts are optional parameters to the CreateHost method.
|
|
|
|
type CreateHostOpts struct {
|
|
|
|
// IP address of the new host. An IP address will be randomly chosen if one
|
|
|
|
// is not given here.
|
|
|
|
IP netip.Addr
|
|
|
|
|
|
|
|
// CanCreateHosts indicates that the bootstrap produced by CreateHost should
|
|
|
|
// give the new host the ability to create new hosts as well.
|
|
|
|
CanCreateHosts bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoiningBootstrap wraps a normal Bootstrap to include extra data which a host
|
|
|
|
// might need while joining a network.
|
|
|
|
type JoiningBootstrap struct {
|
|
|
|
Bootstrap bootstrap.Bootstrap
|
|
|
|
Secrets map[secrets.ID]json.RawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPC defines the methods related to a single network which are available over
|
|
|
|
// the daemon's RPC interface.
|
|
|
|
type RPC interface {
|
2024-12-07 19:39:13 +00:00
|
|
|
// GetBootstrap returns the currently active Bootstrap for the Network. The
|
|
|
|
// PrivateCredentials field will be zero'd out before being returned.
|
|
|
|
GetBootstrap(context.Context) (bootstrap.Bootstrap, error)
|
2024-09-09 14:34:00 +00:00
|
|
|
|
|
|
|
// GetGarageClientParams returns a GarageClientParams for the current
|
|
|
|
// network state.
|
|
|
|
GetGarageClientParams(context.Context) (GarageClientParams, error)
|
|
|
|
|
|
|
|
// RemoveHost removes the host of the given name from the network.
|
|
|
|
RemoveHost(ctx context.Context, hostName nebula.HostName) error
|
|
|
|
|
|
|
|
// CreateHost creates a bootstrap for a new host with the given name and IP
|
|
|
|
// address.
|
2024-12-12 19:20:27 +00:00
|
|
|
//
|
|
|
|
// Errors:
|
|
|
|
// - ErrIPInUse - if IP field of CreateHostOpts is given, and already in use
|
|
|
|
// by another host in the network.
|
2024-12-15 20:39:41 +00:00
|
|
|
// - ErrSecretNotFound - This Network doesn't have the CA signing key.
|
2024-09-09 14:34:00 +00:00
|
|
|
CreateHost(
|
|
|
|
context.Context, nebula.HostName, CreateHostOpts,
|
|
|
|
) (
|
|
|
|
JoiningBootstrap, error,
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateNebulaCertificate creates and signs a new nebula certficate for an
|
|
|
|
// existing host, given the public key for that host. This is currently
|
|
|
|
// mostly useful for creating certs for mobile devices.
|
|
|
|
//
|
|
|
|
// Errors:
|
|
|
|
// - ErrHostNotFound
|
2024-12-15 20:39:41 +00:00
|
|
|
// - ErrSecretNotFound - This Network doesn't have the CA signing key.
|
2024-09-09 14:34:00 +00:00
|
|
|
CreateNebulaCertificate(
|
|
|
|
context.Context, nebula.HostName, nebula.EncryptingPublicKey,
|
|
|
|
) (
|
|
|
|
nebula.Certificate, error,
|
|
|
|
)
|
2024-10-23 18:18:11 +00:00
|
|
|
|
|
|
|
// GetConfig returns the configuration currently in use.
|
|
|
|
GetConfig(context.Context) (daecommon.NetworkConfig, error)
|
2024-10-24 17:52:08 +00:00
|
|
|
|
|
|
|
// SetConfig overrides the current config with the given one, adjusting any
|
|
|
|
// running child processes as needed.
|
2024-12-14 14:57:07 +00:00
|
|
|
//
|
|
|
|
// Errors:
|
|
|
|
// - ErrInvalidConfig
|
2024-10-24 17:52:08 +00:00
|
|
|
SetConfig(context.Context, daecommon.NetworkConfig) error
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network manages membership in a single micropelago network. Each Network
|
|
|
|
// is comprised of a unique IP subnet, hosts connected together on that subnet
|
|
|
|
// via a VPN, an S3 storage layer only accessible to those hosts, plus other
|
|
|
|
// services built on this foundation.
|
|
|
|
//
|
|
|
|
// A single daemon (isle server) can manage multiple networks. Each network is
|
|
|
|
// expected to be independent of the others, ie they should not share any
|
|
|
|
// resources.
|
|
|
|
type Network interface {
|
|
|
|
RPC
|
|
|
|
|
|
|
|
// GetNetworkCreationParams returns the CreationParams that the Network was
|
|
|
|
// originally created with.
|
|
|
|
GetNetworkCreationParams(context.Context) (bootstrap.CreationParams, error)
|
|
|
|
|
|
|
|
// Shutdown blocks until all resources held or created by the Network,
|
|
|
|
// including child processes it has started, have been cleaned up.
|
|
|
|
//
|
|
|
|
// If this returns an error then it's possible that child processes are
|
|
|
|
// still running and are no longer managed.
|
|
|
|
Shutdown() error
|
|
|
|
}
|
|
|
|
|
2024-12-17 15:05:41 +00:00
|
|
|
// Implements constructors interface, methods defined alongside the rest of the
|
|
|
|
// Network implementation.
|
|
|
|
type constructorsImpl struct{}
|
|
|
|
|
|
|
|
func newConstructors() constructors { return constructorsImpl{} }
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Network implementation
|
|
|
|
|
|
|
|
// Opts are optional parameters which can be passed in when initializing a new
|
|
|
|
// Network instance. A nil Opts is equivalent to a zero value.
|
|
|
|
type Opts struct {
|
2024-10-24 17:52:08 +00:00
|
|
|
GarageAdminToken string // Will be randomly generated if left unset.
|
2024-11-11 14:32:15 +00:00
|
|
|
|
|
|
|
// Config will be used as the configuration of the Network from its
|
|
|
|
// initialization onwards.
|
|
|
|
//
|
|
|
|
// If not given then the most recent NetworkConfig for the network will be
|
|
|
|
// used, either that which it was most recently initialized with or which
|
|
|
|
// was passed to [SetConfig].
|
|
|
|
Config *daecommon.NetworkConfig
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Opts) withDefaults() *Opts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(Opts)
|
|
|
|
}
|
2024-10-24 17:52:08 +00:00
|
|
|
|
|
|
|
if o.GarageAdminToken == "" {
|
|
|
|
o.GarageAdminToken = toolkit.RandStr(32)
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
type network struct {
|
2024-10-24 20:14:13 +00:00
|
|
|
logger *mlog.Logger
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-12-12 21:02:00 +00:00
|
|
|
envBinDirPath string
|
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer
|
|
|
|
stateDir toolkit.Dir
|
|
|
|
runtimeDir toolkit.Dir
|
2024-09-09 14:34:00 +00:00
|
|
|
|
|
|
|
opts *Opts
|
|
|
|
|
2024-10-24 17:52:08 +00:00
|
|
|
secretsStore secrets.Store
|
2024-09-09 14:34:00 +00:00
|
|
|
|
|
|
|
l sync.RWMutex
|
|
|
|
children *children.Children
|
2024-10-24 20:14:13 +00:00
|
|
|
networkConfig daecommon.NetworkConfig
|
2024-09-09 14:34:00 +00:00
|
|
|
currBootstrap bootstrap.Bootstrap
|
|
|
|
|
2024-11-05 10:59:45 +00:00
|
|
|
workerCtx context.Context
|
|
|
|
workerCancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
// newNetwork returns an instantiated *network instance. All initialization
|
|
|
|
// steps which are common to all *network creation methods (load, join, create)
|
|
|
|
// are included here as well.
|
|
|
|
func newNetwork(
|
2024-11-05 10:59:45 +00:00
|
|
|
ctx context.Context,
|
2024-09-09 14:34:00 +00:00
|
|
|
logger *mlog.Logger,
|
|
|
|
envBinDirPath string,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir toolkit.Dir,
|
|
|
|
runtimeDir toolkit.Dir,
|
2024-11-11 14:32:15 +00:00
|
|
|
dirsMayExist bool,
|
2024-09-09 14:34:00 +00:00
|
|
|
opts *Opts,
|
2024-11-11 14:32:15 +00:00
|
|
|
) (
|
|
|
|
*network, error,
|
|
|
|
) {
|
|
|
|
ctx, cancel := context.WithCancel(context.WithoutCancel(ctx))
|
|
|
|
|
|
|
|
var (
|
|
|
|
n = &network{
|
2024-12-12 21:02:00 +00:00
|
|
|
logger: logger,
|
|
|
|
envBinDirPath: envBinDirPath,
|
|
|
|
nebulaDeviceNamer: nebulaDeviceNamer,
|
|
|
|
stateDir: stateDir,
|
|
|
|
runtimeDir: runtimeDir,
|
|
|
|
opts: opts.withDefaults(),
|
|
|
|
workerCtx: ctx,
|
|
|
|
workerCancel: cancel,
|
2024-11-11 14:32:15 +00:00
|
|
|
}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
n.networkConfig, err = loadStoreConfig(n.stateDir, n.opts.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("resolving network config: %w", err)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
2024-11-11 14:32:15 +00:00
|
|
|
|
|
|
|
secretsDir, err := n.stateDir.MkChildDir("secrets", dirsMayExist)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating secrets dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.secretsStore = secrets.NewFSStore(secretsDir.Path)
|
|
|
|
|
|
|
|
return n, nil
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
// loadCreationParams returns the CreationParams of a Network which was
|
2024-09-09 14:34:00 +00:00
|
|
|
// Created/Joined with the given state directory.
|
2024-11-11 14:32:15 +00:00
|
|
|
func loadCreationParams(
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir toolkit.Dir,
|
|
|
|
) (
|
|
|
|
bootstrap.CreationParams, error,
|
|
|
|
) {
|
|
|
|
var (
|
|
|
|
bootstrapFilePath = bootstrap.StateDirPath(stateDir.Path)
|
|
|
|
bs bootstrap.Bootstrap
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := jsonutil.LoadFile(&bs, bootstrapFilePath); err != nil {
|
|
|
|
return bootstrap.CreationParams{}, fmt.Errorf(
|
|
|
|
"loading bootstrap from %q: %w", bootstrapFilePath, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.NetworkCreationParams, nil
|
|
|
|
}
|
|
|
|
|
2024-12-17 15:05:41 +00:00
|
|
|
func (constructorsImpl) load(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
envBinDirPath string,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir toolkit.Dir,
|
|
|
|
runtimeDir toolkit.Dir,
|
|
|
|
opts *Opts,
|
|
|
|
) (
|
|
|
|
Network, error,
|
|
|
|
) {
|
2024-11-11 14:32:15 +00:00
|
|
|
n, err := newNetwork(
|
2024-11-05 10:59:45 +00:00
|
|
|
ctx,
|
2024-09-09 14:34:00 +00:00
|
|
|
logger,
|
|
|
|
envBinDirPath,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
2024-11-11 14:32:15 +00:00
|
|
|
true,
|
2024-09-09 14:34:00 +00:00
|
|
|
opts,
|
|
|
|
)
|
2024-11-11 14:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("instantiating Network: %w", err)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-17 15:47:33 +00:00
|
|
|
currBootstrap, err := loadBootstrapFromStateDir(n.stateDir.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("loading bootstrap from state dir: %w", err)
|
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-12-17 15:47:33 +00:00
|
|
|
if err := n.initialize(ctx, currBootstrap, false); err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2024-12-17 15:05:41 +00:00
|
|
|
func (constructorsImpl) join(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
2024-11-10 21:13:06 +00:00
|
|
|
envBinDirPath string,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
joiningBootstrap JoiningBootstrap,
|
|
|
|
stateDir toolkit.Dir,
|
|
|
|
runtimeDir toolkit.Dir,
|
|
|
|
opts *Opts,
|
|
|
|
) (
|
|
|
|
Network, error,
|
|
|
|
) {
|
2024-11-11 14:32:15 +00:00
|
|
|
n, err := newNetwork(
|
2024-11-05 10:59:45 +00:00
|
|
|
ctx,
|
2024-09-09 14:34:00 +00:00
|
|
|
logger,
|
|
|
|
envBinDirPath,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
2024-11-11 14:32:15 +00:00
|
|
|
false,
|
2024-09-09 14:34:00 +00:00
|
|
|
opts,
|
|
|
|
)
|
2024-11-11 14:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("instantiating Network: %w", err)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := secrets.Import(
|
|
|
|
ctx, n.secretsStore, joiningBootstrap.Secrets,
|
|
|
|
); err != nil {
|
|
|
|
return nil, fmt.Errorf("importing secrets: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
if err := n.initialize(ctx, joiningBootstrap.Bootstrap, false); err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2024-12-17 15:05:41 +00:00
|
|
|
func (constructorsImpl) create(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
envBinDirPath string,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir toolkit.Dir,
|
|
|
|
runtimeDir toolkit.Dir,
|
|
|
|
creationParams bootstrap.CreationParams,
|
2024-10-31 12:04:19 +00:00
|
|
|
ipNet nebula.IPNet,
|
2024-09-09 14:34:00 +00:00
|
|
|
hostName nebula.HostName,
|
|
|
|
opts *Opts,
|
|
|
|
) (
|
|
|
|
Network, error,
|
|
|
|
) {
|
|
|
|
nebulaCACreds, err := nebula.NewCACredentials(creationParams.Domain, ipNet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating nebula CA cert: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-09 19:38:10 +00:00
|
|
|
garageRPCSecret := toolkit.RandStr(32)
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
n, err := newNetwork(
|
2024-11-05 10:59:45 +00:00
|
|
|
ctx,
|
2024-09-09 14:34:00 +00:00
|
|
|
logger,
|
|
|
|
envBinDirPath,
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer,
|
2024-09-09 14:34:00 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
2024-11-11 14:32:15 +00:00
|
|
|
false,
|
2024-09-09 14:34:00 +00:00
|
|
|
opts,
|
|
|
|
)
|
2024-11-11 14:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("instantiating Network: %w", err)
|
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
if len(n.networkConfig.Storage.Allocations) < 3 {
|
|
|
|
return nil, ErrInvalidConfig.WithData(
|
|
|
|
"At least three storage allocations are required.",
|
|
|
|
)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = daecommon.SetGarageRPCSecret(ctx, n.secretsStore, garageRPCSecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting garage RPC secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = daecommon.SetNebulaCASigningPrivateKey(
|
|
|
|
ctx, n.secretsStore, nebulaCACreds.SigningPrivateKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("setting nebula CA signing key secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostBootstrap, err := bootstrap.New(
|
|
|
|
nebulaCACreds,
|
|
|
|
creationParams,
|
|
|
|
map[nebula.HostName]bootstrap.Host{},
|
|
|
|
hostName,
|
|
|
|
ipNet.FirstAddr(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing bootstrap data: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
if err := n.initialize(ctx, hostBootstrap, true); err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 10:59:45 +00:00
|
|
|
func (n *network) periodically(
|
2024-11-05 21:31:57 +00:00
|
|
|
label string,
|
2024-11-05 10:59:45 +00:00
|
|
|
fn func(context.Context) error,
|
|
|
|
period time.Duration,
|
|
|
|
) {
|
|
|
|
n.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer n.wg.Done()
|
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
ctx := mctx.Annotate(n.workerCtx, "workerLabel", label)
|
2024-11-05 10:59:45 +00:00
|
|
|
|
|
|
|
ticker := time.NewTicker(period)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
n.logger.Info(ctx, "Starting background job runner")
|
|
|
|
defer n.logger.Info(ctx, "Stopping background job runner")
|
2024-11-05 10:59:45 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2024-11-05 21:31:57 +00:00
|
|
|
n.logger.Info(ctx, "Background job running")
|
2024-11-05 10:59:45 +00:00
|
|
|
if err := fn(ctx); err != nil {
|
2024-11-05 21:31:57 +00:00
|
|
|
n.logger.Error(ctx, "Background job failed", err)
|
2024-11-05 10:59:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
func (n *network) initialize(
|
2024-11-05 20:25:04 +00:00
|
|
|
ctx context.Context,
|
|
|
|
prevBootstrap bootstrap.Bootstrap,
|
|
|
|
isCreate bool,
|
2024-09-09 14:34:00 +00:00
|
|
|
) error {
|
2024-10-31 12:04:19 +00:00
|
|
|
prevThisHost := prevBootstrap.ThisHost()
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// we update this Host's data using whatever configuration has been provided
|
|
|
|
// by the daemon config. This way the network has the most up-to-date
|
|
|
|
// possible bootstrap. This updated bootstrap will later get updated in
|
|
|
|
// garage as a background task, so other hosts will see it as well.
|
2024-09-10 20:51:33 +00:00
|
|
|
currBootstrap, err := coalesceNetworkConfigAndBootstrap(
|
2024-10-31 12:04:19 +00:00
|
|
|
n.networkConfig, prevBootstrap,
|
2024-09-09 14:34:00 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("combining configuration into bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-27 14:16:53 +00:00
|
|
|
n.logger.Info(ctx, "Writing updated bootstrap to state dir")
|
2024-09-09 14:34:00 +00:00
|
|
|
err = writeBootstrapToStateDir(n.stateDir.Path, currBootstrap)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing bootstrap to state dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.currBootstrap = currBootstrap
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Creating child processes")
|
|
|
|
n.children, err = children.New(
|
|
|
|
ctx,
|
|
|
|
n.logger.WithNamespace("children"),
|
|
|
|
n.envBinDirPath,
|
|
|
|
n.secretsStore,
|
2024-09-10 20:51:33 +00:00
|
|
|
n.networkConfig,
|
2024-09-09 14:34:00 +00:00
|
|
|
n.runtimeDir,
|
2024-10-24 17:52:08 +00:00
|
|
|
n.opts.GarageAdminToken,
|
2024-12-12 21:02:00 +00:00
|
|
|
n.nebulaDeviceNamer,
|
2024-10-24 17:52:08 +00:00
|
|
|
n.currBootstrap,
|
2024-09-09 14:34:00 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating child processes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Child processes created")
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
createGarageGlobalBucket := isCreate
|
|
|
|
err = n.postChildrenInit(ctx, prevThisHost, createGarageGlobalBucket)
|
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
n.logger.Error(ctx, "Post-initialization failed, stopping child processes", err)
|
|
|
|
n.children.Shutdown()
|
|
|
|
return fmt.Errorf("performing post-initialization: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 17:52:08 +00:00
|
|
|
// Do this now so that everything is stable before returning. This also
|
2024-11-05 20:25:04 +00:00
|
|
|
// serves a dual-purpose, as it makes sure that the PUT from the postChildrenInit
|
2024-10-24 17:52:08 +00:00
|
|
|
// above has propagated from the local garage instance, if there is one.
|
2024-10-24 20:14:13 +00:00
|
|
|
n.logger.Info(ctx, "Reloading hosts from network storage")
|
|
|
|
if err = n.reloadHosts(ctx); err != nil {
|
2024-10-24 17:52:08 +00:00
|
|
|
return fmt.Errorf("Reloading network bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
n.periodically("reloadHosts", n.reloadHosts, 3*time.Minute)
|
|
|
|
|
2024-11-05 10:59:45 +00:00
|
|
|
n.periodically(
|
2024-11-05 21:31:57 +00:00
|
|
|
"removeOrphanGarageNodes", n.removeOrphanGarageNodes, 1*time.Minute,
|
2024-11-05 10:59:45 +00:00
|
|
|
)
|
2024-09-09 14:34:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
// postChildrenInit performs steps which are required after children have been
|
|
|
|
// initialized.
|
|
|
|
func (n *network) postChildrenInit(
|
|
|
|
ctx context.Context,
|
|
|
|
prevThisHost bootstrap.Host,
|
|
|
|
createGarageGlobalBucket bool,
|
2024-10-31 12:04:19 +00:00
|
|
|
) error {
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
|
|
|
thisHost := n.currBootstrap.ThisHost()
|
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
if len(thisHost.Garage.Instances) > 0 {
|
2024-09-09 14:34:00 +00:00
|
|
|
n.logger.Info(ctx, "Applying garage layout")
|
|
|
|
if err := garageApplyLayout(
|
2024-10-24 17:52:08 +00:00
|
|
|
ctx,
|
|
|
|
n.logger,
|
|
|
|
n.networkConfig,
|
|
|
|
n.opts.GarageAdminToken,
|
2024-10-31 12:04:19 +00:00
|
|
|
prevThisHost, thisHost,
|
2024-09-09 14:34:00 +00:00
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("applying garage layout: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
if createGarageGlobalBucket {
|
2024-09-09 14:34:00 +00:00
|
|
|
n.logger.Info(ctx, "Initializing garage shared global bucket")
|
|
|
|
garageGlobalBucketCreds, err := garageInitializeGlobalBucket(
|
|
|
|
ctx,
|
|
|
|
n.logger,
|
2024-09-10 20:51:33 +00:00
|
|
|
n.networkConfig,
|
2024-10-24 17:52:08 +00:00
|
|
|
n.opts.GarageAdminToken,
|
2024-10-31 12:04:19 +00:00
|
|
|
thisHost,
|
2024-09-09 14:34:00 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing global bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = daecommon.SetGarageS3APIGlobalBucketCredentials(
|
|
|
|
ctx, n.secretsStore, garageGlobalBucketCreds,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("storing global bucket creds: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
for _, alloc := range n.networkConfig.Storage.Allocations {
|
|
|
|
if err := garageWaitForAlloc(
|
|
|
|
ctx, n.logger, alloc, n.opts.GarageAdminToken, thisHost,
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"waiting for alloc %+v to initialize: %w", alloc, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
n.logger.Info(ctx, "Updating host info in garage")
|
2024-11-05 20:25:04 +00:00
|
|
|
err := putGarageBoostrapHost(ctx, n.secretsStore, n.currBootstrap)
|
2024-10-24 20:14:13 +00:00
|
|
|
if err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return fmt.Errorf("updating host info in garage: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-07 19:12:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 20:14:13 +00:00
|
|
|
func (n *network) reloadHosts(ctx context.Context) error {
|
2024-10-07 19:12:47 +00:00
|
|
|
n.l.RLock()
|
|
|
|
currBootstrap := n.currBootstrap
|
|
|
|
n.l.RUnlock()
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Checking for bootstrap changes")
|
2024-10-24 20:14:13 +00:00
|
|
|
newHosts, err := getGarageBootstrapHosts(
|
|
|
|
ctx, n.logger, n.secretsStore, currBootstrap,
|
|
|
|
)
|
2024-10-07 19:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting hosts from garage: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 20:14:13 +00:00
|
|
|
newBootstrap := currBootstrap
|
|
|
|
newBootstrap.Hosts = newHosts
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
if _, err = n.reload(ctx, nil, &newBootstrap); err != nil {
|
2024-10-07 19:12:47 +00:00
|
|
|
return fmt.Errorf("reloading with new host data: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
// In general each host will manage the garage cluster layout of its own storage
|
|
|
|
// allocations via garageApplyLayout. There are three situations which are
|
|
|
|
// handled here, rather than garageApplyLayout:
|
|
|
|
//
|
|
|
|
// - A host removes all of its allocations via SetConfig.
|
|
|
|
// - A host removes all of its allocations by calling Load with no allocations
|
|
|
|
// in the provided daecommon.NetworkConfig.
|
|
|
|
// - A host is removed from the network by another host.
|
|
|
|
//
|
|
|
|
// In all of these cases the host no longer has any garage instances running,
|
|
|
|
// and so can't call garageApplyLayout on itself. To combat this we have all
|
|
|
|
// hosts which do have garage instances running periodically check that there's
|
|
|
|
// not some garage nodes orphaned in the cluster layout, and remove them if so.
|
|
|
|
func (n *network) removeOrphanGarageNodes(ctx context.Context) error {
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
|
|
|
thisHost := n.currBootstrap.ThisHost()
|
|
|
|
if len(thisHost.Garage.Instances) == 0 {
|
|
|
|
n.logger.Info(ctx, "No local garage instances, cannot remove orphans")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
adminClient := newGarageAdminClient(
|
|
|
|
n.logger, n.networkConfig, n.opts.GarageAdminToken, thisHost,
|
|
|
|
)
|
|
|
|
defer adminClient.Close()
|
|
|
|
|
|
|
|
clusterStatus, err := adminClient.Status(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving garage cluster status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buddyIP, buddyNodes := garageNodeBuddyPeers(clusterStatus, thisHost)
|
|
|
|
if len(buddyNodes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = mctx.Annotate(ctx, "buddyIP", buddyIP)
|
|
|
|
|
|
|
|
for _, host := range n.currBootstrap.Hosts {
|
|
|
|
if host.IP() != buddyIP {
|
|
|
|
continue
|
|
|
|
} else if len(host.Garage.Instances) > 0 {
|
|
|
|
n.logger.Info(ctx, "Buddy instance has garage nodes configured in its bootstrap, doing nothing")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either the host is no longer in the network, or it no longer has any
|
|
|
|
// garage instances set on it. Either way, remove its nodes from the cluster
|
|
|
|
// layout.
|
|
|
|
|
|
|
|
buddyNodeIDs := make([]string, len(buddyNodes))
|
|
|
|
for i, buddyNode := range buddyNodes {
|
|
|
|
buddyNodeIDs[i] = buddyNode.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Applying garage layout to remove orphaned garage nodes")
|
|
|
|
if err := adminClient.ApplyLayout(ctx, nil, buddyNodeIDs); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"applying garage cluster layout, removing nodes %+v: %w",
|
|
|
|
buddyNodes,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
// returns the bootstrap prior to the reload being applied.
|
2024-10-24 20:14:13 +00:00
|
|
|
func (n *network) reload(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx context.Context,
|
2024-10-27 14:16:53 +00:00
|
|
|
newNetworkConfig *daecommon.NetworkConfig,
|
|
|
|
newBootstrap *bootstrap.Bootstrap,
|
2024-10-31 12:04:19 +00:00
|
|
|
) (
|
|
|
|
bootstrap.Bootstrap, error,
|
|
|
|
) {
|
2024-10-24 20:14:13 +00:00
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
prevBootstrap := n.currBootstrap
|
|
|
|
|
2024-10-27 14:16:53 +00:00
|
|
|
if newBootstrap != nil {
|
|
|
|
n.currBootstrap = *newBootstrap
|
|
|
|
}
|
|
|
|
|
|
|
|
if newNetworkConfig != nil {
|
|
|
|
n.networkConfig = *newNetworkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if n.currBootstrap, err = coalesceNetworkConfigAndBootstrap(
|
|
|
|
n.networkConfig, n.currBootstrap,
|
|
|
|
); err != nil {
|
2024-10-31 12:04:19 +00:00
|
|
|
return bootstrap.Bootstrap{}, fmt.Errorf(
|
|
|
|
"combining configuration into bootstrap: %w", err,
|
|
|
|
)
|
2024-10-27 14:16:53 +00:00
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
|
2024-10-24 20:14:13 +00:00
|
|
|
n.logger.Info(ctx, "Writing updated bootstrap to state dir")
|
2024-10-27 14:16:53 +00:00
|
|
|
err = writeBootstrapToStateDir(n.stateDir.Path, n.currBootstrap)
|
2024-10-24 20:14:13 +00:00
|
|
|
if err != nil {
|
2024-10-31 12:04:19 +00:00
|
|
|
return bootstrap.Bootstrap{}, fmt.Errorf(
|
|
|
|
"writing bootstrap to state dir: %w", err,
|
|
|
|
)
|
2024-10-24 20:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Reloading child processes")
|
2024-10-27 14:16:53 +00:00
|
|
|
err = n.children.Reload(ctx, n.networkConfig, n.currBootstrap)
|
2024-10-24 19:19:58 +00:00
|
|
|
if err != nil {
|
2024-10-31 12:04:19 +00:00
|
|
|
return bootstrap.Bootstrap{}, fmt.Errorf(
|
|
|
|
"reloading child processes: %w", err,
|
|
|
|
)
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
return prevBootstrap, nil
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func withCurrBootstrap[Res any](
|
|
|
|
n *network, fn func(bootstrap.Bootstrap) (Res, error),
|
|
|
|
) (Res, error) {
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
|
|
|
|
|
|
|
currBootstrap := n.currBootstrap
|
|
|
|
return fn(currBootstrap)
|
|
|
|
}
|
|
|
|
|
2024-10-06 15:15:40 +00:00
|
|
|
func (n *network) getBootstrap() (
|
2024-09-09 14:34:00 +00:00
|
|
|
bootstrap.Bootstrap, error,
|
|
|
|
) {
|
|
|
|
return withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
bootstrap.Bootstrap, error,
|
|
|
|
) {
|
|
|
|
return currBootstrap, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
func (n *network) GetBootstrap(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
bootstrap.Bootstrap, error,
|
|
|
|
) {
|
2024-10-24 20:14:13 +00:00
|
|
|
return withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
2024-12-07 19:39:13 +00:00
|
|
|
bootstrap.Bootstrap, error,
|
2024-10-24 20:14:13 +00:00
|
|
|
) {
|
2024-12-07 19:39:13 +00:00
|
|
|
currBootstrap.PrivateCredentials = nebula.HostPrivateCredentials{}
|
|
|
|
return currBootstrap, nil
|
2024-09-09 14:34:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) GetGarageClientParams(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
GarageClientParams, error,
|
|
|
|
) {
|
|
|
|
return withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
GarageClientParams, error,
|
|
|
|
) {
|
2024-10-24 20:14:13 +00:00
|
|
|
return getGarageClientParams(ctx, n.secretsStore, currBootstrap)
|
2024-09-09 14:34:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) RemoveHost(ctx context.Context, hostName nebula.HostName) error {
|
|
|
|
_, err := withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
struct{}, error,
|
|
|
|
) {
|
2024-10-24 20:14:13 +00:00
|
|
|
garageClientParams, err := getGarageClientParams(
|
|
|
|
ctx, n.secretsStore, currBootstrap,
|
|
|
|
)
|
2024-09-09 14:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return struct{}{}, fmt.Errorf("get garage client params: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := garageClientParams.GlobalBucketS3APIClient()
|
2024-10-07 19:12:47 +00:00
|
|
|
defer client.Close()
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
return struct{}{}, removeGarageBootstrapHost(ctx, client, hostName)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCACreds(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
caSigningPrivateKey nebula.SigningPrivateKey,
|
|
|
|
) nebula.CACredentials {
|
|
|
|
return nebula.CACredentials{
|
|
|
|
Public: currBootstrap.CAPublicCredentials,
|
|
|
|
SigningPrivateKey: caSigningPrivateKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func chooseAvailableIP(b bootstrap.Bootstrap) (netip.Addr, error) {
|
|
|
|
var (
|
|
|
|
cidrIPNet = b.CAPublicCredentials.Cert.Unwrap().Details.Subnets[0]
|
|
|
|
cidrMask = cidrIPNet.Mask
|
|
|
|
cidrIPB = cidrIPNet.IP
|
|
|
|
|
|
|
|
cidr = netip.MustParsePrefix(cidrIPNet.String())
|
|
|
|
cidrIP = cidr.Addr()
|
|
|
|
cidrSuffixBits = cidrIP.BitLen() - cidr.Bits()
|
|
|
|
|
|
|
|
inUseIPs = make(map[netip.Addr]struct{}, len(b.Hosts))
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, host := range b.Hosts {
|
|
|
|
inUseIPs[host.IP()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// first check that there are any addresses at all. We can determine the
|
|
|
|
// number of possible addresses using the network CIDR. The first IP in a
|
|
|
|
// subnet is the network identifier, and is reserved. The last IP is the
|
|
|
|
// broadcast IP, and is also reserved. Hence, the -2.
|
|
|
|
usableIPs := (1 << cidrSuffixBits) - 2
|
|
|
|
if len(inUseIPs) >= usableIPs {
|
|
|
|
return netip.Addr{}, errors.New("no available IPs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to know the subnet broadcast address, so we don't accidentally
|
|
|
|
// produce it.
|
|
|
|
cidrBCastIPB := bytes.Clone(cidrIPB)
|
|
|
|
for i := range cidrBCastIPB {
|
|
|
|
cidrBCastIPB[i] |= ^cidrMask[i]
|
|
|
|
}
|
|
|
|
cidrBCastIP, ok := netip.AddrFromSlice(cidrBCastIPB)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("invalid broadcast ip calculated: %x", cidrBCastIP))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a handful of times to pick an IP at random. This is preferred, as it
|
|
|
|
// leaves less room for two different CreateHost calls to choose the same
|
|
|
|
// IP.
|
|
|
|
for range 20 {
|
|
|
|
b := make([]byte, len(cidrIPB))
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
return netip.Addr{}, fmt.Errorf("reading random bytes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range b {
|
|
|
|
b[i] = cidrIPB[i] | (b[i] & ^cidrMask[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, ok := netip.AddrFromSlice(b)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("generated invalid IP: %x", b))
|
|
|
|
} else if !cidr.Contains(ip) {
|
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"generated IP %v which is not in cidr %v", ip, cidr,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip == cidrIP || ip == cidrBCastIP {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, inUse := inUseIPs[ip]; !inUse {
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If randomly picking fails then just go through IPs one by one until the
|
|
|
|
// free one is found.
|
|
|
|
for ip := cidrIP.Next(); ip != cidrBCastIP; ip = ip.Next() {
|
|
|
|
if _, inUse := inUseIPs[ip]; !inUse {
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("All ips are in-use, but somehow that wasn't determined earlier")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) CreateHost(
|
|
|
|
ctx context.Context,
|
|
|
|
hostName nebula.HostName,
|
|
|
|
opts CreateHostOpts,
|
|
|
|
) (
|
|
|
|
JoiningBootstrap, error,
|
|
|
|
) {
|
|
|
|
n.l.RLock()
|
|
|
|
currBootstrap := n.currBootstrap
|
|
|
|
n.l.RUnlock()
|
|
|
|
|
|
|
|
ip := opts.IP
|
|
|
|
if ip == (netip.Addr{}) {
|
|
|
|
var err error
|
|
|
|
if ip, err = chooseAvailableIP(currBootstrap); err != nil {
|
|
|
|
return JoiningBootstrap{}, fmt.Errorf(
|
|
|
|
"choosing available IP: %w", err,
|
|
|
|
)
|
|
|
|
}
|
2024-12-12 19:20:27 +00:00
|
|
|
} else {
|
|
|
|
for _, host := range currBootstrap.Hosts {
|
|
|
|
if host.IP() == ip {
|
|
|
|
return JoiningBootstrap{}, ErrIPInUse
|
|
|
|
}
|
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caSigningPrivateKey, err := daecommon.GetNebulaCASigningPrivateKey(
|
|
|
|
ctx, n.secretsStore,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-12-15 20:39:41 +00:00
|
|
|
if errors.Is(err, secrets.ErrNotFound) {
|
|
|
|
err = ErrSecretNotFound
|
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
return JoiningBootstrap{}, fmt.Errorf("getting CA signing key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var joiningBootstrap JoiningBootstrap
|
|
|
|
joiningBootstrap.Bootstrap, err = bootstrap.New(
|
|
|
|
makeCACreds(currBootstrap, caSigningPrivateKey),
|
|
|
|
currBootstrap.NetworkCreationParams,
|
|
|
|
currBootstrap.Hosts,
|
|
|
|
hostName,
|
|
|
|
ip,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return JoiningBootstrap{}, fmt.Errorf(
|
|
|
|
"initializing bootstrap data: %w", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
secretsIDs := []secrets.ID{
|
|
|
|
daecommon.GarageRPCSecretSecretID,
|
|
|
|
daecommon.GarageS3APIGlobalBucketCredentialsSecretID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.CanCreateHosts {
|
|
|
|
secretsIDs = append(
|
|
|
|
secretsIDs, daecommon.NebulaCASigningPrivateKeySecretID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if joiningBootstrap.Secrets, err = secrets.Export(
|
|
|
|
ctx, n.secretsStore, secretsIDs,
|
|
|
|
); err != nil {
|
|
|
|
return JoiningBootstrap{}, fmt.Errorf("exporting secrets: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Putting new host in garage")
|
2024-10-24 20:14:13 +00:00
|
|
|
err = putGarageBoostrapHost(ctx, n.secretsStore, joiningBootstrap.Bootstrap)
|
2024-09-09 14:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return JoiningBootstrap{}, fmt.Errorf("putting new host in garage: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.logger.Info(ctx, "Reloading local state with new host")
|
2024-12-12 20:26:09 +00:00
|
|
|
if err = n.reloadHosts(ctx); err != nil {
|
2024-09-09 14:34:00 +00:00
|
|
|
return JoiningBootstrap{}, fmt.Errorf("reloading child processes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return joiningBootstrap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) CreateNebulaCertificate(
|
|
|
|
ctx context.Context,
|
|
|
|
hostName nebula.HostName,
|
|
|
|
hostPubKey nebula.EncryptingPublicKey,
|
|
|
|
) (
|
|
|
|
nebula.Certificate, error,
|
|
|
|
) {
|
|
|
|
return withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
nebula.Certificate, error,
|
|
|
|
) {
|
|
|
|
host, ok := currBootstrap.Hosts[hostName]
|
|
|
|
if !ok {
|
|
|
|
return nebula.Certificate{}, ErrHostNotFound
|
|
|
|
}
|
|
|
|
ip := host.IP()
|
|
|
|
|
|
|
|
caSigningPrivateKey, err := daecommon.GetNebulaCASigningPrivateKey(
|
|
|
|
ctx, n.secretsStore,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-12-15 20:39:41 +00:00
|
|
|
if errors.Is(err, secrets.ErrNotFound) {
|
|
|
|
err = ErrSecretNotFound
|
|
|
|
}
|
2024-09-09 14:34:00 +00:00
|
|
|
return nebula.Certificate{}, fmt.Errorf("getting CA signing key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCreds := makeCACreds(currBootstrap, caSigningPrivateKey)
|
|
|
|
|
|
|
|
return nebula.NewHostCert(caCreds, hostPubKey, hostName, ip)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-23 18:18:11 +00:00
|
|
|
func (n *network) GetConfig(context.Context) (daecommon.NetworkConfig, error) {
|
2024-10-24 20:14:13 +00:00
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
2024-10-23 18:18:11 +00:00
|
|
|
return n.networkConfig, nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 17:52:08 +00:00
|
|
|
func (n *network) SetConfig(
|
|
|
|
ctx context.Context, config daecommon.NetworkConfig,
|
|
|
|
) error {
|
2024-11-11 14:32:15 +00:00
|
|
|
if _, err := loadStoreConfig(n.stateDir, &config); err != nil {
|
|
|
|
return fmt.Errorf("storing new config: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
prevBootstrap, err := n.reload(ctx, &config, nil)
|
|
|
|
if err != nil {
|
2024-10-27 14:16:53 +00:00
|
|
|
return fmt.Errorf("reloading config: %w", err)
|
2024-10-24 17:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 20:25:04 +00:00
|
|
|
if err := n.postChildrenInit(ctx, prevBootstrap.ThisHost(), false); err != nil {
|
2024-10-24 17:52:08 +00:00
|
|
|
return fmt.Errorf("performing post-initialization: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
func (n *network) GetNetworkCreationParams(
|
|
|
|
ctx context.Context,
|
|
|
|
) (
|
|
|
|
bootstrap.CreationParams, error,
|
|
|
|
) {
|
|
|
|
return withCurrBootstrap(n, func(
|
|
|
|
currBootstrap bootstrap.Bootstrap,
|
|
|
|
) (
|
|
|
|
bootstrap.CreationParams, error,
|
|
|
|
) {
|
|
|
|
return currBootstrap.NetworkCreationParams, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Shutdown() error {
|
2024-11-05 10:59:45 +00:00
|
|
|
n.workerCancel()
|
2024-09-09 14:34:00 +00:00
|
|
|
n.wg.Wait()
|
|
|
|
|
|
|
|
if n.children != nil {
|
2024-11-05 10:59:45 +00:00
|
|
|
n.logger.Info(context.Background(), "Shutting down children")
|
2024-09-09 14:34:00 +00:00
|
|
|
n.children.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|