include cidr block range in the infrastructure data

This commit is contained in:
William Banfield
2022-10-24 12:04:50 -04:00
parent caa75ae791
commit 77e7318613
3 changed files with 23 additions and 31 deletions
+16
View File
@@ -7,6 +7,14 @@ import (
"os"
)
const (
dockerIPv4CIDR = "10.186.73.0/24"
dockerIPv6CIDR = "fd80:b10c::/48"
globalIPv4CIDR = "0.0.0.0/0"
globalIPv6CIDR = "0:0::/0"
)
// InfrastructureData contains the relevant information for a set of existing
// infrastructure that is to be used for running a testnet.
type InfrastructureData struct {
@@ -21,6 +29,10 @@ type InfrastructureData struct {
// The key of the map is the name of the instance, which each must correspond
// to the names of one of the testnet nodes defined in the testnet manifest.
Instances map[string]InstanceData `json:"instances"`
// Network is the CIDR notation range of IP addresses that all of the instances'
// IP addresses are expected to be within.
Network string `json:"network"`
}
// InstanceData contains the relevant information for a machine instance backing
@@ -42,6 +54,7 @@ func NewDockerInfrastructureData(m Manifest) (InfrastructureData, error) {
ifd := InfrastructureData{
Provider: "docker",
Instances: make(map[string]InstanceData),
Network: netAddress,
}
for name := range m.Nodes {
ifd.Instances[name] = InstanceData{
@@ -58,5 +71,8 @@ func InfrastructureDataFromFile(p string) (InfrastructureData, error) {
if err != nil {
return InfrastructureData{}, err
}
if ifd.Network == "" {
ifd.Network = globalIPv4CIDR
}
return ifd, nil
}
+5 -29
View File
@@ -21,11 +21,6 @@ import (
const (
randomSeed int64 = 2308084734268
proxyPortFirst uint32 = 5701
dockerIPv4CIDR = "10.186.73.0/24"
dockerIPv6CIDR = "fd80:b10c::/48"
globalIPv4CIDR = "0.0.0.0/0"
globalIPv6CIDR = "0:0::/0"
)
type (
@@ -105,37 +100,18 @@ type Node struct {
// random seed to generate e.g. keys.
func LoadTestnet(manifest Manifest, fname string, ifd InfrastructureData) (*Testnet, error) {
dir := strings.TrimSuffix(fname, filepath.Ext(fname))
// Set up resource generators. These must be deterministic.
var netAddress string
switch ifd.Provider {
case "docker":
netAddress = dockerIPv4CIDR
if manifest.IPv6 {
netAddress = dockerIPv6CIDR
}
default:
// TODO(williambanfield): add list of CIDR blocks to the infrastructure
// data struct to allow tighter validation of IP addresses.
netAddress = globalIPv4CIDR
if manifest.IPv6 {
netAddress = globalIPv6CIDR
}
}
_, ipNet, err := net.ParseCIDR(netAddress)
if err != nil {
return nil, fmt.Errorf("invalid IP network address %q: %w", netAddress, err)
}
ipGen := newIPGenerator(ipNet)
keyGen := newKeyGenerator(randomSeed)
proxyPortGen := newPortGenerator(proxyPortFirst)
_, ipNet, err := net.ParseCIDR(ifd.Network)
if err != nil {
return nil, fmt.Errorf("invalid IP network address %q: %w", ifd.Network, err)
}
testnet := &Testnet{
Name: filepath.Base(dir),
File: fname,
Dir: dir,
IP: ipGen.Network(),
IP: ipNet,
InitialHeight: 1,
InitialState: manifest.InitialState,
Validators: map[*Node]int64{},
+2 -2
View File
@@ -73,7 +73,7 @@ func NewCLI() *CLI {
}
ifd, err = e2e.InfrastructureDataFromFile(p)
if err != nil {
return err
return fmt.Errorf("parsing infrastructure data: %s", err)
}
default:
return fmt.Errorf("unknown infrastructure type '%s'", inft)
@@ -81,7 +81,7 @@ func NewCLI() *CLI {
testnet, err := e2e.LoadTestnet(m, file, ifd)
if err != nil {
return err
return fmt.Errorf("loading testnet: %s", err)
}
cli.testnet = testnet