Skip to content

Commit a7a8400

Browse files
authored
Improve checks when receiving a computed.json file from a REST call (#598)
1 parent eb7284d commit a7a8400

File tree

3 files changed

+110
-96
lines changed

3 files changed

+110
-96
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
1313

1414
- Fix `LoginServiceTests#shouldLoginOnceOnSimultaneousCalls` test. (#587)
1515
- Always use `WorkerpoolAuhorization` to retrieve JWT on Result Proxy. (#588)
16+
- Improve checks when receiving a `computed.json` file from a REST call. (#598)
1617

1718
### Quality
1819

src/main/java/com/iexec/worker/result/ResultService.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import com.iexec.common.utils.FileHelper;
2828
import com.iexec.common.utils.IexecFileHelper;
2929
import com.iexec.common.worker.result.ResultUtils;
30+
import com.iexec.commons.poco.chain.ChainDeal;
3031
import com.iexec.commons.poco.chain.ChainTask;
3132
import com.iexec.commons.poco.chain.ChainTaskStatus;
3233
import com.iexec.commons.poco.chain.WorkerpoolAuthorization;
3334
import com.iexec.commons.poco.task.TaskDescription;
35+
import com.iexec.commons.poco.tee.TeeUtils;
3436
import com.iexec.commons.poco.utils.BytesUtils;
3537
import com.iexec.resultproxy.api.ResultProxyClient;
3638
import com.iexec.worker.chain.CredentialsService;
@@ -54,13 +56,13 @@
5456
@Service
5557
public class ResultService implements Purgeable {
5658
public static final String ERROR_FILENAME = "error.txt";
57-
public static final String WRITE_COMPUTED_FILE_LOG_ARGS = " [chainTaskId:{}, computedFile:{}]";
5859

5960
private final WorkerConfigurationService workerConfigService;
6061
private final CredentialsService credentialsService;
6162
private final IexecHubService iexecHubService;
6263
private final ResultProxyClient resultProxyClient;
6364
private final Map<String, ResultInfo> resultInfoMap = ExpiringTaskMapFactory.getExpiringTaskMap();
65+
private final ObjectMapper mapper = new ObjectMapper();
6466

6567
public ResultService(
6668
WorkerConfigurationService workerConfigService,
@@ -305,34 +307,30 @@ public ComputedFile getComputedFile(String chainTaskId) {
305307
}
306308

307309
/**
308-
* Write computed file. Most likely used by tee-post-compute.
310+
* Writes computed file submitted to the worker through a REST call.
309311
* TODO: check compute stage is successful
310312
*
311313
* @param computedFile computed file to be written
312-
* @return true is computed file is successfully written to disk
314+
* @return {@literal true} is computed file is successfully written to disk, {@literal false} otherwise
313315
*/
314316
public boolean writeComputedFile(ComputedFile computedFile) {
315317
if (computedFile == null || StringUtils.isEmpty(computedFile.getTaskId())) {
316318
log.error("Cannot write computed file [computedFile:{}]", computedFile);
317319
return false;
318320
}
319-
String chainTaskId = computedFile.getTaskId();
320-
ChainTaskStatus chainTaskStatus =
321-
iexecHubService.getChainTask(chainTaskId)
322-
.map(ChainTask::getStatus)
323-
.orElse(null);
321+
final String chainTaskId = computedFile.getTaskId();
322+
log.debug("Received computed file [chainTaskId:{}, computedFile:{}]", chainTaskId, computedFile);
323+
final ChainTask chainTask = iexecHubService.getChainTask(chainTaskId).orElse(null);
324+
final ChainTaskStatus chainTaskStatus = chainTask != null ? chainTask.getStatus() : null;
324325
if (chainTaskStatus != ChainTaskStatus.ACTIVE) {
325-
log.error("Cannot write computed file if task is not active " +
326-
"[chainTaskId:{}, computedFile:{}, chainTaskStatus:{}]",
326+
log.error("Cannot write computed file if task is not active [chainTaskId:{}, computedFile:{}, chainTaskStatus:{}]",
327327
chainTaskId, computedFile, chainTaskStatus);
328328
return false;
329329
}
330-
String computedFilePath =
331-
workerConfigService.getTaskOutputDir(chainTaskId)
332-
+ IexecFileHelper.SLASH_COMPUTED_JSON;
330+
final String computedFilePath = workerConfigService.getTaskOutputDir(chainTaskId)
331+
+ IexecFileHelper.SLASH_COMPUTED_JSON;
333332
if (new File(computedFilePath).exists()) {
334-
log.error("Cannot write computed file if already written" +
335-
WRITE_COMPUTED_FILE_LOG_ARGS,
333+
log.error("Cannot write computed file if already written [chainTaskId:{}, computedFile:{}]",
336334
chainTaskId, computedFile);
337335
return false;
338336
}
@@ -341,15 +339,20 @@ public boolean writeComputedFile(ComputedFile computedFile) {
341339
chainTaskId, computedFile);
342340
return false;
343341
}
344-
boolean isSignatureRequired = iexecHubService.isTeeTask(chainTaskId);
345-
if (isSignatureRequired &&
346-
(StringUtils.isEmpty(computedFile.getEnclaveSignature())
347-
|| stringToBytes(computedFile.getEnclaveSignature()).length != 65)) {
342+
// TODO replace with fast getChainDeal access, only 1 on-chain read instead of 4
343+
final ChainDeal chainDeal = iexecHubService.getChainDeal(chainTask.getDealid()).orElse(null);
344+
if (chainDeal == null || !TeeUtils.isTeeTag(chainDeal.getTag())) {
345+
log.error("Cannot write computed file if task is not of TEE type [chainTaskId:{}, computedFile:{}]",
346+
chainTaskId, computedFile);
347+
return false;
348+
}
349+
// should always be TEE with a valid signature
350+
if (StringUtils.isEmpty(computedFile.getEnclaveSignature())
351+
|| stringToBytes(computedFile.getEnclaveSignature()).length != 65) {
348352
log.error("Cannot write computed file if TEE signature is invalid [chainTaskId:{}, computedFile:{}]",
349353
chainTaskId, computedFile);
350354
return false;
351355
}
352-
ObjectMapper mapper = new ObjectMapper();
353356
try {
354357
String json = mapper.writeValueAsString(computedFile);
355358
Files.write(Paths.get(computedFilePath), json.getBytes());

0 commit comments

Comments
 (0)