generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into 644-apierror-durch-wlsexception-ersetzen
- Loading branch information
Showing
14 changed files
with
254 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
...va/de/muenchen/oss/wahllokalsystem/authservice/configuration/EncryptionConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ls/common/security/EncryptionBuilder.java → ...thservice/security/EncryptionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 55 additions & 7 deletions
62
...vice/src/main/java/de/muenchen/oss/wahllokalsystem/authservice/service/CryptoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,85 @@ | ||
package de.muenchen.oss.wahllokalsystem.authservice.service; | ||
|
||
import de.muenchen.oss.wahllokalsystem.wls.common.security.EncryptionBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
import de.muenchen.oss.wahllokalsystem.authservice.exception.ExceptionConstants; | ||
import de.muenchen.oss.wahllokalsystem.wls.common.exception.TechnischeWlsException; | ||
import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ServiceIDFormatter; | ||
import java.util.Base64; | ||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Getter | ||
@Setter | ||
public class CryptoService { | ||
|
||
private final EncryptionBuilder encryptionBuilder; | ||
private final ServiceIDFormatter formatter; | ||
private final Cipher encryptionCipher; | ||
private final Cipher decryptionCipher; | ||
|
||
@Value("${service.config.crypto.encryptionPrefix}") | ||
private String encryptedPrefix = ""; | ||
|
||
public CryptoService(ServiceIDFormatter formatter, | ||
@Qualifier("encryptionCipher") Cipher encryptionCipher, | ||
@Qualifier("decryptionCipher") Cipher decryptionCipher) { | ||
this.formatter = formatter; | ||
this.encryptionCipher = encryptionCipher; | ||
this.decryptionCipher = decryptionCipher; | ||
} | ||
|
||
public boolean isEncrypted(final String value) { | ||
return value.startsWith(encryptedPrefix); | ||
} | ||
|
||
public String encrypt(final String value) { | ||
return encryptedPrefix + encryptionBuilder.encryptValue(value); | ||
return encryptedPrefix + encryptValue(value); | ||
} | ||
|
||
public String decrypt(final String value) { | ||
if (value.startsWith(encryptedPrefix)) { | ||
if (isEncrypted(value)) { | ||
val encryptedSubstring = value.substring(encryptedPrefix.length()); | ||
return encryptionBuilder.decryptValue(encryptedSubstring); | ||
return decryptValue(encryptedSubstring); | ||
} else { | ||
log.warn("value was already decrypted"); | ||
return value; | ||
} | ||
} | ||
|
||
private String decryptValue(String value) { | ||
if (value != null && !value.isEmpty()) { | ||
try { | ||
val decode = Base64.getUrlDecoder().decode(value.getBytes()); | ||
val finalized = decryptionCipher.doFinal(decode); | ||
return new String(finalized); | ||
} catch (IllegalBlockSizeException | BadPaddingException e) { | ||
log.error("Unable to decrypt the value due to " + e.getClass().getSimpleName() + ". Using direct object reference!", e); | ||
throw TechnischeWlsException.withCode(ExceptionConstants.CRYPTO_EXCEPTION_CODE).inService(formatter.getId()) | ||
.buildWithMessage("Problem bei der Entschlüsselung von Objekt-Referenzen"); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
private String encryptValue(String value) { | ||
if (value != null && !value.isEmpty()) { | ||
try { | ||
val finalized = encryptionCipher.doFinal(value.getBytes()); | ||
value = Base64.getUrlEncoder().encodeToString(finalized); | ||
} catch (IllegalBlockSizeException | BadPaddingException e) { | ||
log.error("Unable to encrypt the value due to " + e.getClass().getSimpleName() + ". Using direct object reference!", e); | ||
throw TechnischeWlsException.withCode(ExceptionConstants.CRYPTO_EXCEPTION_CODE).inService(formatter.getId()) | ||
.buildWithMessage("Problem bei der Verschlüsselung von Objekt-Referenzen"); | ||
} | ||
} | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ spring: | |
|
||
service: | ||
config: | ||
crypto: | ||
key: "please change me" | ||
oauth2: | ||
jwk: | ||
rsa: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ava/de/muenchen/oss/wahllokalsystem/authservice/service/CryptoServiceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.muenchen.oss.wahllokalsystem.authservice.service; | ||
|
||
import de.muenchen.oss.wahllokalsystem.authservice.configuration.AESEncryptionConfiguration; | ||
import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ServiceIDFormatter; | ||
import lombok.val; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest( | ||
classes = { AESEncryptionConfiguration.class, CryptoService.class, ServiceIDFormatter.class }, | ||
properties = { "service.config.crypto.key = 770A8A65DA156D24EE2A093277530142" } | ||
) | ||
class CryptoServiceIntegrationTest { | ||
|
||
@Autowired | ||
CryptoService cryptoService; | ||
|
||
@Test | ||
void should_useProvidedBeans_when_startingContext() { | ||
val valueToEncrypt = "Mzc2NTI2NzIzQUZEQUIzRA=="; | ||
Assertions.assertThat(cryptoService.decrypt(cryptoService.encrypt(valueToEncrypt))).isEqualTo(valueToEncrypt); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
.../src/test/java/de/muenchen/oss/wahllokalsystem/authservice/service/CryptoServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package de.muenchen.oss.wahllokalsystem.authservice.service; | ||
|
||
import de.muenchen.oss.wahllokalsystem.authservice.exception.ExceptionConstants; | ||
import de.muenchen.oss.wahllokalsystem.wls.common.exception.TechnischeWlsException; | ||
import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ServiceIDFormatter; | ||
import java.util.Base64; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import lombok.val; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CryptoServiceTest { | ||
|
||
private static final String ENCRYPTION_PREFIX = "encryptionPrefix"; | ||
|
||
@Mock | ||
ServiceIDFormatter idFormatter; | ||
|
||
@Mock | ||
Cipher cipher; | ||
|
||
@InjectMocks | ||
CryptoService unitUnderTest; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
unitUnderTest.setEncryptedPrefix(ENCRYPTION_PREFIX); | ||
} | ||
|
||
@Nested | ||
class IsEncrypted { | ||
|
||
@Test | ||
void should_returnTrue_when_valueStartsWithPrefix() { | ||
Assertions.assertThat(unitUnderTest.isEncrypted(ENCRYPTION_PREFIX + "the encrypted value")).isTrue(); | ||
} | ||
|
||
@Test | ||
void should_returnFalse_when_valueDoesNotStartWithPrefix() { | ||
unitUnderTest.setEncryptedPrefix("prefix"); | ||
Assertions.assertThat(unitUnderTest.isEncrypted("values without encryption prefix")).isFalse(); | ||
} | ||
} | ||
|
||
@Nested | ||
class Encrypt { | ||
|
||
@Test | ||
void should_returnEncryptedValueWithPrefix_when_valueIsGiven() throws Exception { | ||
val valueToEncrypt = "hello world"; | ||
|
||
val mockedEncryptedValue = "encrypted value".getBytes(); | ||
Mockito.when(cipher.doFinal(valueToEncrypt.getBytes())).thenReturn(mockedEncryptedValue); | ||
|
||
val result = unitUnderTest.encrypt(valueToEncrypt); | ||
|
||
val expectedResult = ENCRYPTION_PREFIX + Base64.getEncoder().encodeToString(mockedEncryptedValue); | ||
Assertions.assertThat(result).isEqualTo(expectedResult); | ||
} | ||
|
||
@Test | ||
void should_returnEncryptionPrefix_when_emptyStringValueIsGiven() { | ||
Assertions.assertThat(unitUnderTest.encrypt("")).isEqualTo(ENCRYPTION_PREFIX); | ||
} | ||
|
||
@Test | ||
void should_returnEncryptionPrefix_when_nullIsGiven() { | ||
Assertions.assertThat(unitUnderTest.encrypt(null)).isEqualTo(ENCRYPTION_PREFIX + null); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("de.muenchen.oss.wahllokalsystem.authservice.service.CryptoServiceTest#exceptionsMappedToWlsException") | ||
void should_throwTechnischeWlsException_when_cipherThrowsException(final Exception exceptionThrownByCipher) throws Exception { | ||
val valueToEncrypt = "hello world"; | ||
|
||
val mockedServiceID = "authService"; | ||
Mockito.when(idFormatter.getId()).thenReturn(mockedServiceID); | ||
Mockito.doThrow(exceptionThrownByCipher).when(cipher).doFinal(valueToEncrypt.getBytes()); | ||
|
||
val expectedException = TechnischeWlsException.withCode(ExceptionConstants.CRYPTO_EXCEPTION_CODE).inService(mockedServiceID) | ||
.buildWithMessage(""); | ||
|
||
Assertions.assertThatThrownBy(() -> unitUnderTest.encrypt(valueToEncrypt)) | ||
.satisfies(exception -> { | ||
Assertions.assertThat(exception) | ||
.usingRecursiveComparison() | ||
.ignoringFields("message") | ||
.isEqualTo(expectedException); | ||
Assertions.assertThat(exception).hasNoNullFieldsOrProperties(); | ||
}); | ||
} | ||
} | ||
|
||
@Nested | ||
class Decrypt { | ||
|
||
@Test | ||
void should_returnValue_when_valueIsNotEncrypted() { | ||
val notEncryptedValue = Base64.getEncoder().encodeToString("not encrypted value".getBytes()); | ||
Assertions.assertThat(unitUnderTest.decrypt(notEncryptedValue)).isEqualTo(notEncryptedValue); | ||
} | ||
|
||
@Test | ||
void should_returnDecryptedValue_when_valueIsGiven() throws Exception { | ||
val encryptedValue = "the encrypted value"; | ||
val encryptedValueAsBase64WithPrefix = ENCRYPTION_PREFIX + Base64.getEncoder().encodeToString(encryptedValue.getBytes()); | ||
|
||
val mockDecrypted = "decrypted value"; | ||
Mockito.when(cipher.doFinal(encryptedValue.getBytes())).thenReturn(mockDecrypted.getBytes()); | ||
|
||
val result = unitUnderTest.decrypt(encryptedValueAsBase64WithPrefix); | ||
|
||
Assertions.assertThat(result).isEqualTo(mockDecrypted); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("de.muenchen.oss.wahllokalsystem.authservice.service.CryptoServiceTest#exceptionsMappedToWlsException") | ||
void should_throwTechnischeWlsException_when_cipherThrowsException(final Exception exceptionThrownByCipher) throws Exception { | ||
val encryptedValue = "the encrypted value"; | ||
val encryptedValueAsBase64WithPrefix = ENCRYPTION_PREFIX + Base64.getEncoder().encodeToString(encryptedValue.getBytes()); | ||
|
||
val mockedServiceID = "authService"; | ||
Mockito.when(idFormatter.getId()).thenReturn(mockedServiceID); | ||
Mockito.doThrow(exceptionThrownByCipher).when(cipher).doFinal(encryptedValue.getBytes()); | ||
|
||
val expectedException = TechnischeWlsException.withCode(ExceptionConstants.CRYPTO_EXCEPTION_CODE).inService(mockedServiceID) | ||
.buildWithMessage(""); | ||
|
||
Assertions.assertThatThrownBy(() -> unitUnderTest.decrypt(encryptedValueAsBase64WithPrefix)) | ||
.satisfies(exception -> { | ||
Assertions.assertThat(exception) | ||
.usingRecursiveComparison() | ||
.ignoringFields("message") | ||
.isEqualTo(expectedException); | ||
Assertions.assertThat(exception).hasNoNullFieldsOrProperties(); | ||
}); | ||
} | ||
} | ||
|
||
public static Stream<Arguments> exceptionsMappedToWlsException() { | ||
val exceptions = Set.of(new IllegalBlockSizeException(), new BadPaddingException()); | ||
|
||
return exceptions.stream().map(exception -> Arguments.of(exception, exception.getClass().getName())); | ||
} | ||
} |
Oops, something went wrong.