Search in sources :

Example 16 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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 17 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.

the class SecretManagementService method createDbEncrypted.

private DbEncryptedSecret createDbEncrypted(final String deviceIdentification, final EncryptedTypedSecret secret, final DbEncryptionKeyReference keyReference) {
    final Date now = new Date();
    final DbEncryptedSecret dbEncryptedSecret = new DbEncryptedSecret();
    dbEncryptedSecret.setDeviceIdentification(deviceIdentification);
    dbEncryptedSecret.setEncodedSecret(HexUtils.toHexString(secret.encryptedSecret));
    dbEncryptedSecret.setSecretType(secret.type);
    dbEncryptedSecret.setSecretStatus(SecretStatus.NEW);
    dbEncryptedSecret.setEncryptionKeyReference(keyReference);
    dbEncryptedSecret.setCreationTime(now);
    return dbEncryptedSecret;
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Example 18 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.

the class SecretManagementService method withdrawExistingKeysWithStatusNew.

private void withdrawExistingKeysWithStatusNew(final String deviceIdentification, final SecretType secretType) {
    // All NEW keys of the device and type are set to status WITHDRAWN.
    final List<DbEncryptedSecret> foundSecrets = this.secretRepository.findSecrets(deviceIdentification, secretType, SecretStatus.NEW);
    for (final DbEncryptedSecret foundSecret : foundSecrets) {
        foundSecret.setSecretStatus(SecretStatus.WITHDRAWN);
        this.secretRepository.save(foundSecret);
        log.warn(String.format("During (GenerateOr)Replace Key Process one or more keys with status NEW of type %s for " + "device %s have been found. These keys will be withdrawn (status WITHDRAWN)", secretType.name(), deviceIdentification));
    }
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)

Example 19 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.

the class SoapServiceSecretManagementIT method generateAndStoreSecrets.

@Test
public void generateAndStoreSecrets() throws IOException {
    final Resource generateAndStoreRequest = new ClassPathResource("test-requests/generateAndStoreSecrets.xml");
    this.mockWebServiceClient.sendRequest(withSoapEnvelope(generateAndStoreRequest)).andExpect(ResponseMatchers.noFault()).andExpect((request, response) -> {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        response.writeTo(outputStream);
        assertThat(outputStream.toString()).contains("Result>OK");
    });
    final List<DbEncryptedSecret> authKeys = this.secretRepository.findSecrets(DEVICE_IDENTIFICATION, SecretType.E_METER_AUTHENTICATION_KEY, SecretStatus.NEW);
    assertThat(authKeys).hasSize(1);
    final DbEncryptedSecret authKey = authKeys.get(0);
    assertThat(authKey.getEncodedSecret()).hasSize(64);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 20 with DbEncryptedSecret

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.

the class SoapServiceSecretManagementIT method createTestData.

/**
 * Create test data for encrypted secrets and related encryptionkey reference(s). So that the
 * EncryptionService can encrypt and decrypt, using the JRE encryption provider.
 *
 * <p>Two secrets (for two types of meter key secrets) and one reference key (valid as of
 * now-1minute) is created.
 */
private void createTestData() {
    final DbEncryptionKeyReference encryptionKey = new DbEncryptionKeyReference();
    encryptionKey.setCreationTime(new Date());
    encryptionKey.setReference("1");
    encryptionKey.setEncryptionProviderType(EncryptionProviderType.JRE);
    encryptionKey.setValidFrom(new Date(System.currentTimeMillis() - 60000));
    encryptionKey.setVersion(1L);
    this.testEntityManager.persist(encryptionKey);
    final DbEncryptedSecret encryptedSecret = new DbEncryptedSecret();
    encryptedSecret.setCreationTime(new Date());
    encryptedSecret.setDeviceIdentification(DEVICE_IDENTIFICATION);
    encryptedSecret.setSecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType.E_METER_AUTHENTICATION_KEY);
    encryptedSecret.setEncodedSecret(E_METER_AUTHENTICATION_KEY_ENCRYPTED_FOR_DB);
    encryptedSecret.setSecretStatus(SecretStatus.ACTIVE);
    encryptedSecret.setEncryptionKeyReference(encryptionKey);
    this.testEntityManager.persist(encryptedSecret);
    final DbEncryptedSecret encryptedSecret2 = new DbEncryptedSecret();
    encryptedSecret2.setCreationTime(new Date());
    encryptedSecret2.setDeviceIdentification(DEVICE_IDENTIFICATION);
    encryptedSecret2.setSecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType.E_METER_ENCRYPTION_KEY_UNICAST);
    encryptedSecret2.setEncodedSecret(E_METER_ENCRYPTION_KEY_UNICAST_ENCRYPTED_FOR_DB);
    encryptedSecret2.setSecretStatus(SecretStatus.ACTIVE);
    encryptedSecret2.setEncryptionKeyReference(encryptionKey);
    this.testEntityManager.persist(encryptedSecret2);
    this.testEntityManager.flush();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Aggregations

DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)29 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)13 Test (org.junit.jupiter.api.Test)12 Date (java.util.Date)11 Then (io.cucumber.java.en.Then)5 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)5 List (java.util.List)4 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)4 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)2 Given (io.cucumber.java.en.Given)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1