2929import com .iexec .sms .api .SmsClient ;
3030import com .iexec .sms .api .SmsClientProvider ;
3131import feign .FeignException ;
32+ import jakarta .annotation .PostConstruct ;
3233import lombok .extern .slf4j .Slf4j ;
3334import org .apache .commons .lang3 .StringUtils ;
3435import org .springframework .context .event .EventListener ;
3536import org .springframework .stereotype .Service ;
3637import org .web3j .crypto .Hash ;
3738
39+ import java .util .Arrays ;
3840import java .util .Optional ;
41+ import java .util .function .Predicate ;
3942
4043import static com .iexec .sms .secret .ReservedSecretKeyName .IEXEC_RESULT_IEXEC_RESULT_PROXY_URL ;
4144
@@ -60,6 +63,16 @@ public SmsService(final IexecHubService iexecHubService,
6063 this .smsClientProvider = smsClientProvider ;
6164 }
6265
66+ @ PostConstruct
67+ void initSmsClients () {
68+ Arrays .stream (TeeFramework .values ())
69+ .map (this ::retrieveSmsUrl )
70+ .filter (Optional ::isPresent )
71+ .map (Optional ::get )
72+ .filter (Predicate .not (String ::isBlank ))
73+ .forEach (smsClientProvider ::getSmsClient );
74+ }
75+
6376 private Optional <String > getVerifiedSmsUrl (final String chainTaskId ) {
6477 final TaskDescription taskDescription = iexecHubService .getTaskDescription (chainTaskId );
6578 return getVerifiedSmsUrl (chainTaskId , taskDescription .getTeeFramework ());
@@ -86,35 +99,31 @@ public Optional<String> getVerifiedSmsUrl(final String chainTaskId, final String
8699 Optional <String > getVerifiedSmsUrl (final String chainTaskId , final TeeFramework teeFrameworkForDeal ) {
87100 log .debug ("getVerifiedSmsUrl [chainTaskId:{}, teeFrameworkForDeal:{}]" , chainTaskId , teeFrameworkForDeal );
88101 if (teeFrameworkForDeal == null ) {
89- log .error ("Can't get verified SMS url with invalid TEE framework " +
90- "from tag [chainTaskId:{}]" , chainTaskId );
102+ log .error ("Can't get verified SMS url with invalid TEE framework from tag [chainTaskId:{}]" , chainTaskId );
91103 return Optional .empty ();
92104 }
93- Optional <String > smsUrl = retrieveSmsUrl (teeFrameworkForDeal );
105+ final Optional <String > smsUrl = retrieveSmsUrl (teeFrameworkForDeal );
94106 if (smsUrl .isEmpty ()) {
95- log .error ("Can't get verified SMS url since type of tag is not " +
96- "supported [chainTaskId:{},teeFrameworkForDeal:{}]" ,
107+ log .error ("Can't get verified SMS url since type of tag is not supported [chainTaskId:{}, teeFrameworkForDeal:{}]" ,
97108 chainTaskId , teeFrameworkForDeal );
98109 return Optional .empty ();
99110 }
100111 final SmsClient smsClient = smsClientProvider .getSmsClient (smsUrl .get ());
101112 if (!checkSmsTeeFramework (smsClient , teeFrameworkForDeal , chainTaskId )) {
102- log .error ("Can't get verified SMS url since tag TEE type " +
103- "does not match SMS TEE type [chainTaskId:{},teeFrameworkForDeal:{}]" ,
113+ log .error ("Can't get verified SMS url since tag TEE type does not match SMS TEE type [chainTaskId:{}, teeFrameworkForDeal:{}]" ,
104114 chainTaskId , teeFrameworkForDeal );
105115 return Optional .empty ();
106116 }
107117 return smsUrl ;
108118 }
109119
110- private Optional <String > retrieveSmsUrl (TeeFramework teeFramework ) {
111- Optional <String > smsUrl = Optional .empty ();
112- if (teeFramework == TeeFramework .SCONE ) {
113- smsUrl = Optional .of (registryConfiguration .getSconeSms ());
114- } else if (teeFramework == TeeFramework .GRAMINE ) {
115- smsUrl = Optional .of (registryConfiguration .getGramineSms ());
116- }
117- return smsUrl ;
120+ private Optional <String > retrieveSmsUrl (final TeeFramework teeFramework ) {
121+ return switch (teeFramework ) {
122+ case SCONE -> Optional .of (registryConfiguration .getScone ());
123+ case GRAMINE -> Optional .of (registryConfiguration .getGramine ());
124+ case TDX -> Optional .of (registryConfiguration .getTdx ());
125+ default -> Optional .empty ();
126+ };
118127 }
119128
120129 private boolean checkSmsTeeFramework (SmsClient smsClient ,
@@ -130,8 +139,7 @@ private boolean checkSmsTeeFramework(SmsClient smsClient,
130139 }
131140
132141 if (smsTeeFramework != teeFrameworkForDeal ) {
133- log .error ("SMS is configured for another TEE framework " +
134- "[chainTaskId:{}, teeFrameworkForDeal:{}, smsTeeFramework:{}]" ,
142+ log .error ("SMS is configured for another TEE framework [chainTaskId:{}, teeFrameworkForDeal:{}, smsTeeFramework:{}]" ,
135143 chainTaskId , teeFrameworkForDeal , smsTeeFramework );
136144 return false ;
137145 }
0 commit comments