use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method storeSecretsEncryptionError.
@Test
public void storeSecretsEncryptionError() throws EncrypterException {
final TypedSecret typedSecret = new TypedSecret("$3cr3t".getBytes(), SecretType.E_METER_MASTER_KEY);
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("keyReferenceString");
when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenThrow(new EncrypterException("Encryption error"));
assertThatIllegalStateException().isThrownBy(() -> this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret)));
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method retrieveSecrets.
@Test
public void retrieveSecrets() throws Exception {
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("1");
final DbEncryptedSecret secret = new DbEncryptedSecret();
secret.setSecretType(SecretType.E_METER_MASTER_KEY);
secret.setEncryptionKeyReference(keyReference);
secret.setEncodedSecret("ABCDEF01234567890123456789ABCDEF");
final List<DbEncryptedSecret> secretList = Arrays.asList(secret);
final byte[] decryptedSecret = "secret0000000001".getBytes();
final byte[] rsaSecret = "1000000000terces".getBytes();
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.ACTIVE)).thenReturn(secretList);
when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(decryptedSecret);
when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
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(rsaSecret);
assertThat(typedSecret.getSecretType()).isEqualTo(SecretType.E_METER_MASTER_KEY);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method retrieveSecretsDecryptionError.
@Test
public void retrieveSecretsDecryptionError() throws EncrypterException {
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("1");
final DbEncryptedSecret secret = new DbEncryptedSecret();
secret.setSecretType(SecretType.E_METER_MASTER_KEY);
secret.setEncryptionKeyReference(keyReference);
secret.setEncodedSecret("0123456789ABCDEF0123456789ABCDEF");
final List<DbEncryptedSecret> secretPage = Arrays.asList(secret);
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.ACTIVE)).thenReturn(secretPage);
when(this.encryptionDelegate.decrypt(any(), any())).thenThrow(new EncrypterException("Decryption error"));
assertThatIllegalStateException().isThrownBy(() -> this.service.retrieveSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY)));
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementService method generateAes128BitsSecret.
private EncryptedTypedSecret generateAes128BitsSecret(final SecretType secretType) {
try {
final DbEncryptionKeyReference currentKey = this.getCurrentKey();
final byte[] aesEncrypted = this.encryptionDelegateForKeyStorage.generateAes128BitsSecret(this.encryptionProviderType, currentKey.getReference());
return new EncryptedTypedSecret(aesEncrypted, secretType, currentKey.getReference(), currentKey.getEncryptionProviderType());
} catch (final EncrypterException ee) {
throw new IllegalStateException("Eror generating secret", ee);
}
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementService method reencryptRsa2Aes.
private EncryptedTypedSecret reencryptRsa2Aes(final EncryptedTypedSecret secret) {
final byte[] aesEncrypted = this.reencryptRsa2Aes(secret.encryptedSecret);
final DbEncryptionKeyReference currentKey = this.getCurrentKey();
return new EncryptedTypedSecret(aesEncrypted, secret.type, currentKey.getReference(), currentKey.getEncryptionProviderType());
}
Aggregations