1818
1919import com .iexec .common .utils .IexecEnvUtils ;
2020import com .iexec .common .utils .IexecFileHelper ;
21+ import com .iexec .commons .poco .chain .DealParams ;
2122import com .iexec .commons .poco .task .TaskDescription ;
2223import com .iexec .commons .poco .tee .TeeEnclaveConfiguration ;
2324import com .iexec .sms .api .config .TeeServicesProperties ;
4647import static com .iexec .sms .api .TeeSessionGenerationError .*;
4748import static com .iexec .sms .secret .ReservedSecretKeyName .*;
4849
50+ /**
51+ * Service to fetch secrets from SMS database in order to prepare TEE tasks sessions for CAS or SPS.
52+ *
53+ * @see com.iexec.sms.tee.session.gramine.GramineSessionMakerService
54+ * @see com.iexec.sms.tee.session.scone.SconeSessionMakerService
55+ */
4956@ Slf4j
5057@ Service
5158public class SecretSessionBaseService {
5259
5360 static final String EMPTY_STRING_VALUE = "" ;
5461 static final String IEXEC_PRE_COMPUTE_OUT = "IEXEC_PRE_COMPUTE_OUT" ;
5562 static final String IEXEC_DATASET_KEY = "IEXEC_DATASET_KEY" ;
63+ static final String IEXEC_APP_DEVELOPER_SECRET_PREFIX = "IEXEC_APP_DEVELOPER_SECRET_" ;
64+ static final String IEXEC_REQUESTER_SECRET_PREFIX = "IEXEC_REQUESTER_SECRET_" ;
5665
5766 private final Web3SecretService web3SecretService ;
5867 private final Web2SecretService web2SecretService ;
@@ -74,9 +83,9 @@ public SecretSessionBaseService(
7483 }
7584
7685 /**
77- * Collect tokens required for different compute stages (pre, in , post).
86+ * Collect tokens required for different compute stages (pre, app , post).
7887 *
79- * @param request session request details
88+ * @param request Session request details
8089 * @return All common tokens for a session, whatever TEE technology is used
8190 */
8291 public SecretSessionBase getSecretsTokens (final TeeSessionRequest request ) throws TeeSessionGenerationException {
@@ -85,10 +94,12 @@ public SecretSessionBase getSecretsTokens(final TeeSessionRequest request) throw
8594 NO_SESSION_REQUEST ,
8695 "Session request must not be null" );
8796 }
88- if (request .getTaskDescription () == null ) {
97+ // Task description or deal params should never be null
98+ // We nevertheless add both checks to cover NullPointerException
99+ if (request .getTaskDescription () == null || request .getTaskDescription ().getDealParams () == null ) {
89100 throw new TeeSessionGenerationException (
90101 NO_TASK_DESCRIPTION ,
91- "Task description must not be null" );
102+ "Task description and deal parameters must both not be null" );
92103 }
93104 final SecretSessionBaseBuilder sessionBase = SecretSessionBase .builder ();
94105 final TaskDescription taskDescription = request .getTaskDescription ();
@@ -104,11 +115,13 @@ public SecretSessionBase getSecretsTokens(final TeeSessionRequest request) throw
104115 return sessionBase .build ();
105116 }
106117
118+ // region pre-compute
119+
107120 /**
108121 * Get tokens to be injected in the pre-compute enclave.
109122 *
110123 * @param request Session request details
111- * @return {@link SecretEnclaveBase} instance
124+ * @return A {@link SecretEnclaveBase} instance
112125 * @throws TeeSessionGenerationException if dataset secret is not found
113126 */
114127 SecretEnclaveBase getPreComputeTokens (final TeeSessionRequest request ) throws TeeSessionGenerationException {
@@ -155,11 +168,15 @@ SecretEnclaveBase getPreComputeTokens(final TeeSessionRequest request) throws Te
155168 .build ();
156169 }
157170
171+ // endregion
172+
173+ // region app-compute
174+
158175 /**
159176 * Get tokens to be injected in the application enclave.
160177 *
161178 * @param request Session request details
162- * @return {@link SecretEnclaveBase} instance
179+ * @return A {@link SecretEnclaveBase} instance
163180 * @throws TeeSessionGenerationException if {@code TaskDescription} is {@literal null} or does not contain a {@code TeeEnclaveConfiguration}
164181 */
165182 SecretEnclaveBase getAppTokens (final TeeSessionRequest request ) throws TeeSessionGenerationException {
@@ -182,13 +199,6 @@ SecretEnclaveBase getAppTokens(final TeeSessionRequest request) throws TeeSessio
182199 }
183200
184201 enclaveBase .mrenclave (enclaveConfig .getFingerprint ());
185- // extract <IEXEC_INPUT_FILE_NAME_N, name>
186- // this map will be empty (not null) if no input file is found
187- IexecEnvUtils .getComputeStageEnvMap (taskDescription )
188- .entrySet ()
189- .stream ()
190- .filter (e -> e .getKey ().startsWith (IexecEnvUtils .IEXEC_INPUT_FILE_NAME_PREFIX ))
191- .forEach (e -> tokens .put (e .getKey (), e .getValue ()));
192202
193203 final Map <String , Object > computeSecrets = getApplicationComputeSecrets (taskDescription );
194204 tokens .putAll (computeSecrets );
@@ -199,6 +209,19 @@ SecretEnclaveBase getAppTokens(final TeeSessionRequest request) throws TeeSessio
199209 .build ();
200210 }
201211
212+ /**
213+ * Get secrets defined for the application execution.
214+ * <p>
215+ * Application secrets can be of two kinds:
216+ * <ul>
217+ * <li>A single application secret defined by the application developer for its application
218+ * <li>Up to several requester secrets pushed by the requester in the database and mapped to the application in
219+ * deal parameters
220+ * </ul>
221+ *
222+ * @param taskDescription A task description
223+ * @return A {@code Map} containing secrets retrieved from the database.
224+ */
202225 private Map <String , Object > getApplicationComputeSecrets (final TaskDescription taskDescription ) {
203226 final Map <String , Object > tokens = new HashMap <>();
204227 final List <TeeTaskComputeSecretHeader > ids = getAppComputeSecretsHeaders (taskDescription );
@@ -207,15 +230,15 @@ private Map<String, Object> getApplicationComputeSecrets(final TaskDescription t
207230 final List <TeeTaskComputeSecret > secrets = teeTaskComputeSecretService .getSecretsForTeeSession (ids );
208231 log .debug ("TeeTaskComputeSecret objects fetched from database [chainTaskId:{}, count:{}]" ,
209232 taskDescription .getChainTaskId (), secrets .size ());
210- for (TeeTaskComputeSecret secret : secrets ) {
233+ for (final TeeTaskComputeSecret secret : secrets ) {
211234 if (!StringUtils .isEmpty (secret .getHeader ().getOnChainObjectAddress ())) {
212235 tokens .put ("IEXEC_APP_DEVELOPER_SECRET" , secret .getValue ());
213- tokens .put (IexecEnvUtils . IEXEC_APP_DEVELOPER_SECRET_PREFIX + "1" , secret .getValue ());
236+ tokens .put (IEXEC_APP_DEVELOPER_SECRET_PREFIX + "1" , secret .getValue ());
214237 } else {
215238 final String secretKey = secret .getHeader ().getKey ();
216- taskDescription .getSecrets ().forEach ((key , value ) -> {
239+ taskDescription .getDealParams (). getIexecSecrets ().forEach ((key , value ) -> {
217240 if (value .equals (secretKey )) {
218- tokens .put (IexecEnvUtils . IEXEC_REQUESTER_SECRET_PREFIX + key , secret .getValue ());
241+ tokens .put (IEXEC_REQUESTER_SECRET_PREFIX + key , secret .getValue ());
219242 }
220243 });
221244 }
@@ -236,8 +259,8 @@ private List<TeeTaskComputeSecretHeader> getAppComputeSecretsHeaders(final TaskD
236259 secretIndex ));
237260 }
238261
239- if (taskDescription .getSecrets () != null && taskDescription .getRequester () != null ) {
240- for (Map .Entry <String , String > secretEntry : taskDescription .getSecrets ().entrySet ()) {
262+ if (taskDescription .getDealParams (). getIexecSecrets () != null && taskDescription .getRequester () != null ) {
263+ for (Map .Entry <String , String > secretEntry : taskDescription .getDealParams (). getIexecSecrets ().entrySet ()) {
241264 try {
242265 final int requesterSecretIndex = Integer .parseInt (secretEntry .getKey ());
243266 if (requesterSecretIndex <= 0 ) {
@@ -261,11 +284,15 @@ private List<TeeTaskComputeSecretHeader> getAppComputeSecretsHeaders(final TaskD
261284 return ids ;
262285 }
263286
287+ // endregion
288+
289+ // region post-compute
290+
264291 /**
265292 * Get tokens to be injected in the post-compute enclave.
266293 *
267294 * @param request Session request details
268- * @return {@link SecretEnclaveBase} instance
295+ * @return A {@link SecretEnclaveBase} instance
269296 * @throws TeeSessionGenerationException if {@code TaskDescription} is {@literal null}
270297 */
271298 SecretEnclaveBase getPostComputeTokens (final TeeSessionRequest request ) throws TeeSessionGenerationException {
@@ -290,7 +317,7 @@ SecretEnclaveBase getPostComputeTokens(final TeeSessionRequest request) throws T
290317 // storage
291318 if (taskDescription .containsCallback ()) {
292319 tokens .putAll (getPostComputeStorageTokens (request , EMPTY_STRING_VALUE , EMPTY_STRING_VALUE ));
293- } else if (DROPBOX_RESULT_STORAGE_PROVIDER .equals (taskDescription .getResultStorageProvider ())) {
320+ } else if (DROPBOX_RESULT_STORAGE_PROVIDER .equals (taskDescription .getDealParams (). getIexecResultStorageProvider ())) {
294321 final String storageToken = secrets .stream ()
295322 .filter (secret -> IEXEC_RESULT_DROPBOX_TOKEN .equals (secret .getHeader ().getAddress ()))
296323 .findFirst ()
@@ -328,10 +355,10 @@ SecretEnclaveBase getPostComputeTokens(final TeeSessionRequest request) throws T
328355
329356 List <Web2SecretHeader > getPostComputeSecretHeaders (final TaskDescription taskDescription , final String workerAddress ) {
330357 final List <Web2SecretHeader > ids = new ArrayList <>();
331- if (taskDescription .isResultEncryption ()) {
358+ if (taskDescription .getDealParams (). isIexecResultEncryption ()) {
332359 ids .add (new Web2SecretHeader (taskDescription .getBeneficiary (), IEXEC_RESULT_ENCRYPTION_PUBLIC_KEY ));
333360 }
334- if (DROPBOX_RESULT_STORAGE_PROVIDER .equals (taskDescription .getResultStorageProvider ())) {
361+ if (DROPBOX_RESULT_STORAGE_PROVIDER .equals (taskDescription .getDealParams (). getIexecResultStorageProvider ())) {
335362 ids .add (new Web2SecretHeader (taskDescription .getRequester (), IEXEC_RESULT_DROPBOX_TOKEN ));
336363 } else {
337364 ids .add (new Web2SecretHeader (taskDescription .getRequester (), IEXEC_RESULT_IEXEC_IPFS_TOKEN ));
@@ -346,7 +373,7 @@ Map<String, String> getPostComputeEncryptionTokens(final TeeSessionRequest reque
346373 final TaskDescription taskDescription = request .getTaskDescription ();
347374 final String taskId = taskDescription .getChainTaskId ();
348375 final Map <String , String > tokens = new HashMap <>();
349- final boolean shouldEncrypt = taskDescription .isResultEncryption ();
376+ final boolean shouldEncrypt = taskDescription .getDealParams (). isIexecResultEncryption ();
350377 // TODO use boolean with quotes instead of yes/no
351378 tokens .put (RESULT_ENCRYPTION , booleanToYesNo (shouldEncrypt ));
352379 tokens .put (RESULT_ENCRYPTION_PUBLIC_KEY , EMPTY_STRING_VALUE );
@@ -380,8 +407,10 @@ Map<String, String> getPostComputeStorageTokens(final TeeSessionRequest request,
380407 if (isCallbackRequested ) {
381408 return tokens ;
382409 }
383- final String storageProvider = taskDescription .getResultStorageProvider ();
384- final String storageProxy = taskDescription .getResultStorageProxy () != null ? taskDescription .getResultStorageProxy () : resultProxyUrl ;
410+ final DealParams dealParams = taskDescription .getDealParams ();
411+ final String storageProvider = dealParams .getIexecResultStorageProvider ();
412+ final String storageProxy = dealParams .getIexecResultStorageProxy () != null ?
413+ dealParams .getIexecResultStorageProxy () : resultProxyUrl ;
385414 if (StringUtils .isEmpty (storageToken )) {
386415 log .error ("Failed to get storage token [taskId:{}, storageProvider:{}, requester:{}]" ,
387416 taskId , storageProvider , taskDescription .getRequester ());
@@ -427,4 +456,6 @@ Map<String, String> getPostComputeSignTokens(final TeeSessionRequest request) th
427456 return tokens ;
428457 }
429458
459+ // endregion
460+
430461}
0 commit comments