Search in sources :

Example 1 with CertificateNotFoundException

use of io.gravitee.am.service.exception.CertificateNotFoundException 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);
        });
    });
}
Also used : X509Certificate(java.security.cert.X509Certificate) KeyPair(java.security.KeyPair) Primary(org.springframework.context.annotation.Primary) BasicConstraints(org.bouncycastle.asn1.x509.BasicConstraints) Date(java.util.Date) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Type(io.gravitee.am.common.event.Type) CertificatePluginSchemaNotFoundException(io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) X500Name(org.bouncycastle.asn1.x500.X500Name) GeneralSecurityException(java.security.GeneralSecurityException) CertificateMetadata(io.gravitee.am.certificate.api.CertificateMetadata) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReferenceType(io.gravitee.am.model.ReferenceType) BigInteger(java.math.BigInteger) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) io.reactivex(io.reactivex) KeyPairGenerator(java.security.KeyPairGenerator) Action(io.gravitee.am.common.event.Action) CertificateNotFoundException(io.gravitee.am.service.exception.CertificateNotFoundException) CertificateWithApplicationsException(io.gravitee.am.service.exception.CertificateWithApplicationsException) KeyStore(java.security.KeyStore) EventType(io.gravitee.am.common.audit.EventType) RandomString(io.gravitee.am.common.utils.RandomString) NewCertificate(io.gravitee.am.service.model.NewCertificate) Base64(java.util.Base64) Payload(io.gravitee.am.model.common.event.Payload) Environment(org.springframework.core.env.Environment) Lazy(org.springframework.context.annotation.Lazy) Certificate(io.gravitee.am.model.Certificate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentSigner(org.bouncycastle.operator.ContentSigner) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) BouncyCastleProviderSingleton(com.nimbusds.jose.crypto.bc.BouncyCastleProviderSingleton) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Event(io.gravitee.am.model.common.event.Event) UpdateCertificate(io.gravitee.am.service.model.UpdateCertificate) CertificateSchema(io.gravitee.am.plugins.certificate.core.CertificateSchema) io.gravitee.am.service(io.gravitee.am.service) Logger(org.slf4j.Logger) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Component(org.springframework.stereotype.Component) Function(io.reactivex.functions.Function) CertificateRepository(io.gravitee.am.repository.management.api.CertificateRepository) CertificateAuditBuilder(io.gravitee.am.service.reporter.builder.management.CertificateAuditBuilder) Collections(java.util.Collections) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CertificatePluginSchemaNotFoundException(io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) RandomString(io.gravitee.am.common.utils.RandomString) IOException(java.io.IOException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) CertificatePluginSchemaNotFoundException(io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) GeneralSecurityException(java.security.GeneralSecurityException) CertificateNotFoundException(io.gravitee.am.service.exception.CertificateNotFoundException) CertificateWithApplicationsException(io.gravitee.am.service.exception.CertificateWithApplicationsException) IOException(java.io.IOException) Date(java.util.Date) Function(io.reactivex.functions.Function) CertificateNotFoundException(io.gravitee.am.service.exception.CertificateNotFoundException) CertificateSchema(io.gravitee.am.plugins.certificate.core.CertificateSchema) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) X509Certificate(java.security.cert.X509Certificate) NewCertificate(io.gravitee.am.service.model.NewCertificate) Certificate(io.gravitee.am.model.Certificate) UpdateCertificate(io.gravitee.am.service.model.UpdateCertificate)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 BouncyCastleProviderSingleton (com.nimbusds.jose.crypto.bc.BouncyCastleProviderSingleton)1 CertificateMetadata (io.gravitee.am.certificate.api.CertificateMetadata)1 EventType (io.gravitee.am.common.audit.EventType)1 Action (io.gravitee.am.common.event.Action)1 Type (io.gravitee.am.common.event.Type)1 RandomString (io.gravitee.am.common.utils.RandomString)1 User (io.gravitee.am.identityprovider.api.User)1 Certificate (io.gravitee.am.model.Certificate)1 ReferenceType (io.gravitee.am.model.ReferenceType)1 Event (io.gravitee.am.model.common.event.Event)1 Payload (io.gravitee.am.model.common.event.Payload)1 CertificateSchema (io.gravitee.am.plugins.certificate.core.CertificateSchema)1 CertificateRepository (io.gravitee.am.repository.management.api.CertificateRepository)1 io.gravitee.am.service (io.gravitee.am.service)1 CertificateNotFoundException (io.gravitee.am.service.exception.CertificateNotFoundException)1 CertificatePluginSchemaNotFoundException (io.gravitee.am.service.exception.CertificatePluginSchemaNotFoundException)1 CertificateWithApplicationsException (io.gravitee.am.service.exception.CertificateWithApplicationsException)1