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);
}
}
}
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);
}
}
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);
}
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));
}
Aggregations