use of io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException in project gravitee-access-management by gravitee-io.
the class CertificateServiceImpl method create.
@Override
public Single<Certificate> create(String domain, NewCertificate newCertificate, User principal) {
LOGGER.debug("Create a new certificate {} for domain {}", newCertificate, domain);
Single<Certificate> certificateSingle = certificatePluginService.getSchema(newCertificate.getType()).switchIfEmpty(Maybe.error(new CertificatePluginSchemaNotFoundException(newCertificate.getType()))).map(schema -> objectMapper.readValue(schema, CertificateSchema.class)).flatMapSingle(new Function<CertificateSchema, SingleSource<Certificate>>() {
@Override
public SingleSource<Certificate> apply(CertificateSchema certificateSchema) throws Exception {
return Single.create(emitter -> {
String certificateId = RandomString.generate();
Certificate certificate = new Certificate();
certificate.setId(certificateId);
certificate.setDomain(domain);
certificate.setName(newCertificate.getName());
certificate.setType(newCertificate.getType());
// handle file
try {
JsonNode certificateConfiguration = objectMapper.readTree(newCertificate.getConfiguration());
certificateSchema.getProperties().entrySet().stream().filter(map -> map.getValue().getWidget() != null && "file".equals(map.getValue().getWidget())).map(map -> map.getKey()).forEach(key -> {
try {
JsonNode file = objectMapper.readTree(certificateConfiguration.get(key).asText());
byte[] data = Base64.getDecoder().decode(file.get("content").asText());
certificate.setMetadata(Collections.singletonMap(CertificateMetadata.FILE, data));
// update configuration to set the file name
((ObjectNode) certificateConfiguration).put(key, file.get("name").asText());
newCertificate.setConfiguration(objectMapper.writeValueAsString(certificateConfiguration));
} catch (IOException ex) {
LOGGER.error("An error occurs while trying to create certificate binaries", ex);
emitter.onError(ex);
}
});
certificate.setConfiguration(newCertificate.getConfiguration());
certificate.setCreatedAt(new Date());
certificate.setUpdatedAt(certificate.getCreatedAt());
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to create certificate configuration", ex);
emitter.onError(ex);
}
emitter.onSuccess(certificate);
});
}
});
return certificateSingle.flatMap(certificate -> certificateRepository.create(certificate)).flatMap(certificate -> {
Event event = new Event(Type.CERTIFICATE, new Payload(certificate.getId(), ReferenceType.DOMAIN, certificate.getDomain(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(certificate));
}).doOnError(ex -> {
LOGGER.error("An error occurs while trying to create a certificate", ex);
throw new TechnicalManagementException("An error occurs while trying to create a certificate", ex);
});
}
use of io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException in project gravitee-access-management by gravitee-io.
the class CertificateServiceImpl method update.
@Override
public Single<Certificate> update(String domain, String id, UpdateCertificate updateCertificate, User principal) {
LOGGER.debug("Update a certificate {} for domain {}", id, domain);
return certificateRepository.findById(id).switchIfEmpty(Maybe.error(new CertificateNotFoundException(id))).flatMapSingle(new Function<Certificate, SingleSource<CertificateWithSchema>>() {
@Override
public SingleSource<CertificateWithSchema> apply(Certificate certificate) throws Exception {
return certificatePluginService.getSchema(certificate.getType()).switchIfEmpty(Maybe.error(new CertificatePluginSchemaNotFoundException(certificate.getType()))).flatMapSingle(new Function<String, SingleSource<? extends CertificateWithSchema>>() {
@Override
public SingleSource<? extends CertificateWithSchema> apply(String schema) throws Exception {
return Single.just(new CertificateWithSchema(certificate, objectMapper.readValue(schema, CertificateSchema.class)));
}
});
}
}).flatMap(oldCertificate -> {
Single<Certificate> certificateSingle = Single.create(emitter -> {
Certificate certificateToUpdate = new Certificate(oldCertificate.getCertificate());
certificateToUpdate.setName(updateCertificate.getName());
try {
CertificateSchema certificateSchema = oldCertificate.getSchema();
JsonNode oldCertificateConfiguration = objectMapper.readTree(oldCertificate.getCertificate().getConfiguration());
JsonNode certificateConfiguration = objectMapper.readTree(updateCertificate.getConfiguration());
certificateSchema.getProperties().entrySet().stream().filter(map -> map.getValue().getWidget() != null && "file".equals(map.getValue().getWidget())).map(map -> map.getKey()).forEach(key -> {
try {
String oldFileInformation = oldCertificateConfiguration.get(key).asText();
String fileInformation = certificateConfiguration.get(key).asText();
// file has changed, let's update it
if (!oldFileInformation.equals(fileInformation)) {
JsonNode file = objectMapper.readTree(certificateConfiguration.get(key).asText());
byte[] data = Base64.getDecoder().decode(file.get("content").asText());
certificateToUpdate.setMetadata(Collections.singletonMap(CertificateMetadata.FILE, data));
// update configuration to set the file path
((ObjectNode) certificateConfiguration).put(key, file.get("name").asText());
updateCertificate.setConfiguration(objectMapper.writeValueAsString(certificateConfiguration));
}
} catch (IOException ex) {
LOGGER.error("An error occurs while trying to update certificate binaries", ex);
emitter.onError(ex);
}
});
certificateToUpdate.setConfiguration(updateCertificate.getConfiguration());
certificateToUpdate.setUpdatedAt(new Date());
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to update certificate configuration", ex);
emitter.onError(ex);
}
emitter.onSuccess(certificateToUpdate);
});
return certificateSingle.flatMap(certificate -> certificateRepository.update(certificate)).flatMap(certificate1 -> {
Event event = new Event(Type.CERTIFICATE, new Payload(certificate1.getId(), ReferenceType.DOMAIN, certificate1.getDomain(), Action.UPDATE));
return eventService.create(event).flatMap(__ -> Single.just(certificate1));
}).onErrorResumeNext(ex -> {
LOGGER.error("An error occurs while trying to update a certificate", ex);
throw new TechnicalManagementException("An error occurs while trying to update a certificate", ex);
});
});
}
use of io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException in project gravitee-access-management by gravitee-io.
the class CertificateServiceProxyImpl method updateSensitiveData.
private Single<UpdateCertificate> updateSensitiveData(UpdateCertificate updateCertificate, Certificate oldCertificate) {
return certificatePluginService.getSchema(oldCertificate.getType()).switchIfEmpty(Single.error(new CertificatePluginSchemaNotFoundException(oldCertificate.getType()))).map(schema -> {
var updateConfig = objectMapper.readTree(updateCertificate.getConfiguration());
var oldConfig = objectMapper.readTree(oldCertificate.getConfiguration());
var schemaConfig = objectMapper.readTree(schema);
super.updateSensitiveData(updateConfig, oldConfig, schemaConfig, updateCertificate::setConfiguration);
return updateCertificate;
});
}
use of io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException in project gravitee-access-management by gravitee-io.
the class CertificateServiceProxyImpl method filterSensitiveData.
private Single<Certificate> filterSensitiveData(Certificate cert) {
return certificatePluginService.getSchema(cert.getType()).switchIfEmpty(Single.error(new CertificatePluginSchemaNotFoundException(cert.getType()))).map(schema -> {
// Duplicate the object to avoid side effect
var filteredEntity = new Certificate(cert);
var schemaNode = objectMapper.readTree(schema);
var configurationNode = objectMapper.readTree(filteredEntity.getConfiguration());
super.filterSensitiveData(schemaNode, configurationNode, filteredEntity::setConfiguration);
return filteredEntity;
});
}
Aggregations