Skip to content

Commit 9ccc13e

Browse files
authored
[client]: Add config flag to service to override default profile path (#4276)
[client]: Add config flag to service to override default profile path (#4276)
1 parent 348d981 commit 9ccc13e

File tree

10 files changed

+36
-14
lines changed

10 files changed

+36
-14
lines changed

client/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func init() {
127127
rootCmd.PersistentFlags().StringVar(&preSharedKey, preSharedKeyFlag, "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.")
128128
rootCmd.PersistentFlags().StringVarP(&hostName, "hostname", "n", "", "Sets a custom hostname for the device")
129129
rootCmd.PersistentFlags().BoolVarP(&anonymizeFlag, "anonymize", "A", false, "anonymize IP addresses and non-netbird.io domains in logs and status output")
130-
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "(DEPRECATED) Netbird config file location")
130+
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "Overrides the default profile file location")
131131

132132
rootCmd.AddCommand(upCmd)
133133
rootCmd.AddCommand(downCmd)

client/cmd/service_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (p *program) Start(svc service.Service) error {
6161
}
6262
}
6363

64-
serverInstance := server.New(p.ctx, util.FindFirstLogPath(logFiles), profilesDisabled)
64+
serverInstance := server.New(p.ctx, util.FindFirstLogPath(logFiles), configPath, profilesDisabled)
6565
if err := serverInstance.Start(); err != nil {
6666
log.Fatalf("failed to start daemon: %v", err)
6767
}

client/cmd/service_installer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func buildServiceArguments() []string {
4141
args = append(args, "--management-url", managementURL)
4242
}
4343

44+
if configPath != "" {
45+
args = append(args, "--config", configPath)
46+
}
47+
4448
for _, logFile := range logFiles {
4549
args = append(args, "--log-file", logFile)
4650
}

client/cmd/testutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func startClientDaemon(
134134
s := grpc.NewServer()
135135

136136
server := client.New(ctx,
137-
"", false)
137+
"", "", false)
138138
if err := server.Start(); err != nil {
139139
t.Fatal(err)
140140
}

client/cmd/up.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func init() {
7979

8080
upCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc)
8181
upCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", profileNameDesc)
82-
upCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "(DEPRECATED) Netbird config file location")
82+
upCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "(DEPRECATED) Netbird config file location. ")
8383

8484
}
8585

@@ -146,6 +146,11 @@ func upFunc(cmd *cobra.Command, args []string) error {
146146
}
147147

148148
func runInForegroundMode(ctx context.Context, cmd *cobra.Command, activeProf *profilemanager.Profile) error {
149+
// override the default profile filepath if provided
150+
if configPath != "" {
151+
_ = profilemanager.NewServiceManager(configPath)
152+
}
153+
149154
err := handleRebrand(cmd)
150155
if err != nil {
151156
return err
@@ -197,6 +202,11 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command, activeProf *pr
197202
}
198203

199204
func runInDaemonMode(ctx context.Context, cmd *cobra.Command, pm *profilemanager.ProfileManager, activeProf *profilemanager.Profile, profileSwitched bool) error {
205+
// Check if deprecated config flag is set and show warning
206+
if cmd.Flag("config").Changed && configPath != "" {
207+
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")
208+
}
209+
200210
customDNSAddressConverted, err := parseCustomDNSAddress(cmd.Flag(dnsResolverAddress).Changed)
201211
if err != nil {
202212
return fmt.Errorf("parse custom DNS address: %v", err)

client/internal/debug/debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ func (g *BundleGenerator) addNetworkMap() error {
558558
}
559559

560560
func (g *BundleGenerator) addStateFile() error {
561-
sm := profilemanager.ServiceManager{}
561+
sm := profilemanager.NewServiceManager("")
562562
path := sm.GetStatePath()
563563
if path == "" {
564564
return nil
@@ -597,7 +597,7 @@ func (g *BundleGenerator) addStateFile() error {
597597
}
598598

599599
func (g *BundleGenerator) addCorruptedStateFiles() error {
600-
sm := profilemanager.ServiceManager{}
600+
sm := profilemanager.NewServiceManager("")
601601
pattern := sm.GetStatePath()
602602
if pattern == "" {
603603
return nil

client/internal/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func NewEngine(
238238
connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit),
239239
}
240240

241-
sm := profilemanager.ServiceManager{}
241+
sm := profilemanager.NewServiceManager("")
242242

243243
path := sm.GetStatePath()
244244
if runtime.GOOS == "ios" {

client/internal/profilemanager/service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ func (a *ActiveProfileState) FilePath() (string, error) {
7575
return filepath.Join(configDir, a.Name+".json"), nil
7676
}
7777

78-
type ServiceManager struct{}
78+
type ServiceManager struct {
79+
}
80+
81+
func NewServiceManager(defaultConfigPath string) *ServiceManager {
82+
if defaultConfigPath != "" {
83+
DefaultConfigPath = defaultConfigPath
84+
}
85+
return &ServiceManager{}
86+
}
7987

8088
func (s *ServiceManager) CopyDefaultProfileIfNotExists() (bool, error) {
8189

client/server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type Server struct {
7474
persistNetworkMap bool
7575
isSessionActive atomic.Bool
7676

77-
profileManager profilemanager.ServiceManager
77+
profileManager *profilemanager.ServiceManager
7878
profilesDisabled bool
7979
}
8080

@@ -86,13 +86,13 @@ type oauthAuthFlow struct {
8686
}
8787

8888
// New server instance constructor.
89-
func New(ctx context.Context, logFile string, profilesDisabled bool) *Server {
89+
func New(ctx context.Context, logFile string, configFile string, profilesDisabled bool) *Server {
9090
return &Server{
9191
rootCtx: ctx,
9292
logFile: logFile,
9393
persistNetworkMap: true,
9494
statusRecorder: peer.NewRecorder(""),
95-
profileManager: profilemanager.ServiceManager{},
95+
profileManager: profilemanager.NewServiceManager(configFile),
9696
profilesDisabled: profilesDisabled,
9797
}
9898
}

client/server/server_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestConnectWithRetryRuns(t *testing.T) {
9494
t.Fatalf("failed to set active profile state: %v", err)
9595
}
9696

97-
s := New(ctx, "debug", false)
97+
s := New(ctx, "debug", "", false)
9898

9999
s.config = config
100100

@@ -151,7 +151,7 @@ func TestServer_Up(t *testing.T) {
151151
t.Fatalf("failed to set active profile state: %v", err)
152152
}
153153

154-
s := New(ctx, "console", false)
154+
s := New(ctx, "console", "", false)
155155

156156
err = s.Start()
157157
require.NoError(t, err)
@@ -227,7 +227,7 @@ func TestServer_SubcribeEvents(t *testing.T) {
227227
t.Fatalf("failed to set active profile state: %v", err)
228228
}
229229

230-
s := New(ctx, "console", false)
230+
s := New(ctx, "console", "", false)
231231

232232
err = s.Start()
233233
require.NoError(t, err)

0 commit comments

Comments
 (0)