diff --git a/client/cmd/root.go b/client/cmd/root.go index 6a8ae27f4..86c76e6ab 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -127,7 +127,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&preSharedKey, preSharedKeyFlag, "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.") rootCmd.PersistentFlags().StringVarP(&hostName, "hostname", "n", "", "Sets a custom hostname for the device") rootCmd.PersistentFlags().BoolVarP(&anonymizeFlag, "anonymize", "A", false, "anonymize IP addresses and non-netbird.io domains in logs and status output") - rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "(DEPRECATED) Netbird config file location") + rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "Overrides the default profile file location") rootCmd.AddCommand(upCmd) rootCmd.AddCommand(downCmd) diff --git a/client/cmd/service_controller.go b/client/cmd/service_controller.go index 6dc6bca9b..14a41e607 100644 --- a/client/cmd/service_controller.go +++ b/client/cmd/service_controller.go @@ -61,7 +61,7 @@ func (p *program) Start(svc service.Service) error { } } - serverInstance := server.New(p.ctx, util.FindFirstLogPath(logFiles), profilesDisabled) + serverInstance := server.New(p.ctx, util.FindFirstLogPath(logFiles), configPath, profilesDisabled) if err := serverInstance.Start(); err != nil { log.Fatalf("failed to start daemon: %v", err) } diff --git a/client/cmd/service_installer.go b/client/cmd/service_installer.go index be8a897dc..ac22000bd 100644 --- a/client/cmd/service_installer.go +++ b/client/cmd/service_installer.go @@ -41,6 +41,10 @@ func buildServiceArguments() []string { args = append(args, "--management-url", managementURL) } + if configPath != "" { + args = append(args, "--config", configPath) + } + for _, logFile := range logFiles { args = append(args, "--log-file", logFile) } diff --git a/client/cmd/testutil_test.go b/client/cmd/testutil_test.go index 5dbc8cd7f..a6be84641 100644 --- a/client/cmd/testutil_test.go +++ b/client/cmd/testutil_test.go @@ -134,7 +134,7 @@ func startClientDaemon( s := grpc.NewServer() server := client.New(ctx, - "", false) + "", "", false) if err := server.Start(); err != nil { t.Fatal(err) } diff --git a/client/cmd/up.go b/client/cmd/up.go index a0c26a207..35ce77539 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -79,7 +79,7 @@ func init() { upCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc) upCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", profileNameDesc) - upCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "(DEPRECATED) Netbird config file location") + upCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "(DEPRECATED) Netbird config file location. ") } @@ -146,6 +146,11 @@ func upFunc(cmd *cobra.Command, args []string) error { } func runInForegroundMode(ctx context.Context, cmd *cobra.Command, activeProf *profilemanager.Profile) error { + // override the default profile filepath if provided + if configPath != "" { + _ = profilemanager.NewServiceManager(configPath) + } + err := handleRebrand(cmd) if err != nil { return err @@ -197,6 +202,11 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command, activeProf *pr } func runInDaemonMode(ctx context.Context, cmd *cobra.Command, pm *profilemanager.ProfileManager, activeProf *profilemanager.Profile, profileSwitched bool) error { + // Check if deprecated config flag is set and show warning + if cmd.Flag("config").Changed && configPath != "" { + cmd.PrintErrf("Warning: Config flag is deprecated on up command, it should be set as a service argument with $NB_CONFIG environment or with \"-config\" flag; netbird service reconfigure --service-env=\"NB_CONFIG=\" or netbird service run --config=\n") + } + customDNSAddressConverted, err := parseCustomDNSAddress(cmd.Flag(dnsResolverAddress).Changed) if err != nil { return fmt.Errorf("parse custom DNS address: %v", err) diff --git a/client/internal/debug/debug.go b/client/internal/debug/debug.go index 71ebf431d..c25c29d03 100644 --- a/client/internal/debug/debug.go +++ b/client/internal/debug/debug.go @@ -558,7 +558,7 @@ func (g *BundleGenerator) addNetworkMap() error { } func (g *BundleGenerator) addStateFile() error { - sm := profilemanager.ServiceManager{} + sm := profilemanager.NewServiceManager("") path := sm.GetStatePath() if path == "" { return nil @@ -597,7 +597,7 @@ func (g *BundleGenerator) addStateFile() error { } func (g *BundleGenerator) addCorruptedStateFiles() error { - sm := profilemanager.ServiceManager{} + sm := profilemanager.NewServiceManager("") pattern := sm.GetStatePath() if pattern == "" { return nil diff --git a/client/internal/engine.go b/client/internal/engine.go index 28d1bff53..f8d3c33bc 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -238,7 +238,7 @@ func NewEngine( connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit), } - sm := profilemanager.ServiceManager{} + sm := profilemanager.NewServiceManager("") path := sm.GetStatePath() if runtime.GOOS == "ios" { diff --git a/client/internal/profilemanager/service.go b/client/internal/profilemanager/service.go index 520eef2e9..faccf5f68 100644 --- a/client/internal/profilemanager/service.go +++ b/client/internal/profilemanager/service.go @@ -75,7 +75,15 @@ func (a *ActiveProfileState) FilePath() (string, error) { return filepath.Join(configDir, a.Name+".json"), nil } -type ServiceManager struct{} +type ServiceManager struct { +} + +func NewServiceManager(defaultConfigPath string) *ServiceManager { + if defaultConfigPath != "" { + DefaultConfigPath = defaultConfigPath + } + return &ServiceManager{} +} func (s *ServiceManager) CopyDefaultProfileIfNotExists() (bool, error) { diff --git a/client/server/server.go b/client/server/server.go index 7eb59c91a..b7c27ba5c 100644 --- a/client/server/server.go +++ b/client/server/server.go @@ -74,7 +74,7 @@ type Server struct { persistNetworkMap bool isSessionActive atomic.Bool - profileManager profilemanager.ServiceManager + profileManager *profilemanager.ServiceManager profilesDisabled bool } @@ -86,13 +86,13 @@ type oauthAuthFlow struct { } // New server instance constructor. -func New(ctx context.Context, logFile string, profilesDisabled bool) *Server { +func New(ctx context.Context, logFile string, configFile string, profilesDisabled bool) *Server { return &Server{ rootCtx: ctx, logFile: logFile, persistNetworkMap: true, statusRecorder: peer.NewRecorder(""), - profileManager: profilemanager.ServiceManager{}, + profileManager: profilemanager.NewServiceManager(configFile), profilesDisabled: profilesDisabled, } } diff --git a/client/server/server_test.go b/client/server/server_test.go index afd38b4a4..c0a3621ec 100644 --- a/client/server/server_test.go +++ b/client/server/server_test.go @@ -94,7 +94,7 @@ func TestConnectWithRetryRuns(t *testing.T) { t.Fatalf("failed to set active profile state: %v", err) } - s := New(ctx, "debug", false) + s := New(ctx, "debug", "", false) s.config = config @@ -151,7 +151,7 @@ func TestServer_Up(t *testing.T) { t.Fatalf("failed to set active profile state: %v", err) } - s := New(ctx, "console", false) + s := New(ctx, "console", "", false) err = s.Start() require.NoError(t, err) @@ -227,7 +227,7 @@ func TestServer_SubcribeEvents(t *testing.T) { t.Fatalf("failed to set active profile state: %v", err) } - s := New(ctx, "console", false) + s := New(ctx, "console", "", false) err = s.Start() require.NoError(t, err)