Skip to content

Commit c4d34e3

Browse files
authored
Refactor SecretSessionBaseService to use dealParams instead of deprecated TaskDescription fields (#277)
1 parent 18c4a33 commit c4d34e3

File tree

5 files changed

+163
-58
lines changed

5 files changed

+163
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
1515
- Use `WorkerpoolAuthorization#getHash` instead of `AuthorizationService#getChallengeForWorker`. (#272)
1616
- Reorder static and final keywords. (#274)
1717
- Update methods visibility and remove redundant checks in `SecretSessionBaseService`. (#276)
18+
- Refactor `SecretSessionBaseService` to use `dealParams` instead of deprecated `TaskDescription` fields. (#277)
1819

1920
### Dependency Upgrades
2021

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version=8.6.0
2-
iexecCommonVersion=8.5.0
2+
iexecCommonVersion=8.5.0-NEXT-SNAPSHOT
33
iexecCommonsPocoVersion=4.1.0-NEXT-SNAPSHOT
44

55
nexusUser

src/main/java/com/iexec/sms/tee/session/base/SecretSessionBaseService.java

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.iexec.common.utils.IexecEnvUtils;
2020
import com.iexec.common.utils.IexecFileHelper;
21+
import com.iexec.commons.poco.chain.DealParams;
2122
import com.iexec.commons.poco.task.TaskDescription;
2223
import com.iexec.commons.poco.tee.TeeEnclaveConfiguration;
2324
import com.iexec.sms.api.config.TeeServicesProperties;
@@ -46,13 +47,21 @@
4647
import static com.iexec.sms.api.TeeSessionGenerationError.*;
4748
import 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
5158
public 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
}

src/test/java/com/iexec/sms/tee/session/TeeSessionTestUtils.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.iexec.sms.tee.session;
1818

19+
import com.iexec.commons.poco.chain.DealParams;
1920
import com.iexec.commons.poco.task.TaskDescription;
2021
import com.iexec.commons.poco.tee.TeeEnclaveConfiguration;
2122
import com.iexec.sms.secret.compute.OnChainObjectType;
@@ -107,11 +108,22 @@ public static TeeSessionRequest.TeeSessionRequestBuilder createSessionRequestBui
107108
.taskDescription(taskDescription);
108109
}
109110

111+
public static DealParams.DealParamsBuilder createDealParams() {
112+
return DealParams.builder()
113+
.iexecArgs(ARGS)
114+
.iexecInputFiles(List.of(INPUT_FILE_URL_1, INPUT_FILE_URL_2))
115+
.iexecResultEncryption(true)
116+
.iexecResultStorageProvider(STORAGE_PROVIDER)
117+
.iexecResultStorageProxy(STORAGE_PROXY)
118+
.iexecSecrets(Map.of("1", REQUESTER_SECRET_KEY_1, "2", REQUESTER_SECRET_KEY_2));
119+
}
120+
110121
public static TaskDescription.TaskDescriptionBuilder createTaskDescription(TeeEnclaveConfiguration enclaveConfig) {
111122
final String appAddress = createEthereumAddress();
112123
final String requesterAddress = createEthereumAddress();
113124
final String beneficiaryAddress = createEthereumAddress();
114125
final String workerpoolAddress = createEthereumAddress();
126+
final DealParams dealParams = createDealParams().build();
115127
return TaskDescription.builder()
116128
.workerpoolOwner(workerpoolAddress)
117129
.chainTaskId(TASK_ID)
@@ -124,12 +136,7 @@ public static TaskDescription.TaskDescriptionBuilder createTaskDescription(TeeEn
124136
.datasetChecksum(DATASET_CHECKSUM)
125137
.requester(requesterAddress)
126138
.beneficiary(beneficiaryAddress)
127-
.cmd(ARGS)
128-
.inputFiles(List.of(INPUT_FILE_URL_1, INPUT_FILE_URL_2))
129-
.isResultEncryption(true)
130-
.resultStorageProvider(STORAGE_PROVIDER)
131-
.resultStorageProxy(STORAGE_PROXY)
132-
.secrets(Map.of("1", REQUESTER_SECRET_KEY_1, "2", REQUESTER_SECRET_KEY_2))
139+
.dealParams(dealParams)
133140
.botSize(1)
134141
.botFirstIndex(0)
135142
.botIndex(0);

0 commit comments

Comments
 (0)