|
|
|
@ -98,6 +98,9 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
if (StringUtils.isNotEmpty(event.getOldName()) && !event.getOldName().equals(event.getNewName())) { |
|
|
|
keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getOldName())); |
|
|
|
} |
|
|
|
if (event.getCertificateHash() != null) { |
|
|
|
keys.add(DeviceProfileCacheKey.fromCertificateHash(event.getCertificateHash())); |
|
|
|
} |
|
|
|
cache.evict(keys); |
|
|
|
} |
|
|
|
|
|
|
|
@ -135,10 +138,12 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
try { |
|
|
|
savedDeviceProfile = deviceProfileDao.saveAndFlush(deviceProfile.getTenantId(), deviceProfile); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(savedDeviceProfile.getTenantId(), savedDeviceProfile.getName(), |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, savedDeviceProfile.getId(), savedDeviceProfile.isDefault())); |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, savedDeviceProfile.getId(), savedDeviceProfile.isDefault(), |
|
|
|
deviceProfile.getCertificateHash() != null ? deviceProfile.getCertificateHash() : null)); |
|
|
|
} catch (Exception t) { |
|
|
|
handleEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, null, deviceProfile.isDefault())); |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, null, deviceProfile.isDefault(), |
|
|
|
deviceProfile.getCertificateHash() != null ? deviceProfile.getCertificateHash() : null)); |
|
|
|
checkConstraintViolation(t, |
|
|
|
Map.of("device_profile_name_unq_key", DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS, |
|
|
|
"device_provision_key_unq_key", "Device profile with such provision device key already exists!", |
|
|
|
@ -178,7 +183,8 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
deleteEntityRelations(tenantId, deviceProfileId); |
|
|
|
deviceProfileDao.removeById(tenantId, deviceProfileId.getId()); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), |
|
|
|
null, deviceProfile.getId(), deviceProfile.isDefault())); |
|
|
|
null, deviceProfile.getId(), deviceProfile.isDefault(), |
|
|
|
deviceProfile.getCertificateHash() != null ? deviceProfile.getCertificateHash() : null)); |
|
|
|
} catch (Exception t) { |
|
|
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
|
|
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_device_profile")) { |
|
|
|
@ -284,14 +290,14 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
boolean changed = false; |
|
|
|
if (previousDefaultDeviceProfile == null) { |
|
|
|
deviceProfileDao.save(tenantId, deviceProfile); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true)); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, null)); |
|
|
|
changed = true; |
|
|
|
} else if (!previousDefaultDeviceProfile.getId().equals(deviceProfile.getId())) { |
|
|
|
previousDefaultDeviceProfile.setDefault(false); |
|
|
|
deviceProfileDao.save(tenantId, previousDefaultDeviceProfile); |
|
|
|
deviceProfileDao.save(tenantId, deviceProfile); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(previousDefaultDeviceProfile.getTenantId(), previousDefaultDeviceProfile.getName(), null, previousDefaultDeviceProfile.getId(), false)); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true)); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(previousDefaultDeviceProfile.getTenantId(), previousDefaultDeviceProfile.getName(), null, previousDefaultDeviceProfile.getId(), false, null)); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, null)); |
|
|
|
changed = true; |
|
|
|
} |
|
|
|
return changed; |
|
|
|
@ -336,13 +342,17 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
} |
|
|
|
|
|
|
|
private void formatDeviceProfileCertificate(DeviceProfile deviceProfile) { |
|
|
|
String cert = regexCertificateChain(deviceProfile.getCertificateValue()); |
|
|
|
String certificateValue = deviceProfile.getCertificateValue(); |
|
|
|
String cert = regexCertificateChain(certificateValue); |
|
|
|
String sha3Hash = EncryptionUtil.getSha3Hash(cert); |
|
|
|
deviceProfile.setCertificateHash(sha3Hash); |
|
|
|
if (!isCertificateChain(certificateValue)) { |
|
|
|
deviceProfile.setCertificateValue(EncryptionUtil.certTrimNewLines(certificateValue)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String regexCertificateChain(String chain) { |
|
|
|
String regex = "-----BEGIN CERTIFICATE-----\\s*((.+\\s+)*?)-----END CERTIFICATE----"; |
|
|
|
String regex = "-----BEGIN CERTIFICATE-----\\s*((.+\\s+)*?)-----END CERTIFICATE-----"; |
|
|
|
Pattern pattern = Pattern.compile(regex); |
|
|
|
Matcher matcher = pattern.matcher(chain); |
|
|
|
if (matcher.find()) { |
|
|
|
@ -351,4 +361,9 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
return chain; |
|
|
|
} |
|
|
|
|
|
|
|
private boolean isCertificateChain(String certificateValue) { |
|
|
|
int count = certificateValue.split("-----BEGIN CERTIFICATE", -1).length - 1; |
|
|
|
return count > 1; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|