Skip to content

Commit 751c44e

Browse files
committed
Migrate root keys, fix device-session, fix password hash.
1 parent 06f5911 commit 751c44e

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

main.go

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/go-redis/redis/v8"
2121
"github.com/gofrs/uuid"
2222
"github.com/jmoiron/sqlx"
23+
"github.com/lib/pq"
2324
_ "github.com/lib/pq"
2425
"github.com/lib/pq/hstore"
2526
"github.com/spf13/cobra"
@@ -474,7 +475,7 @@ func migrateGateways() {
474475
intToUUID(asGateway.OrganizationID),
475476
asGateway.CreatedAt,
476477
asGateway.UpdatedAt,
477-
asGateway.LastPingSentAt,
478+
asGateway.LastSeenAt,
478479
asGateway.Name,
479480
asGateway.Description,
480481
asGateway.Latitude,
@@ -741,14 +742,17 @@ func migrateDevices() {
741742
panic(err)
742743
}
743744

745+
// Migrate device keys
746+
migrateDeviceKeys(asDEV.DevEUI)
747+
744748
// Migrate device queue
745749
migrateDeviceQueue(asDEV.DevEUI, asDEV.AppSKey)
746750

747751
// Migrate metrics
748752
migrateDeviceMetrics(dev.DevEUI)
749753

750754
// Device session
751-
ds, err := getDeviceSession(dev.DevEUI)
755+
ds, err := getDeviceSession(dev.DevEUI, asDEV.AppSKey)
752756
if err != nil {
753757
// Device-session not found error
754758
continue
@@ -764,6 +768,55 @@ func migrateDevices() {
764768
}
765769
}
766770

771+
func migrateDeviceKeys(devEUI []byte) {
772+
log.Println("Migrating device-keys")
773+
774+
type DeviceKeys struct {
775+
CreatedAt time.Time `db:"created_at"`
776+
UpdatedAt time.Time `db:"updated_at"`
777+
DevEUI []byte `db:"dev_eui"`
778+
NwkKey []byte `db:"nwk_key"`
779+
AppKey []byte `db:"app_key"`
780+
JoinNonce int `db:"join_nonce"`
781+
}
782+
deviceKeys := []DeviceKeys{}
783+
err := asDB.Select(&deviceKeys, "select * from device_keys where dev_eui = $1", devEUI)
784+
if err != nil {
785+
panic(err)
786+
}
787+
788+
devNonces := []int64{}
789+
err = nsDB.Select(&devNonces, "select dev_nonce from device_activation where dev_eui = $1", devEUI)
790+
if err != nil {
791+
panic(err)
792+
}
793+
794+
for _, dk := range deviceKeys {
795+
_, err = csDB.Exec(`
796+
insert into device_keys (
797+
dev_eui,
798+
created_at,
799+
updated_at,
800+
nwk_key,
801+
app_key,
802+
dev_nonces,
803+
join_nonce
804+
) values ($1, $2, $3, $4, $5, $6, $7)`,
805+
dk.DevEUI,
806+
dk.CreatedAt,
807+
dk.UpdatedAt,
808+
dk.NwkKey,
809+
dk.AppKey,
810+
pq.Int64Array(devNonces),
811+
dk.JoinNonce,
812+
)
813+
if err != nil {
814+
panic(err)
815+
}
816+
}
817+
818+
}
819+
767820
func migrateDeviceQueue(devEUI []byte, appSKeyB []byte) {
768821
log.Println("Migrating device-queue")
769822
var appSKey lorawan.AES128Key
@@ -845,7 +898,7 @@ func hstoreToJSON(h hstore.Hstore) string {
845898
return string(b)
846899
}
847900

848-
func getDeviceSession(devEUI []byte) (pbnew.DeviceSession, error) {
901+
func getDeviceSession(devEUI []byte, appSKey []byte) (pbnew.DeviceSession, error) {
849902
key := fmt.Sprintf("%slora:ns:device:%s", nsPrefix, hex.EncodeToString(devEUI))
850903
var dsOld pbold.DeviceSessionPB
851904

@@ -872,8 +925,8 @@ func getDeviceSession(devEUI []byte) (pbnew.DeviceSession, error) {
872925
SNwkSIntKey: dsOld.SNwkSIntKey,
873926
NwkSEncKey: dsOld.NwkSEncKey,
874927
AppSKey: &pbnew.KeyEnvelope{
875-
KekLabel: dsOld.GetAppSKeyEnvelope().GetKekLabel(),
876-
AesKey: dsOld.GetAppSKeyEnvelope().GetAesKey(),
928+
KekLabel: "",
929+
AesKey: appSKey,
877930
},
878931
FCntUp: dsOld.FCntUp,
879932
NFCntDown: dsOld.NFCntDown,
@@ -944,16 +997,16 @@ func getDeviceSession(devEUI []byte) (pbnew.DeviceSession, error) {
944997
}
945998

946999
func saveDeviceSession(ds pbnew.DeviceSession) {
947-
devAddrKey := fmt.Sprintf("%sdevaddr:%s", csPrefix, hex.EncodeToString(ds.DevAddr))
948-
devSessKey := fmt.Sprintf("%sdevice:%s:ds", csPrefix, hex.EncodeToString(ds.DevEui))
1000+
devAddrKey := fmt.Sprintf("%sdevaddr:{%s}", csPrefix, hex.EncodeToString(ds.DevAddr))
1001+
devSessKey := fmt.Sprintf("%sdevice:{%s}:ds", csPrefix, hex.EncodeToString(ds.DevEui))
9491002

9501003
b, err := proto.Marshal(&ds)
9511004
if err != nil {
9521005
panic(err)
9531006
}
9541007

9551008
pipe := csRedis.TxPipeline()
956-
pipe.SAdd(context.Background(), devAddrKey, hex.EncodeToString(ds.DevEui))
1009+
pipe.SAdd(context.Background(), devAddrKey, ds.DevEui)
9571010
pipe.PExpire(context.Background(), devAddrKey, time.Duration(csSessionTTL)*time.Hour*24)
9581011
if _, err := pipe.Exec(context.Background()); err != nil {
9591012
panic(err)
@@ -1005,7 +1058,7 @@ func getDeviceGateway(devEUI []byte) (pbnew.DeviceGatewayRxInfo, error) {
10051058
}
10061059

10071060
func saveDeviceGateway(devGW pbnew.DeviceGatewayRxInfo) {
1008-
key := fmt.Sprintf("%sdevice:%s:gwrx", nsPrefix, hex.EncodeToString(devGW.DevEui))
1061+
key := fmt.Sprintf("%sdevice:{%s}:gwrx", nsPrefix, hex.EncodeToString(devGW.DevEui))
10091062
b, err := proto.Marshal(&devGW)
10101063
if err != nil {
10111064
panic(err)
@@ -1132,5 +1185,5 @@ func migratePassword(s string) string {
11321185
panic("Invalid password hash " + s)
11331186
}
11341187

1135-
return fmt.Sprintf("$pbkdf2-%s$i=$%s,l=16$%s$%s", parts[1], parts[2], parts[3], parts[4])
1188+
return fmt.Sprintf("$pbkdf2-%s$i=%s,l=64$%s$%s", parts[1], parts[2], strings.TrimRight(parts[3], "="), strings.TrimRight(parts[4], "="))
11361189
}

0 commit comments

Comments
 (0)