Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/cli/broker.toit
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,13 @@ class Broker:
identity := {
"device_id": "$device.id",
"organization_id": "$device.organization-id",
"hardware_id": "$device.hardware-id",
// Retained while the current broker API persists the legacy state shape.
"hardware_id": "$device.id",
}
state := {
"identity": identity,
}
broker-connection_.notify-created
--hardware-id=device.hardware-id
--device-id=device.id
--state=state

Expand Down
2 changes: 1 addition & 1 deletion src/cli/brokers/broker.toit
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ interface BrokerCli implements Authenticatable:
state of the device; until it connects to the broker there is
(probably) only identity information in it.
*/
notify-created --hardware-id/Uuid --device-id/Uuid --state/Map -> none
notify-created --device-id/Uuid --state/Map -> none

/**
Fetches all events of the given $types for all devices in the $device-ids list.
Expand Down
13 changes: 4 additions & 9 deletions src/cli/brokers/http/base.toit
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ class BrokerCliHttp implements BrokerCli:
"path": "/toit-artemis-assets/$scope/firmware/$id",
}

notify-created --hardware-id/Uuid --device-id/Uuid --state/Map -> none:
// hardware-id is only used by overrides (e.g., the Supabase variant
// writing into the auth-side devices table). The on-the-wire
// notify-broker-created RPC takes just the device-id and state.
notify-created --device-id/Uuid --state/Map -> none:
send-request_ COMMAND-NOTIFY-BROKER-CREATED_ {
"_device_id": "$device-id",
"_state": state,
Expand Down Expand Up @@ -411,18 +408,16 @@ class BrokerCliHttp implements BrokerCli:
A $BrokerCliHttp specialisation for shared-tenancy HTTP deployments.

In a shared-tenancy deployment the broker also owns the auth-side device
record; this override sends the hardware-id and the configured scope
(organization-id) so the broker can populate that record alongside the
broker-side state.
record; this override sends the configured scope (organization-id) so the
broker can populate that record alongside the broker-side state.
*/
class BrokerCliHttpShared extends BrokerCliHttp:
constructor server-config/ServerConfigHttp --id/string:
super server-config --id=id

notify-created --hardware-id/Uuid --device-id/Uuid --state/Map -> none:
notify-created --device-id/Uuid --state/Map -> none:
send-request_ COMMAND-NOTIFY-BROKER-CREATED_ {
"_device_id": "$device-id",
"_hardware_id": "$hardware-id",
"_organization_id": server-config_.scope.to-json,
"_state": state,
}
7 changes: 4 additions & 3 deletions src/cli/brokers/supabase/supabase.toit
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ class BrokerCliSupabase extends BrokerCliHttp:
the device row in the auth-side `devices` table as part of the
notify-created handshake.
*/
notify-created --hardware-id/Uuid --device-id/Uuid --state/Map -> none:
notify-created --device-id/Uuid --state/Map -> none:
if server-config_.tenancy == TENANCY-SHARED:
// The existing schema keeps both columns; they now hold the same ID.
supabase-client_.rest.insert "devices" --no-return-inserted {
"id": "$hardware-id",
"id": "$device-id",
"alias": "$device-id",
"organization_id": server-config_.scope.to-json,
}
super --hardware-id=hardware-id --device-id=device-id --state=state
super --device-id=device-id --state=state

extra-headers -> Map:
bearer/string := supabase-client_.session_
Expand Down
22 changes: 4 additions & 18 deletions src/cli/device.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ import uuid show Uuid
import .firmware

class Device:
/**
The hardware ID of the device.

On the Artemis server, this is the primary key and simply called "id".
The hardware ID is generated by the Artemis server.

This ID is generally *not* shared with the user.
*/
hardware-id/Uuid

/**
The device ID.

This ID is the one under which users identify a device.
It's also called "alias" in the Artemis server.
*/
id/Uuid

Expand All @@ -29,11 +18,10 @@ class Device:
*/
organization-id/Uuid

constructor --.hardware-id --.id --.organization-id:
constructor --.id --.organization-id:

constructor.from-json-identity identity/Map:
device := identity["artemis.device"]
hardware-id = Uuid.parse device["hardware_id"]
id = Uuid.parse device["device_id"]
organization-id = Uuid.parse device["organization_id"]

Expand All @@ -42,7 +30,8 @@ class Device:
"artemis.device": {
"device_id" : "$id",
"organization_id" : "$organization-id",
"hardware_id" : "$hardware-id",
// Kept so older Artemis service images can read new identities.
"hardware_id" : "$id",
},
}

Expand Down Expand Up @@ -105,22 +94,19 @@ class DeviceDetailed extends Device:

initial-state := reported-state-firmware ? null : state
local-organization-id := ?
local-hardware-id := ?
local-id := ?

if initial-state:
identity := initial-state["identity"]
local-organization-id = Uuid.parse identity["organization_id"]
local-hardware-id = Uuid.parse identity["hardware_id"]
local-id = Uuid.parse identity["device_id"]
else:
old-firmware := Firmware.encoded reported-state-firmware["firmware"]
device := old-firmware.device-specific "artemis.device"
local-organization-id = Uuid.parse device["organization_id"]
local-hardware-id = Uuid.parse device["hardware_id"]
local-id = Uuid.parse device["device_id"]

super --hardware-id=local-hardware-id --id=local-id --organization-id=local-organization-id
super --id=local-id --organization-id=local-organization-id

pod-id-firmware -> Uuid?:
return pod-id-from-state_ reported-state-firmware
Expand Down
10 changes: 5 additions & 5 deletions src/cli/firmware.toit
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class Firmware:
The device-specific data.

This data contains information such as the device ID, the organization ID,
the hardware ID and the wifi configuration. It also contains the "parts"
field which describes the individual parts of the firmware, and the sdk
version that was used to create the firmware.
and the wifi configuration. It also contains the "parts" field which
describes the individual parts of the firmware, and the sdk version that
was used to create the firmware.
*/
device-specific-data/ByteArray
/** A decoded version of the $device-specific-data. */
Expand Down Expand Up @@ -98,7 +98,8 @@ class Firmware:
device-map := {
"device_id": "$device.id",
"organization_id": "$device.organization-id",
"hardware_id": "$device.hardware-id",
// Kept so older Artemis service images can read new firmware.
"hardware_id": "$device.id",
}
while true:
device-specific := ubjson.encode {
Expand All @@ -124,7 +125,6 @@ class Firmware:
return Device
--id=Uuid.parse device-map["device_id"]
--organization-id=Uuid.parse device-map["organization_id"]
--hardware-id=Uuid.parse device-map["hardware_id"]

/** The sdk version that was used for this firmware. */
sdk-version -> string:
Expand Down
8 changes: 3 additions & 5 deletions src/cli/fleet.toit
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,9 @@ class FleetWithDevices extends Fleet:
/**
Provisions a device.

Mints a fresh hardware-id locally and registers the device with the
broker (which, for a shared-tenancy deployment, also creates the
corresponding row in the auth provider's device table).
Registers the device with the broker. For a shared-tenancy deployment,
the broker also creates the corresponding row in the auth provider's
device table.

Writes the identity file to $out-path.
*/
Expand All @@ -1108,7 +1108,6 @@ class FleetWithDevices extends Fleet:
broker.ensure-authenticated

device := Device
--hardware-id=random-uuid
--id=device-id
--organization-id=organization-id

Expand Down Expand Up @@ -1225,6 +1224,5 @@ class FleetWithDevices extends Fleet:
identity := read-base64-ubjson identity-path
device-map := identity["artemis.device"]
return Device
--hardware-id=Uuid.parse device-map["hardware_id"]
--id=Uuid.parse device-map["device_id"]
--organization-id=Uuid.parse device-map["organization_id"]
9 changes: 1 addition & 8 deletions src/service/device.toit
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ class Device:
*/
id/Uuid

/**
The hardware ID of the device.

This ID was chosen during provisioning and is globally unique.
*/
hardware-id/Uuid

/**
The organization ID of the device.
*/
Expand Down Expand Up @@ -95,7 +88,7 @@ class Device:

storage_/Storage

constructor --.id --.hardware-id --.organization-id --.firmware-state/Map --storage/Storage:
constructor --.id --.organization-id --.firmware-state/Map --storage/Storage:
storage_ = storage
current-state = firmware-state
load_
Expand Down
1 change: 0 additions & 1 deletion src/service/run/esp32.toit
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ main arguments:
artemis-device-map := device-specific "artemis.device"
device := Device
--id=Uuid.parse artemis-device-map["device_id"]
--hardware-id=Uuid.parse artemis-device-map["hardware_id"]
--organization-id=Uuid.parse artemis-device-map["organization_id"]
--firmware-state=config
--storage=storage
Expand Down
1 change: 0 additions & 1 deletion src/service/run/host.toit
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ main arguments:
artemis-device-map := device-specific "artemis.device"
device := Device
--id=Uuid.parse artemis-device-map["device_id"]
--hardware-id=Uuid.parse artemis-device-map["hardware_id"]
--organization-id=Uuid.parse artemis-device-map["organization_id"]
--firmware-state=config
--storage=storage
Expand Down
2 changes: 0 additions & 2 deletions src/service/run/simulate.toit
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ run-host --pod/Pod --identity-path/string --cli/Cli -> none:
device-identity := identity["artemis.device"]

artemis-device := artemis-device.Device
--hardware-id=Uuid.parse device-identity["hardware_id"]
--organization-id=Uuid.parse device-identity["organization_id"]
--id=Uuid.parse device-identity["device_id"]

Expand Down Expand Up @@ -91,7 +90,6 @@ run-host --pod/Pod --identity-path/string --cli/Cli -> none:
while true:
device := Device
--id=artemis-device.id
--hardware-id=artemis-device.hardware-id
--organization-id=artemis-device.organization-id
--firmware-state=config
--storage=storage
Expand Down
28 changes: 14 additions & 14 deletions tests/artemis-server.toit
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class TestArtemisServer:

interface ArtemisServerBackdoor:
/**
Fetches the information of the device with the given $hardware-id.
Fetches the information of the device with the given $device-id.

Returns a list of [hardware_id, fleet_id, alias]. If no alias exists, uses "" instead.
Returns a list of [device_id, fleet_id, alias]. If no alias exists, uses "" instead.
*/
fetch-device-information --hardware-id/Uuid -> List
fetch-device-information --device-id/Uuid -> List

/** Whether there exists a '$type'-event for the given $hardware-id. */
has-event --hardware-id/Uuid --type/string -> bool
/** Whether there exists a '$type'-event for the given $device-id. */
has-event --device-id/Uuid --type/string -> bool

/**
Removes the device with the given $device-id.
Expand Down Expand Up @@ -58,18 +58,18 @@ class ToitHttpBackdoor implements ArtemisServerBackdoor:

constructor .server:

fetch-device-information --hardware-id/Uuid -> List:
entry/DeviceEntry := server.devices["$hardware-id"]
fetch-device-information --device-id/Uuid -> List:
entry/DeviceEntry := server.devices["$device-id"]
return [
Uuid.parse entry.id,
Uuid.parse entry.organization-id,
Uuid.parse entry.alias,
]

has-event --hardware-id/Uuid --type/string -> bool:
hardware-id-string := "$hardware-id"
has-event --device-id/Uuid --type/string -> bool:
device-id-string := "$device-id"
server.events.do: | entry/EventEntry |
if entry.device-id == hardware-id-string and
if entry.device-id == device-id-string and
entry.data is Map and (entry.data.get "type") == type:
return true
return false
Expand Down Expand Up @@ -133,21 +133,21 @@ class SupabaseBackdoor implements ArtemisServerBackdoor:

constructor .server-config_ .service-key_:

fetch-device-information --hardware-id/Uuid -> List:
fetch-device-information --device-id/Uuid -> List:
entry := query_ "devices" [
equals "id" "$hardware-id",
equals "id" "$device-id",
]
return [
Uuid.parse entry[0]["id"],
Uuid.parse entry[0]["organization_id"],
Uuid.parse entry[0]["alias"],
]

has-event --hardware-id/Uuid --type/string -> bool:
has-event --device-id/Uuid --type/string -> bool:
// For simplicity just run through all entries.
// In the test-setup we should not have that many.
entries := query_ "events" [
equals "device_id" "$hardware-id",
equals "device_id" "$device-id",
]
if not entries: return false
entries.do:
Expand Down
8 changes: 3 additions & 5 deletions tests/broker-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import .utils
// that are not already in the database.
DEVICE1 ::= Device
--id=random-uuid
--hardware-id=random-uuid
--organization-id=TEST-ORGANIZATION-UUID
--firmware-state={:}
--storage=Storage
DEVICE2 ::= Device
--id=random-uuid
--hardware-id=random-uuid
--organization-id=TEST-ORGANIZATION-UUID
--firmware-state={:}
--storage=Storage
Expand All @@ -56,19 +54,19 @@ run-test
identity := {
"device_id": "$device.id",
"organization_id": "$device.organization-id",
"hardware_id": "$device.hardware-id",
}
state := {
"identity": identity,
}
broker-cli.notify-created --hardware-id=device.hardware-id --device-id=device.id --state=state
broker-cli.notify-created --device-id=device.id --state=state

if broker-name == "http-toit-shared" or broker-name == "supabase-local-artemis":
// A shared-tenancy broker must populate the auth-side device record
// as part of notify-created.
[DEVICE1, DEVICE2].do: | device/Device |
auth-record := test-broker.backdoor.get-auth-device --hardware-id=device.hardware-id
auth-record := test-broker.backdoor.get-auth-device --device-id=device.id
expect-not-null auth-record
expect-equals "$device.id" auth-record["id"]
expect-equals "$device.id" auth-record["alias"]
expect-equals "$device.organization-id" auth-record["organization_id"]

Expand Down
Loading
Loading