Skip to content

Commit

Permalink
validateCustomApplication moved to executeBlocking application service
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-zinchenko committed Nov 19, 2024
1 parent 09fe5c6 commit 8be9aca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -30,15 +26,13 @@
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;

import java.nio.charset.StandardCharsets;
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")
Expand All @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -174,24 +166,6 @@ private Future<Pair<ResourceItemMetadata, String>> getResourceData(ResourceDescr
}, false);
}

private void validateCustomApplication(Application application) {
try {
Config config = context.getConfig();
List<ResourceDescriptor> 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());
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -218,6 +223,24 @@ public List<Application> getApplications(ResourceDescriptor resource,
return applications;
}

public void validateCustomApplication(Application application, ProxyContext context) {
try {
Config config = context.getConfig();
List<ResourceDescriptor> 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<ResourceItemMetadata, Application> putApplication(ResourceDescriptor resource, EtagHeader etag, Application application) {
prepareApplication(resource, application);

Expand Down

0 comments on commit 8be9aca

Please sign in to comment.