SecretManagementService { public List<TypedSecret> retrieveSecrets(final String deviceIdentification, final List<SecretType> secretTypes) { try { return secretTypes.stream() .map(secretType -> this.retrieveSecret(deviceIdentification, secretType)) .map(Optional::get) .collect(Collectors.toList()); } catch (final Exception exc) { throw new IllegalStateException( String.format("Something went wrong retrieving secrets for device %s", deviceIdentification), exc); } } SecretManagementService(
            @Qualifier("DefaultEncryptionDelegate") final EncryptionDelegate defaultEncryptionDelegate,
            final EncryptionProviderType encryptionProviderType, final DbEncryptedSecretRepository secretRepository,
            final DbEncryptionKeyRepository keyRepository); void storeSecrets(final String deviceIdentification, final List<TypedSecret> secrets); List<TypedSecret> retrieveSecrets(final String deviceIdentification, final List<SecretType> secretTypes); Optional<TypedSecret> retrieveSecret(final String deviceIdentification, final SecretType secretType);  }
@Test public void retrieveSecrets() throws Exception { final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference(); final DbEncryptedSecret secret = new DbEncryptedSecret(); secret.setSecretType(SecretType.E_METER_MASTER_KEY); secret.setEncryptionKeyReference(keyReference); final Secret decryptedSecret = new Secret("secret".getBytes()); when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(1L); when(this.secretRepository.findById(any())).thenReturn(Optional.of(secret)); when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(decryptedSecret); final List<TypedSecret> typedSecrets = this.service.retrieveSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY)); assertThat(typedSecrets).isNotNull(); assertThat(typedSecrets.size()).isEqualTo(1); final TypedSecret typedSecret = typedSecrets.get(0); assertThat(typedSecret.getSecret()).isEqualTo(HexUtils.toHexString("secret".getBytes())); assertThat(typedSecret.getSecretType()).isEqualTo(SecretType.E_METER_MASTER_KEY); }
@Test public void retrieveSecrets_decryptionError() { final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference(); final DbEncryptedSecret secret = new DbEncryptedSecret(); secret.setSecretType(SecretType.E_METER_MASTER_KEY); secret.setEncryptionKeyReference(keyReference); when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(1L); when(this.secretRepository.findById(1L)).thenReturn(Optional.of(secret)); when(this.encryptionDelegate.decrypt(any(), any())).thenThrow(new RuntimeException("Decryption error")); assertThatIllegalStateException().isThrownBy( () -> this.service.retrieveSecrets("SOME_DEVICE", Arrays.asList(SecretType.E_METER_MASTER_KEY))); }
@Test public void retrieveSecrets_secretWithoutKey() { final DbEncryptedSecret secret = new DbEncryptedSecret(); when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(1L); when(this.secretRepository.findById(1L)).thenReturn(Optional.of(secret)); assertThatIllegalStateException().isThrownBy( () -> this.service.retrieveSecrets("SOME_DEVICE", Arrays.asList(SecretType.E_METER_MASTER_KEY))); }
@Test public void retrieveSecrets_noSecrets() { when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(null); assertThatIllegalStateException().isThrownBy( () -> this.service.retrieveSecrets("SOME_DEVICE", Arrays.asList(SecretType.E_METER_MASTER_KEY))); }