use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class DlmsDatabase method getJreEncryptionKey.
private DbEncryptionKeyReference getJreEncryptionKey(final Date now) {
final DbEncryptionKeyReference jreEncryptionKey = new DbEncryptionKeyReference();
jreEncryptionKey.setEncryptionProviderType(EncryptionProviderType.JRE);
jreEncryptionKey.setReference("1");
jreEncryptionKey.setValidFrom(now);
jreEncryptionKey.setCreationTime(now);
jreEncryptionKey.setModificationTime(now);
jreEncryptionKey.setModifiedBy("DlmsDatabase (Cucumber)");
jreEncryptionKey.setVersion(1L);
return jreEncryptionKey;
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class DlmsDeviceSteps method createDlmsDeviceInSecretManagementDatabase.
private void createDlmsDeviceInSecretManagementDatabase(final DlmsDevice dlmsDevice, final Map<String, String> inputSettings) {
final String deviceType = inputSettings.getOrDefault(PlatformSmartmeteringKeys.DEVICE_TYPE, SMART_METER_E);
final List<SecretBuilder> secretBuilders = new ArrayList<>();
if (inputSettings.containsKey(PlatformSmartmeteringKeys.LLS1_ACTIVE) && "true".equals(inputSettings.get(PlatformSmartmeteringKeys.LLS1_ACTIVE))) {
secretBuilders.add(this.getAppropriateSecretBuilder(PlatformSmartmeteringKeys.PASSWORD, inputSettings));
} else if (this.isGasSmartMeter(deviceType)) {
secretBuilders.add(this.getAppropriateSecretBuilder(MBUS_DEFAULT_KEY, inputSettings));
/*
* Don't insert a default value for the M-Bus User key. So only
* enable the builder if an M-Bus User key is explicitly configured
* in the step data.
*/
if (inputSettings.containsKey(MBUS_USER_KEY)) {
secretBuilders.add(this.getAppropriateSecretBuilder(MBUS_USER_KEY, inputSettings));
}
if (inputSettings.containsKey(KEY_DEVICE_FIRMWARE_UPDATE_KEY)) {
secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_FIRMWARE_UPDATE_KEY, inputSettings));
}
} else if (this.isESmartMeter(deviceType)) {
secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_ENCRYPTIONKEY, inputSettings));
secretBuilders.add(this.getAppropriateSecretBuilder(PlatformSmartmeteringKeys.KEY_DEVICE_MASTERKEY, inputSettings));
secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_AUTHENTICATIONKEY, inputSettings));
}
final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
secretBuilders.stream().filter(Objects::nonNull).map(builder -> builder.withDeviceIdentification(dlmsDevice.getDeviceIdentification()).withEncryptionKeyReference(encryptionKeyRef)).map(SecretBuilder::build).forEach(this.encryptedSecretRepository::save);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementService method getCurrentKey.
private DbEncryptionKeyReference getCurrentKey() {
final Date now = new Date();
final List<DbEncryptionKeyReference> keyRefs = this.keyRepository.findByTypeAndValid(this.encryptionProviderType, now);
if (keyRefs.size() > 1) {
final String messageFormat = "Multiple encryption keys found of type %s that are valid at %s";
throw new IllegalStateException(String.format(messageFormat, this.encryptionProviderType, now));
} else if (keyRefs.isEmpty()) {
final String messageFormat = "No encryption key of type %s found that is valid at %s";
throw new NoSuchElementException(String.format(messageFormat, this.encryptionProviderType, now));
}
return keyRefs.get(0);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method getSecret.
private DbEncryptedSecret getSecret(final SecretType secretType, final int minutesOld) {
final DbEncryptedSecret secret = new DbEncryptedSecret();
secret.setDeviceIdentification(SOME_DEVICE);
secret.setSecretStatus(SecretStatus.NEW);
secret.setSecretType(secretType);
secret.setCreationTime(new Date(System.currentTimeMillis() - (minutesOld * 60000L)));
secret.setEncodedSecret("1234567890abcdef");
final DbEncryptionKeyReference encryptionKeyReference = new DbEncryptionKeyReference();
encryptionKeyReference.setEncryptionProviderType(EncryptionProviderType.HSM);
encryptionKeyReference.setReference("1");
secret.setEncryptionKeyReference(encryptionKeyReference);
return secret;
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method storeSecretsExistingSecret.
@Test
public void storeSecretsExistingSecret() throws Exception {
final TypedSecret typedSecret = new TypedSecret("n3w$3cr3t0000001".getBytes(), SecretType.E_METER_MASTER_KEY);
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("1");
final EncryptedSecret encryptedSecret = new EncryptedSecret(ENCRYPTION_PROVIDER_TYPE, "n3w$3cr3t0000001".getBytes());
final DbEncryptedSecret existingDbSecret = new DbEncryptedSecret();
existingDbSecret.setCreationTime(new Date());
existingDbSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
existingDbSecret.setEncodedSecret("1234567890ABCDEF");
existingDbSecret.setDeviceIdentification(SOME_DEVICE);
existingDbSecret.setEncryptionKeyReference(keyReference);
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(// encrypt new DB secret
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).isNotNull();
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();
}
Aggregations