-
Notifications
You must be signed in to change notification settings - Fork 3
Support ChirpStack v4 #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8ee784c
0943e96
633458c
02ad46e
a5b51fe
6a6c1f7
d4f338a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ import ( | |
|
|
||
| "github.com/spf13/pflag" | ||
| "go.thethings.network/lorawan-stack-migrate/pkg/source" | ||
| "go.thethings.network/lorawan-stack/v3/pkg/fetch" | ||
| "go.thethings.network/lorawan-stack/v3/pkg/frequencyplans" | ||
| "go.thethings.network/lorawan-stack/v3/pkg/types" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials" | ||
|
|
@@ -32,96 +34,113 @@ import ( | |
|
|
||
| const dialTimeout = 10 * time.Second | ||
|
|
||
| func New() (*Config, *pflag.FlagSet) { | ||
| var ( | ||
| config = &Config{} | ||
| flags = &pflag.FlagSet{} | ||
| ) | ||
| type Config struct { | ||
| src source.Config | ||
|
|
||
| apiKey, caCertPath, url, joinEUI string | ||
| flags *pflag.FlagSet | ||
| FPStore *frequencyplans.Store | ||
| insecure bool | ||
|
|
||
| ClientConn *grpc.ClientConn | ||
|
|
||
| flags.StringVar(&config.url, | ||
| ExportVars, | ||
| ExportSession bool | ||
| FrequencyPlanID string | ||
| JoinEUI *types.EUI64 | ||
| } | ||
|
|
||
| func New() *Config { | ||
| config := &Config{ | ||
| flags: &pflag.FlagSet{}, | ||
| } | ||
|
|
||
| config.flags.StringVar(&config.url, | ||
| "api-url", | ||
| os.Getenv("CHIRPSTACK_API_URL"), | ||
| "", | ||
| "ChirpStack API URL") | ||
| flags.StringVar(&config.token, | ||
| "api-token", | ||
| os.Getenv("CHIRPSTACK_API_TOKEN"), | ||
| "ChirpStack API Token") | ||
| flags.StringVar(&config.caPath, | ||
| "api-ca", | ||
| os.Getenv("CHIRPSTACK_API_CA"), | ||
| "(optional) CA for TLS") | ||
| flags.BoolVar(&config.insecure, | ||
| "api-insecure", | ||
| os.Getenv("CHIRPSTACK_API_INSECURE") == "1", | ||
| config.flags.StringVar(&config.apiKey, | ||
| "api-key", | ||
| "", | ||
| "ChirpStack API key") | ||
| config.flags.StringVar(&config.caCertPath, | ||
| "ca-cert-path", | ||
| "", | ||
| "(optional) Path to the CA certificate file for ChirpStack API TLS connections") | ||
| config.flags.BoolVar(&config.insecure, | ||
| "insecure", | ||
| false, | ||
| "Do not connect to ChirpStack over TLS") | ||
| flags.BoolVar(&config.ExportVars, | ||
| config.flags.BoolVar(&config.ExportVars, | ||
| "export-vars", | ||
| false, | ||
| "Export device variables from ChirpStack") | ||
| flags.BoolVar(&config.ExportSession, | ||
| config.flags.BoolVar(&config.ExportSession, | ||
| "export-session", | ||
| true, | ||
| false, | ||
| "Export device session keys from ChirpStack") | ||
| flags.StringVar(&config.joinEUI, | ||
| config.flags.StringVar(&config.joinEUI, | ||
| "join-eui", | ||
| os.Getenv("JOIN_EUI"), | ||
| "", | ||
| "JoinEUI of exported devices") | ||
| flags.StringVar(&config.FrequencyPlanID, | ||
| config.flags.StringVar(&config.FrequencyPlanID, | ||
| "frequency-plan-id", | ||
| os.Getenv("FREQUENCY_PLAN_ID"), | ||
| "", | ||
| "Frequency Plan ID of exported devices") | ||
|
|
||
| return config, flags | ||
| return config | ||
| } | ||
|
|
||
| type Config struct { | ||
| source.Config | ||
|
|
||
| ClientConn *grpc.ClientConn | ||
| func (c *Config) Initialize(src source.Config) error { | ||
| c.src = src | ||
|
|
||
| token, caPath, url, | ||
| FrequencyPlanID string | ||
|
|
||
| joinEUI string | ||
| JoinEUI *types.EUI64 | ||
|
|
||
| insecure, | ||
| ExportVars, | ||
| ExportSession bool | ||
| } | ||
|
|
||
| func (c *Config) Initialize() error { | ||
| if c.token == "" { | ||
| if c.apiKey = os.Getenv("CHIRPSTACK_API_KEY"); c.apiKey == "" { | ||
| return errNoAPIToken.New() | ||
| } | ||
| if c.url == "" { | ||
| if c.url = os.Getenv("CHIRPSTACK_API_URL"); c.url == "" { | ||
| return errNoAPIURL.New() | ||
| } | ||
| if c.FrequencyPlanID == "" { | ||
| if c.FrequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.FrequencyPlanID == "" { | ||
| return errNoFrequencyPlan.New() | ||
| } | ||
|
|
||
| if c.joinEUI = os.Getenv("JOIN_EUI"); 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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this always override the CLI flags ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed. We do this for all the other sources the same currently. I think we should fix this separately for all the sources.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see this fixed everywhere before we make a new release, as it is very broken. The change itself comes from #97 (comment) , but the idea is that only secrets shouldn't be initialized as flag defaults - the rest of the setup does not need this.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me. I'll file a follow up before we make a release. |
||
| 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), | ||
| grpc.WithBlock(), | ||
| grpc.WithPerRPCCredentials(token(c.token)), | ||
| grpc.WithPerRPCCredentials(token(c.apiKey)), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fpFetcher, err := fetch.FromHTTP(http.DefaultClient, src.FrequencyPlansURL) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.FPStore = frequencyplans.NewStore(fpFetcher) | ||
| return nil | ||
| } | ||
|
|
||
| // Flags returns the flags for the configuration. | ||
| func (c *Config) Flags() *pflag.FlagSet { | ||
| return c.flags | ||
| } | ||
|
|
||
| func (c *Config) dialGRPC(opts ...grpc.DialOption) error { | ||
| if c.insecure { | ||
| opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| } else { | ||
| tlsConfig, err := generateTLSConfig(c.caPath) | ||
| tlsConfig, err := generateTLSConfig(c.caCertPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a mistake in the current (released) changelog that's fixed.