From 119130d8b17c2cc18c61391d9e53003d0a1c75a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szrnka?= Date: Wed, 11 Dec 2024 12:07:13 +0100 Subject: [PATCH] GMS-179 Additional backend side validation implemented --- .../secret/SecretLengthValidatorService.java | 6 ++++++ .../gms/functions/secret/dto/SecretValueDto.java | 6 ++---- .../secret/SecretLengthValidatorServiceTest.java | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java index 5668771e..0681cc79 100644 --- a/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java +++ b/code/gms-backend/src/main/java/io/github/gms/functions/secret/SecretLengthValidatorService.java @@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.security.KeyStore; @@ -36,6 +37,10 @@ public class SecretLengthValidatorService { public BooleanValueDto validateValueLength(SecretValueDto dto) { try { + if (!StringUtils.hasText(dto.getValue())) { + return new BooleanValueDto(false); + } + KeystoreEntity keystoreEntity = keystoreRepository.findById(dto.getKeystoreId()) .orElseThrow(() -> new GmsException("KeystoreEntity not found!", GMS_002)); @@ -47,6 +52,7 @@ public BooleanValueDto validateValueLength(SecretValueDto dto) { .keystorePath(keystorePath + keystoreEntity.getUserId() + SLASH + keystoreEntity.getFileName()) .build()); + log.info("Input value size: {}", dto.getValue().length()); String encryptedValue = cryptoService.encrypt(dto.getValue(), new KeystorePair(keystoreAliasEntity, keystore)); log.info("Encrypted secret value size: {}", encryptedValue.length()); diff --git a/code/gms-backend/src/main/java/io/github/gms/functions/secret/dto/SecretValueDto.java b/code/gms-backend/src/main/java/io/github/gms/functions/secret/dto/SecretValueDto.java index 8e487e46..e75119f9 100644 --- a/code/gms-backend/src/main/java/io/github/gms/functions/secret/dto/SecretValueDto.java +++ b/code/gms-backend/src/main/java/io/github/gms/functions/secret/dto/SecretValueDto.java @@ -1,11 +1,9 @@ package io.github.gms.functions.secret.dto; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Getter +@Setter @Builder @NoArgsConstructor @AllArgsConstructor diff --git a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java index 76133953..20ed2d8c 100644 --- a/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java +++ b/code/gms-backend/src/test/java/io/github/gms/functions/secret/SecretLengthValidatorServiceTest.java @@ -60,6 +60,20 @@ public void setup() { addAppender(SecretLengthValidatorService.class); } + @Test + void validateValueLength_whenValueIsEmpty_thenReturnFalse() { + // arrange + SecretValueDto dto = createSecretValueDto(1L, 1L); + dto.setValue(null); + + // act + BooleanValueDto response = secretLengthValidatorService.validateValueLength(dto); + + // assert + assertNotNull(response); + assertFalse(response.getValue()); + } + @Test void validateValueLength_whenKeystoreEntityNotFound_thenReturnFalse() { // arrange @@ -150,6 +164,7 @@ void validateValueLength_whenEncryptionSucceeded_thenReturnTrue() assertNotNull(response); assertTrue(response.getValue()); assertLogContains(logAppender, "Encrypted secret value size: 14"); + assertLogContains(logAppender, "Input value size: 9"); verify(keystoreRepository).findById(1L); verify(keystoreAliasRepository).findById(1L); verify(keystoreDataService).getKeyStore(any(GetKeystore.class));