From da259bb4cbdf6a0efb958239ae8085b2ebeae8eb Mon Sep 17 00:00:00 2001 From: George Date: Fri, 15 May 2020 09:54:57 -0400 Subject: [PATCH] Fix empty values serialization --- cmd/yggdrasil/main.go | 4 ++-- src/popura/config.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index e57f3cf..f2af438 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -107,13 +107,13 @@ func run_yggdrasil() { // their configuration file with newly mapped names (like above) or to // convert from plain JSON to commented HJSON. if *normaliseconf { - fmt.Println(popura.SaveConfig(yggConfig, popConfig, *confjson)) + fmt.Println(popura.SaveConfig(*yggConfig, *popConfig, *confjson)) return } case *genconf: // Generate a new configuration and print it to stdout. yggConfig, popConfig = popura.GenerateConfig() - fmt.Println(popura.SaveConfig(yggConfig, popConfig, *confjson)) + fmt.Println(popura.SaveConfig(*yggConfig, *popConfig, *confjson)) return default: // No flags were provided, therefore print the list of flags to stdout. diff --git a/src/popura/config.go b/src/popura/config.go index 2f50cf0..940597b 100644 --- a/src/popura/config.go +++ b/src/popura/config.go @@ -26,13 +26,39 @@ func GenerateConfig() (*config.NodeConfig, *PopuraConfig) { return config.GenerateConfig(), &popConfig } -func SaveConfig(yggConfig *config.NodeConfig, popConfig *PopuraConfig, isjson bool) string { +// initialize empty values for correct JSON serialization +func correctEmptyValues(yggConfig *config.NodeConfig) { + if len(yggConfig.TunnelRouting.IPv4LocalSubnets) == 0 { + yggConfig.TunnelRouting.IPv4LocalSubnets = []string{} + } + if len(yggConfig.TunnelRouting.IPv6LocalSubnets) == 0 { + yggConfig.TunnelRouting.IPv6LocalSubnets = []string{} + } + if len(yggConfig.TunnelRouting.IPv4RemoteSubnets) == 0 { + yggConfig.TunnelRouting.IPv4RemoteSubnets = make(map[string]string) + } + if len(yggConfig.TunnelRouting.IPv6RemoteSubnets) == 0 { + yggConfig.TunnelRouting.IPv6RemoteSubnets = make(map[string]string) + } + if len(yggConfig.SessionFirewall.WhitelistEncryptionPublicKeys) == 0 { + yggConfig.SessionFirewall.WhitelistEncryptionPublicKeys = []string{} + } + if len(yggConfig.SessionFirewall.BlacklistEncryptionPublicKeys) == 0 { + yggConfig.SessionFirewall.BlacklistEncryptionPublicKeys = []string{} + } + if len(yggConfig.NodeInfo) == 0 { + yggConfig.NodeInfo = make(map[string]interface{}) + } +} + +func SaveConfig(yggConfig config.NodeConfig, popConfig PopuraConfig, isjson bool) string { // combine config structs into one and marshal it // FIXME hjson comments are lost var combo map[string]interface{} - ybs, _ := json.Marshal(yggConfig) - pbs, _ := json.Marshal(popConfig) + correctEmptyValues(&yggConfig) + ybs, _ := json.Marshal(&yggConfig) + pbs, _ := json.Marshal(&popConfig) json.Unmarshal(ybs, &combo) json.Unmarshal(pbs, &combo)