Search in sources :

Example 1 with RestApiAuditProperty

use of org.niis.xroad.restapi.config.audit.RestApiAuditProperty in project X-Road by nordic-institute.

the class ServiceDescriptionService method updateWsdlUrl.

/**
 * Update the WSDL url of the selected ServiceDescription.
 * Refreshing a WSDL is also an update of wsdl,
 * it just updates to the same URL value
 *
 * @param serviceDescriptionType
 * @param url the new url
 * @return ServiceDescriptionType
 * @throws WsdlParser.WsdlNotFoundException if a wsdl was not found at the url
 * @throws WrongServiceDescriptionTypeException if SD with given id was not a WSDL based one
 * @throws InvalidWsdlException if WSDL at the url was invalid
 * @throws UnhandledWarningsException if there were warnings that were not ignored
 * @throws InvalidUrlException if url was empty or invalid
 * @throws InvalidServiceUrlException if the WSDL has services with invalid urls
 * @throws WsdlUrlAlreadyExistsException conflict: another service description has same url
 * @throws ServiceAlreadyExistsException conflict: same service exists in another SD
 * @throws InterruptedException if the thread running the WSDL validator is interrupted. <b>The
 * interrupted thread has already been handled with so you can choose to ignore this exception if you so
 * please.</b>
 */
private ServiceDescriptionType updateWsdlUrl(ServiceDescriptionType serviceDescriptionType, String url, boolean ignoreWarnings) throws InvalidWsdlException, WsdlParser.WsdlNotFoundException, WrongServiceDescriptionTypeException, UnhandledWarningsException, ServiceAlreadyExistsException, InvalidUrlException, WsdlUrlAlreadyExistsException, InterruptedException, InvalidServiceUrlException {
    auditDataHelper.put(serviceDescriptionType.getClient().getIdentifier());
    Map<RestApiAuditProperty, Object> wsdlAuditData = auditDataHelper.putMap(RestApiAuditProperty.WSDL);
    auditDataHelper.putServiceDescriptionUrl(serviceDescriptionType);
    if (auditDataHelper.dataIsForEvent(RestApiAuditEvent.EDIT_SERVICE_DESCRIPTION)) {
        auditDataHelper.put(RestApiAuditProperty.URL_NEW, url);
    }
    // Shouldn't be able to edit e.g. REST service descriptions with a WSDL URL
    if (serviceDescriptionType.getType() != DescriptionType.WSDL) {
        throw new WrongServiceDescriptionTypeException("Existing service description (id: " + serviceDescriptionType.getId().toString() + " is not WSDL");
    }
    ClientType client = serviceDescriptionType.getClient();
    WsdlProcessingResult wsdlProcessingResult = processWsdl(client, url, serviceDescriptionType.getId());
    List<ServiceType> newServices = wsdlProcessingResult.getParsedServices().stream().map(serviceInfo -> serviceInfoToServiceType(serviceInfo, serviceDescriptionType)).collect(Collectors.toList());
    // find what services were added or removed
    ServiceChangeChecker.ServiceChanges serviceChanges = serviceChangeChecker.check(serviceDescriptionType.getService(), newServices);
    // On refresh the service properties (URL, timeout, SSL authentication) should not change
    // so the existing values must be kept. This applies to a case when 1) the WSDL URL remains the same
    // and 2) the WSDL URL is changed. When the WSDL URL is changed (2), the service properties must keep
    // the same values in case the WSDL fetched from the new URL contains services with the same service code.
    updateServicePoperties(serviceDescriptionType, newServices);
    wsdlAuditData.put(RestApiAuditProperty.SERVICES_ADDED, serviceChanges.getAddedFullServiceCodes());
    wsdlAuditData.put(RestApiAuditProperty.SERVICES_DELETED, serviceChanges.getRemovedFullServiceCodes());
    // collect all types of warnings, throw Exception if not ignored
    List<WarningDeviation> allWarnings = new ArrayList<>();
    allWarnings.addAll(wsdlProcessingResult.getWarnings());
    if (!serviceChanges.isEmpty()) {
        allWarnings.addAll(createServiceChangeWarnings(serviceChanges));
    }
    if (!ignoreWarnings && !allWarnings.isEmpty()) {
        throw new UnhandledWarningsException(allWarnings);
    }
    serviceDescriptionType.setRefreshedDate(new Date());
    serviceDescriptionType.setUrl(url);
    List<String> newServiceCodes = newServices.stream().map(ServiceType::getServiceCode).collect(Collectors.toList());
    // service codes that will be REMOVED
    List<String> removedServiceCodes = serviceChanges.getRemovedServices().stream().map(ServiceType::getServiceCode).collect(Collectors.toList());
    // replace all old services with the new ones
    serviceDescriptionType.getService().clear();
    serviceDescriptionType.getService().addAll(newServices);
    // clear AccessRights that belong to non-existing services
    client.getAcl().removeIf(accessRightType -> {
        String serviceCode = accessRightType.getEndpoint().getServiceCode();
        return removedServiceCodes.contains(serviceCode) && !newServiceCodes.contains(serviceCode);
    });
    // remove related endpoints
    client.getEndpoint().removeIf(endpointType -> removedServiceCodes.contains(endpointType.getServiceCode()));
    // add new endpoints
    Collection<EndpointType> endpointsToAdd = resolveNewEndpoints(client, serviceDescriptionType);
    client.getEndpoint().addAll(endpointsToAdd);
    return serviceDescriptionType;
}
Also used : Arrays(java.util.Arrays) ServiceDescriptionType(ee.ria.xroad.common.conf.serverconf.model.ServiceDescriptionType) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ServiceType(ee.ria.xroad.common.conf.serverconf.model.ServiceType) WARNING_OPENAPI_VALIDATION_WARNINGS(org.niis.xroad.restapi.exceptions.DeviationCodes.WARNING_OPENAPI_VALIDATION_WARNINGS) ERROR_INVALID_SERVICE_IDENTIFIER(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_INVALID_SERVICE_IDENTIFIER) ERROR_WRONG_TYPE(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_WRONG_TYPE) DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) EndpointType(ee.ria.xroad.common.conf.serverconf.model.EndpointType) AccessRightType(ee.ria.xroad.common.conf.serverconf.model.AccessRightType) ERROR_SERVICE_EXISTS(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_SERVICE_EXISTS) FormatUtils(org.niis.xroad.restapi.util.FormatUtils) AuditDataHelper(org.niis.xroad.restapi.config.audit.AuditDataHelper) Map(java.util.Map) WARNING_WSDL_VALIDATION_WARNINGS(org.niis.xroad.restapi.exceptions.DeviationCodes.WARNING_WSDL_VALIDATION_WARNINGS) ClientType(ee.ria.xroad.common.conf.serverconf.model.ClientType) WsdlValidator(org.niis.xroad.securityserver.restapi.wsdl.WsdlValidator) EncodedIdentifierValidator(ee.ria.xroad.common.validation.EncodedIdentifierValidator) UnsupportedOpenApiVersionException(org.niis.xroad.securityserver.restapi.wsdl.UnsupportedOpenApiVersionException) Collection(java.util.Collection) EndpointHelper(org.niis.xroad.securityserver.restapi.util.EndpointHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) ServiceException(org.niis.xroad.restapi.service.ServiceException) ERROR_EXISTING_URL(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_EXISTING_URL) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ERROR_EXISTING_SERVICE_CODE(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_EXISTING_SERVICE_CODE) RestApiAuditEvent(org.niis.xroad.restapi.config.audit.RestApiAuditEvent) DeviationAwareRuntimeException(org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Service(org.springframework.stereotype.Service) WsdlParser(org.niis.xroad.securityserver.restapi.wsdl.WsdlParser) InvalidWsdlException(org.niis.xroad.securityserver.restapi.wsdl.InvalidWsdlException) WarningDeviation(org.niis.xroad.restapi.exceptions.WarningDeviation) WARNING_ADDING_SERVICES(org.niis.xroad.restapi.exceptions.DeviationCodes.WARNING_ADDING_SERVICES) UnhandledWarningsException(org.niis.xroad.restapi.service.UnhandledWarningsException) OpenApiParser(org.niis.xroad.securityserver.restapi.wsdl.OpenApiParser) ErrorDeviation(org.niis.xroad.restapi.exceptions.ErrorDeviation) SecurityServerFormatUtils(org.niis.xroad.securityserver.restapi.util.SecurityServerFormatUtils) ERROR_WSDL_EXISTS(org.niis.xroad.restapi.exceptions.DeviationCodes.ERROR_WSDL_EXISTS) RestApiAuditProperty(org.niis.xroad.restapi.config.audit.RestApiAuditProperty) WARNING_DELETING_SERVICES(org.niis.xroad.restapi.exceptions.DeviationCodes.WARNING_DELETING_SERVICES) Data(lombok.Data) ServiceDescriptionRepository(org.niis.xroad.securityserver.restapi.repository.ServiceDescriptionRepository) ClientId(ee.ria.xroad.common.identifier.ClientId) Hibernate(org.hibernate.Hibernate) Transactional(org.springframework.transaction.annotation.Transactional) ClientType(ee.ria.xroad.common.conf.serverconf.model.ClientType) ArrayList(java.util.ArrayList) RestApiAuditProperty(org.niis.xroad.restapi.config.audit.RestApiAuditProperty) Date(java.util.Date) WarningDeviation(org.niis.xroad.restapi.exceptions.WarningDeviation) UnhandledWarningsException(org.niis.xroad.restapi.service.UnhandledWarningsException) ServiceType(ee.ria.xroad.common.conf.serverconf.model.ServiceType) EndpointType(ee.ria.xroad.common.conf.serverconf.model.EndpointType)

Aggregations

AccessRightType (ee.ria.xroad.common.conf.serverconf.model.AccessRightType)1 ClientType (ee.ria.xroad.common.conf.serverconf.model.ClientType)1 DescriptionType (ee.ria.xroad.common.conf.serverconf.model.DescriptionType)1 EndpointType (ee.ria.xroad.common.conf.serverconf.model.EndpointType)1 ServiceDescriptionType (ee.ria.xroad.common.conf.serverconf.model.ServiceDescriptionType)1 ServiceType (ee.ria.xroad.common.conf.serverconf.model.ServiceType)1 ClientId (ee.ria.xroad.common.identifier.ClientId)1 EncodedIdentifierValidator (ee.ria.xroad.common.validation.EncodedIdentifierValidator)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Data (lombok.Data)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1