Skip to content

Commit

Permalink
all: Init flags without exposing values
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaIyer committed Nov 6, 2023
1 parent dabeba5 commit 9720ffa
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions pkg/source/firefly/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -90,33 +103,47 @@ 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
}

// 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
Expand Down

0 comments on commit 9720ffa

Please sign in to comment.