Search in sources :

Example 26 with DbEncryptedSecret

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

Example 27 with DbEncryptedSecret

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

the class DlmsDeviceSteps method theEncryptedSecretTableInTheSecretManagementDatabaseShouldContainKeysForDevice.

@Then("the encrypted_secret table in the secret management database should contain {string} keys for device {string}")
public void theEncryptedSecretTableInTheSecretManagementDatabaseShouldContainKeysForDevice(final String secretTypeString, final String deviceIdentification, final Map<String, String> inputSettings) {
    final SecretType secretType = this.getSecretTypeByKeyTypeInputName(secretTypeString);
    for (final String keyName : inputSettings.keySet()) {
        final String secretStatus = inputSettings.get(keyName);
        final String dbEncryptedSecretValue = SecurityKey.valueOf(keyName).getDatabaseKey();
        final List<DbEncryptedSecret> dbEncryptedSecret = this.encryptedSecretRepository.findSecrets(deviceIdentification, secretType, SecretStatus.valueOf(secretStatus));
        assertThat(dbEncryptedSecret).withFailMessage("No dbEncryptedSecret for %s with status %s found", secretTypeString, secretStatus).isNotEmpty();
        final List<String> actualEncodedSecrets = dbEncryptedSecret.stream().map(DbEncryptedSecret::getEncodedSecret).collect(Collectors.toList());
        assertThat(actualEncodedSecrets).withFailMessage("Wrong dbEncryptedSecret for %s with status %s expected %s to be contained in: %s", secretTypeString, secretStatus, dbEncryptedSecretValue, actualEncodedSecrets).contains(dbEncryptedSecretValue);
    }
}
Also used : SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Then(io.cucumber.java.en.Then)

Example 28 with DbEncryptedSecret

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

the class DlmsDeviceSteps method theNewKeysAreStoredInTheDatabaseInAnotherEncryptionThenTheEncryptionOfTheKeysReceivedInTheSOAPRequest.

@Then("^the new keys are stored in the database in another encryption then the encryption of the keys received in the SOAP request$")
public void theNewKeysAreStoredInTheDatabaseInAnotherEncryptionThenTheEncryptionOfTheKeysReceivedInTheSOAPRequest() {
    final String keyDeviceIdentification = PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION;
    final String deviceIdentification = (String) ScenarioContext.current().get(keyDeviceIdentification);
    assertThat(deviceIdentification).as("Device identification must be in the scenario context for key " + keyDeviceIdentification).isNotNull();
    final String deviceDescription = "DLMS device with identification " + deviceIdentification;
    final DlmsDevice dlmsDevice = this.findExistingDlmsDevice(deviceIdentification);
    final DbEncryptedSecret masterKey = this.findExistingSecurityKey(dlmsDevice, E_METER_MASTER_KEY, "Master key");
    final String receivedMasterKey = (String) ScenarioContext.current().get(PlatformSmartmeteringKeys.KEY_DEVICE_MASTERKEY);
    assertThat(masterKey.getEncodedSecret()).as("Stored master key for " + deviceDescription + " must be different from received key").isNotEqualTo(receivedMasterKey);
    final DbEncryptedSecret authenticationKey = this.findExistingSecurityKey(dlmsDevice, E_METER_AUTHENTICATION_KEY, "Authentication key");
    final String receivedAuthenticationKey = (String) ScenarioContext.current().get(KEY_DEVICE_AUTHENTICATIONKEY);
    assertThat(authenticationKey.getEncodedSecret()).as("Stored authentication key for " + deviceDescription + " must be different from received key").isNotEqualTo(receivedAuthenticationKey);
    final DbEncryptedSecret encryptionKey = this.findExistingSecurityKey(dlmsDevice, E_METER_ENCRYPTION_KEY_UNICAST, "Encryption key");
    final String receivedEncryptionKey = (String) ScenarioContext.current().get(KEY_DEVICE_AUTHENTICATIONKEY);
    assertThat(encryptionKey.getEncodedSecret()).as("Stored encryption key for " + deviceDescription + " must be different from received key").isNotEqualTo(receivedEncryptionKey);
}
Also used : DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Then(io.cucumber.java.en.Then)

Example 29 with DbEncryptedSecret

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

the class DlmsDeviceSteps method findAllSecretsForDevice.

private List<DbEncryptedSecret> findAllSecretsForDevice(final String deviceIdentification) {
    final DbEncryptedSecret searchByIdExample = new DbEncryptedSecret();
    searchByIdExample.setDeviceIdentification(deviceIdentification);
    return this.encryptedSecretRepository.findAll(Example.of(searchByIdExample));
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)

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