use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class DbEncryptionKeyRepositoryIT method findNoValidTo.
@Test
public void findNoValidTo() {
final List<DbEncryptionKeyReference> results = this.repository.findByTypeAndValid(EncryptionProviderType.HSM, new Date());
assertThat(results.size()).isEqualTo(1);
final DbEncryptionKeyReference keyReference = results.get(0);
assertThat(keyReference).isNotNull();
assertThat(keyReference.getId()).isNotNull();
assertThat(keyReference.getEncryptionProviderType()).isEqualTo(EncryptionProviderType.HSM);
assertThat(keyReference.getReference()).isEqualTo("keyRef1");
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class DbEncryptionKeyRepositoryIT method findValidTo.
@Test
public void findValidTo() {
final List<DbEncryptionKeyReference> results = this.repository.findByTypeAndValid(EncryptionProviderType.JRE, new Date());
assertThat(results.size()).isEqualTo(1);
final DbEncryptionKeyReference keyReference = results.get(0);
assertThat(keyReference).isNotNull();
assertThat(keyReference.getId()).isNotNull();
assertThat(keyReference.getEncryptionProviderType()).isEqualTo(EncryptionProviderType.JRE);
assertThat(keyReference.getReference()).isEqualTo("keyRef2");
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class DbEncryptedSecretRepositoryIT method persistTestData.
@BeforeEach
public void persistTestData() {
this.dbEncryptionKeyReference = new DbEncryptionKeyReference();
this.dbEncryptionKeyReference.setCreationTime(new Date());
this.dbEncryptionKeyReference.setReference("keyRef1");
this.dbEncryptionKeyReference.setEncryptionProviderType(EncryptionProviderType.HSM);
this.dbEncryptionKeyReference.setValidFrom(new Date(System.currentTimeMillis() - 60000));
this.dbEncryptionKeyReference.setVersion(1L);
this.dbEncryptionKeyReference = this.entityManager.persist(this.dbEncryptionKeyReference);
final DbEncryptedSecret instance = new DbEncryptedSecret();
instance.setDeviceIdentification(DEVICE_IDENTIFICATION);
instance.setSecretType(SecretType.E_METER_AUTHENTICATION_KEY);
instance.setSecretStatus(SecretStatus.ACTIVE);
instance.setEncodedSecret(HexUtils.toHexString("$3cr3t".getBytes()));
instance.setEncryptionKeyReference(this.dbEncryptionKeyReference);
instance.setCreationTime(new Date());
this.dbEncryptedSecret = this.entityManager.persist(instance);
this.entityManager.flush();
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method storeSecretWhenNewSecretAlreadyExists.
@Test
public void storeSecretWhenNewSecretAlreadyExists() 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());
final DbEncryptedSecret dbEncryptedSecret = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 100);
when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
when(this.keyRepository.findByTypeAndReference(ENCRYPTION_PROVIDER_TYPE, "1")).thenReturn(keyReference);
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_ENCRYPTION_KEY_UNICAST, SecretStatus.NEW)).thenReturn(Arrays.asList(dbEncryptedSecret));
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();
final ArgumentCaptor<DbEncryptedSecret> secretToWithdrawArgumentCaptor = ArgumentCaptor.forClass(DbEncryptedSecret.class);
verify(this.secretRepository, times(1)).save(secretToWithdrawArgumentCaptor.capture());
final DbEncryptedSecret savedSecretToWithdraw = secretToWithdrawArgumentCaptor.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.getEncryptionKeyReference()).isEqualTo(keyReference);
assertThat(savedSecret.getCreationTime()).isNotNull();
assertThat(savedSecret.getEncodedSecret()).isEqualTo(HexUtils.toHexString(encryptedSecret.getSecret()));
assertThat(savedSecret.getSecretStatus()).isEqualTo(SecretStatus.NEW);
assertThat(savedSecretToWithdraw.getDeviceIdentification()).isEqualTo(SOME_DEVICE);
assertThat(savedSecretToWithdraw.getSecretType()).isEqualTo(typedSecret.getSecretType());
assertThat(savedSecretToWithdraw.getEncryptionKeyReference()).isEqualTo(dbEncryptedSecret.getEncryptionKeyReference());
assertThat(savedSecretToWithdraw.getCreationTime()).isNotNull();
assertThat(savedSecretToWithdraw.getEncodedSecret()).isEqualTo(dbEncryptedSecret.getEncodedSecret());
assertThat(savedSecretToWithdraw.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method generateAndStoreSecretWhenNewSecretAlreadyExists.
@Test
public void generateAndStoreSecretWhenNewSecretAlreadyExists() 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 DbEncryptedSecret dbEncryptedSecret = this.getSecret(SecretType.E_METER_ENCRYPTION_KEY_UNICAST, 100);
final Date originalCreationTime = dbEncryptedSecret.getCreationTime();
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setReference(reference);
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setValidFrom(now);
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(dbEncryptedSecret));
when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(secret);
when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
final SecretType secretType = SecretType.E_METER_ENCRYPTION_KEY_UNICAST;
this.service.generateAndStoreSecrets(SOME_DEVICE, Arrays.asList(secretType));
final List<DbEncryptedSecret> foundSecrets = this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_ENCRYPTION_KEY_UNICAST, SecretStatus.NEW);
assertThat(foundSecrets).hasSize(1);
verify(this.secretRepository, never()).saveAll(Arrays.asList(dbEncryptedSecret));
assertThat(dbEncryptedSecret.getCreationTime()).isEqualTo(originalCreationTime);
assertThat(dbEncryptedSecret.getSecretStatus()).isEqualTo(SecretStatus.WITHDRAWN);
}
Aggregations