2024-06-10 16:56:36 +00:00
|
|
|
// Package bootstrap deals with the parsing and creation of bootstrap.json
|
|
|
|
// files. It also contains some helpers which rely on bootstrap data.
|
2021-04-20 21:31:37 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
2024-12-07 19:39:13 +00:00
|
|
|
"cmp"
|
2024-06-10 16:56:36 +00:00
|
|
|
"encoding/json"
|
2021-04-20 21:31:37 +00:00
|
|
|
"fmt"
|
2024-06-10 16:56:36 +00:00
|
|
|
"isle/nebula"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/toolkit"
|
2024-07-20 10:36:21 +00:00
|
|
|
"maps"
|
2024-07-12 13:30:21 +00:00
|
|
|
"net/netip"
|
2022-10-15 16:41:07 +00:00
|
|
|
"path/filepath"
|
2024-12-07 19:39:13 +00:00
|
|
|
"slices"
|
2024-09-23 17:04:14 +00:00
|
|
|
"strings"
|
2024-09-09 14:34:00 +00:00
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 12:13:53 +00:00
|
|
|
// StateDirPath returns the path within the user's state directory where the
|
2022-10-26 22:23:39 +00:00
|
|
|
// bootstrap file is stored.
|
2024-06-17 12:13:53 +00:00
|
|
|
func StateDirPath(dataDirPath string) string {
|
2024-06-10 16:56:36 +00:00
|
|
|
return filepath.Join(dataDirPath, "bootstrap.json")
|
2022-10-26 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 11:11:18 +00:00
|
|
|
// CreationParams are general parameters used when creating a new network. These
|
|
|
|
// are available to all hosts within the network via their bootstrap files.
|
|
|
|
type CreationParams struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Domain string
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
// NewCreationParams instantiates and returns a CreationParams.
|
|
|
|
func NewCreationParams(name, domain string) CreationParams {
|
|
|
|
return CreationParams{
|
|
|
|
ID: toolkit.RandStr(32),
|
|
|
|
Name: name,
|
|
|
|
Domain: domain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotate implements the mctx.Annotator interface.
|
|
|
|
func (p CreationParams) Annotate(aa mctx.Annotations) {
|
|
|
|
aa["networkID"] = p.ID
|
|
|
|
aa["networkName"] = p.Name
|
|
|
|
aa["networkDomain"] = p.Domain
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:04:14 +00:00
|
|
|
// Matches returns true if the given string matches some aspect of the
|
|
|
|
// CreationParams.
|
|
|
|
func (p CreationParams) Matches(str string) bool {
|
|
|
|
if strings.HasPrefix(p.ID, str) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(p.Name, str) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(p.Domain, str) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conflicts returns true if either CreationParams has some parameter which
|
|
|
|
// overlaps with that of the other.
|
|
|
|
func (p CreationParams) Conflicts(p2 CreationParams) bool {
|
|
|
|
if p.ID == p2.ID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(p.Name, p2.Name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.EqualFold(p.Domain, p2.Domain) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
// Bootstrap contains all information which is needed by a host daemon to join a
|
|
|
|
// network on boot.
|
2022-10-15 14:28:03 +00:00
|
|
|
type Bootstrap struct {
|
2024-07-14 11:11:18 +00:00
|
|
|
NetworkCreationParams CreationParams
|
|
|
|
CAPublicCredentials nebula.CAPublicCredentials
|
2024-06-10 20:31:29 +00:00
|
|
|
|
|
|
|
PrivateCredentials nebula.HostPrivateCredentials
|
|
|
|
HostAssigned `json:"-"`
|
|
|
|
SignedHostAssigned nebula.Signed[HostAssigned] // signed by CA
|
|
|
|
|
2024-07-12 13:30:21 +00:00
|
|
|
Hosts map[nebula.HostName]Host
|
2024-06-10 20:31:29 +00:00
|
|
|
}
|
2022-10-16 15:05:05 +00:00
|
|
|
|
2024-07-20 10:36:21 +00:00
|
|
|
// New initializes and returns a new Bootstrap file for a new host.
|
2024-06-10 20:31:29 +00:00
|
|
|
func New(
|
|
|
|
caCreds nebula.CACredentials,
|
2024-07-14 11:11:18 +00:00
|
|
|
adminCreationParams CreationParams,
|
2024-07-20 10:36:21 +00:00
|
|
|
existingHosts map[nebula.HostName]Host,
|
2024-07-12 13:30:21 +00:00
|
|
|
name nebula.HostName,
|
|
|
|
ip netip.Addr,
|
2024-06-10 20:31:29 +00:00
|
|
|
) (
|
|
|
|
Bootstrap, error,
|
|
|
|
) {
|
2024-12-07 19:39:13 +00:00
|
|
|
host, hostPrivCreds, err := NewHost(caCreds, name, ip)
|
2024-06-10 20:31:29 +00:00
|
|
|
if err != nil {
|
2024-12-07 19:39:13 +00:00
|
|
|
return Bootstrap{}, fmt.Errorf("creating host: %w", err)
|
2024-06-10 16:56:36 +00:00
|
|
|
}
|
2022-10-15 14:28:03 +00:00
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
signedAssigned, err := nebula.Sign(
|
|
|
|
host.HostAssigned, caCreds.SigningPrivateKey,
|
|
|
|
)
|
2024-06-10 20:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return Bootstrap{}, fmt.Errorf("signing assigned fields: %w", err)
|
2024-06-10 16:56:36 +00:00
|
|
|
}
|
2024-06-10 20:31:29 +00:00
|
|
|
|
2024-07-20 10:36:21 +00:00
|
|
|
existingHosts = maps.Clone(existingHosts)
|
2024-12-07 19:39:13 +00:00
|
|
|
existingHosts[name] = host
|
2024-07-20 10:36:21 +00:00
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
return Bootstrap{
|
2024-07-14 11:11:18 +00:00
|
|
|
NetworkCreationParams: adminCreationParams,
|
|
|
|
CAPublicCredentials: caCreds.Public,
|
|
|
|
PrivateCredentials: hostPrivCreds,
|
2024-12-07 19:39:13 +00:00
|
|
|
HostAssigned: host.HostAssigned,
|
2024-07-14 11:11:18 +00:00
|
|
|
SignedHostAssigned: signedAssigned,
|
2024-07-20 10:36:21 +00:00
|
|
|
Hosts: existingHosts,
|
2024-06-10 20:31:29 +00:00
|
|
|
}, nil
|
2022-10-15 14:28:03 +00:00
|
|
|
}
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface. It will
|
|
|
|
// automatically populate the HostAssigned field by unwrapping the
|
|
|
|
// SignedHostAssigned field.
|
2024-07-07 10:44:49 +00:00
|
|
|
func (b *Bootstrap) UnmarshalJSON(data []byte) error {
|
|
|
|
type inner Bootstrap
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, (*inner)(b))
|
|
|
|
if err != nil {
|
2024-12-07 19:39:13 +00:00
|
|
|
return fmt.Errorf("json unmarshaling: %w", err)
|
2024-07-07 10:44:49 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
// Generally this will be filled, but during unit tests we sometimes leave
|
|
|
|
// it empty for convenience.
|
|
|
|
if b.SignedHostAssigned != nil {
|
|
|
|
b.HostAssigned, err = b.SignedHostAssigned.Unwrap(
|
|
|
|
b.CAPublicCredentials.SigningKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unwrapping HostAssigned: %w", err)
|
|
|
|
}
|
2024-06-10 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 10:44:49 +00:00
|
|
|
return nil
|
2022-10-15 14:28:03 +00:00
|
|
|
}
|
2022-10-11 19:24:53 +00:00
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
// ThisHost is a shortcut for b.Hosts[b.HostName], but will panic if the
|
|
|
|
// HostName isn't found in the Hosts map.
|
|
|
|
func (b Bootstrap) ThisHost() Host {
|
2024-06-10 20:31:29 +00:00
|
|
|
host, ok := b.Hosts[b.Name]
|
2022-10-15 14:28:03 +00:00
|
|
|
if !ok {
|
2024-06-10 20:31:29 +00:00
|
|
|
panic(fmt.Sprintf("hostname %q not defined in bootstrap's hosts", b.Name))
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
return host
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
2022-10-15 16:41:07 +00:00
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
// HostsOrdered returns the Hosts as a slice in a deterministic order.
|
|
|
|
func (b Bootstrap) HostsOrdered() []Host {
|
|
|
|
hosts := make([]Host, 0, len(b.Hosts))
|
|
|
|
for _, host := range b.Hosts {
|
2022-10-15 16:41:07 +00:00
|
|
|
hosts = append(hosts, host)
|
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
slices.SortFunc(hosts, func(a, b Host) int {
|
|
|
|
return cmp.Compare(a.Name, b.Name)
|
2024-06-10 16:56:36 +00:00
|
|
|
})
|
2022-10-15 16:41:07 +00:00
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
return hosts
|
2022-10-15 16:41:07 +00:00
|
|
|
}
|