2022-10-15 14:28:03 +00:00
|
|
|
package bootstrap
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-10 16:56:36 +00:00
|
|
|
"isle/nebula"
|
2022-10-29 19:11:40 +00:00
|
|
|
"net"
|
2022-10-15 16:41:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NebulaHost describes the nebula configuration of a Host which is relevant for
|
|
|
|
// other hosts to know.
|
2021-04-20 21:31:37 +00:00
|
|
|
type NebulaHost struct {
|
2024-06-10 20:31:29 +00:00
|
|
|
PublicAddr string
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
// GarageHost describes a single garage instance in the GarageHost.
|
2021-04-20 21:31:37 +00:00
|
|
|
type GarageHostInstance struct {
|
2024-06-10 16:56:36 +00:00
|
|
|
ID string
|
|
|
|
RPCPort int
|
|
|
|
S3APIPort int
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
// GarageHost describes the garage configuration of a Host which is relevant for
|
|
|
|
// other hosts to know.
|
2021-04-20 21:31:37 +00:00
|
|
|
type GarageHost struct {
|
2024-06-10 16:56:36 +00:00
|
|
|
Instances []GarageHostInstance
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
// HostAssigned are all fields related to a host which were assigned to it by an
|
|
|
|
// admin.
|
|
|
|
type HostAssigned struct {
|
|
|
|
Name string
|
|
|
|
PublicCredentials nebula.HostPublicCredentials
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
// HostConfigured are all the fields a host can configure for itself.
|
|
|
|
type HostConfigured struct {
|
|
|
|
Nebula NebulaHost `json:",omitempty"`
|
|
|
|
Garage GarageHost `json:",omitempty"`
|
|
|
|
}
|
2022-10-29 19:11:40 +00:00
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
// AuthenticatedHost wraps all the data about a host which other hosts may know
|
|
|
|
// about it, such that those hosts can authenticate that the data is valid and
|
|
|
|
// approved by an admin.
|
|
|
|
type AuthenticatedHost struct {
|
|
|
|
Assigned nebula.Signed[HostAssigned] // signed by CA
|
|
|
|
Configured nebula.Signed[HostConfigured] // signed by host
|
|
|
|
}
|
2022-11-05 14:23:29 +00:00
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
// Unwrap attempts to authenticate and unwrap the Host embedded in this
|
|
|
|
// instance. nebula.ErrInvalidSignature is returned if any signatures are
|
|
|
|
// invalid.
|
|
|
|
func (ah AuthenticatedHost) Unwrap(caCreds nebula.CAPublicCredentials) (Host, error) {
|
|
|
|
assigned, err := ah.Assigned.Unwrap(caCreds.SigningKey)
|
2022-11-05 14:23:29 +00:00
|
|
|
if err != nil {
|
2024-06-10 20:31:29 +00:00
|
|
|
return Host{}, fmt.Errorf("unwrapping assigned fields using CA public key: %w", err)
|
2022-11-05 14:23:29 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
configured, err := ah.Configured.Unwrap(assigned.PublicCredentials.SigningKey)
|
|
|
|
if err != nil {
|
|
|
|
return Host{}, fmt.Errorf("unwrapping configured fields using host public key: %w", err)
|
2022-11-05 14:23:29 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
return Host{assigned, configured}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host contains all data bout a host which other hosts may know about it.
|
|
|
|
//
|
|
|
|
// A Host should only be obtained over the network as an AuthenticatedHost, and
|
|
|
|
// subsequently Unwrapped.
|
|
|
|
type Host struct {
|
|
|
|
HostAssigned
|
|
|
|
HostConfigured
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP returns the IP address encoded in the Host's nebula certificate, or panics
|
|
|
|
// if there is an error.
|
|
|
|
//
|
|
|
|
// This assumes that the Host and its data has already been verified against the
|
|
|
|
// CA signing key.
|
|
|
|
func (h Host) IP() net.IP {
|
|
|
|
ip, err := nebula.IPFromHostCertPEM(h.PublicCredentials.CertPEM)
|
2022-10-29 19:11:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("could not parse IP out of cert for host %q: %w", h.Name, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
}
|