From 69ccb7a55d9c09053e9fda4d1dd100bee923d1fb Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Wed, 13 Mar 2024 11:31:46 +0100 Subject: [PATCH 1/5] all: Override flags only if env is set --- pkg/source/chirpstack/config/config.go | 37 +++++++++++++++----- pkg/source/firefly/config.go | 47 +++++++++++++++++++------- pkg/source/wanesy/config.go | 15 ++++++-- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git a/pkg/source/chirpstack/config/config.go b/pkg/source/chirpstack/config/config.go index a38768f..a8bf56c 100644 --- a/pkg/source/chirpstack/config/config.go +++ b/pkg/source/chirpstack/config/config.go @@ -94,26 +94,47 @@ func New() *Config { func (c *Config) Initialize(src source.Config) error { c.src = src - if c.apiKey = os.Getenv("CHIRPSTACK_API_KEY"); c.apiKey == "" { + if apiKey := os.Getenv("CHIRPSTACK_API_KEY"); apiKey != "" { + c.apiKey = apiKey + } + if url := os.Getenv("CHIRPSTACK_API_URL"); url != "" { + c.url = url + } + if caCertPath := os.Getenv("CHIRPSTACK_CA_CERT_PATH"); caCertPath != "" { + c.caCertPath = caCertPath + } + if insecure := os.Getenv("CHIRPSTACK_INSECURE"); insecure != "" { + c.insecure = insecure == "true" + } + if exportVars := os.Getenv("EXPORT_VARS"); exportVars != "" { + c.ExportVars = exportVars == "true" + } + if exportSession := os.Getenv("EXPORT_SESSION"); exportSession != "" { + c.ExportSession = exportSession == "true" + } + if joinEUI := os.Getenv("JOIN_EUI"); joinEUI != "" { + c.joinEUI = joinEUI + } + if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { + c.FrequencyPlanID = frequencyPlanID + } + + if c.apiKey == "" { return errNoAPIToken.New() } - if c.url = os.Getenv("CHIRPSTACK_API_URL"); c.url == "" { + if c.url == "" { return errNoAPIURL.New() } - if c.FrequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.FrequencyPlanID == "" { + if c.FrequencyPlanID == "" { return errNoFrequencyPlan.New() } - if c.joinEUI = os.Getenv("JOIN_EUI"); c.joinEUI == "" { + if c.joinEUI == "" { return errNoJoinEUI.New() } c.JoinEUI = &types.EUI64{} if err := c.JoinEUI.UnmarshalText([]byte(c.joinEUI)); err != nil { return errInvalidJoinEUI.WithAttributes("join_eui", c.joinEUI) } - c.caCertPath = os.Getenv("CHIRPSTACK_CA_CERT_PATH") - c.insecure = os.Getenv("CHIRPSTACK_INSECURE") == "true" - c.ExportSession = os.Getenv("EXPORT_SESSION") == "true" - c.ExportVars = os.Getenv("EXPORT_VARS") == "true" err := c.dialGRPC( grpc.FailOnNonTempDialError(true), diff --git a/pkg/source/firefly/config.go b/pkg/source/firefly/config.go index 32edfd2..dda57dd 100644 --- a/pkg/source/firefly/config.go +++ b/pkg/source/firefly/config.go @@ -104,34 +104,55 @@ where the devices are exported but they are still valid on the firefly server func (c *Config) Initialize(src source.Config) error { c.src = src - if c.appID = os.Getenv("APP_ID"); c.appID == "" { - return errNoAppID.New() + if appID := os.Getenv("APP_ID"); appID == "" { + c.appID = appID } - if c.frequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.frequencyPlanID == "" { - return errNoFrequencyPlanID.New() + if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID == "" { + c.frequencyPlanID = frequencyPlanID } - if c.joinEUI = os.Getenv("JOIN_EUI"); c.joinEUI == "" { - return errNoJoinEUI.New() + if joinEUI := os.Getenv("JOIN_EUI"); joinEUI == "" { + c.joinEUI = joinEUI } - if invalidateKeys := os.Getenv("JOIN_EUI"); invalidateKeys == "true" { + if invalidateKeys := os.Getenv("INVALIDATE_KEYS"); invalidateKeys == "true" { c.invalidateKeys = true } if all := os.Getenv("ALL"); all == "true" { c.all = true } - if c.Host = os.Getenv("FIREFLY_HOST"); c.Host == "" { - return errNoHost.New() + if host := os.Getenv("FIREFLY_HOST"); host == "" { + c.Host = host } - if c.APIKey = os.Getenv("FIREFLY_API_KEY"); c.APIKey == "" { - return errNoAPIKey.New() + if apiKey := os.Getenv("FIREFLY_API_KEY"); apiKey == "" { + c.APIKey = apiKey + } + if caCertPath := os.Getenv("FIREFLY_CA_CERT_PATH"); caCertPath == "" { + c.CACertPath = caCertPath } - c.CACertPath = os.Getenv("FIREFLY_CA_CERT_PATH") if useHTTP := os.Getenv("FIREFLY_USE_HTTP"); useHTTP == "true" { c.UseHTTP = true } + if macVersion := os.Getenv("MAC_VERSION"); macVersion == "" { + c.macVersion = macVersion + } + + if c.appID == "" { + return errNoAppID.New() + } + if c.frequencyPlanID == "" { + return errNoFrequencyPlanID.New() + } + if c.joinEUI == "" { + return errNoJoinEUI.New() + } + + if c.Host == "" { + return errNoHost.New() + } + if c.APIKey == "" { + return errNoAPIKey.New() + } - c.macVersion = os.Getenv("MAC_VERSION") switch c.macVersion { case "1.0.0": c.derivedMacVersion = ttnpb.MACVersion_MAC_V1_0 diff --git a/pkg/source/wanesy/config.go b/pkg/source/wanesy/config.go index b649783..5c0e632 100644 --- a/pkg/source/wanesy/config.go +++ b/pkg/source/wanesy/config.go @@ -72,13 +72,22 @@ func NewConfig() *Config { func (c *Config) Initialize(src source.Config) error { c.src = src - if c.appID = os.Getenv("APP_ID"); c.appID == "" { + if appID := os.Getenv("APP_ID"); appID != "" { + c.appID = appID + } + if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { + c.frequencyPlanID = frequencyPlanID + } + if csvPath := os.Getenv("CSV_PATH"); csvPath != "" { + c.csvPath = csvPath + } + if c.appID == "" { return errNoAppID.New() } - if c.frequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.frequencyPlanID == "" { + if c.frequencyPlanID == "" { return errNoFrequencyPlanID.New() } - if c.csvPath = os.Getenv("CSV_PATH"); c.csvPath == "" { + if c.csvPath == "" { return errNoCSVFileProvided.New() } From dfff8ff0236c51fcdb799bbb814f9f63c499b28a Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Wed, 13 Mar 2024 11:48:59 +0100 Subject: [PATCH 2/5] dev: Don't initialize defaults from env --- pkg/source/tts/config/config.go | 95 ++++++++++++++++++++++++--------- pkg/source/tts/config/errors.go | 1 + pkg/source/tts/source.go | 5 +- pkg/source/tts/tts.go | 4 +- 4 files changed, 77 insertions(+), 28 deletions(-) diff --git a/pkg/source/tts/config/config.go b/pkg/source/tts/config/config.go index 18d1729..6cfc9b5 100644 --- a/pkg/source/tts/config/config.go +++ b/pkg/source/tts/config/config.go @@ -66,61 +66,61 @@ func (c *serverConfig) applyDefaults() { ) } -func New() (*Config, *pflag.FlagSet) { - var ( - config = &Config{ServerConfig: &serverConfig{}} - flags = &pflag.FlagSet{} - ) +func New() *Config { + config := &Config{ + ServerConfig: &serverConfig{}, + flags: &pflag.FlagSet{}, + } - flags.StringVar(&config.AppID, + config.flags.StringVar(&config.AppID, "app-id", - os.Getenv("TTS_APP_ID"), + "", "TTS Application ID") - flags.StringVar(&config.appAPIKey, + config.flags.StringVar(&config.appAPIKey, "app-api-key", - os.Getenv("TTS_APP_API_KEY"), + "", "TTS Application Access Key (with 'devices' permissions)") - flags.StringVar(&config.caPath, + config.flags.StringVar(&config.caPath, "ca-file", - os.Getenv("TTS_CA_FILE"), + "", "TTS Path to a CA file (optional)") - flags.BoolVar(&config.insecure, + config.flags.BoolVar(&config.insecure, "insecure", false, "TTS allow TCP connection") - flags.StringVar(&config.ServerConfig.defaultGRPCAddress, + config.flags.StringVar(&config.ServerConfig.defaultGRPCAddress, "default-grpc-address", - os.Getenv("TTS_DEFAULT_GRPC_ADDRESS"), + "", "TTS default GRPC Address (optional)") - flags.StringVar(&config.ServerConfig.ApplicationServerGRPCAddress, + config.flags.StringVar(&config.ServerConfig.ApplicationServerGRPCAddress, "application-server-grpc-address", - os.Getenv("TTS_APPLICATION_SERVER_GRPC_ADDRESS"), + "", "TTS Application Server GRPC Address") - flags.StringVar(&config.ServerConfig.IdentityServerGRPCAddress, + config.flags.StringVar(&config.ServerConfig.IdentityServerGRPCAddress, "identity-server-grpc-address", - os.Getenv("TTS_IDENTITY_SERVER_GRPC_ADDRESS"), + "", "TTS Identity Server GRPC Address") - flags.StringVar(&config.ServerConfig.JoinServerGRPCAddress, + config.flags.StringVar(&config.ServerConfig.JoinServerGRPCAddress, "join-server-grpc-address", - os.Getenv("TTS_JOIN_SERVER_GRPC_ADDRESS"), + "", "TTS Join Server GRPC Address") - flags.StringVar(&config.ServerConfig.NetworkServerGRPCAddress, + config.flags.StringVar(&config.ServerConfig.NetworkServerGRPCAddress, "network-server-grpc-address", - os.Getenv("TTS_NETWORK_SERVER_GRPC_ADDRESS"), + "", "TTS Network Server GRPC Address") - flags.BoolVar(&config.NoSession, + config.flags.BoolVar(&config.NoSession, "no-session", false, "TTS export devices without session") - flags.BoolVar(&config.DeleteSourceDevice, + config.flags.BoolVar(&config.DeleteSourceDevice, "delete-source-device", false, "TTS delete exported devices") - return config, flags + return config } type Config struct { @@ -135,11 +135,51 @@ type Config struct { NoSession bool DeleteSourceDevice bool AppID string + + flags *pflag.FlagSet } func (c *Config) Initialize(rootConfig source.Config) error { c.Config = rootConfig + if appID := os.Getenv("TTS_APP_ID"); appID != "" { + c.AppID = appID + } + if appAPIKey := os.Getenv("TTS_APP_API_KEY"); appAPIKey != "" { + c.appAPIKey = appAPIKey + } + if caPath := os.Getenv("TTS_CA_FILE"); caPath != "" { + c.caPath = caPath + } + if insecure := os.Getenv("TTS_INSECURE"); insecure == "true" { + c.insecure = true + } + if noSession := os.Getenv("TTS_NO_SESSION"); noSession == "true" { + c.NoSession = true + } + if deleteSourceDevice := os.Getenv("TTS_DELETE_SOURCE_DEVICE"); deleteSourceDevice == "true" { + c.DeleteSourceDevice = true + } + if defaultGRPCAddress := os.Getenv("TTS_DEFAULT_GRPC_ADDRESS"); defaultGRPCAddress != "" { + c.ServerConfig.defaultGRPCAddress = defaultGRPCAddress + } + if applicationServerGRPCAddress := os.Getenv("TTS_APPLICATION_SERVER_GRPC_ADDRESS"); applicationServerGRPCAddress != "" { + c.ServerConfig.ApplicationServerGRPCAddress = applicationServerGRPCAddress + } + if identityServerGRPCAddress := os.Getenv("TTS_IDENTITY_SERVER_GRPC_ADDRESS"); identityServerGRPCAddress != "" { + c.ServerConfig.IdentityServerGRPCAddress = identityServerGRPCAddress + } + if joinServerGRPCAddress := os.Getenv("TTS_JOIN_SERVER_GRPC_ADDRESS"); joinServerGRPCAddress != "" { + c.ServerConfig.JoinServerGRPCAddress = joinServerGRPCAddress + } + if networkServerGRPCAddress := os.Getenv("TTS_NETWORK_SERVER_GRPC_ADDRESS"); networkServerGRPCAddress != "" { + c.ServerConfig.NetworkServerGRPCAddress = networkServerGRPCAddress + } + + if c.AppID == "" { + return errNoAppID.New() + } + if c.appAPIKey == "" { return errNoAppAPIKey.New() } @@ -193,3 +233,8 @@ func setCustomCA(path string) error { } return nil } + +// Flags returns the flags for the configuration. +func (c *Config) Flags() *pflag.FlagSet { + return c.flags +} diff --git a/pkg/source/tts/config/errors.go b/pkg/source/tts/config/errors.go index 43f37ee..62f9765 100644 --- a/pkg/source/tts/config/errors.go +++ b/pkg/source/tts/config/errors.go @@ -17,6 +17,7 @@ package config import "go.thethings.network/lorawan-stack/v3/pkg/errors" var ( + errNoAppID = errors.DefineInvalidArgument("no_app_id", "no app id") errNoAppAPIKey = errors.DefineInvalidArgument("no_app_api_key", "no app api key") errNoIdentityServerGRPCAddress = errors.DefineInvalidArgument("no_identity_server_grpc_address", "no identity server grpc address") errNoJoinServerGRPCAddress = errors.DefineInvalidArgument("no_join_server_grpc_address", "no join server grpc address") diff --git a/pkg/source/tts/source.go b/pkg/source/tts/source.go index 83c19f2..56dd69e 100644 --- a/pkg/source/tts/source.go +++ b/pkg/source/tts/source.go @@ -34,10 +34,13 @@ type Source struct { func createNewSource(cfg *config.Config) source.CreateSource { return func(ctx context.Context, rootCfg source.Config) (source.Source, error) { + if err := cfg.Initialize(rootCfg); err != nil { + return nil, err + } return Source{ ctx: ctx, config: cfg, - }, cfg.Initialize(rootCfg) + }, nil } } diff --git a/pkg/source/tts/tts.go b/pkg/source/tts/tts.go index b6b756d..f24f79a 100644 --- a/pkg/source/tts/tts.go +++ b/pkg/source/tts/tts.go @@ -20,12 +20,12 @@ import ( ) func init() { - cfg, flags := config.New() + cfg := config.New() source.RegisterSource(source.Registration{ Name: "tts", Description: "Migrate from The Things Stack", - FlagSet: flags, + FlagSet: cfg.Flags(), Create: createNewSource(cfg), }) } From 63aa02ca6842cb243cc6ac24cf89c1b6964ee87b Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Wed, 13 Mar 2024 12:50:50 +0100 Subject: [PATCH 3/5] all: Use env only when set --- CHANGELOG.md | 2 + pkg/source/ttnv2/config.go | 100 +++++++++++++++++++++++++++---------- pkg/source/ttnv2/source.go | 5 +- pkg/source/ttnv2/ttnv2.go | 4 +- 4 files changed, 81 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f36307..1af248e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed +- Flags are always only loaded from the env. Now flags are loaded from env only if set. + ## [v0.11.2] (2024-03-04) ### Fixed diff --git a/pkg/source/ttnv2/config.go b/pkg/source/ttnv2/config.go index 53b9a36..9d4b7ea 100644 --- a/pkg/source/ttnv2/config.go +++ b/pkg/source/ttnv2/config.go @@ -35,62 +35,62 @@ const ( clientName = "ttn-lw-migrate" ) -func New() (*Config, *pflag.FlagSet) { - var ( - config = &Config{sdkConfig: ttnsdk.NewCommunityConfig(clientName)} - flags = &pflag.FlagSet{} - ) +func NewConfig() *Config { + config := &Config{ + sdkConfig: ttnsdk.NewCommunityConfig(clientName), + flags: &pflag.FlagSet{}, + } - flags.StringVar(&config.frequencyPlanID, + config.flags.StringVar(&config.frequencyPlanID, "frequency-plan-id", - os.Getenv("FREQUENCY_PLAN_ID"), + "", "Frequency Plan ID of exported devices") - flags.StringVar(&config.appID, + config.flags.StringVar(&config.appID, "app-id", - os.Getenv("TTNV2_APP_ID"), + "", "TTN Application ID") - flags.StringVar(&config.appAccessKey, + config.flags.StringVar(&config.appAccessKey, "app-access-key", - os.Getenv("TTNV2_APP_ACCESS_KEY"), + "", "TTN Application Access Key (with 'devices' permissions") - flags.StringVar(&config.caCert, + config.flags.StringVar(&config.caCert, "ca-cert", - os.Getenv("TTNV2_CA_CERT"), + "", "(only for private networks)") - flags.StringVar(&config.sdkConfig.HandlerAddress, + config.flags.StringVar(&config.sdkConfig.HandlerAddress, "handler-address", - os.Getenv("TTNV2_HANDLER_ADDRESS"), + "", "(only for private networks) Address for the Handler") - flags.StringVar(&config.sdkConfig.AccountServerAddress, + config.flags.StringVar(&config.sdkConfig.AccountServerAddress, "account-server-address", - os.Getenv("TTNV2_ACCOUNT_SERVER_ADDRESS"), + "", "(only for private networks) Address for the Account Server") - flags.StringVar(&config.sdkConfig.AccountServerClientID, + config.flags.StringVar(&config.sdkConfig.AccountServerClientID, "account-server-client-id", - os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_ID"), + "", "(only for private networks) Client ID for the Account Server") - flags.StringVar(&config.sdkConfig.AccountServerClientSecret, + config.flags.StringVar(&config.sdkConfig.AccountServerClientSecret, "account-server-client-secret", - os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET"), + "", "(only for private networks) Client secret for the Account Server") - flags.StringVar(&config.sdkConfig.DiscoveryServerAddress, + config.flags.StringVar(&config.sdkConfig.DiscoveryServerAddress, "discovery-server-address", - os.Getenv("TTNV2_DISCOVERY_SERVER_ADDRESS"), + "", "(only for private networks) Address for the Discovery Server") - flags.BoolVar(&config.sdkConfig.DiscoveryServerInsecure, + config.flags.BoolVar(&config.sdkConfig.DiscoveryServerInsecure, "discovery-server-insecure", false, "(only for private networks) Not recommended") - flags.BoolVar(&config.withSession, + config.flags.BoolVar(&config.withSession, "with-session", true, "Export device session keys and frame counters") - flags.BoolVar(&config.resetsToFrequencyPlan, + config.flags.BoolVar(&config.resetsToFrequencyPlan, "resets-to-frequency-plan", false, "Configure preset frequencies for ABP devices so that they match the used Frequency Plan") - return config, flags + return config } type Config struct { @@ -106,10 +106,48 @@ type Config struct { dryRun bool resetsToFrequencyPlan bool + flags *pflag.FlagSet fpStore *frequencyplans.Store } func (c *Config) Initialize(rootConfig source.Config) error { + if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { + c.frequencyPlanID = frequencyPlanID + } + if appID := os.Getenv("TTNV2_APP_ID"); appID != "" { + c.appID = appID + } + if appAccessKey := os.Getenv("TTNV2_APP_ACCESS_KEY"); appAccessKey != "" { + c.appAccessKey = appAccessKey + } + if caCert := os.Getenv("TTNV2_CA_CERT"); caCert != "" { + c.caCert = caCert + } + if handlerAddress := os.Getenv("TTNV2_HANDLER_ADDRESS"); handlerAddress != "" { + c.sdkConfig.HandlerAddress = handlerAddress + } + if accountServerAddress := os.Getenv("TTNV2_ACCOUNT_SERVER_ADDRESS"); accountServerAddress != "" { + c.sdkConfig.AccountServerAddress = accountServerAddress + } + if accountServerClientID := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_ID"); accountServerClientID != "" { + c.sdkConfig.AccountServerClientID = accountServerClientID + } + if accountServerClientSecret := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET"); accountServerClientSecret != "" { + c.sdkConfig.AccountServerClientSecret = accountServerClientSecret + } + if discoveryServerAddress := os.Getenv("TTNV2_DISCOVERY_SERVER_ADDRESS"); discoveryServerAddress != "" { + c.sdkConfig.DiscoveryServerAddress = discoveryServerAddress + } + if discoveryServerInsecure := os.Getenv("TTNV2_DISCOVERY_SERVER_INSECURE"); discoveryServerInsecure == "true" { + c.sdkConfig.DiscoveryServerInsecure = true + } + if withSession := os.Getenv("TTNV2_WITH_SESSION"); withSession == "true" { + c.withSession = true + } + if resetsToFrequencyPlan := os.Getenv("TTNV2_RESETS_TO_FREQUENCY_PLAN"); resetsToFrequencyPlan == "true" { + c.resetsToFrequencyPlan = true + } + if c.caCert != "" { if c.sdkConfig.TLSConfig == nil { c.sdkConfig.TLSConfig = new(tls.Config) @@ -128,6 +166,9 @@ func (c *Config) Initialize(rootConfig source.Config) error { rootCAs.AppendCertsFromPEM(pemBytes) } + if c.appID == "" { + return errNoAppID.New() + } if c.appAccessKey == "" { return errNoAppAccessKey.New() } @@ -155,3 +196,8 @@ func (c *Config) Initialize(rootConfig source.Config) error { return nil } + +// Flags returns the flags for the configuration. +func (c *Config) Flags() *pflag.FlagSet { + return c.flags +} diff --git a/pkg/source/ttnv2/source.go b/pkg/source/ttnv2/source.go index ebd666e..0f72376 100644 --- a/pkg/source/ttnv2/source.go +++ b/pkg/source/ttnv2/source.go @@ -50,6 +50,9 @@ func createNewSource(cfg *Config) source.CreateSource { // NewSource creates a new TTNv2 Source. func NewSource(ctx context.Context, cfg *Config, rootCfg source.Config) (source.Source, error) { + if err := cfg.Initialize(rootCfg); err != nil { + return nil, err + } s := &Source{ ctx: ctx, config: cfg, @@ -60,7 +63,7 @@ func NewSource(ctx context.Context, cfg *Config, rootCfg source.Config) (source. return nil, err } s.mgr = newDeviceManager(ctx, mgr) - return s, cfg.Initialize(rootCfg) + return s, nil } // ExportDevice implements the source.Source interface. diff --git a/pkg/source/ttnv2/ttnv2.go b/pkg/source/ttnv2/ttnv2.go index a5f4093..5be3624 100644 --- a/pkg/source/ttnv2/ttnv2.go +++ b/pkg/source/ttnv2/ttnv2.go @@ -17,12 +17,12 @@ package ttnv2 import "go.thethings.network/lorawan-stack-migrate/pkg/source" func init() { - cfg, flags := New() + cfg := NewConfig() source.RegisterSource(source.Registration{ Name: "ttnv2", Description: "Migrate from The Things Network Stack V2", - FlagSet: flags, + FlagSet: cfg.Flags(), Create: createNewSource(cfg), }) } From 79b86ec72c27ed36bf662f35fd956e25300fec43 Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Wed, 13 Mar 2024 14:41:04 +0100 Subject: [PATCH 4/5] all: Set defaults from env for non sensitive flags --- pkg/source/chirpstack/config/config.go | 38 ++++-------------- pkg/source/firefly/config.go | 50 +++++------------------- pkg/source/ttnv2/config.go | 52 ++++++------------------- pkg/source/tts/config/config.go | 53 ++++++-------------------- pkg/source/wanesy/config.go | 15 ++------ 5 files changed, 43 insertions(+), 165 deletions(-) diff --git a/pkg/source/chirpstack/config/config.go b/pkg/source/chirpstack/config/config.go index a8bf56c..5fbd478 100644 --- a/pkg/source/chirpstack/config/config.go +++ b/pkg/source/chirpstack/config/config.go @@ -57,7 +57,7 @@ func New() *Config { config.flags.StringVar(&config.url, "api-url", - "", + os.Getenv("CHIRPSTACK_API_URL"), "ChirpStack API URL") config.flags.StringVar(&config.apiKey, "api-key", @@ -65,27 +65,27 @@ func New() *Config { "ChirpStack API key") config.flags.StringVar(&config.caCertPath, "ca-cert-path", - "", + os.Getenv("CHIRPSTACK_CA_CERT_PATH"), "(optional) Path to the CA certificate file for ChirpStack API TLS connections") config.flags.BoolVar(&config.insecure, "insecure", - false, + os.Getenv("CHIRPSTACK_INSECURE") == "true", "Do not connect to ChirpStack over TLS") config.flags.BoolVar(&config.ExportVars, "export-vars", - false, + os.Getenv("EXPORT_VARS") == "true", "Export device variables from ChirpStack") config.flags.BoolVar(&config.ExportSession, "export-session", - false, + os.Getenv("EXPORT_SESSION") == "true", "Export device session keys from ChirpStack") config.flags.StringVar(&config.joinEUI, "join-eui", - "", + os.Getenv("JOIN_EUI"), "JoinEUI of exported devices") config.flags.StringVar(&config.FrequencyPlanID, "frequency-plan-id", - "", + os.Getenv("FREQUENCY_PLAN_ID"), "Frequency Plan ID of exported devices") return config @@ -94,31 +94,9 @@ func New() *Config { func (c *Config) Initialize(src source.Config) error { c.src = src - if apiKey := os.Getenv("CHIRPSTACK_API_KEY"); apiKey != "" { + if apiKey := os.Getenv("CHIRPSTACK_API_KEY"); apiKey != "" && c.apiKey == "" { c.apiKey = apiKey } - if url := os.Getenv("CHIRPSTACK_API_URL"); url != "" { - c.url = url - } - if caCertPath := os.Getenv("CHIRPSTACK_CA_CERT_PATH"); caCertPath != "" { - c.caCertPath = caCertPath - } - if insecure := os.Getenv("CHIRPSTACK_INSECURE"); insecure != "" { - c.insecure = insecure == "true" - } - if exportVars := os.Getenv("EXPORT_VARS"); exportVars != "" { - c.ExportVars = exportVars == "true" - } - if exportSession := os.Getenv("EXPORT_SESSION"); exportSession != "" { - c.ExportSession = exportSession == "true" - } - if joinEUI := os.Getenv("JOIN_EUI"); joinEUI != "" { - c.joinEUI = joinEUI - } - if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { - c.FrequencyPlanID = frequencyPlanID - } - if c.apiKey == "" { return errNoAPIToken.New() } diff --git a/pkg/source/firefly/config.go b/pkg/source/firefly/config.go index dda57dd..9c51370 100644 --- a/pkg/source/firefly/config.go +++ b/pkg/source/firefly/config.go @@ -53,11 +53,11 @@ func NewConfig() *Config { config.flags.StringVar(&config.Host, "host", - "", + os.Getenv("FIREFLY_HOST"), "Host of the Firefly API. Don't use the scheme (http/https). Port is optional") config.flags.StringVar(&config.CACertPath, "ca-cert-path", - "", + os.Getenv("FIREFLY_CA_CERT_PATH"), "(optional) Path to the CA certificate for the Firefly API") config.flags.StringVar(&config.APIKey, "api-key", @@ -65,24 +65,24 @@ func NewConfig() *Config { "Key to access the Firefly API") config.flags.StringVar(&config.joinEUI, "join-eui", - "", + os.Getenv("JOIN_EUI"), "JoinEUI for the exported devices") config.flags.StringVar(&config.frequencyPlanID, "frequency-plan-id", - "", + os.Getenv("FREQUENCY_PLAN_ID"), "Frequency Plan ID for the exported devices") config.flags.StringVar(&config.macVersion, "mac-version", - "", + os.Getenv("MAC_VERSION"), `LoRaWAN MAC version for the exported devices. Supported options are 1.0.0, 1.0.1, 1.0.2a, 1.0.2b, 1.0.3, 1.1.0a, 1.1.0b`) config.flags.StringVar(&config.appID, "app-id", - "", + os.Getenv("APP_ID"), "Application ID for the exported devices") config.flags.BoolVar(&config.invalidateKeys, "invalidate-keys", - false, + os.Getenv("INVALIDATE_KEYS") == "true", `Invalidate the root and/or session keys of the devices on the Firefly server. This is necessary to prevent both networks from communicating with the same device. The last byte of the keys will be incremented by 0x01. This enables an easy rollback if necessary. @@ -91,11 +91,11 @@ where the devices are exported but they are still valid on the firefly server `) config.flags.BoolVar(&config.UseHTTP, "use-http", - false, + os.Getenv("FIREFLY_USE_HTTP") == "true", "(optional) Use HTTP instead of HTTPS for the Firefly API. Only for testing") config.flags.BoolVar(&config.all, "all", - false, + os.Getenv("EXPORT_ALL") == "true", "Export all devices that the API key has access to. This is only used by the application command") return config } @@ -104,38 +104,9 @@ where the devices are exported but they are still valid on the firefly server func (c *Config) Initialize(src source.Config) error { c.src = src - if appID := os.Getenv("APP_ID"); appID == "" { - c.appID = appID - } - if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID == "" { - c.frequencyPlanID = frequencyPlanID - } - if joinEUI := os.Getenv("JOIN_EUI"); joinEUI == "" { - c.joinEUI = joinEUI - } - if invalidateKeys := os.Getenv("INVALIDATE_KEYS"); invalidateKeys == "true" { - c.invalidateKeys = true - } - if all := os.Getenv("ALL"); all == "true" { - c.all = true - } - - if host := os.Getenv("FIREFLY_HOST"); host == "" { - c.Host = host - } - if apiKey := os.Getenv("FIREFLY_API_KEY"); apiKey == "" { + if apiKey := os.Getenv("FIREFLY_API_KEY"); apiKey == "" && c.APIKey == "" { c.APIKey = apiKey } - if caCertPath := os.Getenv("FIREFLY_CA_CERT_PATH"); caCertPath == "" { - c.CACertPath = caCertPath - } - if useHTTP := os.Getenv("FIREFLY_USE_HTTP"); useHTTP == "true" { - c.UseHTTP = true - } - if macVersion := os.Getenv("MAC_VERSION"); macVersion == "" { - c.macVersion = macVersion - } - if c.appID == "" { return errNoAppID.New() } @@ -145,7 +116,6 @@ func (c *Config) Initialize(src source.Config) error { if c.joinEUI == "" { return errNoJoinEUI.New() } - if c.Host == "" { return errNoHost.New() } diff --git a/pkg/source/ttnv2/config.go b/pkg/source/ttnv2/config.go index 9d4b7ea..9e5d6e9 100644 --- a/pkg/source/ttnv2/config.go +++ b/pkg/source/ttnv2/config.go @@ -43,11 +43,11 @@ func NewConfig() *Config { config.flags.StringVar(&config.frequencyPlanID, "frequency-plan-id", - "", + os.Getenv("FREQUENCY_PLAN_ID"), "Frequency Plan ID of exported devices") config.flags.StringVar(&config.appID, "app-id", - "", + os.Getenv("TTNV2_APP_ID"), "TTN Application ID") config.flags.StringVar(&config.appAccessKey, "app-access-key", @@ -55,19 +55,19 @@ func NewConfig() *Config { "TTN Application Access Key (with 'devices' permissions") config.flags.StringVar(&config.caCert, "ca-cert", - "", + os.Getenv("TTNV2_CA_CERT"), "(only for private networks)") config.flags.StringVar(&config.sdkConfig.HandlerAddress, "handler-address", - "", + os.Getenv("TTNV2_HANDLER_ADDRESS"), "(only for private networks) Address for the Handler") config.flags.StringVar(&config.sdkConfig.AccountServerAddress, "account-server-address", - "", + os.Getenv("TTNV2_ACCOUNT_SERVER_ADDRESS"), "(only for private networks) Address for the Account Server") config.flags.StringVar(&config.sdkConfig.AccountServerClientID, "account-server-client-id", - "", + os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_ID"), "(only for private networks) Client ID for the Account Server") config.flags.StringVar(&config.sdkConfig.AccountServerClientSecret, "account-server-client-secret", @@ -75,19 +75,19 @@ func NewConfig() *Config { "(only for private networks) Client secret for the Account Server") config.flags.StringVar(&config.sdkConfig.DiscoveryServerAddress, "discovery-server-address", - "", + os.Getenv("TTNV2_DISCOVERY_SERVER_ADDRESS"), "(only for private networks) Address for the Discovery Server") config.flags.BoolVar(&config.sdkConfig.DiscoveryServerInsecure, "discovery-server-insecure", - false, + os.Getenv("TTNV2_DISCOVERY_SERVER_INSECURE") == "true", "(only for private networks) Not recommended") config.flags.BoolVar(&config.withSession, "with-session", - true, + os.Getenv("TTNV2_WITH_SESSION") == "true", "Export device session keys and frame counters") config.flags.BoolVar(&config.resetsToFrequencyPlan, "resets-to-frequency-plan", - false, + os.Getenv("TTNV2_RESETS_TO_FREQUENCY_PLAN") == "true", "Configure preset frequencies for ABP devices so that they match the used Frequency Plan") return config @@ -111,42 +111,12 @@ type Config struct { } func (c *Config) Initialize(rootConfig source.Config) error { - if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { - c.frequencyPlanID = frequencyPlanID - } - if appID := os.Getenv("TTNV2_APP_ID"); appID != "" { - c.appID = appID - } - if appAccessKey := os.Getenv("TTNV2_APP_ACCESS_KEY"); appAccessKey != "" { + if appAccessKey := os.Getenv("TTNV2_APP_ACCESS_KEY"); appAccessKey != "" && c.appAccessKey == "" { c.appAccessKey = appAccessKey } - if caCert := os.Getenv("TTNV2_CA_CERT"); caCert != "" { - c.caCert = caCert - } - if handlerAddress := os.Getenv("TTNV2_HANDLER_ADDRESS"); handlerAddress != "" { - c.sdkConfig.HandlerAddress = handlerAddress - } - if accountServerAddress := os.Getenv("TTNV2_ACCOUNT_SERVER_ADDRESS"); accountServerAddress != "" { - c.sdkConfig.AccountServerAddress = accountServerAddress - } - if accountServerClientID := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_ID"); accountServerClientID != "" { - c.sdkConfig.AccountServerClientID = accountServerClientID - } if accountServerClientSecret := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET"); accountServerClientSecret != "" { c.sdkConfig.AccountServerClientSecret = accountServerClientSecret } - if discoveryServerAddress := os.Getenv("TTNV2_DISCOVERY_SERVER_ADDRESS"); discoveryServerAddress != "" { - c.sdkConfig.DiscoveryServerAddress = discoveryServerAddress - } - if discoveryServerInsecure := os.Getenv("TTNV2_DISCOVERY_SERVER_INSECURE"); discoveryServerInsecure == "true" { - c.sdkConfig.DiscoveryServerInsecure = true - } - if withSession := os.Getenv("TTNV2_WITH_SESSION"); withSession == "true" { - c.withSession = true - } - if resetsToFrequencyPlan := os.Getenv("TTNV2_RESETS_TO_FREQUENCY_PLAN"); resetsToFrequencyPlan == "true" { - c.resetsToFrequencyPlan = true - } if c.caCert != "" { if c.sdkConfig.TLSConfig == nil { diff --git a/pkg/source/tts/config/config.go b/pkg/source/tts/config/config.go index 6cfc9b5..a2beb51 100644 --- a/pkg/source/tts/config/config.go +++ b/pkg/source/tts/config/config.go @@ -74,7 +74,7 @@ func New() *Config { config.flags.StringVar(&config.AppID, "app-id", - "", + os.Getenv("TTS_APP_ID"), "TTS Application ID") config.flags.StringVar(&config.appAPIKey, "app-api-key", @@ -83,41 +83,41 @@ func New() *Config { config.flags.StringVar(&config.caPath, "ca-file", - "", + os.Getenv("TTS_CA_FILE"), "TTS Path to a CA file (optional)") config.flags.BoolVar(&config.insecure, "insecure", - false, + os.Getenv("TTS_INSECURE") == "true", "TTS allow TCP connection") config.flags.StringVar(&config.ServerConfig.defaultGRPCAddress, "default-grpc-address", - "", + os.Getenv("TTS_DEFAULT_GRPC_ADDRESS"), "TTS default GRPC Address (optional)") config.flags.StringVar(&config.ServerConfig.ApplicationServerGRPCAddress, "application-server-grpc-address", - "", + os.Getenv("TTS_APPLICATION_SERVER_GRPC_ADDRESS"), "TTS Application Server GRPC Address") config.flags.StringVar(&config.ServerConfig.IdentityServerGRPCAddress, "identity-server-grpc-address", - "", + os.Getenv("TTS_IDENTITY_SERVER_GRPC_ADDRESS"), "TTS Identity Server GRPC Address") config.flags.StringVar(&config.ServerConfig.JoinServerGRPCAddress, "join-server-grpc-address", - "", + os.Getenv("TTS_JOIN_SERVER_GRPC_ADDRESS"), "TTS Join Server GRPC Address") config.flags.StringVar(&config.ServerConfig.NetworkServerGRPCAddress, "network-server-grpc-address", - "", + os.Getenv("TTS_NETWORK_SERVER_GRPC_ADDRESS"), "TTS Network Server GRPC Address") config.flags.BoolVar(&config.NoSession, "no-session", - false, + os.Getenv("TTS_NO_SESSION") == "true", "TTS export devices without session") config.flags.BoolVar(&config.DeleteSourceDevice, "delete-source-device", - false, + os.Getenv("TTS_DELETE_SOURCE_DEVICE") == "true", "TTS delete exported devices") return config @@ -142,40 +142,9 @@ type Config struct { func (c *Config) Initialize(rootConfig source.Config) error { c.Config = rootConfig - if appID := os.Getenv("TTS_APP_ID"); appID != "" { - c.AppID = appID - } - if appAPIKey := os.Getenv("TTS_APP_API_KEY"); appAPIKey != "" { + if appAPIKey := os.Getenv("TTS_APP_API_KEY"); appAPIKey != "" && c.appAPIKey == "" { c.appAPIKey = appAPIKey } - if caPath := os.Getenv("TTS_CA_FILE"); caPath != "" { - c.caPath = caPath - } - if insecure := os.Getenv("TTS_INSECURE"); insecure == "true" { - c.insecure = true - } - if noSession := os.Getenv("TTS_NO_SESSION"); noSession == "true" { - c.NoSession = true - } - if deleteSourceDevice := os.Getenv("TTS_DELETE_SOURCE_DEVICE"); deleteSourceDevice == "true" { - c.DeleteSourceDevice = true - } - if defaultGRPCAddress := os.Getenv("TTS_DEFAULT_GRPC_ADDRESS"); defaultGRPCAddress != "" { - c.ServerConfig.defaultGRPCAddress = defaultGRPCAddress - } - if applicationServerGRPCAddress := os.Getenv("TTS_APPLICATION_SERVER_GRPC_ADDRESS"); applicationServerGRPCAddress != "" { - c.ServerConfig.ApplicationServerGRPCAddress = applicationServerGRPCAddress - } - if identityServerGRPCAddress := os.Getenv("TTS_IDENTITY_SERVER_GRPC_ADDRESS"); identityServerGRPCAddress != "" { - c.ServerConfig.IdentityServerGRPCAddress = identityServerGRPCAddress - } - if joinServerGRPCAddress := os.Getenv("TTS_JOIN_SERVER_GRPC_ADDRESS"); joinServerGRPCAddress != "" { - c.ServerConfig.JoinServerGRPCAddress = joinServerGRPCAddress - } - if networkServerGRPCAddress := os.Getenv("TTS_NETWORK_SERVER_GRPC_ADDRESS"); networkServerGRPCAddress != "" { - c.ServerConfig.NetworkServerGRPCAddress = networkServerGRPCAddress - } - if c.AppID == "" { return errNoAppID.New() } diff --git a/pkg/source/wanesy/config.go b/pkg/source/wanesy/config.go index 5c0e632..92aaef6 100644 --- a/pkg/source/wanesy/config.go +++ b/pkg/source/wanesy/config.go @@ -51,15 +51,15 @@ func NewConfig() *Config { } config.flags.StringVar(&config.frequencyPlanID, "frequency-plan-id", - "", + os.Getenv("FREQUENCY_PLAN_ID"), "Frequency Plan ID for the exported devices") config.flags.StringVar(&config.appID, "app-id", - "", + os.Getenv("APP_ID"), "Application ID for the exported devices") config.flags.StringVar(&config.csvPath, "csv-path", - "", + os.Getenv("CSV_PATH"), "Path to the CSV file exported from Wanesy Management Center") config.flags.BoolVar(&config.all, "all", @@ -72,15 +72,6 @@ func NewConfig() *Config { func (c *Config) Initialize(src source.Config) error { c.src = src - if appID := os.Getenv("APP_ID"); appID != "" { - c.appID = appID - } - if frequencyPlanID := os.Getenv("FREQUENCY_PLAN_ID"); frequencyPlanID != "" { - c.frequencyPlanID = frequencyPlanID - } - if csvPath := os.Getenv("CSV_PATH"); csvPath != "" { - c.csvPath = csvPath - } if c.appID == "" { return errNoAppID.New() } From 72d0a609ad4af18466fae8b9c77e9d0fd4d2a654 Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Wed, 13 Mar 2024 14:50:16 +0100 Subject: [PATCH 5/5] dev: Fix flag conditions --- pkg/source/firefly/config.go | 2 +- pkg/source/ttnv2/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/source/firefly/config.go b/pkg/source/firefly/config.go index 9c51370..1f759f2 100644 --- a/pkg/source/firefly/config.go +++ b/pkg/source/firefly/config.go @@ -104,7 +104,7 @@ where the devices are exported but they are still valid on the firefly server func (c *Config) Initialize(src source.Config) error { c.src = src - if apiKey := os.Getenv("FIREFLY_API_KEY"); apiKey == "" && c.APIKey == "" { + if apiKey := os.Getenv("FIREFLY_API_KEY"); apiKey != "" && c.APIKey == "" { c.APIKey = apiKey } if c.appID == "" { diff --git a/pkg/source/ttnv2/config.go b/pkg/source/ttnv2/config.go index 9e5d6e9..33d6d93 100644 --- a/pkg/source/ttnv2/config.go +++ b/pkg/source/ttnv2/config.go @@ -114,7 +114,7 @@ func (c *Config) Initialize(rootConfig source.Config) error { if appAccessKey := os.Getenv("TTNV2_APP_ACCESS_KEY"); appAccessKey != "" && c.appAccessKey == "" { c.appAccessKey = appAccessKey } - if accountServerClientSecret := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET"); accountServerClientSecret != "" { + if accountServerClientSecret := os.Getenv("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET"); accountServerClientSecret != "" && c.sdkConfig.AccountServerClientSecret == "" { c.sdkConfig.AccountServerClientSecret = accountServerClientSecret }