use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class SecretBuilder method build.
@Override
public DbEncryptedSecret build() {
final DbEncryptedSecret securityKey = new DbEncryptedSecret();
securityKey.setDeviceIdentification(this.deviceIdentification);
securityKey.setSecretType(this.secretType);
securityKey.setEncodedSecret(this.key);
securityKey.setSecretStatus(this.status == null ? SecretStatus.ACTIVE : this.status);
securityKey.setCreationTime(this.creationTime == null ? new Date() : this.creationTime);
securityKey.setEncryptionKeyReference(this.encryptionKeyReference);
return securityKey;
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementService method getUpdatedSecretsForActivation.
private List<DbEncryptedSecret> getUpdatedSecretsForActivation(final String deviceIdentification, final SecretType secretType) {
final List<DbEncryptedSecret> updatedSecrets = new ArrayList<>();
final Optional<DbEncryptedSecret> newSecretOptional = this.getSingleDbEncryptedSecret(deviceIdentification, secretType, SecretStatus.NEW);
if (newSecretOptional.isPresent()) {
final Optional<DbEncryptedSecret> activeSecretOptional = this.getSingleDbEncryptedSecret(deviceIdentification, secretType, SecretStatus.ACTIVE);
if (activeSecretOptional.isPresent()) {
final DbEncryptedSecret currentSecret = activeSecretOptional.get();
currentSecret.setSecretStatus(SecretStatus.EXPIRED);
updatedSecrets.add(currentSecret);
}
final DbEncryptedSecret newSecret = newSecretOptional.get();
newSecret.setSecretStatus(SecretStatus.ACTIVE);
updatedSecrets.add(newSecret);
} else {
log.info("No new secret of secret type {} present for activation.", secretType);
}
return updatedSecrets;
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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.DbEncryptedSecret in project open-smart-grid-platform by OSGP.
the class SecretManagementServiceTest method activateSecretsWithCurrentActiveSecret.
@Test
public void activateSecretsWithCurrentActiveSecret() {
final DbEncryptedSecret newSecret = new DbEncryptedSecret();
newSecret.setId(1L);
newSecret.setSecretStatus(SecretStatus.NEW);
final DbEncryptedSecret activeSecret = new DbEncryptedSecret();
newSecret.setId(2L);
newSecret.setSecretStatus(SecretStatus.ACTIVE);
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.ACTIVE)).thenReturn(Arrays.asList(activeSecret));
when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.NEW)).thenReturn(Arrays.asList(newSecret));
when(this.secretRepository.saveAll(any())).thenReturn(Arrays.asList(newSecret, activeSecret));
this.service.activateNewSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY));
assertThat(newSecret.getSecretStatus()).isEqualTo(SecretStatus.ACTIVE);
assertThat(activeSecret.getSecretStatus()).isEqualTo(SecretStatus.EXPIRED);
}
use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret 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