Search in sources :

Example 6 with DbEncryptedSecret

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;
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Example 7 with DbEncryptedSecret

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;
}
Also used : ArrayList(java.util.ArrayList) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)

Example 8 with DbEncryptedSecret

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;
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Example 9 with DbEncryptedSecret

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);
}
Also used : DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Test(org.junit.jupiter.api.Test)

Example 10 with DbEncryptedSecret

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();
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) List(java.util.List) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) EncryptedSecret(org.opensmartgridplatform.shared.security.EncryptedSecret) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)29 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)13 Test (org.junit.jupiter.api.Test)12 Date (java.util.Date)11 Then (io.cucumber.java.en.Then)5 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)5 List (java.util.List)4 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)4 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)2 Given (io.cucumber.java.en.Given)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1