Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var downCmd = &cobra.Command{
Use: "down",
Short: "down netbird connections",
RunE: func(cmd *cobra.Command, args []string) error {
SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
2 changes: 1 addition & 1 deletion client/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func isUnixRunningDesktop() bool {
}

func setEnvAndFlags(cmd *cobra.Command) error {
SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
3 changes: 2 additions & 1 deletion client/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/spf13/cobra"

"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/util"
)

var logoutCmd = &cobra.Command{
Use: "logout",
Short: "logout from the Netbird Management Service and delete peer",
RunE: func(cmd *cobra.Command, args []string) error {
SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
4 changes: 2 additions & 2 deletions client/cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ var profileSelectCmd = &cobra.Command{
}

func setupCmd(cmd *cobra.Command) error {
SetFlagsFromEnvVars(rootCmd)
SetFlagsFromEnvVars(cmd)
util.SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(cmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
36 changes: 2 additions & 34 deletions client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/cenkalti/backoff/v4"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/netbirdio/netbird/client/internal/profilemanager"
"github.com/netbirdio/netbird/util"
)

const (
Expand Down Expand Up @@ -194,38 +194,6 @@ func SetupCloseHandler(ctx context.Context, cancel context.CancelFunc) {
}()
}

// SetFlagsFromEnvVars reads and updates flag values from environment variables with prefix WT_
func SetFlagsFromEnvVars(cmd *cobra.Command) {
flags := cmd.PersistentFlags()
flags.VisitAll(func(f *pflag.Flag) {
oldEnvVar := FlagNameToEnvVar(f.Name, "WT_")

if value, present := os.LookupEnv(oldEnvVar); present {
err := flags.Set(f.Name, value)
if err != nil {
log.Infof("unable to configure flag %s using variable %s, err: %v", f.Name, oldEnvVar, err)
}
}

newEnvVar := FlagNameToEnvVar(f.Name, "NB_")

if value, present := os.LookupEnv(newEnvVar); present {
err := flags.Set(f.Name, value)
if err != nil {
log.Infof("unable to configure flag %s using variable %s, err: %v", f.Name, newEnvVar, err)
}
}
})
}

// FlagNameToEnvVar converts flag name to environment var name adding a prefix,
// replacing dashes and making all uppercase (e.g. setup-keys is converted to NB_SETUP_KEYS according to the input prefix)
func FlagNameToEnvVar(cmdFlag string, prefix string) string {
parsed := strings.ReplaceAll(cmdFlag, "-", "_")
upper := strings.ToUpper(parsed)
return prefix + upper
}

// DialClientGRPCServer returns client connection to the daemon server.
func DialClientGRPCServer(ctx context.Context, addr string) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
Expand Down Expand Up @@ -382,7 +350,7 @@ func migrateToNetbird(oldPath, newPath string) bool {
}

func getClient(cmd *cobra.Command) (*grpc.ClientConn, error) {
SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)
cmd.SetOut(cmd.OutOrStdout())

conn, err := DialClientGRPCServer(cmd.Context(), daemonAddr)
Expand Down
3 changes: 2 additions & 1 deletion client/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"

"github.com/netbirdio/netbird/client/iface"
"github.com/netbirdio/netbird/util"
)

func TestInitCommands(t *testing.T) {
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestSetFlagsFromEnvVars(t *testing.T) {
Long: "test",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
SetFlagsFromEnvVars(cmd)
util.SetFlagsFromEnvVars(cmd)
},
}

Expand Down
4 changes: 2 additions & 2 deletions client/cmd/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (p *program) Stop(srv service.Service) error {

// Common setup for service control commands
func setupServiceControlCommand(cmd *cobra.Command, ctx context.Context, cancel context.CancelFunc) (service.Service, error) {
SetFlagsFromEnvVars(rootCmd)
SetFlagsFromEnvVars(serviceCmd)
util.SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(serviceCmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
4 changes: 2 additions & 2 deletions client/cmd/service_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var ErrGetServiceStatus = fmt.Errorf("failed to get service status")

// Common service command setup
func setupServiceCommand(cmd *cobra.Command) error {
SetFlagsFromEnvVars(rootCmd)
SetFlagsFromEnvVars(serviceCmd)
util.SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(serviceCmd)
cmd.SetOut(cmd.OutOrStdout())
return handleRebrand(cmd)
}
Expand Down
4 changes: 2 additions & 2 deletions client/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ var sshCmd = &cobra.Command{
},
Short: "connect to a remote SSH server",
RunE: func(cmd *cobra.Command, args []string) error {
SetFlagsFromEnvVars(rootCmd)
SetFlagsFromEnvVars(cmd)
util.SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(cmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
2 changes: 1 addition & 1 deletion client/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
}

func statusFunc(cmd *cobra.Command, args []string) error {
SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
4 changes: 2 additions & 2 deletions client/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func init() {
}

func upFunc(cmd *cobra.Command, args []string) error {
SetFlagsFromEnvVars(rootCmd)
SetFlagsFromEnvVars(cmd)
util.SetFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(cmd)

cmd.SetOut(cmd.OutOrStdout())

Expand Down
35 changes: 0 additions & 35 deletions relay/cmd/env.go

This file was deleted.

2 changes: 1 addition & 1 deletion relay/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cobraConfig.LogLevel, "log-level", "info", "log level")
rootCmd.PersistentFlags().StringVar(&cobraConfig.LogFile, "log-file", "console", "log file")

setFlagsFromEnvVars(rootCmd)
util.SetFlagsFromEnvVars(rootCmd)
}

func Execute() error {
Expand Down
35 changes: 0 additions & 35 deletions signal/cmd/env.go

This file was deleted.

2 changes: 1 addition & 1 deletion signal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,5 @@ func init() {
runCmd.Flags().StringVar(&signalLetsencryptDomain, "letsencrypt-domain", "", "a domain to issue Let's Encrypt certificate for. Enables TLS using Let's Encrypt. Will fetch and renew certificate, and run the server with TLS")
runCmd.Flags().StringVar(&signalCertFile, "cert-file", "", "Location of your SSL certificate. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect")
runCmd.Flags().StringVar(&signalCertKey, "cert-key", "", "Location of your SSL certificate private key. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect")
setFlagsFromEnvVars(runCmd)
util.SetFlagsFromEnvVars(runCmd)
}
56 changes: 56 additions & 0 deletions util/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package util

import (
"os"
"path"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// setFlagsFromEnvVars reads and updates flag values from environment variables with prefix WT_
func SetFlagsFromEnvVars(cmd *cobra.Command) {
// Fetch the credentials directory if it exists
credsDir, present := os.LookupEnv("CREDENTIALS_DIRECTORY")

flags := cmd.PersistentFlags()
flags.VisitAll(func(f *pflag.Flag) {
name := flagNameToUpper(f.Name)

// Try to get the value from the credential directory
if present {
data, e := os.ReadFile(path.Join(credsDir, name))

if e == nil {
err := flags.Set(f.Name, strings.TrimSuffix(string(data), "\n"))

if err != nil {
log.Infof("unable to configure flag %s using credential %s, err: %v", f.Name, name, err)
} else {
return
}
}
}

// Fallback to env variable, which is constructed by adding the required prefix
// E.g. SETUP_KEYS -> NB_SETUP_KEYS
envName := "NB_" + name

if value, varPresent := os.LookupEnv(envName); varPresent {
err := flags.Set(f.Name, value)

if err != nil {
log.Infof("unable to configure flag %s using variable %s, err: %v", f.Name, envName, err)
}
}
})
}

// flagNameToUpper converts a flag name to its corresponding base env name
// replacing dashes by underscores and making the result uppercase
// E.g. setup-keys -> SETUP_KEYS
func flagNameToUpper(cmdFlag string) string {
return strings.ToUpper(strings.ReplaceAll(cmdFlag, "-", "_"))
}