From 81d4a35b246ab0cd904135a8199042da4c48c6a5 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 16 Oct 2022 17:05:05 +0200 Subject: [PATCH] Introduce admin.CreationParams --- dnsmasq/bin/dnsmasq-entrypoint | 4 +- go-workspace/src/admin/admin.go | 13 +++++ go-workspace/src/bootstrap/bootstrap.go | 73 +++++++++++++++--------- go-workspace/src/cmd/entrypoint/hosts.go | 2 + 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/dnsmasq/bin/dnsmasq-entrypoint b/dnsmasq/bin/dnsmasq-entrypoint index 242dbec..ceac0ea 100644 --- a/dnsmasq/bin/dnsmasq-entrypoint +++ b/dnsmasq/bin/dnsmasq-entrypoint @@ -16,13 +16,15 @@ tmp="$(mktemp -d -t cryptic-net-dnsmasq-entrypoint-XXX)" thisHostName=$(tar xzf "$_BOOTSTRAP_PATH" --to-stdout ./hostname) thisHostIP=$(cat "$tmp"/hosts/"$thisHostName".yml | yq '.nebula.ip') + domain=$(tar xzf "$_BOOTSTRAP_PATH" --to-stdout ./admin/creation-params.yml | yq '.domain') + echo "listen-address=$thisHostIP" >> "$conf_path" ls -1 "$tmp"/hosts | while read hostYml; do hostName=$(echo "$hostYml" | cut -d. -f1) hostIP=$(cat "$tmp"/hosts/"$hostYml" | yq '.nebula.ip') - echo "address=/${hostName}.hosts.cryptic.io/$hostIP" >> "$conf_path" + echo "address=/${hostName}.hosts.$domain/$hostIP" >> "$conf_path" done ) diff --git a/go-workspace/src/admin/admin.go b/go-workspace/src/admin/admin.go index 7263ea6..0dbac5a 100644 --- a/go-workspace/src/admin/admin.go +++ b/go-workspace/src/admin/admin.go @@ -14,6 +14,8 @@ import ( ) const ( + creationParamsPath = "admin/creation-params.yml" + nebulaCertsCACertPath = "nebula/certs/ca.crt" nebulaCertsCAKeyPath = "nebula/certs/ca.key" @@ -22,8 +24,17 @@ const ( garageRPCSecretPath = "garage/rpc-secret.txt" ) +// 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 { + Domain string `yaml:"domain"` + CIDRs []string `yaml:"cidrs"` +} + // Admin is used for accessing all information contained within an admin.tgz. type Admin struct { + CreationParams CreationParams + NebulaCACert nebula.CACert GarageRPCSecret string @@ -41,6 +52,7 @@ func FromFS(adminFS fs.FS) (Admin, error) { into interface{} path string }{ + {&a.CreationParams, creationParamsPath}, {&a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, {&a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath}, } @@ -91,6 +103,7 @@ func (a Admin) WriteTo(into io.Writer) error { value interface{} path string }{ + {a.CreationParams, creationParamsPath}, {a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, {a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath}, } diff --git a/go-workspace/src/bootstrap/bootstrap.go b/go-workspace/src/bootstrap/bootstrap.go index facb44a..ef5d896 100644 --- a/go-workspace/src/bootstrap/bootstrap.go +++ b/go-workspace/src/bootstrap/bootstrap.go @@ -3,6 +3,7 @@ package bootstrap import ( + "cryptic-net/admin" "cryptic-net/garage" "cryptic-net/nebula" "cryptic-net/tarutil" @@ -20,12 +21,15 @@ import ( // Paths within the bootstrap FS which for general data. const ( - hostNamePath = "hostname" + adminCreationParamsPath = "admin/creation-params.yml" + hostNamePath = "hostname" ) // Bootstrap is used for accessing all information contained within a // bootstrap.tgz file. type Bootstrap struct { + AdminCreationParams admin.CreationParams + Hosts map[string]Host HostName string @@ -48,12 +52,18 @@ func FromFS(bootstrapFS fs.FS) (Bootstrap, error) { return Bootstrap{}, fmt.Errorf("loading hosts info from fs: %w", err) } - if err = yamlutil.LoadYamlFSFile( - &b.GarageGlobalBucketS3APICredentials, - bootstrapFS, - garageGlobalBucketKeyYmlPath, - ); err != nil { - return Bootstrap{}, fmt.Errorf("loading %q from fs: %w", garageGlobalBucketKeyYmlPath, err) + filesToLoadAsYAML := []struct { + into interface{} + path string + }{ + {&b.AdminCreationParams, adminCreationParamsPath}, + {&b.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, + } + + for _, f := range filesToLoadAsYAML { + if err := yamlutil.LoadYamlFSFile(f.into, bootstrapFS, f.path); err != nil { + return Bootstrap{}, fmt.Errorf("loading %q from fs: %w", f.path, err) + } } filesToLoadAsString := []struct { @@ -106,6 +116,36 @@ func (b Bootstrap) WriteTo(into io.Writer) error { w := tarutil.NewTGZWriter(into) + for _, host := range b.Hosts { + + hostB, err := yaml.Marshal(host) + if err != nil { + return fmt.Errorf("yaml encoding host %#v: %w", host, err) + } + + path := filepath.Join(hostsDirPath, host.Name+".yml") + + w.WriteFileBytes(path, hostB) + } + + filesToWriteAsYAML := []struct { + value interface{} + path string + }{ + {b.AdminCreationParams, adminCreationParamsPath}, + {b.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, + } + + for _, f := range filesToWriteAsYAML { + + b, err := yaml.Marshal(f.value) + if err != nil { + return fmt.Errorf("yaml encoding data for %q: %w", f.path, err) + } + + w.WriteFileBytes(f.path, b) + } + filesToWriteAsString := []struct { value string path string @@ -121,25 +161,6 @@ func (b Bootstrap) WriteTo(into io.Writer) error { w.WriteFileBytes(f.path, []byte(f.value)) } - garageGlobalBucketKeyB, err := yaml.Marshal(b.GarageGlobalBucketS3APICredentials) - if err != nil { - return fmt.Errorf("yaml encoding garage global bucket creds: %w", err) - } - - w.WriteFileBytes(garageGlobalBucketKeyYmlPath, garageGlobalBucketKeyB) - - for _, host := range b.Hosts { - - hostB, err := yaml.Marshal(host) - if err != nil { - return fmt.Errorf("yaml encoding host %#v: %w", host, err) - } - - path := filepath.Join(hostsDirPath, host.Name+".yml") - - w.WriteFileBytes(path, hostB) - } - return w.Close() } diff --git a/go-workspace/src/cmd/entrypoint/hosts.go b/go-workspace/src/cmd/entrypoint/hosts.go index e308098..576e685 100644 --- a/go-workspace/src/cmd/entrypoint/hosts.go +++ b/go-workspace/src/cmd/entrypoint/hosts.go @@ -219,6 +219,8 @@ var subCmdHostsMakeBootstrap = subCmd{ } newBootstrap := bootstrap.Bootstrap{ + AdminCreationParams: adm.CreationParams, + Hosts: hosts, HostName: *name,