From c815ad86fd7ccf2804995f8c956540f76c188535 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Thu, 18 Jul 2024 18:06:09 +0200 Subject: [PATCH] Fix macOS DNS unclean shutdown restore call on startup (#2286) previously, we called the restore method from the startup when there was an unclean shutdown. But it never had the state keys to clean since they are stored in memory this change addresses the issue by falling back to default values when restoring the host's DNS --- client/internal/dns/host_darwin.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/client/internal/dns/host_darwin.go b/client/internal/dns/host_darwin.go index 70d2443ef..b5f6f9295 100644 --- a/client/internal/dns/host_darwin.go +++ b/client/internal/dns/host_darwin.go @@ -110,19 +110,17 @@ func (s *systemConfigurator) applyDNSConfig(config HostDNSConfig) error { } func (s *systemConfigurator) restoreHostDNS() error { - lines := "" - for key := range s.createdKeys { - lines += buildRemoveKeyOperation(key) + keys := s.getRemovableKeysWithDefaults() + for _, key := range keys { keyType := "search" if strings.Contains(key, matchSuffix) { keyType = "match" } log.Infof("removing %s domains from system", keyType) - } - _, err := runSystemConfigCommand(wrapCommand(lines)) - if err != nil { - log.Errorf("got an error while cleaning the system configuration: %s", err) - return fmt.Errorf("clean system: %w", err) + err := s.removeKeyFromSystemConfig(key) + if err != nil { + log.Errorf("failed to remove %s domains from system: %s", keyType, err) + } } if err := removeUncleanShutdownIndicator(); err != nil { @@ -132,6 +130,19 @@ func (s *systemConfigurator) restoreHostDNS() error { return nil } +func (s *systemConfigurator) getRemovableKeysWithDefaults() []string { + if len(s.createdKeys) == 0 { + // return defaults for startup calls + return []string{getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix), getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix)} + } + + keys := make([]string, 0, len(s.createdKeys)) + for key := range s.createdKeys { + keys = append(keys, key) + } + return keys +} + func (s *systemConfigurator) removeKeyFromSystemConfig(key string) error { line := buildRemoveKeyOperation(key) _, err := runSystemConfigCommand(wrapCommand(line))