Search in sources :

Example 11 with DbEncryptionKeyReference

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)));
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) Test(org.junit.jupiter.api.Test)

Example 12 with DbEncryptionKeyReference

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);
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) Test(org.junit.jupiter.api.Test)

Example 13 with DbEncryptionKeyReference

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)));
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Test(org.junit.jupiter.api.Test)

Example 14 with DbEncryptionKeyReference

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);
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 15 with DbEncryptionKeyReference

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());
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)

Aggregations

DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)25 Date (java.util.Date)16 Test (org.junit.jupiter.api.Test)13 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)13 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)7 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)4 List (java.util.List)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)3 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Given (io.cucumber.java.en.Given)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Transactional (org.springframework.transaction.annotation.Transactional)1