From 9720ffadbd6efc4251e3bd71f766a27a67b3d4b8 Mon Sep 17 00:00:00 2001 From: Krishna Iyer Easwaran Date: Mon, 6 Nov 2023 18:35:39 +0100 Subject: [PATCH] all: Init flags without exposing values --- pkg/source/firefly/config.go | 65 +++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/pkg/source/firefly/config.go b/pkg/source/firefly/config.go index 6bd1138..8f7e801 100644 --- a/pkg/source/firefly/config.go +++ b/pkg/source/firefly/config.go @@ -48,40 +48,53 @@ type Config struct { // NewConfig returns a new Firefly configuration. func NewConfig() *Config { config := &Config{ - flags: &pflag.FlagSet{}, + flags: &pflag.FlagSet{}, + joinEUI: os.Getenv("JOIN_EUI"), + appID: os.Getenv("APP_ID"), + frequencyPlanID: os.Getenv("FREQUENCY_PLAN_ID"), + macVersion: os.Getenv("MAC_VERSION"), + invalidateKeys: (os.Getenv("INVALIDATE_KEYS") == "true"), + all: (os.Getenv("ALL") == "true"), + Config: client.Config{ + Host: os.Getenv("FIREFLY_HOST"), + CACertPath: os.Getenv("FIREFLY_CA_CERT_PATH"), + APIKey: os.Getenv("FIREFLY_API_KEY"), + UseHTTP: (os.Getenv("FIREFLY_USE_HTTP") == "true"), + }, } + 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", - os.Getenv("FIREFLY_API_KEY"), + "", "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", - (os.Getenv("INVALIDATE_KEYS") == "true"), + false, `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. @@ -90,11 +103,11 @@ where the devices are exported but they are still valid on the firefly server `) config.flags.BoolVar(&config.UseHTTP, "use-http", - (os.Getenv("FIREFLY_USE_HTTP") == "true"), + false, "(optional) Use HTTP instead of HTTPS for the Firefly API. Only for testing") config.flags.BoolVar(&config.all, "all", - (os.Getenv("ALL") == "true"), + false, "Export all devices that the API key has access to. This is only used by the application command") return config } @@ -102,21 +115,35 @@ where the devices are exported but they are still valid on the firefly server // Initialize the configuration. func (c *Config) Initialize(src source.Config) error { c.src = src - if c.appID == "" { + + if c.appID = os.Getenv("APP_ID"); c.appID == "" { return errNoAppID.New() } - if c.Host == "" { + if c.frequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.frequencyPlanID == "" { + return errNoFrequencyPlanID.New() + } + if c.joinEUI = os.Getenv("JOIN_EUI"); c.joinEUI == "" { + return errNoJoinEUI.New() + } + if invalidateKeys := os.Getenv("JOIN_EUI"); 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 c.APIKey == "" { + if c.APIKey = os.Getenv("FIREFLY_API_KEY"); c.APIKey == "" { return errNoAPIKey.New() } - if c.joinEUI == "" { - return errNoJoinEUI.New() - } - if c.frequencyPlanID == "" { - return errNoFrequencyPlanID.New() + c.CACertPath = os.Getenv("FIREFLY_CA_CERT_PATH") + if useHTTP := os.Getenv("FIREFLY_USE_HTTP"); useHTTP == "true" { + c.UseHTTP = true } + + c.macVersion = os.Getenv("MAC_VERSION") switch c.macVersion { case "1.0.0": c.derivedMacVersion = ttnpb.MACVersion_MAC_V1_0