Search in sources :

Example 21 with DbEncryptionKeyReference

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

the class SecretManagementServiceTest method storeSecrets.

@Test
public void storeSecrets() throws Exception {
    final TypedSecret typedSecret = new TypedSecret("rsaSecret00000001".getBytes(), SecretType.E_METER_ENCRYPTION_KEY_UNICAST);
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setReference("1");
    final EncryptedSecret encryptedSecret = new EncryptedSecret(ENCRYPTION_PROVIDER_TYPE, "aesSecret0000001".getBytes());
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.keyRepository.findByTypeAndReference(ENCRYPTION_PROVIDER_TYPE, "1")).thenReturn(keyReference);
    when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenReturn(encryptedSecret);
    this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret));
    final ArgumentCaptor<List<DbEncryptedSecret>> secretListArgumentCaptor = this.getListOfDbEncryptedSecretsArgumentCaptor();
    verify(this.secretRepository).saveAll(secretListArgumentCaptor.capture());
    final List<DbEncryptedSecret> savedSecrets = secretListArgumentCaptor.getValue();
    assertThat(savedSecrets).isNotNull();
    assertThat(savedSecrets.size()).isEqualTo(1);
    final DbEncryptedSecret savedSecret = savedSecrets.get(0);
    assertThat(savedSecret.getDeviceIdentification()).isEqualTo(SOME_DEVICE);
    assertThat(savedSecret.getSecretType()).isEqualTo(typedSecret.getSecretType());
    assertThat(savedSecret.getEncodedSecret()).isEqualTo(HexUtils.toHexString(encryptedSecret.getSecret()));
    assertThat(savedSecret.getEncryptionKeyReference()).isEqualTo(keyReference);
    assertThat(savedSecret.getCreationTime()).isNotNull();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) List(java.util.List) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Test(org.junit.jupiter.api.Test)

Example 22 with DbEncryptionKeyReference

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

the class DbEncryptionKeyRepositoryIT method persistTestData.

@BeforeEach
public void persistTestData() {
    DbEncryptionKeyReference encryptionKey1 = new DbEncryptionKeyReference();
    encryptionKey1.setCreationTime(new Date());
    encryptionKey1.setReference("keyRef1");
    encryptionKey1.setEncryptionProviderType(EncryptionProviderType.HSM);
    encryptionKey1.setValidFrom(new Date(System.currentTimeMillis() - 60000));
    encryptionKey1.setVersion(1L);
    encryptionKey1 = this.entityManager.persist(encryptionKey1);
    DbEncryptionKeyReference encryptionKey2 = new DbEncryptionKeyReference();
    encryptionKey2.setCreationTime(new Date());
    encryptionKey2.setReference("keyRef2");
    encryptionKey2.setEncryptionProviderType(EncryptionProviderType.JRE);
    encryptionKey2.setValidFrom(new Date(System.currentTimeMillis() - 60000));
    encryptionKey2.setValidTo(new Date(System.currentTimeMillis() + 60000));
    encryptionKey2.setVersion(1L);
    encryptionKey2 = this.entityManager.persist(encryptionKey2);
    DbEncryptionKeyReference encryptionKey3 = new DbEncryptionKeyReference();
    encryptionKey3.setCreationTime(new Date());
    encryptionKey3.setReference("keyRef3");
    encryptionKey3.setEncryptionProviderType(EncryptionProviderType.JRE);
    encryptionKey3.setValidFrom(new Date(System.currentTimeMillis() - 3600000));
    encryptionKey3.setValidTo(new Date(System.currentTimeMillis() - 60000));
    encryptionKey3.setVersion(1L);
    encryptionKey3 = this.entityManager.persist(encryptionKey3);
    this.entityManager.flush();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with DbEncryptionKeyReference

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

the class DlmsDatabase method prepareDatabaseForScenario.

/**
 * Before each scenario dlms related stuff needs to be removed.
 */
@Transactional(transactionManager = "txMgrDlms")
public void prepareDatabaseForScenario() {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    this.dlmsDeviceRepo.deleteAllInBatch();
    this.secretRepository.deleteAllInBatch();
    this.encryptionKeyRepository.deleteAllInBatch();
    final DbEncryptionKeyReference jreEncryptionKey = this.getJreEncryptionKey(new Date());
    this.encryptionKeyRepository.save(jreEncryptionKey);
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with DbEncryptionKeyReference

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

the class DlmsDeviceSteps method simulateFailureOfChangeFromPreviousKeyOfDevice.

@Given("simulate failure of change from previous key of device \"{}\"")
public void simulateFailureOfChangeFromPreviousKeyOfDevice(final String id, final Map<String, String> inputSettings) {
    for (final String keyTypeInputName : inputSettings.keySet()) {
        final String securityTypeInputName = inputSettings.get(keyTypeInputName);
        final SecretType secretType = this.getSecretTypeByKeyTypeInputName(keyTypeInputName);
        final String key = SecurityKey.valueOf(securityTypeInputName).getDatabaseKey();
        final List<DbEncryptedSecret> currentlyActiveKeys = this.encryptedSecretRepository.findSecrets(id, secretType, SecretStatus.ACTIVE);
        for (final DbEncryptedSecret currentlyActiveKey : currentlyActiveKeys) {
            currentlyActiveKey.setSecretStatus(SecretStatus.NEW);
            this.encryptedSecretRepository.save(currentlyActiveKey);
        }
        final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
        final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(id).withSecretType(secretType).withKey(key).withSecretStatus(SecretStatus.ACTIVE).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date()).build();
        this.encryptedSecretRepository.save(secret);
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date) Given(io.cucumber.java.en.Given)

Example 25 with DbEncryptionKeyReference

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

the class DlmsDeviceSteps method registerNewKeys.

private void registerNewKeys(final long minutesAgo, final Map<String, String> inputSettings) {
    if (!inputSettings.containsKey(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION)) {
        throw new IllegalArgumentException("No device identification provided");
    }
    final String deviceIdentification = inputSettings.get(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION);
    final List<SecretType> secretTypesToCreate = Arrays.asList(E_METER_AUTHENTICATION_KEY, E_METER_ENCRYPTION_KEY_UNICAST);
    final List<String> keyTypeInputNames = secretTypesToCreate.stream().map(this::getKeyTypeInputName).collect(Collectors.toList());
    if (Collections.disjoint(inputSettings.keySet(), keyTypeInputNames)) {
        throw new IllegalArgumentException("None of the following keys provided: " + keyTypeInputNames);
    }
    final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
    for (int i = 0; i < secretTypesToCreate.size(); i++) {
        if (inputSettings.containsKey(keyTypeInputNames.get(i))) {
            final String inputKeyName = inputSettings.get(keyTypeInputNames.get(i));
            final String key = SecurityKey.valueOf(inputKeyName).getDatabaseKey();
            final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(deviceIdentification).withSecretType(secretTypesToCreate.get(i)).withKey(key).withSecretStatus(SecretStatus.NEW).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date(System.currentTimeMillis() - (minutesAgo * 60000L))).build();
            this.encryptedSecretRepository.save(secret);
        }
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

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