-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
552 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package awsiot | ||
|
||
import ( | ||
"go.thethings.network/lorawan-stack-migrate/pkg/source" | ||
"go.thethings.network/lorawan-stack-migrate/pkg/source/awsiot/config" | ||
) | ||
|
||
func init() { | ||
cfg := config.New() | ||
|
||
source.RegisterSource(source.Registration{ | ||
Name: "awsiot", | ||
Description: "Migrate from AWS IoT", | ||
FlagSet: cfg.Flags(), | ||
Create: createNewSource(cfg), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/iotwireless" | ||
"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" | ||
) | ||
|
||
type Config struct { | ||
source.Config | ||
|
||
Client *iotwireless.Client | ||
|
||
AppID string | ||
FrequencyPlanID string | ||
NoSession bool | ||
|
||
flags *pflag.FlagSet | ||
fpStore *frequencyplans.Store | ||
} | ||
|
||
func New() *Config { | ||
c := &Config{ | ||
flags: new(pflag.FlagSet), | ||
} | ||
|
||
c.flags.StringVar(&c.AppID, | ||
"app-id", | ||
os.Getenv("APP_ID"), | ||
"Application ID for the exported devices") | ||
c.flags.StringVar(&c.FrequencyPlanID, | ||
"frequency-plan-id", | ||
os.Getenv("FREQUENCY_PLAN_ID"), | ||
"Frequency Plan ID for the exported devices") | ||
c.flags.BoolVar(&c.NoSession, | ||
"no-session", | ||
os.Getenv("NO_SESSION") == "true", | ||
"TTS export devices without session") | ||
|
||
return c | ||
} | ||
|
||
func (c *Config) Initialize(rootCfg source.Config) error { | ||
c.Config = rootCfg | ||
|
||
cfg, err := config.LoadDefaultConfig(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
c.Client = iotwireless.NewFromConfig(cfg) | ||
|
||
fpFetcher, err := fetch.FromHTTP(http.DefaultClient, c.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) FPStore() *frequencyplans.Store { | ||
return c.fpStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package awsiot | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/iotwireless/types" | ||
"go.thethings.network/lorawan-stack-migrate/pkg/util" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
ttntypes "go.thethings.network/lorawan-stack/v3/pkg/types" | ||
) | ||
|
||
type Device struct{ *types.LoRaWANDevice } | ||
|
||
func (d Device) SetFields(dev *ttnpb.EndDevice, noSession bool) (err error) { | ||
if dev.Ids == nil { | ||
dev.Ids = &ttnpb.EndDeviceIdentifiers{} | ||
} | ||
|
||
eui, err := util.UnmarshalTextToBytes(&ttntypes.EUI64{}, aws.ToString(d.DevEui)) | ||
if err != nil { | ||
return err | ||
} | ||
dev.Ids.DevEui = eui | ||
|
||
if dev.Session == nil { | ||
dev.Session = &ttnpb.Session{} | ||
} | ||
if dev.Session.Keys == nil { | ||
dev.Session.Keys = &ttnpb.SessionKeys{} | ||
} | ||
|
||
if noSession { | ||
return nil | ||
} | ||
|
||
apb, otaa := d.sessionKeys() | ||
keys := dev.Session.Keys | ||
if err := unmarshalKeys([]struct { | ||
envelope *ttnpb.KeyEnvelope | ||
key *string | ||
}{ | ||
{keys.AppSKey, apb.appSKey}, | ||
{keys.NwkSEncKey, apb.nwkSKey}, | ||
{keys.FNwkSIntKey, apb.fNwkSIntKey}, | ||
{keys.SNwkSIntKey, apb.sNwkSIntKey}, | ||
{dev.RootKeys.AppKey, otaa.appKey}, | ||
{dev.RootKeys.NwkKey, otaa.nwkKey}, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
var b []byte | ||
if b, err = util.UnmarshalTextToBytes(&ttntypes.DevAddr{}, aws.ToString(apb.devAddr)); err != nil { | ||
return err | ||
} | ||
dev.Session.DevAddr = b | ||
if b, err = util.UnmarshalTextToBytes(&ttntypes.EUI64{}, aws.ToString(otaa.joinEUI)); err != nil { | ||
return err | ||
} | ||
dev.Ids.JoinEui = b | ||
|
||
return nil | ||
} | ||
|
||
type apbKeys struct { | ||
appSKey, fNwkSIntKey, nwkSKey, sNwkSIntKey, devAddr *string | ||
} | ||
|
||
type otaaKeys struct { | ||
joinEUI, appKey, nwkKey *string | ||
} | ||
|
||
func (d Device) sessionKeys() (apb apbKeys, otaa otaaKeys) { | ||
if v := d.AbpV1_0_x; v != nil { | ||
apb = apbKeys{ | ||
devAddr: v.DevAddr, | ||
appSKey: v.SessionKeys.AppSKey, | ||
nwkSKey: v.SessionKeys.NwkSKey, | ||
} | ||
} | ||
if v := d.AbpV1_1; v != nil { | ||
apb = apbKeys{ | ||
devAddr: v.DevAddr, | ||
appSKey: v.SessionKeys.AppSKey, | ||
fNwkSIntKey: v.SessionKeys.FNwkSIntKey, | ||
nwkSKey: v.SessionKeys.NwkSEncKey, | ||
sNwkSIntKey: v.SessionKeys.SNwkSIntKey, | ||
} | ||
} | ||
if o := d.OtaaV1_0_x; o != nil { | ||
otaa = otaaKeys{ | ||
appKey: o.AppKey, | ||
joinEUI: o.AppEui, | ||
} | ||
if eui := o.AppEui; eui != nil { | ||
otaa.joinEUI = eui | ||
} | ||
} | ||
if o := d.OtaaV1_1; o != nil { | ||
otaa = otaaKeys{ | ||
appKey: o.AppKey, | ||
joinEUI: o.JoinEui, | ||
nwkKey: o.NwkKey, | ||
} | ||
} | ||
return apb, otaa | ||
} | ||
|
||
func unmarshalKeys(data []struct { | ||
envelope *ttnpb.KeyEnvelope | ||
key *string | ||
}, | ||
) (err error) { | ||
for _, v := range data { | ||
if v.envelope == nil { | ||
v.envelope = &ttnpb.KeyEnvelope{} | ||
} | ||
b, err := util.UnmarshalTextToBytes(&ttntypes.AES128Key{}, aws.ToString(v.key)) | ||
if err != nil { | ||
return err | ||
} | ||
v.envelope.Key = b | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package awsiot | ||
|
||
import "go.thethings.network/lorawan-stack/v3/pkg/errors" | ||
|
||
var ( | ||
errNoValuesInCSV = errors.DefineInvalidArgument("no_values_in_csv", "no values in CSV file") | ||
errNoAppID = errors.DefineInvalidArgument("no_app_id", "no app id") | ||
errNoCSVFileProvided = errors.DefineInvalidArgument("no_csv_file_provided", "no csv file provided") | ||
errNoJoinEUI = errors.DefineInvalidArgument("no_join_eui", "no join eui") | ||
errNoDeviceFound = errors.DefineInvalidArgument("no_device_found", "no device with eui `{eui}` found") | ||
errNoFrequencyPlanID = errors.DefineInvalidArgument("no_frequency_plan_id", "no frequency plan ID") | ||
errInvalidMACVersion = errors.DefineInvalidArgument("invalid_mac_version", "invalid MAC version `{mac_version}`") | ||
errInvalidPHYForMACVersion = errors.DefineInvalidArgument("invalid_phy_for_mac_version", "invalid PHY version `{phy_version}` for MAC version `{mac_version}`") | ||
) |
Oops, something went wrong.