[client]: Add config flag to service to override default profile path (#4276)

[client]: Add config flag to service to override default profile path (#4276)
This commit is contained in:
hakansa
2025-08-05 12:33:43 +03:00
committed by GitHub
parent 348d981b2c
commit 9ccc13e6ea
10 changed files with 36 additions and 14 deletions

View File

@@ -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().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().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().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(upCmd)
rootCmd.AddCommand(downCmd) rootCmd.AddCommand(downCmd)

View File

@@ -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 { if err := serverInstance.Start(); err != nil {
log.Fatalf("failed to start daemon: %v", err) log.Fatalf("failed to start daemon: %v", err)
} }

View File

@@ -41,6 +41,10 @@ func buildServiceArguments() []string {
args = append(args, "--management-url", managementURL) args = append(args, "--management-url", managementURL)
} }
if configPath != "" {
args = append(args, "--config", configPath)
}
for _, logFile := range logFiles { for _, logFile := range logFiles {
args = append(args, "--log-file", logFile) args = append(args, "--log-file", logFile)
} }

View File

@@ -134,7 +134,7 @@ func startClientDaemon(
s := grpc.NewServer() s := grpc.NewServer()
server := client.New(ctx, server := client.New(ctx,
"", false) "", "", false)
if err := server.Start(); err != nil { if err := server.Start(); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -79,7 +79,7 @@ func init() {
upCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc) upCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc)
upCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", profileNameDesc) 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 { 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) err := handleRebrand(cmd)
if err != nil { if err != nil {
return err 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 { 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=<file_path>\" or netbird service run --config=<file_path>\n")
}
customDNSAddressConverted, err := parseCustomDNSAddress(cmd.Flag(dnsResolverAddress).Changed) customDNSAddressConverted, err := parseCustomDNSAddress(cmd.Flag(dnsResolverAddress).Changed)
if err != nil { if err != nil {
return fmt.Errorf("parse custom DNS address: %v", err) return fmt.Errorf("parse custom DNS address: %v", err)

View File

@@ -558,7 +558,7 @@ func (g *BundleGenerator) addNetworkMap() error {
} }
func (g *BundleGenerator) addStateFile() error { func (g *BundleGenerator) addStateFile() error {
sm := profilemanager.ServiceManager{} sm := profilemanager.NewServiceManager("")
path := sm.GetStatePath() path := sm.GetStatePath()
if path == "" { if path == "" {
return nil return nil
@@ -597,7 +597,7 @@ func (g *BundleGenerator) addStateFile() error {
} }
func (g *BundleGenerator) addCorruptedStateFiles() error { func (g *BundleGenerator) addCorruptedStateFiles() error {
sm := profilemanager.ServiceManager{} sm := profilemanager.NewServiceManager("")
pattern := sm.GetStatePath() pattern := sm.GetStatePath()
if pattern == "" { if pattern == "" {
return nil return nil

View File

@@ -238,7 +238,7 @@ func NewEngine(
connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit), connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit),
} }
sm := profilemanager.ServiceManager{} sm := profilemanager.NewServiceManager("")
path := sm.GetStatePath() path := sm.GetStatePath()
if runtime.GOOS == "ios" { if runtime.GOOS == "ios" {

View File

@@ -75,7 +75,15 @@ func (a *ActiveProfileState) FilePath() (string, error) {
return filepath.Join(configDir, a.Name+".json"), nil 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) { func (s *ServiceManager) CopyDefaultProfileIfNotExists() (bool, error) {

View File

@@ -74,7 +74,7 @@ type Server struct {
persistNetworkMap bool persistNetworkMap bool
isSessionActive atomic.Bool isSessionActive atomic.Bool
profileManager profilemanager.ServiceManager profileManager *profilemanager.ServiceManager
profilesDisabled bool profilesDisabled bool
} }
@@ -86,13 +86,13 @@ type oauthAuthFlow struct {
} }
// New server instance constructor. // 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{ return &Server{
rootCtx: ctx, rootCtx: ctx,
logFile: logFile, logFile: logFile,
persistNetworkMap: true, persistNetworkMap: true,
statusRecorder: peer.NewRecorder(""), statusRecorder: peer.NewRecorder(""),
profileManager: profilemanager.ServiceManager{}, profileManager: profilemanager.NewServiceManager(configFile),
profilesDisabled: profilesDisabled, profilesDisabled: profilesDisabled,
} }
} }

View File

@@ -94,7 +94,7 @@ func TestConnectWithRetryRuns(t *testing.T) {
t.Fatalf("failed to set active profile state: %v", err) t.Fatalf("failed to set active profile state: %v", err)
} }
s := New(ctx, "debug", false) s := New(ctx, "debug", "", false)
s.config = config s.config = config
@@ -151,7 +151,7 @@ func TestServer_Up(t *testing.T) {
t.Fatalf("failed to set active profile state: %v", err) t.Fatalf("failed to set active profile state: %v", err)
} }
s := New(ctx, "console", false) s := New(ctx, "console", "", false)
err = s.Start() err = s.Start()
require.NoError(t, err) require.NoError(t, err)
@@ -227,7 +227,7 @@ func TestServer_SubcribeEvents(t *testing.T) {
t.Fatalf("failed to set active profile state: %v", err) t.Fatalf("failed to set active profile state: %v", err)
} }
s := New(ctx, "console", false) s := New(ctx, "console", "", false)
err = s.Start() err = s.Start()
require.NoError(t, err) require.NoError(t, err)