Skip to content

Commit 144c54a

Browse files
authored
Fix several issues raised by SonarQube Cloud (#642)
1 parent 05d72c3 commit 144c54a

File tree

12 files changed

+136
-113
lines changed

12 files changed

+136
-113
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
1616
- Replace deprecated `connect` with `connectAsync` in `StompClientService`. (#627)
1717
- Remove redundant blockchain calls to diminish pressure on Ethereum JSON-RPC API. (#632)
1818
- Stop using `TestUtils` in `ContributionServiceTests`. (#640)
19+
- Fix several issues raised by SonarQube Cloud. (#642)
1920

2021
### Breaking API changes
2122

src/main/java/com/iexec/worker/chain/EnclaveAuthorizationService.java

Lines changed: 3 additions & 6 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.
@@ -17,16 +17,15 @@
1717
package com.iexec.worker.chain;
1818

1919
import com.iexec.commons.poco.security.Signature;
20-
import com.iexec.commons.poco.tee.TeeEnclaveChallengeSignature;
2120
import com.iexec.commons.poco.utils.BytesUtils;
2221
import com.iexec.commons.poco.utils.EthAddress;
22+
import com.iexec.commons.poco.utils.HashUtils;
2323
import com.iexec.commons.poco.utils.SignatureUtils;
2424
import lombok.extern.slf4j.Slf4j;
2525
import org.springframework.stereotype.Service;
2626

2727
import static com.iexec.commons.poco.utils.SignatureUtils.isExpectedSignerOnSignedMessageHash;
2828

29-
3029
@Slf4j
3130
@Service
3231
public class EnclaveAuthorizationService {
@@ -59,9 +58,7 @@ public boolean isVerifiedEnclaveSignature(String chainTaskId,
5958
return false;
6059
}
6160

62-
String messageHash =
63-
TeeEnclaveChallengeSignature.getMessageHash(resultHash,
64-
resultSeal);
61+
final String messageHash = HashUtils.concatenateAndHash(resultHash, resultSeal);
6562

6663
return isExpectedSignerOnSignedMessageHash(messageHash,
6764
new Signature(enclaveSignature), enclaveChallenge);

src/main/java/com/iexec/worker/config/WorkerConfigurationService.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Service;
2424

2525
import java.io.File;
26+
import java.util.Objects;
2627

2728
import static java.lang.management.ManagementFactory.getOperatingSystemMXBean;
2829

@@ -137,16 +138,13 @@ public String getCPU() {
137138
* get max(numberOfJvmCpus -1, 1).
138139
*/
139140
public int getCpuCount() {
140-
int defaultAvailableCpuCount = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
141-
if (overrideAvailableCpuCount == null) {
142-
return defaultAvailableCpuCount;
143-
}
144-
return overrideAvailableCpuCount;
141+
final int defaultAvailableCpuCount = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
142+
return Objects.requireNonNullElse(overrideAvailableCpuCount, defaultAvailableCpuCount);
145143
}
146144

147145
public int getMemorySize() {
148-
com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean) getOperatingSystemMXBean();
149-
return Long.valueOf(os.getTotalPhysicalMemorySize() / (1024 * 1024 * 1024)).intValue();//in GB
146+
final com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean) getOperatingSystemMXBean();
147+
return (int) os.getTotalMemorySize() / (1024 * 1024 * 1024); // conversion to GB
150148
}
151149

152150
public String getHttpProxyHost() {

src/main/java/com/iexec/worker/feign/BaseFeignClient.java

Lines changed: 1 addition & 12 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.
@@ -19,11 +19,9 @@
1919
import feign.FeignException;
2020
import lombok.extern.slf4j.Slf4j;
2121
import org.springframework.http.HttpStatus;
22-
import org.springframework.http.ResponseEntity;
2322

2423
import java.util.function.Function;
2524

26-
2725
@Slf4j
2826
public abstract class BaseFeignClient {
2927

@@ -93,15 +91,6 @@ private String toHttpStatus(int status) {
9391
return HttpStatus.valueOf(status).toString();
9492
}
9593

96-
boolean is2xxSuccess(ResponseEntity<?> response) {
97-
int status = response.getStatusCodeValue();
98-
return status > 0 && HttpStatus.valueOf(status).is2xxSuccessful();
99-
}
100-
101-
boolean is2xxSuccess(int status) {
102-
return status > 0 && HttpStatus.valueOf(status).is2xxSuccessful();
103-
}
104-
10594
private void sleep(int millis) {
10695
try {
10796
Thread.sleep(millis);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ public boolean writeComputedFile(final ComputedFile computedFile) {
347347
chainTaskId, computedFile);
348348
return false;
349349
}
350-
// TODO replace with fast getChainDeal access, only 1 on-chain read instead of 4
351350
final ChainDeal chainDeal = iexecHubService.getChainDeal(chainTask.getDealid()).orElse(null);
352351
if (chainDeal == null || !TeeUtils.isTeeTag(chainDeal.getTag())) {
353352
log.error("Cannot write computed file if task is not of TEE type [chainTaskId:{}, computedFile:{}]",

src/main/java/com/iexec/worker/utils/ExecutorUtils.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
2+
* Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
1616

1717
package com.iexec.worker.utils;
1818

@@ -26,8 +26,7 @@
2626
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2727
public class ExecutorUtils {
2828

29-
public static Executor
30-
newSingleThreadExecutorWithFixedSizeQueue(int queueSize, String threadNamePrefix) {
29+
public static Executor newSingleThreadExecutorWithFixedSizeQueue(final int queueSize, final String threadNamePrefix) {
3130
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
3231
executor.setCorePoolSize(1);
3332
executor.setMaxPoolSize(1);

src/main/java/com/iexec/worker/utils/LoggingUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 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.
@@ -16,7 +16,10 @@
1616

1717
package com.iexec.worker.utils;
1818

19+
import lombok.AccessLevel;
20+
import lombok.NoArgsConstructor;
1921

22+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2023
public class LoggingUtils {
2124

2225
public static String getHighlightedMessage(String message) {

src/test/java/com/iexec/worker/TestUtils.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Copyright 2020 IEXEC BLOCKCHAIN TECH
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
2+
* Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
1616

1717
package com.iexec.worker;
1818

Lines changed: 39 additions & 39 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.
@@ -17,7 +17,7 @@
1717
package com.iexec.worker.chain;
1818

1919
import com.iexec.commons.poco.security.Signature;
20-
import com.iexec.commons.poco.tee.TeeEnclaveChallengeSignature;
20+
import com.iexec.commons.poco.utils.HashUtils;
2121
import com.iexec.commons.poco.utils.SignatureUtils;
2222
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.api.extension.ExtendWith;
@@ -41,77 +41,77 @@ class EnclaveAuthorizationServiceTests {
4141

4242
@Test
4343
void isVerifiedEnclaveSignature() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
44-
String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
45-
String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
46-
String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
44+
final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
45+
final String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
46+
final String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
4747

48-
String messageHash = TeeEnclaveChallengeSignature.getMessageHash(resultHash, resultSeal);
49-
Credentials credentials = Credentials.create(Keys.createEcKeyPair());
48+
final String messageHash = HashUtils.concatenateAndHash(resultHash, resultSeal);
49+
final Credentials credentials = Credentials.create(Keys.createEcKeyPair());
5050

51-
String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
52-
Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
51+
final String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
52+
final Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
5353

54-
boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
54+
final boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
5555
assertThat(isVerifiedEnclaveSignature).isTrue();
5656
}
5757

5858
@Test
5959
void isNotVerifiedEnclaveSignatureSinceWrongResultHash() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
60-
String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
61-
String resultHash = "0x1";
62-
String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
60+
final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
61+
final String resultHash = "0x1";
62+
final String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
6363

64-
String messageHash = TeeEnclaveChallengeSignature.getMessageHash(resultHash, resultSeal);
65-
Credentials credentials = Credentials.create(Keys.createEcKeyPair());
64+
final String messageHash = HashUtils.concatenateAndHash(resultHash, resultSeal);
65+
final Credentials credentials = Credentials.create(Keys.createEcKeyPair());
6666

67-
String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
68-
Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
67+
final String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
68+
final Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
6969

70-
boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
70+
final boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
7171
assertThat(isVerifiedEnclaveSignature).isFalse();
7272
}
7373

7474
@Test
7575
void isNotVerifiedEnclaveSignatureSinceWrongResultSeal() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
76-
String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
77-
String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
78-
String resultSeal = "0x3";
76+
final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
77+
final String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
78+
final String resultSeal = "0x3";
7979

80-
String messageHash = TeeEnclaveChallengeSignature.getMessageHash(resultHash, resultSeal);
81-
Credentials credentials = Credentials.create(Keys.createEcKeyPair());
80+
final String messageHash = HashUtils.concatenateAndHash(resultHash, resultSeal);
81+
final Credentials credentials = Credentials.create(Keys.createEcKeyPair());
8282

83-
String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
84-
Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
83+
final String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
84+
final Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
8585

86-
boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
86+
final boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), credentials.getAddress());
8787
assertThat(isVerifiedEnclaveSignature).isFalse();
8888
}
8989

9090
@Test
9191
void isNotVerifiedEnclaveSignatureSinceWrongEnclaveChallenge() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
92-
String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
93-
String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
94-
String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
92+
final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
93+
final String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
94+
final String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
9595

96-
String messageHash = TeeEnclaveChallengeSignature.getMessageHash(resultHash, resultSeal);
97-
Credentials credentials = Credentials.create(Keys.createEcKeyPair());
96+
final String messageHash = HashUtils.concatenateAndHash(resultHash, resultSeal);
97+
final Credentials credentials = Credentials.create(Keys.createEcKeyPair());
9898

99-
String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
100-
Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
99+
final String hexPrivateKey = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPrivateKey());
100+
final Signature signature = SignatureUtils.signMessageHashAndGetSignature(messageHash, hexPrivateKey);
101101

102-
boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), "0x1");
102+
final boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, signature.getValue(), "0x1");
103103
assertThat(isVerifiedEnclaveSignature).isFalse();
104104
}
105105

106106
@Test
107107
void isNotVerifiedEnclaveSignatureSinceWrongEnclaveSignature() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
108-
String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
109-
String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
110-
String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
108+
final String chainTaskId = "0x0000000000000000000000000000000000000000000000000000000000000001";
109+
final String resultHash = "0x0000000000000000000000000000000000000000000000000000000000000002";
110+
final String resultSeal = "0x0000000000000000000000000000000000000000000000000000000000000003";
111111

112-
Credentials credentials = Credentials.create(Keys.createEcKeyPair());
112+
final Credentials credentials = Credentials.create(Keys.createEcKeyPair());
113113

114-
boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, "0x1", credentials.getAddress());
114+
final boolean isVerifiedEnclaveSignature = enclaveAuthorizationService.isVerifiedEnclaveSignature(chainTaskId, resultHash, resultSeal, "0x1", credentials.getAddress());
115115
assertThat(isVerifiedEnclaveSignature).isFalse();
116116
}
117117
}

src/test/java/com/iexec/worker/chain/WebSocketBlockchainListenerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static String getServiceUrl(String serviceHost, int servicePort) {
8080
@Test
8181
void shouldConnect() {
8282
await().atMost(10L, TimeUnit.SECONDS)
83-
.until(() -> Objects.requireNonNull(meterRegistry.find(LATEST_BLOCK_METRIC_NAME).gauge()).value() != 0.0);
83+
.until(() -> web3jService.getLatestBlockNumber() != 0);
8484
assertThat(meterRegistry.find(TX_COUNT_METRIC_NAME).tag("block", "latest").gauge())
8585
.isNotNull()
8686
.extracting(Gauge::value)

0 commit comments

Comments
 (0)