diff --git a/server/src/main/java/com/epam/aidial/core/server/controller/ResourceController.java b/server/src/main/java/com/epam/aidial/core/server/controller/ResourceController.java index 333e214b..4c09d73c 100644 --- a/server/src/main/java/com/epam/aidial/core/server/controller/ResourceController.java +++ b/server/src/main/java/com/epam/aidial/core/server/controller/ResourceController.java @@ -1,21 +1,17 @@ package com.epam.aidial.core.server.controller; import com.epam.aidial.core.config.Application; -import com.epam.aidial.core.config.Config; import com.epam.aidial.core.server.Proxy; import com.epam.aidial.core.server.ProxyContext; import com.epam.aidial.core.server.data.Conversation; import com.epam.aidial.core.server.data.Prompt; import com.epam.aidial.core.server.data.ResourceTypes; import com.epam.aidial.core.server.security.AccessService; -import com.epam.aidial.core.server.security.EncryptionService; import com.epam.aidial.core.server.service.ApplicationService; import com.epam.aidial.core.server.service.InvitationService; import com.epam.aidial.core.server.service.PermissionDeniedException; import com.epam.aidial.core.server.service.ResourceNotFoundException; import com.epam.aidial.core.server.service.ShareService; -import com.epam.aidial.core.server.util.CustomAppValidationException; -import com.epam.aidial.core.server.util.CustomApplicationUtils; import com.epam.aidial.core.server.util.ProxyUtil; import com.epam.aidial.core.server.util.ResourceDescriptorFactory; import com.epam.aidial.core.storage.data.MetadataBase; @@ -30,7 +26,6 @@ import io.vertx.core.Vertx; import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpMethod; -import jakarta.validation.ValidationException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.Pair; @@ -38,7 +33,6 @@ import java.util.List; import static com.epam.aidial.core.storage.http.HttpStatus.BAD_REQUEST; -import static com.epam.aidial.core.storage.http.HttpStatus.INTERNAL_SERVER_ERROR; @Slf4j @SuppressWarnings("checkstyle:Indentation") @@ -52,7 +46,6 @@ public class ResourceController extends AccessControlBaseController { private final InvitationService invitationService; private final boolean metadata; private final AccessService accessService; - private final EncryptionService encryptionService; public ResourceController(Proxy proxy, ProxyContext context, boolean metadata) { // PUT and DELETE require write access, GET - read @@ -64,7 +57,6 @@ public ResourceController(Proxy proxy, ProxyContext context, boolean metadata) { this.lockService = proxy.getLockService(); this.invitationService = proxy.getInvitationService(); this.resourceService = proxy.getResourceService(); - this.encryptionService = proxy.getEncryptionService(); this.metadata = metadata; } @@ -174,24 +166,6 @@ private Future> getResourceData(ResourceDescr }, false); } - private void validateCustomApplication(Application application) { - try { - Config config = context.getConfig(); - List files = CustomApplicationUtils.getFiles(config, application, encryptionService, - resourceService); - files.stream().filter(resource -> !(resourceService.hasResource(resource) - && accessService.hasReadAccess(resource, context))) - .findAny().ifPresent(file -> { - throw new HttpException(BAD_REQUEST, "No read access to file: " + file.getUrl()); - }); - CustomApplicationUtils.modifyEndpointForCustomApplication(config, application); - } catch (ValidationException | IllegalArgumentException e) { - throw new HttpException(BAD_REQUEST, "Custom application validation failed", e); - } catch (CustomAppValidationException e) { - throw new HttpException(INTERNAL_SERVER_ERROR, "Custom application validation failed", e); - } - } - private Future putResource(ResourceDescriptor descriptor) { if (descriptor.isFolder()) { return context.respond(BAD_REQUEST, "Folder not allowed: " + descriptor.getUrl()); @@ -228,7 +202,7 @@ private Future putResource(ResourceDescriptor descriptor) { EtagHeader etag = pair.getKey(); Application application = ProxyUtil.convertToObject(pair.getValue(), Application.class); return vertx.executeBlocking(() -> { - validateCustomApplication(application); + applicationService.validateCustomApplication(application, context); return applicationService.putApplication(descriptor, etag, application).getKey(); }, false); }); diff --git a/server/src/main/java/com/epam/aidial/core/server/service/ApplicationService.java b/server/src/main/java/com/epam/aidial/core/server/service/ApplicationService.java index 03e3b386..1dcfd798 100644 --- a/server/src/main/java/com/epam/aidial/core/server/service/ApplicationService.java +++ b/server/src/main/java/com/epam/aidial/core/server/service/ApplicationService.java @@ -1,6 +1,7 @@ package com.epam.aidial.core.server.service; import com.epam.aidial.core.config.Application; +import com.epam.aidial.core.config.Config; import com.epam.aidial.core.config.Features; import com.epam.aidial.core.server.ProxyContext; import com.epam.aidial.core.server.controller.ApplicationUtil; @@ -10,6 +11,7 @@ import com.epam.aidial.core.server.security.AccessService; import com.epam.aidial.core.server.security.EncryptionService; import com.epam.aidial.core.server.util.BucketBuilder; +import com.epam.aidial.core.server.util.CustomAppValidationException; import com.epam.aidial.core.server.util.CustomApplicationUtils; import com.epam.aidial.core.server.util.ProxyUtil; import com.epam.aidial.core.server.util.ResourceDescriptorFactory; @@ -25,10 +27,10 @@ import com.epam.aidial.core.storage.service.ResourceService; import com.epam.aidial.core.storage.util.EtagHeader; import com.epam.aidial.core.storage.util.UrlUtil; -import com.fasterxml.jackson.core.JsonProcessingException; import io.vertx.core.Vertx; import io.vertx.core.http.HttpClient; import io.vertx.core.json.JsonObject; +import jakarta.validation.ValidationException; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.mutable.MutableObject; @@ -45,6 +47,9 @@ import java.util.function.Consumer; import java.util.function.Supplier; +import static com.epam.aidial.core.storage.http.HttpStatus.BAD_REQUEST; +import static com.epam.aidial.core.storage.http.HttpStatus.INTERNAL_SERVER_ERROR; + @Slf4j public class ApplicationService { @@ -218,6 +223,24 @@ public List getApplications(ResourceDescriptor resource, return applications; } + public void validateCustomApplication(Application application, ProxyContext context) { + try { + Config config = context.getConfig(); + List files = CustomApplicationUtils.getFiles(config, application, encryptionService, + resourceService); + files.stream().filter(resource -> !(resourceService.hasResource(resource) + && context.getProxy().getAccessService().hasReadAccess(resource, context))) + .findAny().ifPresent(file -> { + throw new HttpException(BAD_REQUEST, "No read access to file: " + file.getUrl()); + }); + CustomApplicationUtils.modifyEndpointForCustomApplication(config, application); + } catch (ValidationException | IllegalArgumentException e) { + throw new HttpException(BAD_REQUEST, "Custom application validation failed", e); + } catch (CustomAppValidationException e) { + throw new HttpException(INTERNAL_SERVER_ERROR, "Custom application validation failed", e); + } + } + public Pair putApplication(ResourceDescriptor resource, EtagHeader etag, Application application) { prepareApplication(resource, application);