Skip to content

Commit debade3

Browse files
authored
fix: apply correct environment variables to dapp docker container based on task execution mode (#656)
1 parent 1f3b0ff commit debade3

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed

src/main/java/com/iexec/worker/compute/app/AppComputeService.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,50 +60,44 @@ public AppComputeService(
6060
this.appComputeDurationsService = appComputeDurationsService;
6161
}
6262

63-
public AppComputeResponse runCompute(TaskDescription taskDescription,
64-
TeeSessionGenerationResponse secureSession) {
65-
String chainTaskId = taskDescription.getChainTaskId();
66-
final List<String> env = IexecEnvUtils.getComputeStageEnvList(taskDescription);
63+
public AppComputeResponse runCompute(final TaskDescription taskDescription,
64+
final TeeSessionGenerationResponse secureSession) {
65+
final String chainTaskId = taskDescription.getChainTaskId();
6766

6867
final List<Bind> binds = new ArrayList<>();
6968
binds.add(Bind.parse(dockerService.getInputBind(chainTaskId)));
7069
binds.add(Bind.parse(dockerService.getIexecOutBind(chainTaskId)));
7170

71+
final SgxDriverMode sgxDriverMode;
72+
final List<String> env;
7273
if (taskDescription.isTeeTask()) {
73-
final TeeService teeService = teeServicesManager
74-
.getTeeService(taskDescription.getTeeFramework());
75-
76-
final List<String> strings = teeService
77-
.buildComputeDockerEnv(taskDescription, secureSession);
78-
env.addAll(strings);
79-
80-
final List<Bind> additionalBindings =
81-
teeService.getAdditionalBindings().stream().map(Bind::parse).toList();
82-
binds.addAll(additionalBindings);
74+
final TeeService teeService = teeServicesManager.getTeeService(taskDescription.getTeeFramework());
75+
env = teeService.buildComputeDockerEnv(taskDescription, secureSession);
76+
binds.addAll(teeService.getAdditionalBindings().stream().map(Bind::parse).toList());
77+
sgxDriverMode = sgxService.getSgxDriverMode();
78+
} else {
79+
env = IexecEnvUtils.getComputeStageEnvList(taskDescription);
80+
sgxDriverMode = SgxDriverMode.NONE;
8381
}
8482

85-
HostConfig hostConfig = HostConfig.newHostConfig()
83+
final HostConfig hostConfig = HostConfig.newHostConfig()
8684
.withBinds(binds)
8785
.withDevices(sgxService.getSgxDevices());
8886
// Enclave should be able to connect to the LAS
8987
if (taskDescription.isTeeTask()) {
9088
hostConfig.withNetworkMode(workerConfigService.getDockerNetworkName());
9189
}
92-
DockerRunRequest runRequest = DockerRunRequest.builder()
90+
final DockerRunRequest runRequest = DockerRunRequest.builder()
9391
.hostConfig(hostConfig)
9492
.chainTaskId(chainTaskId)
9593
.imageUri(taskDescription.getAppUri())
9694
.containerName(getTaskContainerName(chainTaskId))
9795
.cmd(taskDescription.getDealParams().getIexecArgs())
9896
.env(env)
9997
.maxExecutionTime(taskDescription.getMaxExecutionTime())
100-
.sgxDriverMode(
101-
taskDescription.isTeeTask()
102-
? sgxService.getSgxDriverMode()
103-
: SgxDriverMode.NONE
104-
)
98+
.sgxDriverMode(sgxDriverMode)
10599
.build();
106-
DockerRunResponse dockerResponse = dockerService.run(runRequest);
100+
final DockerRunResponse dockerResponse = dockerService.run(runRequest);
107101
final Duration executionDuration = dockerResponse.getExecutionDuration();
108102
if (executionDuration != null) {
109103
appComputeDurationsService.addDurationForTask(chainTaskId, executionDuration.toMillis());

src/test/java/com/iexec/worker/compute/app/AppComputeServiceTests.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import com.iexec.commons.poco.chain.DealParams;
2929
import com.iexec.commons.poco.task.TaskDescription;
3030
import com.iexec.commons.poco.tee.TeeEnclaveConfiguration;
31+
import com.iexec.commons.poco.utils.BytesUtils;
3132
import com.iexec.sms.api.TeeSessionGenerationResponse;
3233
import com.iexec.worker.config.WorkerConfigurationService;
3334
import com.iexec.worker.docker.DockerService;
@@ -44,9 +45,6 @@
4445
import org.mockito.junit.jupiter.MockitoExtension;
4546

4647
import java.time.Duration;
47-
import java.util.ArrayList;
48-
import java.util.Arrays;
49-
import java.util.Collections;
5048
import java.util.List;
5149

5250
import static org.mockito.ArgumentMatchers.any;
@@ -55,8 +53,8 @@
5553
@ExtendWith(MockitoExtension.class)
5654
class AppComputeServiceTests {
5755

56+
private static final String CHAIN_DEAL_ID = "CHAIN_DEAL_ID";
5857
private static final String CHAIN_TASK_ID = "CHAIN_TASK_ID";
59-
private static final String DATASET_URI = "DATASET_URI";
6058
private static final String APP_URI = "APP_URI";
6159
private static final String WORKER_NAME = "WORKER_NAME";
6260
private static final TeeSessionGenerationResponse SECURE_SESSION = mock(TeeSessionGenerationResponse.class);
@@ -66,13 +64,17 @@ class AppComputeServiceTests {
6664
public static final long HEAP_SIZE = 1024;
6765

6866
private final DealParams dealParams = DealParams.builder()
69-
.iexecInputFiles(Arrays.asList("file0", "file1"))
67+
.iexecInputFiles(List.of("file0", "file1"))
7068
.build();
7169

7270
private final TaskDescription.TaskDescriptionBuilder taskDescriptionBuilder = TaskDescription.builder()
71+
.chainDealId(CHAIN_DEAL_ID)
7372
.chainTaskId(CHAIN_TASK_ID)
73+
.botIndex(0)
74+
.botSize(1)
75+
.botFirstIndex(0)
7476
.appUri(APP_URI)
75-
.datasetUri(DATASET_URI)
77+
.datasetAddress(BytesUtils.EMPTY_ADDRESS)
7678
.maxExecutionTime(MAX_EXECUTION_TIME)
7779
.dealParams(dealParams)
7880
.isTeeTask(true);
@@ -103,16 +105,14 @@ void shouldRunCompute() {
103105
String iexecOutBind = IEXEC_OUT + ":" + IexecFileHelper.SLASH_IEXEC_OUT;
104106
when(dockerService.getIexecOutBind(CHAIN_TASK_ID)).thenReturn(iexecOutBind);
105107
when(workerConfigService.getWorkerName()).thenReturn(WORKER_NAME);
106-
DockerRunResponse expectedDockerRunResponse = DockerRunResponse
107-
.builder()
108+
DockerRunResponse expectedDockerRunResponse = DockerRunResponse.builder()
108109
.finalStatus(DockerRunFinalStatus.SUCCESS)
109110
.executionDuration(Duration.ofSeconds(10))
110111
.build();
111112
when(dockerService.run(any())).thenReturn(expectedDockerRunResponse);
112113

113-
AppComputeResponse appComputeResponse =
114-
appComputeService.runCompute(taskDescription,
115-
SECURE_SESSION);
114+
final AppComputeResponse appComputeResponse =
115+
appComputeService.runCompute(taskDescription, SECURE_SESSION);
116116

117117
Assertions.assertThat(appComputeResponse.isSuccessful()).isTrue();
118118
verify(dockerService).run(any());
@@ -123,7 +123,7 @@ void shouldRunCompute() {
123123
argumentCaptor.getAllValues().get(0);
124124
HostConfig hostConfig = HostConfig.newHostConfig()
125125
.withBinds(Bind.parse(inputBind), Bind.parse(iexecOutBind))
126-
.withDevices(new ArrayList<>());
126+
.withDevices(List.of());
127127
Assertions.assertThat(dockerRunRequest).isEqualTo(
128128
DockerRunRequest.builder()
129129
.hostConfig(hostConfig)
@@ -145,10 +145,8 @@ void shouldRunComputeWithTeeAndConnectAppToLas() {
145145
.build();
146146
when(teeServicesManager.getTeeService(any())).thenReturn(teeMockedService);
147147
when(teeMockedService.buildComputeDockerEnv(taskDescription, SECURE_SESSION))
148-
.thenReturn(Arrays.asList("var0", "var1"));
149-
List<String> env = new ArrayList<>(Arrays.asList("var0", "var1"));
150-
env.addAll(IexecEnvUtils.getComputeStageEnvList(taskDescription));
151-
Collections.sort(env);
148+
.thenReturn(List.of("var0", "var1"));
149+
final List<String> env = List.of("var0", "var1");
152150
String inputBind = INPUT + ":" + IexecFileHelper.SLASH_IEXEC_IN;
153151
when(dockerService.getInputBind(CHAIN_TASK_ID)).thenReturn(inputBind);
154152
String iexecOutBind = IEXEC_OUT + ":" + IexecFileHelper.SLASH_IEXEC_OUT;
@@ -176,7 +174,6 @@ void shouldRunComputeWithTeeAndConnectAppToLas() {
176174
verify(dockerService).run(argumentCaptor.capture());
177175
DockerRunRequest dockerRunRequest =
178176
argumentCaptor.getAllValues().get(0);
179-
Collections.sort(dockerRunRequest.getEnv());
180177
HostConfig hostConfig = HostConfig.newHostConfig()
181178
.withBinds(Bind.parse(inputBind), Bind.parse(iexecOutBind))
182179
.withDevices(devices)

0 commit comments

Comments
 (0)