Search in sources :

Example 1 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class DlmsDatabase method getJreEncryptionKey.

private DbEncryptionKeyReference getJreEncryptionKey(final Date now) {
    final DbEncryptionKeyReference jreEncryptionKey = new DbEncryptionKeyReference();
    jreEncryptionKey.setEncryptionProviderType(EncryptionProviderType.JRE);
    jreEncryptionKey.setReference("1");
    jreEncryptionKey.setValidFrom(now);
    jreEncryptionKey.setCreationTime(now);
    jreEncryptionKey.setModificationTime(now);
    jreEncryptionKey.setModifiedBy("DlmsDatabase (Cucumber)");
    jreEncryptionKey.setVersion(1L);
    return jreEncryptionKey;
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)

Example 2 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class DlmsDeviceSteps method createDlmsDeviceInSecretManagementDatabase.

private void createDlmsDeviceInSecretManagementDatabase(final DlmsDevice dlmsDevice, final Map<String, String> inputSettings) {
    final String deviceType = inputSettings.getOrDefault(PlatformSmartmeteringKeys.DEVICE_TYPE, SMART_METER_E);
    final List<SecretBuilder> secretBuilders = new ArrayList<>();
    if (inputSettings.containsKey(PlatformSmartmeteringKeys.LLS1_ACTIVE) && "true".equals(inputSettings.get(PlatformSmartmeteringKeys.LLS1_ACTIVE))) {
        secretBuilders.add(this.getAppropriateSecretBuilder(PlatformSmartmeteringKeys.PASSWORD, inputSettings));
    } else if (this.isGasSmartMeter(deviceType)) {
        secretBuilders.add(this.getAppropriateSecretBuilder(MBUS_DEFAULT_KEY, inputSettings));
        /*
       * Don't insert a default value for the M-Bus User key. So only
       * enable the builder if an M-Bus User key is explicitly configured
       * in the step data.
       */
        if (inputSettings.containsKey(MBUS_USER_KEY)) {
            secretBuilders.add(this.getAppropriateSecretBuilder(MBUS_USER_KEY, inputSettings));
        }
        if (inputSettings.containsKey(KEY_DEVICE_FIRMWARE_UPDATE_KEY)) {
            secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_FIRMWARE_UPDATE_KEY, inputSettings));
        }
    } else if (this.isESmartMeter(deviceType)) {
        secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_ENCRYPTIONKEY, inputSettings));
        secretBuilders.add(this.getAppropriateSecretBuilder(PlatformSmartmeteringKeys.KEY_DEVICE_MASTERKEY, inputSettings));
        secretBuilders.add(this.getAppropriateSecretBuilder(KEY_DEVICE_AUTHENTICATIONKEY, inputSettings));
    }
    final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
    secretBuilders.stream().filter(Objects::nonNull).map(builder -> builder.withDeviceIdentification(dlmsDevice.getDeviceIdentification()).withEncryptionKeyReference(encryptionKeyRef)).map(SecretBuilder::build).forEach(this.encryptedSecretRepository::save);
}
Also used : SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 3 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference in project open-smart-grid-platform by OSGP.

the class SecretManagementService method getCurrentKey.

private DbEncryptionKeyReference getCurrentKey() {
    final Date now = new Date();
    final List<DbEncryptionKeyReference> keyRefs = this.keyRepository.findByTypeAndValid(this.encryptionProviderType, now);
    if (keyRefs.size() > 1) {
        final String messageFormat = "Multiple encryption keys found of type %s that are valid at %s";
        throw new IllegalStateException(String.format(messageFormat, this.encryptionProviderType, now));
    } else if (keyRefs.isEmpty()) {
        final String messageFormat = "No encryption key of type %s found that is valid at %s";
        throw new NoSuchElementException(String.format(messageFormat, this.encryptionProviderType, now));
    }
    return keyRefs.get(0);
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) Date(java.util.Date) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference 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 5 with DbEncryptionKeyReference

use of org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference 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

DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)25 Date (java.util.Date)16 Test (org.junit.jupiter.api.Test)13 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)13 TypedSecret (org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret)7 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)4 List (java.util.List)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)3 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)3 EncryptedSecret (org.opensmartgridplatform.shared.security.EncryptedSecret)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Given (io.cucumber.java.en.Given)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Transactional (org.springframework.transaction.annotation.Transactional)1