Search in sources :

Example 1 with SecretBuilder

use of org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder in project open-smart-grid-platform by OSGP.

the class DlmsDeviceSteps method getAppropriateSecretBuilder.

private SecretBuilder getAppropriateSecretBuilder(final String keyTypeInputName, final Map<String, String> inputSettings) {
    final SecurityKeyType keyType = this.securityKeyTypesByInputName.get(keyTypeInputName);
    if (keyType == null) {
        throw new IllegalArgumentException(String.format("Unknown key type name %s; available types names: %s", keyTypeInputName, this.securityKeyTypesByInputName.keySet()));
    }
    if (inputSettings.containsKey(keyTypeInputName)) {
        final String inputKeyName = inputSettings.get(keyTypeInputName);
        final String dbEncryptedKey = SecurityKey.valueOf(inputKeyName).getDatabaseKey();
        if (dbEncryptedKey != null && !dbEncryptedKey.trim().isEmpty()) {
            return new SecretBuilder().withSecurityKeyType(keyType).withKey(dbEncryptedKey);
        } else {
            // secret storing
            return null;
        }
    } else {
        return this.getDefaultSecretBuilder(keyType);
    }
}
Also used : SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)

Example 2 with SecretBuilder

use of org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder 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 SecretBuilder

use of org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder in project open-smart-grid-platform by OSGP.

the class DlmsDeviceSteps method simulateFailureOfChangeFromPreviousKeyOfDevice.

@Given("simulate failure of change from previous key of device \"{}\"")
public void simulateFailureOfChangeFromPreviousKeyOfDevice(final String id, final Map<String, String> inputSettings) {
    for (final String keyTypeInputName : inputSettings.keySet()) {
        final String securityTypeInputName = inputSettings.get(keyTypeInputName);
        final SecretType secretType = this.getSecretTypeByKeyTypeInputName(keyTypeInputName);
        final String key = SecurityKey.valueOf(securityTypeInputName).getDatabaseKey();
        final List<DbEncryptedSecret> currentlyActiveKeys = this.encryptedSecretRepository.findSecrets(id, secretType, SecretStatus.ACTIVE);
        for (final DbEncryptedSecret currentlyActiveKey : currentlyActiveKeys) {
            currentlyActiveKey.setSecretStatus(SecretStatus.NEW);
            this.encryptedSecretRepository.save(currentlyActiveKey);
        }
        final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
        final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(id).withSecretType(secretType).withKey(key).withSecretStatus(SecretStatus.ACTIVE).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date()).build();
        this.encryptedSecretRepository.save(secret);
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date) Given(io.cucumber.java.en.Given)

Example 4 with SecretBuilder

use of org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder in project open-smart-grid-platform by OSGP.

the class DlmsDeviceSteps method registerNewKeys.

private void registerNewKeys(final long minutesAgo, final Map<String, String> inputSettings) {
    if (!inputSettings.containsKey(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION)) {
        throw new IllegalArgumentException("No device identification provided");
    }
    final String deviceIdentification = inputSettings.get(PlatformSmartmeteringKeys.DEVICE_IDENTIFICATION);
    final List<SecretType> secretTypesToCreate = Arrays.asList(E_METER_AUTHENTICATION_KEY, E_METER_ENCRYPTION_KEY_UNICAST);
    final List<String> keyTypeInputNames = secretTypesToCreate.stream().map(this::getKeyTypeInputName).collect(Collectors.toList());
    if (Collections.disjoint(inputSettings.keySet(), keyTypeInputNames)) {
        throw new IllegalArgumentException("None of the following keys provided: " + keyTypeInputNames);
    }
    final DbEncryptionKeyReference encryptionKeyRef = this.encryptionKeyRepository.findByTypeAndValid(EncryptionProviderType.JRE, new Date()).iterator().next();
    for (int i = 0; i < secretTypesToCreate.size(); i++) {
        if (inputSettings.containsKey(keyTypeInputNames.get(i))) {
            final String inputKeyName = inputSettings.get(keyTypeInputNames.get(i));
            final String key = SecurityKey.valueOf(inputKeyName).getDatabaseKey();
            final DbEncryptedSecret secret = new SecretBuilder().withDeviceIdentification(deviceIdentification).withSecretType(secretTypesToCreate.get(i)).withKey(key).withSecretStatus(SecretStatus.NEW).withEncryptionKeyReference(encryptionKeyRef).withCreationTime(new Date(System.currentTimeMillis() - (minutesAgo * 60000L))).build();
            this.encryptedSecretRepository.save(secret);
        }
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) SecretBuilder(org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder) SecretType(org.opensmartgridplatform.secretmanagement.application.domain.SecretType) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) Date(java.util.Date)

Aggregations

SecretBuilder (org.opensmartgridplatform.cucumber.platform.smartmetering.builders.entities.SecretBuilder)4 Date (java.util.Date)3 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)3 DbEncryptedSecret (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret)2 SecretType (org.opensmartgridplatform.secretmanagement.application.domain.SecretType)2 Given (io.cucumber.java.en.Given)1 ArrayList (java.util.ArrayList)1 SecurityKeyType (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)1