use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class DbEncryptedSecretRepositoryIT method findSecretsOutdatedKeyRef.
@Test
public void findSecretsOutdatedKeyRef() {
final Date now = new Date();
this.dbEncryptionKeyReference.setValidTo(now);
this.dbEncryptionKeyReference = this.entityManager.persist(this.dbEncryptionKeyReference);
DbEncryptionKeyReference newEncryptionKeyRef = new DbEncryptionKeyReference();
newEncryptionKeyRef.setCreationTime(now);
newEncryptionKeyRef.setReference("keyRef2");
newEncryptionKeyRef.setEncryptionProviderType(EncryptionProviderType.HSM);
newEncryptionKeyRef.setValidFrom(now);
newEncryptionKeyRef.setVersion(1L);
newEncryptionKeyRef = this.entityManager.persist(newEncryptionKeyRef);
final List<DbEncryptedSecret> secretsList = this.repository.findSecrets(this.dbEncryptedSecret.getDeviceIdentification(), this.dbEncryptedSecret.getSecretType(), SecretStatus.ACTIVE);
assertThat(secretsList).hasSize(1);
final DbEncryptedSecret secret = secretsList.get(0);
assertThat(secret.getId()).isEqualTo(this.dbEncryptedSecret.getId());
assertThat(secret.getEncryptionKeyReference().getId()).isEqualTo(this.dbEncryptionKeyReference.getId());
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method activateSecretsNoCurrentActiveSecret.
@Test
public void activateSecretsNoCurrentActiveSecret() {
final DbEncryptedSecret newSecret = new DbEncryptedSecret();
newSecret.setId(1L);
newSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
newSecret.setSecretStatus(SecretStatus.NEW);
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.NEW)).thenReturn(Arrays.asList(newSecret));
when(this.secretRepository.saveAll(Arrays.asList(newSecret))).thenReturn(Arrays.asList(newSecret));
this.service.activateNewSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY));
assertThat(newSecret.getSecretStatus()).isEqualTo(SecretStatus.ACTIVE);
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.getSecretType()).isEqualTo(newSecret.getSecretType());
assertThat(savedSecret.getSecretStatus()).isEqualTo(SecretStatus.ACTIVE);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method generateAndStoreSecretsWhenNewSecretsAlreadyExists.
@Test
public void generateAndStoreSecretsWhenNewSecretsAlreadyExists() throws Exception {
final Date now = new Date();
final String reference = "1";
final byte[] aesSecret = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
final byte[] secret = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
final byte[] rsaSecret = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setReference(reference);
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setValidFrom(now);
final DbEncryptedSecret secretOldEncryption = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 100);
final DbEncryptedSecret secretOlderEncryption = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 1000);
final Date originalCreationTimeEncryptionSecret = secretOldEncryption.getCreationTime();
final Date olderCreationTime = secretOlderEncryption.getCreationTime();
when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
when(this.encryptionDelegate.generateAes128BitsSecret(ENCRYPTION_PROVIDER_TYPE, reference)).thenReturn(aesSecret);
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_ENCRYPTION_KEY_UNICAST, SecretStatus.NEW)).thenReturn(Arrays.asList(secretOldEncryption, secretOlderEncryption));
when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(secret);
when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
final SecretType encryptionSecretType = SecretType.E_METER_ENCRYPTION_KEY_UNICAST;
final SecretType authenticationSecretType = SecretType.E_METER_AUTHENTICATION_KEY;
this.service.generateAndStoreSecrets(SOME_DEVICE, Arrays.asList(encryptionSecretType, authenticationSecretType));
verify(this.secretRepository, never()).saveAll(Arrays.asList(secretOldEncryption));
verify(this.secretRepository, never()).saveAll(Arrays.asList(secretOldEncryption));
assertThat(secretOldEncryption.getCreationTime()).isEqualTo(originalCreationTimeEncryptionSecret);
assertThat(secretOldEncryption.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
assertThat(secretOlderEncryption.getCreationTime().getTime()).isEqualTo(olderCreationTime.getTime());
assertThat(secretOlderEncryption.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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();
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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);
}
}
Aggregations