Skip to content

Commit 9d4bc89

Browse files
authored
refactor(pairing): use the guid as primary key for ownership vouchers (astarte-platform#1800)
originally, we had decided to allow multiple ownership vouchers for a single device guid, with the motivation being that the rendezvous server was supposed to be in charge of deciding the validity of ownership vouchers and lift us from that job. in practice, we implemented our application logic as if there was always a single voucher for a guid, by simply adding `limit: 1` to all our queries. this also added complexity and was investigated as a possible source of bugs multiple times. for our implementation, supporting multiple vouchers for each guid doesn't really make sense anymore, as our vouchers are short-lived anyway, so let's just allow a single voucher for a guid! Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
1 parent 50a9287 commit 9d4bc89

File tree

6 files changed

+8
-14
lines changed

6 files changed

+8
-14
lines changed

apps/astarte_housekeeping/lib/astarte_housekeeping/realms/queries.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ defmodule Astarte.Housekeeping.Realms.Queries do
555555
private_key blob,
556556
voucher_data blob,
557557
guid blob,
558-
PRIMARY KEY (guid, voucher_data)
558+
PRIMARY KEY (guid)
559559
);
560560
"""
561561

apps/astarte_housekeeping/priv/migrations/realm/0009_create_ownership_vouchers_table.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ CREATE TABLE :keyspace.ownership_vouchers (
22
private_key blob,
33
voucher_data blob,
44
guid blob,
5-
PRIMARY KEY (guid, voucher_data)
5+
PRIMARY KEY (guid)
66
);

apps/astarte_housekeeping/test/support/helpers/database.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ defmodule Astarte.Housekeeping.Helpers.Database do
7272
CREATE TABLE :keyspace.ownership_vouchers (
7373
private_key blob,
7474
voucher_data blob,
75-
device_id uuid,
76-
PRIMARY KEY (device_id, voucher_data)
75+
guid blob,
76+
PRIMARY KEY (guid)
7777
);
7878
"""
7979

apps/astarte_pairing/lib/astarte_pairing/queries.ex

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,33 +241,27 @@ defmodule Astarte.Pairing.Queries do
241241
def get_ownership_voucher(realm_name, guid) do
242242
keyspace_name = Realm.keyspace_name(realm_name)
243243

244-
# FIXME: functions that depends on this one shall handle one or more ownership voucher, keeping just the first for now
245244
query =
246245
from o in OwnershipVoucher,
247246
prefix: ^keyspace_name,
248-
where: o.guid == ^guid,
249-
limit: 1,
250247
select: o.voucher_data
251248

252249
consistency = Consistency.domain_model(:read)
253250

254-
Repo.fetch_one(query, consistency: consistency)
251+
Repo.fetch(query, guid, consistency: consistency)
255252
end
256253

257254
def get_owner_private_key(realm_name, guid) do
258255
keyspace_name = Realm.keyspace_name(realm_name)
259256

260-
# FIXME: functions that depends on this one shall handle one or more private key, keeping just the first for now
261257
query =
262258
from o in OwnershipVoucher,
263259
prefix: ^keyspace_name,
264-
where: o.guid == ^guid,
265-
limit: 1,
266260
select: o.private_key
267261

268262
consistency = Consistency.domain_model(:read)
269263

270-
Repo.fetch_one(query, consistency: consistency)
264+
Repo.fetch(query, guid, consistency: consistency)
271265
end
272266

273267
def create_ownership_voucher(

apps/astarte_pairing/test/support/helpers/database.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule Astarte.Helpers.Database do
8080
private_key blob,
8181
voucher_data blob,
8282
guid blob,
83-
PRIMARY KEY (guid, voucher_data)
83+
PRIMARY KEY (guid)
8484
);
8585
"""
8686

libs/astarte_data_access/lib/astarte_data_access/fdo/ownership_voucher.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule Astarte.DataAccess.FDO.OwnershipVoucher do
2424
@primary_key false
2525
typed_schema "ownership_vouchers" do
2626
field :private_key, :binary
27-
field :voucher_data, :binary, primary_key: true
27+
field :voucher_data, :binary
2828
field :guid, Astarte.DataAccess.UUID, primary_key: true
2929
end
3030

0 commit comments

Comments
 (0)