Skip to content
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

Fetch device from IS for Unclaim and GetClaimStatus RPCs #6325

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ and allows clients to claim end devices.
| Method Name | Method | Pattern | Body |
| ----------- | ------ | ------- | ---- |
| `Claim` | `POST` | `/api/v3/edcs/claim` | `*` |
| `Unclaim` | `DELETE` | `/api/v3/edcs/claim/{application_ids.application_id}/devices/{device_id}` | `*` |
| `Unclaim` | `DELETE` | `/api/v3/edcs/claim/{application_ids.application_id}/devices/{device_id}` | |
| `GetInfoByJoinEUI` | `POST` | `/api/v3/edcs/claim/info` | `*` |
| `GetClaimStatus` | `GET` | `/api/v3/edcs/claim/{application_ids.application_id}/devices/{device_id}` | |
| `AuthorizeApplication` | `POST` | `/api/v3/edcs/applications/{application_ids.application_id}/authorize` | `*` |
Expand Down
51 changes: 22 additions & 29 deletions api/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7974,35 +7974,28 @@
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "object",
"properties": {
"application_ids": {
"type": "object"
},
"dev_eui": {
"type": "string",
"format": "string",
"example": "70B3D57ED000ABCD",
"description": "The LoRaWAN DevEUI."
},
"join_eui": {
"type": "string",
"format": "string",
"example": "70B3D57ED000ABCD",
"description": "The LoRaWAN JoinEUI (AppEUI until LoRaWAN 1.0.3 end devices)."
},
"dev_addr": {
"type": "string",
"format": "string",
"example": "2600ABCD",
"description": "The LoRaWAN DevAddr."
}
}
}
"name": "dev_eui",
"description": "The LoRaWAN DevEUI.",
"in": "query",
"required": false,
"type": "string",
"format": "string"
},
{
"name": "join_eui",
"description": "The LoRaWAN JoinEUI (AppEUI until LoRaWAN 1.0.3 end devices).",
"in": "query",
"required": false,
"type": "string",
"format": "string"
},
{
"name": "dev_addr",
"description": "The LoRaWAN DevAddr.",
"in": "query",
"required": false,
"type": "string",
"format": "string"
}
],
"tags": [
Expand Down
1 change: 0 additions & 1 deletion api/deviceclaimingserver.proto
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ service EndDeviceClaimingServer {
rpc Unclaim(EndDeviceIdentifiers) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/edcs/claim/{application_ids.application_id}/devices/{device_id}",
body: "*"
};
};

Expand Down
5 changes: 1 addition & 4 deletions cypress/integration/console/devices/unclaim.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ describe('Device un-claiming', () => {
cy.findByRole('button', { name: /Unclaim and delete end device/ }).click()
})

cy.wait('@unclaim-request').its('request.body').should('deep.equal', {
KrishnaIyer marked this conversation as resolved.
Show resolved Hide resolved
dev_eui: ns.end_device.ids.dev_eui,
join_eui: '0000000000000000',
})
cy.wait('@unclaim-request').its('request.body').should('deep.equal', {})
KrishnaIyer marked this conversation as resolved.
Show resolved Hide resolved

cy.findByTestId('error-notification').should('not.exist')

Expand Down
63 changes: 46 additions & 17 deletions pkg/deviceclaimingserver/grpc_end_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
errParseQRCode = errors.Define("parse_qr_code", "parse QR code failed")
errQRCodeData = errors.DefineInvalidArgument("qr_code_data", "invalid QR code data")
errNoJoinEUI = errors.DefineInvalidArgument("no_join_eui", "failed to extract JoinEUI from request")
errNoEUI = errors.DefineInvalidArgument("no_eui", "DevEUI/JoinEUI not set for device")
errNoEUI = errors.DefineFailedPrecondition("no_eui", "DevEUI/JoinEUI not set for device")
errMethodUnavailable = errors.DefineUnimplemented("method_unavailable", "method unavailable")
errClaimingNotSupported = errors.DefineAborted(
"claiming_not_supported",
Expand Down Expand Up @@ -118,21 +118,21 @@ func (edcs *endDeviceClaimingServer) Unclaim(
ctx context.Context,
in *ttnpb.EndDeviceIdentifiers,
) (*emptypb.Empty, error) {
if in.DevEui == nil || in.JoinEui == nil {
return nil, errNoEUI.New()
}
if err := rights.RequireApplication(ctx, in.GetApplicationIds(),
ttnpb.Right_RIGHT_APPLICATION_DEVICES_WRITE,
); err != nil {
dev, err := edcs.getEndDevice(ctx, in, ttnpb.Right_RIGHT_APPLICATION_DEVICES_WRITE)
if err != nil {
return nil, err
}
ids := dev.GetIds()
if ids.JoinEui == nil || ids.DevEui == nil {
return nil, errNoEUI.New()
}

joinEUI := types.MustEUI64(in.JoinEui).OrZero()
joinEUI := types.MustEUI64(ids.JoinEui).OrZero()
claimer := edcs.DCS.endDeviceClaimingUpstream.JoinEUIClaimer(ctx, joinEUI)
if claimer == nil {
return nil, errClaimingNotSupported.WithAttributes("eui", joinEUI)
}
if err := claimer.Unclaim(ctx, in); err != nil {
if err := claimer.Unclaim(ctx, ids); err != nil {
return nil, err
}
return ttnpb.Empty, nil
Expand All @@ -156,18 +156,47 @@ func (edcs *endDeviceClaimingServer) GetClaimStatus(
ctx context.Context,
in *ttnpb.EndDeviceIdentifiers,
) (*ttnpb.GetClaimStatusResponse, error) {
if in.DevEui == nil || in.JoinEui == nil {
return nil, errNoEUI.New()
}
if err := rights.RequireApplication(ctx, in.GetApplicationIds(),
ttnpb.Right_RIGHT_APPLICATION_DEVICES_READ,
); err != nil {
dev, err := edcs.getEndDevice(ctx, in, ttnpb.Right_RIGHT_APPLICATION_DEVICES_READ)
if err != nil {
return nil, err
}
joinEUI := types.MustEUI64(in.JoinEui).OrZero()
ids := dev.GetIds()
if ids.JoinEui == nil || ids.DevEui == nil {
return nil, errNoEUI.New()
}

joinEUI := types.MustEUI64(ids.JoinEui).OrZero()
claimer := edcs.DCS.endDeviceClaimingUpstream.JoinEUIClaimer(ctx, joinEUI)
if claimer == nil {
return nil, errClaimingNotSupported.WithAttributes("eui", joinEUI)
}
return claimer.GetClaimStatus(ctx, in)
return claimer.GetClaimStatus(ctx, ids)
}

func (edcs *endDeviceClaimingServer) getEndDevice(
ctx context.Context,
ids *ttnpb.EndDeviceIdentifiers,
requiredRights ...ttnpb.Right,
) (*ttnpb.EndDevice, error) {
if err := rights.RequireApplication(ctx, ids.GetApplicationIds(),
requiredRights...,
); err != nil {
return nil, err
}
conn, err := edcs.DCS.GetPeerConn(ctx, ttnpb.ClusterRole_ENTITY_REGISTRY, nil)
if err != nil {
return nil, err
}
client, err := ttnpb.NewEndDeviceRegistryClient(conn), nil
if err != nil {
return nil, err
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, edcs.DCS.AllowInsecureForCredentials())
if err != nil {
return nil, err
}
return client.Get(ctx, &ttnpb.GetEndDeviceRequest{
EndDeviceIds: ids,
FieldMask: ttnpb.FieldMask("ids"),
}, callOpt)
}
73 changes: 63 additions & 10 deletions pkg/deviceclaimingserver/grpc_end_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ var (
registeredApplicationIDs = &ttnpb.ApplicationIdentifiers{
ApplicationId: "test-application",
}
registeredApplicationKey = "test-key"
registeredEndDeviceID = "test-end-device"
registeredJoinEUI = types.EUI64{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C}
unRegisteredJoinEUI = types.EUI64{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D}
registeredDevEUI = types.EUI64{0x00, 0x04, 0xA3, 0x0B, 0x00, 0x1C, 0x05, 0x30}
authenticationCode = "BEEF1234"
registeredApplicationKey = "test-key"
registeredEndDeviceID = "test-end-device"
registeredJoinEUI = types.EUI64{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C}
unRegisteredJoinEUI = types.EUI64{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D}
registeredDevEUI = types.EUI64{0x00, 0x04, 0xA3, 0x0B, 0x00, 0x1C, 0x05, 0x30}
deviceIDWithoutEUIs = "test-device-without-euis"
deviceIDClaimingNotSupported = "test-device-without-claiming-support"
authenticationCode = "BEEF1234"
)

func mustHavePeer(ctx context.Context, c *component.Component, role ttnpb.ClusterRole) {
Expand Down Expand Up @@ -141,6 +143,26 @@ func TestEndDeviceClaimingServer(t *testing.T) {
},
},
)
is.EndDeviceRegistry().Add(
ctx,
&ttnpb.EndDevice{
Ids: &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: deviceIDClaimingNotSupported,
JoinEui: unRegisteredJoinEUI.Bytes(),
DevEui: registeredDevEUI.Bytes(),
},
},
)
is.EndDeviceRegistry().Add(
ctx,
&ttnpb.EndDevice{
Ids: &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: deviceIDWithoutEUIs,
},
},
)

// GetInfoByJoinEUI.
resp, err := edcsClient.GetInfoByJoinEUI(ctx, &ttnpb.GetInfoByJoinEUIRequest{
Expand Down Expand Up @@ -225,6 +247,24 @@ func TestEndDeviceClaimingServer(t *testing.T) {
return errors.IsPermissionDenied(err)
},
},
{
Name: "WrongEUI",
Req: &ttnpb.ClaimEndDeviceRequest{
SourceDevice: &ttnpb.ClaimEndDeviceRequest_AuthenticatedIdentifiers_{
AuthenticatedIdentifiers: &ttnpb.ClaimEndDeviceRequest_AuthenticatedIdentifiers{
JoinEui: unRegisteredJoinEUI.Bytes(),
DevEui: registeredDevEUI.Bytes(),
AuthenticationCode: "TEST1234",
},
},
TargetApplicationIds: registeredApplicationIDs,
TargetDeviceId: "target-device",
},
CallOpts: authorizedCallOpt,
ErrorAssertion: func(err error) bool {
return errors.IsAborted(err)
},
},
{
Name: "ValidDevice",
Req: &ttnpb.ClaimEndDeviceRequest{
Expand Down Expand Up @@ -258,9 +298,16 @@ func TestEndDeviceClaimingServer(t *testing.T) {
// GetClaimStatus.
status, err := edcsClient.GetClaimStatus(ctx, &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: registeredEndDeviceID,
DeviceId: deviceIDWithoutEUIs,
}, authorizedCallOpt)
a.So(errors.IsInvalidArgument(err), should.BeTrue)
a.So(errors.IsFailedPrecondition(err), should.BeTrue)
a.So(status, should.BeNil)

status, err = edcsClient.GetClaimStatus(ctx, &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: deviceIDClaimingNotSupported,
}, authorizedCallOpt)
a.So(errors.IsAborted(err), should.BeTrue)
a.So(status, should.BeNil)

status, err = edcsClient.GetClaimStatus(ctx, &ttnpb.EndDeviceIdentifiers{
Expand All @@ -284,9 +331,15 @@ func TestEndDeviceClaimingServer(t *testing.T) {
// Unclaim.
_, err = edcsClient.Unclaim(ctx, &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: registeredEndDeviceID,
DeviceId: deviceIDWithoutEUIs,
}, authorizedCallOpt)
a.So(errors.IsFailedPrecondition(err), should.BeTrue)

_, err = edcsClient.Unclaim(ctx, &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
DeviceId: deviceIDClaimingNotSupported,
}, authorizedCallOpt)
a.So(errors.IsInvalidArgument(err), should.BeTrue)
a.So(errors.IsAborted(err), should.BeTrue)

_, err = edcsClient.Unclaim(ctx, &ttnpb.EndDeviceIdentifiers{
ApplicationIds: registeredApplicationIDs,
Expand Down
Loading