Skip to content

Commit

Permalink
GMS-179 Additional backend side validation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-szrnka committed Dec 11, 2024
1 parent 3710101 commit 119130d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 119130d

Please sign in to comment.