|
1
|
+ |
defmodule OpenAgents.Machines.PairingExpiryTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
IDENTITY-011: a pairing window closes on its own clock.
|
|
4
|
+ |
|
|
5
|
+ |
Before the sweep, `claim_pairing/2` was the only thing that expired a
|
|
6
|
+ |
pairing, so every property here failed for the same reason: nobody polled.
|
|
7
|
+ |
"""
|
|
8
|
+ |
|
|
9
|
+ |
use OpenAgents.DataCase, async: true
|
|
10
|
+ |
|
|
11
|
+ |
alias OpenAgents.Inference
|
|
12
|
+ |
alias OpenAgents.Machines
|
|
13
|
+ |
alias OpenAgents.Machines.{Machine, Pairing, TokenVault}
|
|
14
|
+ |
alias OpenAgents.Repo
|
|
15
|
+ |
|
|
16
|
+ |
defp user(key) do
|
|
17
|
+ |
{:ok, user} =
|
|
18
|
+ |
OpenAgents.Accounts.upsert_github_user(%{
|
|
19
|
+ |
github_id: :erlang.phash2({__MODULE__, key}),
|
|
20
|
+ |
github_login: "pairing-expiry-#{key}",
|
|
21
|
+ |
github_avatar_url: "https://avatars.githubusercontent.com/u/1?v=4"
|
|
22
|
+ |
})
|
|
23
|
+ |
|
|
24
|
+ |
user
|
|
25
|
+ |
end
|
|
26
|
+ |
|
|
27
|
+ |
defp start_pairing do
|
|
28
|
+ |
{:ok, started} = Machines.start_pairing(%{"name" => "box", "tier" => "probe"})
|
|
29
|
+ |
started
|
|
30
|
+ |
end
|
|
31
|
+ |
|
|
32
|
+ |
defp close_window(pairing_id) do
|
|
33
|
+ |
Pairing
|
|
34
|
+ |
|> Repo.get!(pairing_id)
|
|
35
|
+ |
|> Ecto.Changeset.change(expires_at: DateTime.add(DateTime.utc_now(), -3_600, :second))
|
|
36
|
+ |
|> Repo.update!()
|
|
37
|
+ |
end
|
|
38
|
+ |
|
|
39
|
+ |
test "an approved pairing nobody claims loses its sealed token" do
|
|
40
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
41
|
+ |
{:ok, _machine} = Machines.approve_pairing(user("sealed"), code)
|
|
42
|
+ |
|
|
43
|
+ |
assert is_binary(Repo.get!(Pairing, pairing.id).token_ciphertext),
|
|
44
|
+ |
"the fixture must actually hold a sealed token"
|
|
45
|
+ |
|
|
46
|
+ |
close_window(pairing.id)
|
|
47
|
+ |
|
|
48
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
49
|
+ |
|
|
50
|
+ |
swept = Repo.get!(Pairing, pairing.id)
|
|
51
|
+ |
assert swept.status == "expired"
|
|
52
|
+ |
assert is_nil(swept.token_ciphertext)
|
|
53
|
+ |
end
|
|
54
|
+ |
|
|
55
|
+ |
test "the computer an unclaimed pairing created is revoked" do
|
|
56
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
57
|
+ |
{:ok, machine} = Machines.approve_pairing(user("computer"), code)
|
|
58
|
+ |
close_window(pairing.id)
|
|
59
|
+ |
|
|
60
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
61
|
+ |
|
|
62
|
+ |
revoked = Repo.get!(Machine, machine.id)
|
|
63
|
+ |
assert revoked.status == "revoked"
|
|
64
|
+ |
assert revoked.revoked_at
|
|
65
|
+ |
end
|
|
66
|
+ |
|
|
67
|
+ |
test "the revoked computer's inference grants close with it" do
|
|
68
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
69
|
+ |
owner = user("grants")
|
|
70
|
+ |
{:ok, machine} = Machines.approve_pairing(owner, code)
|
|
71
|
+ |
{:ok, conversation} = OpenAgents.Conversations.ensure_conversation(owner)
|
|
72
|
+ |
|
|
73
|
+ |
{:ok, grant, _token} =
|
|
74
|
+ |
Inference.mint(%{
|
|
75
|
+ |
owner_visitor_id: conversation.visitor_id,
|
|
76
|
+ |
conversation_id: conversation.id,
|
|
77
|
+ |
machine_id: machine.id
|
|
78
|
+ |
})
|
|
79
|
+ |
|
|
80
|
+ |
assert grant.status == "active"
|
|
81
|
+ |
|
|
82
|
+ |
close_window(pairing.id)
|
|
83
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
84
|
+ |
|
|
85
|
+ |
assert Repo.reload!(grant).status == "revoked"
|
|
86
|
+ |
end
|
|
87
|
+ |
|
|
88
|
+ |
test "the sweep announces the revocation so an open channel closes" do
|
|
89
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
90
|
+ |
{:ok, machine} = Machines.approve_pairing(user("broadcast"), code)
|
|
91
|
+ |
:ok = Phoenix.PubSub.subscribe(OpenAgents.PubSub, "machine:#{machine.id}")
|
|
92
|
+ |
|
|
93
|
+ |
close_window(pairing.id)
|
|
94
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
95
|
+ |
|
|
96
|
+ |
machine_id = machine.id
|
|
97
|
+ |
assert_receive {:machine_revoked, ^machine_id}
|
|
98
|
+ |
end
|
|
99
|
+ |
|
|
100
|
+ |
test "a pending pairing nobody approves expires too" do
|
|
101
|
+ |
%{pairing: pairing} = start_pairing()
|
|
102
|
+ |
close_window(pairing.id)
|
|
103
|
+ |
|
|
104
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
105
|
+ |
assert Repo.get!(Pairing, pairing.id).status == "expired"
|
|
106
|
+ |
end
|
|
107
|
+ |
|
|
108
|
+ |
test "a pairing still inside its window is left alone" do
|
|
109
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
110
|
+ |
{:ok, machine} = Machines.approve_pairing(user("fresh"), code)
|
|
111
|
+ |
|
|
112
|
+ |
assert Machines.expire_elapsed_pairings() == 0
|
|
113
|
+ |
|
|
114
|
+ |
assert Repo.get!(Pairing, pairing.id).status == "approved"
|
|
115
|
+ |
assert Repo.get!(Machine, machine.id).status == "active"
|
|
116
|
+ |
end
|
|
117
|
+ |
|
|
118
|
+ |
test "a claimed pairing is never swept, and its computer keeps working" do
|
|
119
|
+ |
%{pairing: pairing, code: code, poll_secret: poll_secret} = start_pairing()
|
|
120
|
+ |
{:ok, machine} = Machines.approve_pairing(user("claimed"), code)
|
|
121
|
+ |
{:ok, %{token: token}} = Machines.claim_pairing(pairing.id, poll_secret)
|
|
122
|
+ |
|
|
123
|
+ |
close_window(pairing.id)
|
|
124
|
+ |
assert Machines.expire_elapsed_pairings() == 0
|
|
125
|
+ |
|
|
126
|
+ |
assert Repo.get!(Pairing, pairing.id).status == "claimed"
|
|
127
|
+ |
assert {:ok, %Machine{id: id}} = Machines.authenticate_token(token)
|
|
128
|
+ |
assert id == machine.id
|
|
129
|
+ |
end
|
|
130
|
+ |
|
|
131
|
+ |
test "the sweep is idempotent" do
|
|
132
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
133
|
+ |
{:ok, _machine} = Machines.approve_pairing(user("idempotent"), code)
|
|
134
|
+ |
close_window(pairing.id)
|
|
135
|
+ |
|
|
136
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
137
|
+ |
assert Machines.expire_elapsed_pairings() == 0
|
|
138
|
+ |
end
|
|
139
|
+ |
|
|
140
|
+ |
# The claim path and the sweep must reach the same state, or the sweep would
|
|
141
|
+ |
# be a second, weaker expiry rather than the same one on a clock.
|
|
142
|
+ |
test "the sweep leaves the row where a late claim would have left it" do
|
|
143
|
+ |
%{pairing: swept, code: swept_code} = start_pairing()
|
|
144
|
+ |
{:ok, _} = Machines.approve_pairing(user("parity-a"), swept_code)
|
|
145
|
+ |
close_window(swept.id)
|
|
146
|
+ |
assert Machines.expire_elapsed_pairings() == 1
|
|
147
|
+ |
|
|
148
|
+ |
%{pairing: claimed, code: claimed_code, poll_secret: secret} = start_pairing()
|
|
149
|
+ |
{:ok, _} = Machines.approve_pairing(user("parity-b"), claimed_code)
|
|
150
|
+ |
close_window(claimed.id)
|
|
151
|
+ |
assert {:error, :pairing_expired} = Machines.claim_pairing(claimed.id, secret)
|
|
152
|
+ |
|
|
153
|
+ |
fields = fn id ->
|
|
154
|
+ |
row = Repo.get!(Pairing, id)
|
|
155
|
+ |
{row.status, row.token_ciphertext, Repo.get!(Machine, row.machine_id).status}
|
|
156
|
+ |
end
|
|
157
|
+ |
|
|
158
|
+ |
assert fields.(swept.id) == fields.(claimed.id)
|
|
159
|
+ |
end
|
|
160
|
+ |
|
|
161
|
+ |
# The sweep's selection is the only reader `machine_pairings_expires_at_index`
|
|
162
|
+ |
# has, and it is invisible to every outcome above: `expire_elapsed_pairing/1`
|
|
163
|
+ |
# re-reads each row under `FOR UPDATE` and refuses anything terminal or still
|
|
164
|
+ |
# fresh, so widening the predicate leaves all of those tests green while the
|
|
165
|
+ |
# index quietly stops being used. It is proved directly for that reason.
|
|
166
|
+ |
describe "the sweep's selection" do
|
|
167
|
+ |
test "names elapsed pending and approved pairings and nothing else" do
|
|
168
|
+ |
%{pairing: elapsed_pending} = start_pairing()
|
|
169
|
+ |
close_window(elapsed_pending.id)
|
|
170
|
+ |
|
|
171
|
+ |
%{pairing: elapsed_approved, code: approved_code} = start_pairing()
|
|
172
|
+ |
{:ok, _} = Machines.approve_pairing(user("select-approved"), approved_code)
|
|
173
|
+ |
close_window(elapsed_approved.id)
|
|
174
|
+ |
|
|
175
|
+ |
%{pairing: elapsed_claimed, code: claimed_code, poll_secret: secret} = start_pairing()
|
|
176
|
+ |
{:ok, _} = Machines.approve_pairing(user("select-claimed"), claimed_code)
|
|
177
|
+ |
{:ok, _} = Machines.claim_pairing(elapsed_claimed.id, secret)
|
|
178
|
+ |
close_window(elapsed_claimed.id)
|
|
179
|
+ |
|
|
180
|
+ |
%{pairing: fresh} = start_pairing()
|
|
181
|
+ |
|
|
182
|
+ |
selected = Machines.elapsed_pairing_ids(DateTime.utc_now())
|
|
183
|
+ |
|
|
184
|
+ |
assert Enum.sort(selected) == Enum.sort([elapsed_pending.id, elapsed_approved.id])
|
|
185
|
+ |
refute elapsed_claimed.id in selected
|
|
186
|
+ |
refute fresh.id in selected
|
|
187
|
+ |
end
|
|
188
|
+ |
|
|
189
|
+ |
test "the predicate is served by machine_pairings_expires_at_index" do
|
|
190
|
+ |
# A cold table is small enough that a sequential scan is the cheaper plan,
|
|
191
|
+ |
# so the planner is asked which index it would use, not whether it bothers.
|
|
192
|
+ |
# `SET LOCAL` so the choice dies with the sandbox's transaction and cannot
|
|
193
|
+ |
# follow this connection back into the pool.
|
|
194
|
+ |
Repo.query!("SET LOCAL enable_seqscan = off")
|
|
195
|
+ |
|
|
196
|
+ |
# The production query itself, not a copy of it, so a predicate that stops
|
|
197
|
+ |
# naming `expires_at` fails here as well as above.
|
|
198
|
+ |
{sql, params} =
|
|
199
|
+ |
Ecto.Adapters.SQL.to_sql(
|
|
200
|
+ |
:all,
|
|
201
|
+ |
Repo,
|
|
202
|
+ |
Machines.elapsed_pairing_query(DateTime.utc_now())
|
|
203
|
+ |
)
|
|
204
|
+ |
|
|
205
|
+ |
plan =
|
|
206
|
+ |
Repo.query!("EXPLAIN " <> sql, params).rows
|
|
207
|
+ |
|> Enum.map_join("\n", &hd/1)
|
|
208
|
+ |
|
|
209
|
+ |
assert plan =~ "machine_pairings_expires_at_index", plan
|
|
210
|
+ |
end
|
|
211
|
+ |
end
|
|
212
|
+ |
|
|
213
|
+ |
# TokenVault carries exactly one AAD on the strength of this bound, and
|
|
214
|
+ |
# CANON-002 states it as settled. It was not: nothing enforced it.
|
|
215
|
+ |
test "no sealed ciphertext survives a closed window" do
|
|
216
|
+ |
for key <- ["survivor-a", "survivor-b"] do
|
|
217
|
+ |
%{pairing: pairing, code: code} = start_pairing()
|
|
218
|
+ |
{:ok, _} = Machines.approve_pairing(user(key), code)
|
|
219
|
+ |
close_window(pairing.id)
|
|
220
|
+ |
end
|
|
221
|
+ |
|
|
222
|
+ |
_swept = Machines.expire_elapsed_pairings()
|
|
223
|
+ |
|
|
224
|
+ |
stale =
|
|
225
|
+ |
Repo.all(
|
|
226
|
+ |
from p in Pairing,
|
|
227
|
+ |
where: not is_nil(p.token_ciphertext) and p.expires_at <= ^DateTime.utc_now(),
|
|
228
|
+ |
select: p.token_ciphertext
|
|
229
|
+ |
)
|
|
230
|
+ |
|
|
231
|
+ |
assert stale == [],
|
|
232
|
+ |
"sealed tokens outlived their window: #{inspect(Enum.map(stale, &TokenVault.open/1))}"
|
|
233
|
+ |
end
|
|
234
|
+ |
end
|