Skip to content

Commit

Permalink
all: Set defaults from env for non sensitive flags
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaIyer committed Mar 13, 2024
1 parent 63aa02c commit 79b86ec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 165 deletions.
38 changes: 8 additions & 30 deletions pkg/source/chirpstack/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,35 @@ 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",
"",
"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
Expand All @@ -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()
}
Expand Down
50 changes: 10 additions & 40 deletions pkg/source/firefly/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ 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",
"",
"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.
Expand All @@ -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
}
Expand All @@ -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()
}
Expand All @@ -145,7 +116,6 @@ func (c *Config) Initialize(src source.Config) error {
if c.joinEUI == "" {
return errNoJoinEUI.New()
}

if c.Host == "" {
return errNoHost.New()
}
Expand Down
52 changes: 11 additions & 41 deletions pkg/source/ttnv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,51 @@ 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",
"",
"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",
"",
"(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
Expand All @@ -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 {
Expand Down
53 changes: 11 additions & 42 deletions pkg/source/tts/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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()
}
Expand Down
Loading

0 comments on commit 79b86ec

Please sign in to comment.