Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/main/java/com/iexec/sms/secret/SecretController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
* Copyright 2020-2026 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import com.iexec.sms.authorization.AuthorizationService;
import com.iexec.sms.secret.web2.NotAnExistingSecretException;
import com.iexec.sms.secret.web2.SameSecretException;
import com.iexec.sms.secret.web2.Web2SecretService;
import com.iexec.sms.secret.web3.Web3SecretService;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -115,7 +114,7 @@ public ResponseEntity<String> updateWeb2Secret(@RequestHeader String authorizati
@RequestParam String ownerAddress,
@RequestParam String secretName,
@RequestBody String newSecretValue) {
String challenge = authorizationService.getChallengeForSetWeb2Secret(ownerAddress, secretName, newSecretValue);
final String challenge = authorizationService.getChallengeForSetWeb2Secret(ownerAddress, secretName, newSecretValue);

if (!authorizationService.isSignedByHimself(challenge, authorization, ownerAddress)) {
log.error("Unauthorized to updateWeb2Secret [expectedChallenge:{}]", challenge);
Expand All @@ -129,8 +128,6 @@ public ResponseEntity<String> updateWeb2Secret(@RequestHeader String authorizati
try {
web2SecretService.updateSecret(ownerAddress, secretName, newSecretValue);
return ResponseEntity.noContent().build();
} catch (SameSecretException ignored) {
return ResponseEntity.noContent().build();
} catch (NoSuchElementException | NotAnExistingSecretException e) {
return ResponseEntity.notFound().build();
}
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/com/iexec/sms/secret/web2/SameSecretException.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ public boolean addSecret(String ownerAddress, String secretAddress, String secre
* @param ownerAddress Address of the secret owner.
* @param secretAddress Address of the secret.
* @param newSecretValue New, unencrypted value of the secret.
* @return The {@link Web2Secret} that has been saved.
* @throws NotAnExistingSecretException thrown when the requested secret does not exist.
* @throws SameSecretException thrown when the requested secret already contains the encrypted value.
*/
public Web2Secret updateSecret(String ownerAddress, String secretAddress, String newSecretValue) throws NotAnExistingSecretException, SameSecretException {
public void updateSecret(String ownerAddress, String secretAddress, String newSecretValue) throws NotAnExistingSecretException {
final Optional<Web2Secret> oSecret = getSecret(ownerAddress, secretAddress);
if (oSecret.isEmpty()) {
log.error("Secret does not exist, can't update it [ownerAddress:{}, secretAddress:{}]",
Expand All @@ -145,12 +143,11 @@ public Web2Secret updateSecret(String ownerAddress, String secretAddress, String
if (Objects.equals(secret.getValue(), encryptedValue)) {
log.info("No need to update secret [ownerAddress:{}, secretAddress:{}]",
ownerAddress, secretAddress);
throw new SameSecretException(ownerAddress, secretAddress);
return;
}

final Web2Secret newSecret = secret.withValue(encryptedValue);
final Web2Secret savedSecret = web2SecretRepository.save(newSecret);
cacheSecretService.putSecretExistenceInCache(savedSecret.getHeader(), true);
return savedSecret;
}
}
15 changes: 5 additions & 10 deletions src/test/java/com/iexec/sms/secret/SecretControllerTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 IEXEC BLOCKCHAIN TECH
* Copyright 2022-2026 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,14 +18,13 @@

import com.iexec.sms.authorization.AuthorizationService;
import com.iexec.sms.secret.web2.NotAnExistingSecretException;
import com.iexec.sms.secret.web2.SameSecretException;
import com.iexec.sms.secret.web2.Web2SecretService;
import com.iexec.sms.secret.web3.Web3SecretService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -35,6 +34,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class SecretControllerTests {

private static final String AUTHORIZATION = "AUTHORIZATION";
Expand All @@ -59,11 +59,6 @@ class SecretControllerTests {

private static final SecureRandom seed = new SecureRandom();

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}

//region isWeb3SecretSet
@Test
void shouldReturnNoContentWhenWeb3SecretExists() {
Expand Down Expand Up @@ -233,7 +228,7 @@ void failToUpdateWeb2SecretWhenPayloadTooLarge() {
}

@Test
void failToUpdateWeb2SecretWhenSecretIsMissing() throws NotAnExistingSecretException, SameSecretException {
void failToUpdateWeb2SecretWhenSecretIsMissing() throws NotAnExistingSecretException {
when(authorizationService.getChallengeForSetWeb2Secret(WEB2_OWNER_ADDRESS, WEB2_SECRET_NAME, WEB2_SECRET_VALUE))
.thenReturn(CHALLENGE);
when(authorizationService.isSignedByHimself(CHALLENGE, AUTHORIZATION, WEB2_OWNER_ADDRESS))
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/com/iexec/sms/secret/web2/Web2SecretServiceTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 IEXEC BLOCKCHAIN TECH
* Copyright 2022-2026 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,8 +25,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
Expand All @@ -39,6 +40,7 @@
import static org.mockito.Mockito.*;

@DataJpaTest
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Web2SecretServiceTests {
private static final String OWNER_ADDRESS = "ownerAddress";
Expand Down Expand Up @@ -73,7 +75,6 @@ void initLog() {

@BeforeEach
void beforeEach() {
MockitoAnnotations.openMocks(this);
memoryLogAppender.reset();
web2SecretRepository.deleteAll();
web2CacheSecretService.clear();
Expand Down Expand Up @@ -215,15 +216,16 @@ void shouldNotAddSecretWhenNull() {

// region updateSecret
@Test
void shouldUpdateSecret() throws NotAnExistingSecretException, SameSecretException {
void shouldUpdateSecret() throws NotAnExistingSecretException {
final Web2Secret encryptedSecret = new Web2Secret(OWNER_ADDRESS, SECRET_ADDRESS, ENCRYPTED_SECRET_VALUE);
final String newSecretValue = "newSecretValue";
final String newEncryptedSecretValue = "newEncryptedSecretValue";
web2SecretRepository.save(encryptedSecret);
when(encryptionService.encrypt(newSecretValue))
.thenReturn(newEncryptedSecretValue);

final Web2Secret newSecret = web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, newSecretValue);
web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, newSecretValue);
final Web2Secret newSecret = web2SecretRepository.findById(encryptedSecret.getHeader()).orElseThrow();
assertAll(
() -> assertThat(newSecret).extracting(Web2Secret::getHeader).usingRecursiveComparison().isEqualTo(new Web2SecretHeader(OWNER_ADDRESS, SECRET_ADDRESS)),
() -> assertThat(newSecret).extracting(Web2Secret::getValue).isEqualTo(newEncryptedSecretValue),
Expand All @@ -243,17 +245,17 @@ void shouldNotUpdateSecretIfMissing() {
}

@Test
void shouldNotUpdateSecretIfSameValue() {
void shouldNotUpdateSecretIfSameValue() throws NotAnExistingSecretException {
final Web2Secret encryptedSecret = new Web2Secret(OWNER_ADDRESS, SECRET_ADDRESS, ENCRYPTED_SECRET_VALUE);
web2SecretRepository.save(encryptedSecret);
when(encryptionService.encrypt(PLAIN_SECRET_VALUE))
.thenReturn(ENCRYPTED_SECRET_VALUE);

final SameSecretException exception = assertThrows(SameSecretException.class,
() -> web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, PLAIN_SECRET_VALUE));
web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, PLAIN_SECRET_VALUE);
final Web2Secret newSecret = web2SecretRepository.findById(encryptedSecret.getHeader()).orElseThrow();
assertAll(
() -> assertEquals(OWNER_ADDRESS, exception.getOwnerAddress()),
() -> assertEquals(SECRET_ADDRESS, exception.getSecretAddress()),
() -> assertThat(newSecret).extracting(Web2Secret::getHeader).usingRecursiveComparison().isEqualTo(new Web2SecretHeader(OWNER_ADDRESS, SECRET_ADDRESS)),
() -> assertThat(newSecret).extracting(Web2Secret::getValue).isEqualTo(ENCRYPTED_SECRET_VALUE),
() -> assertThat(web2SecretRepository.count()).isOne()
);
}
Expand Down
Loading