use of org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret 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();
}
use of org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret in project open-smart-grid-platform by OSGP.
the class SoapEndpointDataTypeConverter method encryptAndConvertSoapTypedSecret.
private org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret encryptAndConvertSoapTypedSecret(final TypedSecret typedSecret) {
final org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret soapTypedSecret = new org.opensmartgridplatform.ws.schema.core.secret.management.TypedSecret();
final byte[] rsaSecret = typedSecret.getSecret();
soapTypedSecret.setSecret(HexUtils.toHexString(rsaSecret));
final SecretType secretType = typedSecret.getSecretType();
soapTypedSecret.setType(this.convertToSoapSecretType(secretType));
return soapTypedSecret;
}
use of org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret 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.TypedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method storeSecretsEncryptionError.
@Test
public void storeSecretsEncryptionError() throws EncrypterException {
final TypedSecret typedSecret = new TypedSecret("$3cr3t".getBytes(), SecretType.E_METER_MASTER_KEY);
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("keyReferenceString");
when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenThrow(new EncrypterException("Encryption error"));
assertThatIllegalStateException().isThrownBy(() -> this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret)));
}
use of org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method retrieveSecrets.
@Test
public void retrieveSecrets() throws Exception {
final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
keyReference.setReference("1");
final DbEncryptedSecret secret = new DbEncryptedSecret();
secret.setSecretType(SecretType.E_METER_MASTER_KEY);
secret.setEncryptionKeyReference(keyReference);
secret.setEncodedSecret("ABCDEF01234567890123456789ABCDEF");
final List<DbEncryptedSecret> secretList = Arrays.asList(secret);
final byte[] decryptedSecret = "secret0000000001".getBytes();
final byte[] rsaSecret = "1000000000terces".getBytes();
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.ACTIVE)).thenReturn(secretList);
when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(decryptedSecret);
when(this.encrypterForSecretManagementClient.encrypt(any())).thenReturn(rsaSecret);
final List<TypedSecret> typedSecrets = this.service.retrieveSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY));
assertThat(typedSecrets).isNotNull();
assertThat(typedSecrets.size()).isEqualTo(1);
final TypedSecret typedSecret = typedSecrets.get(0);
assertThat(typedSecret.getSecret()).isEqualTo(rsaSecret);
assertThat(typedSecret.getSecretType()).isEqualTo(SecretType.E_METER_MASTER_KEY);
}
Aggregations