@@ -56,11 +56,7 @@ defmodule Astarte.Pairing.Engine do
5656 end
5757 end
5858
59- def get_agent_public_key_pems ( realm_name ) do
60- keyspace = CQLUtils . realm_name_to_keyspace_name ( realm_name , Config . astarte_instance_id! ( ) )
61-
62- Queries . get_agent_public_key_pems ( keyspace )
63- end
59+ defdelegate get_agent_public_key_pems ( realm_name ) , to: Queries
6460
6561 def get_credentials (
6662 :astarte_mqtt_v1 ,
@@ -88,13 +84,12 @@ defmodule Astarte.Pairing.Engine do
8884 Config . cassandra_node! ( ) ,
8985 cqex_options
9086 ) ,
91- { :ok , device_row } <- Queries . select_device_for_credentials_request ( client , device_id ) ,
87+ { :ok , device } <- Queries . fetch_device ( realm , device_id ) ,
9288 { :authorized? , true } <-
93- { :authorized? ,
94- CredentialsSecret . verify ( credentials_secret , device_row [ :credentials_secret ] ) } ,
89+ { :authorized? , CredentialsSecret . verify ( credentials_secret , device . credentials_secret ) } ,
9590 { :credentials_inhibited? , false } <-
96- { :credentials_inhibited? , device_row [ : inhibit_credentials_request] } ,
97- _ <- CFSSLCredentials . revoke ( device_row [ : cert_serial] , device_row [ : cert_aki] ) ,
91+ { :credentials_inhibited? , device . inhibit_credentials_request } ,
92+ _ <- CFSSLCredentials . revoke ( device . cert_serial , device . cert_aki ) ,
9893 encoded_device_id <- Device . encode_device_id ( device_id ) ,
9994 { :ok , % { cert: cert , aki: _aki , serial: _serial } = cert_data } <-
10095 CFSSLCredentials . get_certificate ( csr , realm , encoded_device_id ) ,
@@ -104,7 +99,7 @@ defmodule Astarte.Pairing.Engine do
10499 device_id ,
105100 cert_data ,
106101 ip_tuple ,
107- device_row [ : first_credentials_request]
102+ device . first_credentials_request
108103 ) do
109104 { :ok , % { client_crt: cert } }
110105 else
@@ -139,23 +134,12 @@ defmodule Astarte.Pairing.Engine do
139134
140135 def get_info ( realm , hardware_id , credentials_secret ) do
141136 Logger . debug ( "get_info request for device #{ inspect ( hardware_id ) } in realm #{ inspect ( realm ) } " )
142- keyspace_name = CQLUtils . realm_name_to_keyspace_name ( realm , Config . astarte_instance_id! ( ) )
143-
144- cqex_options =
145- Config . cqex_options! ( )
146- |> Keyword . put ( :keyspace , keyspace_name )
147137
148138 with { :ok , device_id } <- Device . decode_device_id ( hardware_id , allow_extended_id: true ) ,
149- { :ok , client } <-
150- Client . new (
151- Config . cassandra_node! ( ) ,
152- cqex_options
153- ) ,
154- { :ok , device_row } <- Queries . select_device_for_info ( client , device_id ) ,
139+ { :ok , device } <- Queries . fetch_device ( realm , device_id ) ,
155140 { :authorized? , true } <-
156- { :authorized? ,
157- CredentialsSecret . verify ( credentials_secret , device_row [ :credentials_secret ] ) } do
158- device_status = device_status_string ( device_row )
141+ { :authorized? , CredentialsSecret . verify ( credentials_secret , device . credentials_secret ) } do
142+ device_status = device_status_string ( device )
159143 protocols = get_protocol_info ( )
160144
161145 { :ok , % { version: @ version , device_status: device_status , protocols: protocols } }
@@ -180,22 +164,12 @@ defmodule Astarte.Pairing.Engine do
180164 )
181165
182166 :telemetry . execute ( [ :astarte , :pairing , :register_new_device ] , % { } , % { realm: realm } )
183- keyspace_name = CQLUtils . realm_name_to_keyspace_name ( realm , Config . astarte_instance_id! ( ) )
184-
185- cqex_options =
186- Config . cqex_options! ( )
187- |> Keyword . put ( :keyspace , keyspace_name )
188167
189168 with { :ok , device_id } <- Device . decode_device_id ( hardware_id , allow_extended_id: true ) ,
190169 :ok <- verify_can_register_device ( realm , device_id ) ,
191- { :ok , client } <-
192- Client . new (
193- Config . cassandra_node! ( ) ,
194- cqex_options
195- ) ,
196170 credentials_secret <- CredentialsSecret . generate ( ) ,
197171 secret_hash <- CredentialsSecret . hash ( credentials_secret ) ,
198- :ok <- Queries . register_device ( client , device_id , hardware_id , secret_hash , opts ) do
172+ :ok <- Queries . register_device ( realm , device_id , hardware_id , secret_hash , opts ) do
199173 { :ok , credentials_secret }
200174 else
201175 { :error , :shutdown } ->
@@ -207,19 +181,19 @@ defmodule Astarte.Pairing.Engine do
207181 end
208182
209183 defp verify_can_register_device ( realm_name , device_id ) do
210- # An already existing device should always be able to retrieve a new credentials secret
211- case Queries . check_already_registered_device ( realm_name , device_id ) do
212- { :ok , true } ->
184+ try do
185+ if Queries . check_already_registered_device ( realm_name , device_id ) do
186+ # An already existing device should always be able to retrieve a new credentials secret
213187 :ok
214-
215- { :ok , false } ->
188+ else
216189 verify_can_register_new_device ( realm_name )
217-
218- { :error , reason } ->
190+ end
191+ rescue
192+ err ->
219193 # Consider a failing database as a negative answer
220194 _ =
221195 Logger . warning (
222- "Failed to verify if unconfirmed device #{ Device . encode_device_id ( device_id ) } exists, reason: #{ inspect ( reason ) } " ,
196+ "Failed to verify if unconfirmed device #{ Device . encode_device_id ( device_id ) } exists, reason: #{ Exception . message ( err ) } " ,
223197 realm_name: realm_name
224198 )
225199
@@ -250,26 +224,9 @@ defmodule Astarte.Pairing.Engine do
250224 "in realm #{ inspect ( realm ) } "
251225 )
252226
253- keyspace_name = CQLUtils . realm_name_to_keyspace_name ( realm , Config . astarte_instance_id! ( ) )
254-
255- cqex_options =
256- Config . cqex_options! ( )
257- |> Keyword . put ( :keyspace , keyspace_name )
258-
259227 with { :ok , device_id } <- Device . decode_device_id ( encoded_device_id ) ,
260- { :ok , client } <-
261- Client . new (
262- Config . cassandra_node! ( ) ,
263- cqex_options
264- ) ,
265- :ok <- Queries . unregister_device ( client , device_id ) do
228+ :ok <- Queries . unregister_device ( realm , device_id ) do
266229 :ok
267- else
268- { :error , :shutdown } ->
269- { :error , :realm_not_found }
270-
271- { :error , reason } ->
272- { :error , reason }
273230 end
274231 end
275232
@@ -278,21 +235,10 @@ defmodule Astarte.Pairing.Engine do
278235 "verify_credentials request for device #{ inspect ( hardware_id ) } in realm #{ inspect ( realm ) } "
279236 )
280237
281- keyspace_name = CQLUtils . realm_name_to_keyspace_name ( realm , Config . astarte_instance_id! ( ) )
282-
283- cqex_options =
284- Config . cqex_options! ( )
285- |> Keyword . put ( :keyspace , keyspace_name )
286-
287238 with { :ok , device_id } <- Device . decode_device_id ( hardware_id , allow_extended_id: true ) ,
288- { :ok , client } <-
289- Client . new (
290- Config . cassandra_node! ( ) ,
291- cqex_options
292- ) ,
293- { :ok , device_row } <- Queries . select_device_for_verify_credentials ( client , device_id ) ,
239+ { :ok , device } <- Queries . fetch_device ( realm , device_id ) ,
294240 { :authorized? , true } <-
295- { :authorized? , CredentialsSecret . verify ( secret , device_row [ : credentials_secret] ) } do
241+ { :authorized? , CredentialsSecret . verify ( secret , device . credentials_secret ) } do
296242 CertVerifier . verify ( client_crt , Config . ca_cert! ( ) )
297243 else
298244 { :authorized? , false } ->
@@ -317,17 +263,12 @@ defmodule Astarte.Pairing.Engine do
317263 { :error , :unknown_protocol }
318264 end
319265
320- defp device_status_string ( device_row ) do
266+ defp device_status_string ( device ) do
321267 # The device is pending until the first credendtial request
322268 cond do
323- Keyword . get ( device_row , :inhibit_credentials_request ) ->
324- "inhibited"
325-
326- Keyword . get ( device_row , :first_credentials_request ) ->
327- "confirmed"
328-
329- true ->
330- "pending"
269+ device . inhibit_credentials_request -> "inhibited"
270+ device . first_credentials_request -> "confirmed"
271+ true -> "pending"
331272 end
332273 end
333274
0 commit comments