Skip to content

Commit

Permalink
Add DevNonce Clear Functionality (#660)
Browse files Browse the repository at this point in the history
* Add DevNonce Clear Functionality

Details:
- DevNonce clear functionality helps to remove older device
activation records from device_activation table in NS.
- Using this method we can clear older or already generated device activation
records from database to prevent "DevNonce already exits" error in OTAA method

*Note: Server keeps latest 20 records in device_activation table of NS database
for manage recent device activation.

Signed-off-by: SAGAR PATEL <sagar.a.patel@slscorp.com>

* Update chirpstack-api.

Co-authored-by: Orne Brocaar <info@brocaar.com>
  • Loading branch information
sagar-patel-sls and brocaar authored Feb 8, 2022
1 parent b911e62 commit ce8c8d8
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 150 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/NickBall/go-aes-key-wrap v0.0.0-20170929221519-1c3aa3e4dfc5
github.com/aws/aws-sdk-go v1.26.3
github.com/brocaar/chirpstack-api/go/v3 v3.12.4
github.com/brocaar/chirpstack-api/go/v3 v3.12.5
github.com/brocaar/lorawan v0.0.0-20211122090658-49524ce5fb5b
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/eclipse/paho.mqtt.golang v1.3.1
Expand Down Expand Up @@ -53,5 +53,3 @@ require (
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
)

// replace github.com/brocaar/chirpstack-api/go/v3 => ../chirpstack-api/go
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwj
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2 h1:oMCHnXa6CCCafdPDbMh/lWRhRByN0VFLvv+g+ayx1SI=
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/brocaar/chirpstack-api/go/v3 v3.12.4 h1:D0/TJrtckPByXUB399ZQ0wUsp9mB+LNI5AxQp8DWYj0=
github.com/brocaar/chirpstack-api/go/v3 v3.12.4/go.mod h1:v8AWP19nOJK4rwJsr1+weDfpUc4UNLbRh8Eygn4Oh00=
github.com/brocaar/chirpstack-api/go/v3 v3.12.5 h1:sLV+zSZLUPnNCo2mf+gsw0ektbSiSHDvDn+RGs3ucgA=
github.com/brocaar/chirpstack-api/go/v3 v3.12.5/go.mod h1:v8AWP19nOJK4rwJsr1+weDfpUc4UNLbRh8Eygn4Oh00=
github.com/brocaar/lorawan v0.0.0-20211122090658-49524ce5fb5b h1:5cxv6403vkw54cjJMt2hVO5AZ8TdjD7emEWwJD++mYY=
github.com/brocaar/lorawan v0.0.0-20211122090658-49524ce5fb5b/go.mod h1:Vlf3gOwizqX4y3snWe/i2EqRT83HvYuwBjRu39PevW0=
github.com/caarlos0/ctrlc v1.0.0 h1:2DtF8GSIcajgffDFJzyG15vO+1PuBWOMUdFut7NnXhw=
Expand Down
36 changes: 36 additions & 0 deletions internal/api/external/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,42 @@ func (a *DeviceAPI) GetStats(ctx context.Context, req *pb.GetDeviceStatsRequest)
}, nil
}

// ClearDeviceNonces deletes the device older activation records for the given DevEUI.
// TODO: These are clear older DevNonce records from device activation records in Network Server
// TODO: These clears all DevNonce records but keeps latest 20 records for maintain device activation status
func (a *DeviceAPI) ClearDeviceNonces(ctx context.Context, req *pb.ClearDeviceNoncesRequest) (*empty.Empty, error) {
var eui lorawan.EUI64
if err := eui.UnmarshalText([]byte(req.DevEui)); err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, err.Error())
}

if err := a.validator.Validate(ctx,
auth.ValidateNodeAccess(eui, auth.Update)); err != nil {
return nil, grpc.Errorf(codes.Unauthenticated, "authentication failed: %s", err)
}

d, err := storage.GetDevice(ctx, storage.DB(), eui, false, true)
if err != nil {
return nil, helpers.ErrToRPCError(err)
}

n, err := storage.GetNetworkServerForDevEUI(ctx, storage.DB(), d.DevEUI)
if err != nil {
return nil, helpers.ErrToRPCError(err)
}

nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return nil, helpers.ErrToRPCError(err)
}

_, _ = nsClient.ClearDeviceNonces(ctx, &ns.ClearDeviceNoncesRequest{
DevEui: d.DevEUI[:],
})

return &empty.Empty{}, nil
}

func (a *DeviceAPI) returnList(count int, devices []storage.DeviceListItem) (*pb.ListDeviceResponse, error) {
resp := pb.ListDeviceResponse{
TotalCount: int64(count),
Expand Down
10 changes: 10 additions & 0 deletions internal/backend/networkserver/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type Client struct {
GetVersionResponse ns.GetVersionResponse

GetADRAlgorithmsResponse ns.GetADRAlgorithmsResponse

ClearDeviceNoncesChan chan ns.ClearDeviceNoncesRequest
ClearDeviceNoncesResponse empty.Empty
}

// NewClient creates a new Client.
Expand Down Expand Up @@ -199,6 +202,7 @@ func NewClient() *Client {
FlushMulticastQueueForMulticastGroupChan: make(chan ns.FlushMulticastQueueForMulticastGroupRequest, 100),
GetMulticastQueueItemsForMulticastGroupChan: make(chan ns.GetMulticastQueueItemsForMulticastGroupRequest, 100),
GenerateGatewayClientCertificateChan: make(chan ns.GenerateGatewayClientCertificateRequest, 100),
ClearDeviceNoncesChan: make(chan ns.ClearDeviceNoncesRequest, 100),
}
}

Expand Down Expand Up @@ -491,3 +495,9 @@ func (n *Client) GenerateGatewayClientCertificate(ctx context.Context, in *ns.Ge
func (n *Client) GetADRAlgorithms(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*ns.GetADRAlgorithmsResponse, error) {
return &n.GetADRAlgorithmsResponse, nil
}

// ClearDeviceNonces method.
func (n *Client) ClearDeviceNonces(ctx context.Context, in *ns.ClearDeviceNoncesRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
n.ClearDeviceNoncesChan <- *in
return &n.ClearDeviceNoncesResponse, nil
}
Loading

0 comments on commit ce8c8d8

Please sign in to comment.