33import java .util .Optional ;
44
55import com .iexec .common .chain .ContributionAuthorization ;
6- import com .iexec .common .security .Signature ;
76import com .iexec .common .sms .secrets .SmsSecret ;
8- import com .iexec .common .sms .SmsRequest ;
9- import com .iexec .common .sms .SmsRequestData ;
10- import com .iexec .common .sms .scone .SconeSecureSessionResponse .SconeSecureSession ;
117import com .iexec .common .sms .secrets .SmsSecretResponse ;
128import com .iexec .common .sms .secrets .TaskSecrets ;
13- import com .iexec .common .utils .BytesUtils ;
149import com .iexec .common .utils .FileHelper ;
15- import com .iexec .common .utils .HashUtils ;
1610import com .iexec .worker .chain .CredentialsService ;
17- import com .iexec .worker .feign .CustomSmsFeignClient ;
11+ import com .iexec .worker .feign .client . SmsClient ;
1812
1913import org .springframework .http .ResponseEntity ;
2014import org .springframework .retry .annotation .Recover ;
2115import org .springframework .retry .annotation .Retryable ;
2216import org .springframework .stereotype .Service ;
23- import org .web3j .crypto .Sign ;
2417
2518import feign .FeignException ;
2619import lombok .extern .slf4j .Slf4j ;
3124public class SmsService {
3225
3326 private CredentialsService credentialsService ;
34- private CustomSmsFeignClient customSmsFeignClient ;
27+ private SmsClient smsClient ;
3528
36- public SmsService (CredentialsService credentialsService , CustomSmsFeignClient customSmsFeignClient ) {
29+ public SmsService (CredentialsService credentialsService , SmsClient smsClient ) {
3730 this .credentialsService = credentialsService ;
38- this .customSmsFeignClient = customSmsFeignClient ;
31+ this .smsClient = smsClient ;
3932 }
4033
4134 @ Retryable (value = FeignException .class )
4235 public Optional <TaskSecrets > fetchTaskSecrets (ContributionAuthorization contributionAuth ) {
4336 String chainTaskId = contributionAuth .getChainTaskId ();
37+ String authorization = getAuthorizationString (contributionAuth );
38+ ResponseEntity <SmsSecretResponse > response = smsClient .getUnTeeSecrets (authorization , contributionAuth );
39+ if (!response .getStatusCode ().is2xxSuccessful ()) {
40+ return Optional .empty ();
41+ }
4442
45- SmsRequest smsRequest = buildSmsRequest (contributionAuth );
46-
47- SmsSecretResponse smsResponse = customSmsFeignClient .getUnTeeSecrets (smsRequest );
48-
49-
43+ SmsSecretResponse smsResponse = response .getBody ();
5044 if (smsResponse == null ) {
5145 log .error ("Received null response from SMS [chainTaskId:{}]" , chainTaskId );
5246 return Optional .empty ();
@@ -69,11 +63,10 @@ public Optional<TaskSecrets> fetchTaskSecrets(ContributionAuthorization contribu
6963 }
7064
7165 @ Recover
72- private boolean fetchTaskSecrets (FeignException e , ContributionAuthorization contributionAuth ) {
73- log .error ("Failed to get task secrets from SMS [chainTaskId:{}, attempts:3]" ,
74- contributionAuth .getChainTaskId ());
75- e .printStackTrace ();
76- return false ;
66+ private Optional <TaskSecrets > fetchTaskSecrets (FeignException e , ContributionAuthorization contributionAuth ) {
67+ log .error ("Failed to get task secrets from SMS [chainTaskId:{}, httpStatus:{}, exception:{}, attempts:3]" ,
68+ contributionAuth .getChainTaskId (), e .status (), e .getMessage ());
69+ return Optional .empty ();
7770 }
7871
7972 public void saveSecrets (String chainTaskId ,
@@ -109,43 +102,21 @@ public void saveSecrets(String chainTaskId,
109102 }
110103
111104 @ Retryable (value = FeignException .class )
112- public String getSconeSecureSession (ContributionAuthorization contributionAuth ) {
113- String chainTaskId = contributionAuth .getChainTaskId ();
114- SmsRequest smsRequest = buildSmsRequest (contributionAuth );
115-
116- String sessionId = customSmsFeignClient .generateTeeSession (smsRequest );
117-
118- if (sessionId .isEmpty ()) {
119- log .error ("Received null session from SMS [chainTaskId:{}]" , chainTaskId );
120- return "" ;
121- }
122-
123- return sessionId ;
105+ public String createTeeSession (ContributionAuthorization contributionAuth ) {
106+ String authorization = getAuthorizationString (contributionAuth );
107+ ResponseEntity <String > response = smsClient .createTeeSession (authorization , contributionAuth );
108+ return response .getStatusCode ().is2xxSuccessful () ? response .getBody () : "" ;
124109 }
125110
126111 @ Recover
127- private String getSconeSecureSession (FeignException e , ContributionAuthorization contributionAuth ) {
128- log .error ("Failed to generate secure session [chainTaskId:{}, attempts:3]" ,
129- contributionAuth .getChainTaskId ());
130- e .printStackTrace ();
112+ private String createTeeSession (FeignException e , ContributionAuthorization contributionAuth ) {
113+ log .error ("Failed to create secure session [chainTaskId:{}, httpStatus:{}, exception:{}, attempts:3]" ,
114+ contributionAuth .getChainTaskId (), e .status (), e .getMessage ());
131115 return "" ;
132116 }
133117
134- public SmsRequest buildSmsRequest (ContributionAuthorization contributionAuth ) {
135- String hash = HashUtils .concatenateAndHash (contributionAuth .getWorkerWallet (),
136- contributionAuth .getChainTaskId (), contributionAuth .getEnclaveChallenge ());
137-
138- Sign .SignatureData workerSignature = Sign .signPrefixedMessage (
139- BytesUtils .stringToBytes (hash ), credentialsService .getCredentials ().getEcKeyPair ());
140-
141- SmsRequestData smsRequestData = SmsRequestData .builder ()
142- .chainTaskId (contributionAuth .getChainTaskId ())
143- .workerAddress (contributionAuth .getWorkerWallet ())
144- .enclaveChallenge (contributionAuth .getEnclaveChallenge ())
145- .coreSignature (contributionAuth .getSignature ().getValue ())
146- .workerSignature (new Signature (workerSignature ).getValue ())
147- .build ();
148-
149- return new SmsRequest (smsRequestData );
118+ private String getAuthorizationString (ContributionAuthorization contributionAuth ) {
119+ String challenge = contributionAuth .getHash ();
120+ return credentialsService .hashAndSignMessage (challenge ).getValue ();
150121 }
151- }
122+ }
0 commit comments