Skip to content

Commit

Permalink
refactoring of filtering after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-zinchenko committed Nov 20, 2024
1 parent 45f6fe1 commit aeecbf5
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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.ApplicationData;
import com.epam.aidial.core.server.data.ListData;
import com.epam.aidial.core.server.data.ResourceLink;
import com.epam.aidial.core.server.data.ResourceTypes;
Expand All @@ -13,7 +13,6 @@
import com.epam.aidial.core.server.service.PermissionDeniedException;
import com.epam.aidial.core.server.service.ResourceNotFoundException;
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 Down Expand Up @@ -45,15 +44,9 @@ public ApplicationController(ProxyContext context) {
}

public Future<?> getApplication(String applicationId) {
DeploymentController.selectDeployment(context, applicationId)
DeploymentController.selectDeployment(context, applicationId, true, true)
.map(deployment -> {
if (deployment instanceof Application application) {
try {
application =
CustomApplicationUtils.filterCustomClientProperties(context.getConfig(), application);
} catch (CustomAppValidationException e) {
throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
return application;
}
throw new ResourceNotFoundException("Application is not found: " + applicationId);
Expand All @@ -67,30 +60,22 @@ public Future<?> getApplication(String applicationId) {

public Future<?> getApplications() {
Config config = context.getConfig();
List<ApplicationData> list = new ArrayList<>();

for (Application application : config.getApplications().values()) {
if (DeploymentController.hasAccess(context, application)) {
application = CustomApplicationUtils.filterCustomClientProperties(config, application);
ApplicationData data = ApplicationUtil.mapApplication(application);
list.add(data);
Proxy proxy = context.getProxy();

return proxy.getVertx().executeBlocking(() -> {
List<Application> list = new ArrayList<>();
for (Application application : config.getApplications().values()) {
if (DeploymentController.hasAccess(context, application)) {
application = CustomApplicationUtils.filterCustomClientProperties(config, application);
list.add(application);
}
}
}

Future<List<ApplicationData>> future = Future.succeededFuture(list);

if (applicationService.isIncludeCustomApps()) {
future = vertx.executeBlocking(() -> applicationService.getAllApplications(context), false)
.map(apps -> {
apps.forEach(app -> list.add(ApplicationUtil.mapApplication(app)));
return list;
});
}

future.onSuccess(apps -> context.respond(HttpStatus.OK, new ListData<>(apps)))
if (applicationService.isIncludeCustomApps()) {
list.addAll(applicationService.getAllApplications(context));
}
return list.stream().map(ApplicationUtil::mapApplication).toList();
}).onSuccess(apps -> context.respond(HttpStatus.OK, new ListData<>(apps)))
.onFailure(this::respondError);

return Future.succeededFuture();
}

public Future<?> deployApplication() {
Expand Down Expand Up @@ -130,7 +115,7 @@ public Future<?> getApplicationLogs() {
String url = ProxyUtil.convertToObject(body, ResourceLink.class).url();
ResourceDescriptor resource = decodeUrl(url);
checkAccess(resource);
return vertx.executeBlocking(() -> applicationService.getApplicationLogs(resource, context), false);
return vertx.executeBlocking(() -> applicationService.getApplicationLogs(resource), false);
})
.onSuccess(logs -> context.respond(HttpStatus.OK, logs))
.onFailure(this::respondError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,28 @@ public Future<?> getDeployments() {
return context.respond(HttpStatus.OK, list);
}

public static Future<Deployment> selectDeployment(ProxyContext context, String id) {
public static Future<Deployment> selectDeployment(ProxyContext context, String id, boolean filterCustomProperties, boolean modifyEndpoint) {
Deployment deployment = context.getConfig().selectDeployment(id);
Proxy proxy = context.getProxy();
if (deployment != null) {
if (!DeploymentController.hasAccess(context, deployment)) {
return Future.failedFuture(new PermissionDeniedException("Forbidden deployment: " + id));
} else {
try {
if (deployment instanceof Application application) {
application =
CustomApplicationUtils.modifyEndpointForCustomApplication(context.getConfig(), application);
return Future.succeededFuture(application);
if (!modifyEndpoint && !filterCustomProperties) {
return Future.succeededFuture(deployment);
}
return proxy.getVertx().executeBlocking(() -> {
Application modifiedApp = application;
if (filterCustomProperties) {
modifiedApp = CustomApplicationUtils.filterCustomClientProperties(context.getConfig(), application);
}
if (modifyEndpoint) {
modifiedApp = CustomApplicationUtils.modifyEndpointForCustomApplication(context.getConfig(), modifiedApp);
}
return modifiedApp;
});
}
return Future.succeededFuture(deployment);
} catch (Throwable e) {
Expand All @@ -83,7 +94,7 @@ public static Future<Deployment> selectDeployment(ProxyContext context, String i
}
}

Proxy proxy = context.getProxy();

return proxy.getVertx().executeBlocking(() -> {
String url;
ResourceDescriptor resource;
Expand All @@ -103,8 +114,15 @@ public static Future<Deployment> selectDeployment(ProxyContext context, String i
throw new PermissionDeniedException();
}

Application app = proxy.getApplicationService().getApplication(resource, context).getValue();
app = CustomApplicationUtils.filterCustomClientPropertiesWhenNoWriteAccess(context, resource, app);
Application app = proxy.getApplicationService().getApplication(resource).getValue();

if (filterCustomProperties) {
app = CustomApplicationUtils.filterCustomClientPropertiesWhenNoWriteAccess(context, resource, app);
}
if (modifyEndpoint) {
app = CustomApplicationUtils.modifyEndpointForCustomApplication(context.getConfig(), app);
}

return app;
}, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DeploymentFeatureController(Proxy proxy, ProxyContext context) {
}

public Future<?> handle(String deploymentId, Function<Deployment, String> endpointGetter, boolean requireEndpoint) {
DeploymentController.selectDeployment(context, deploymentId).map(dep -> {
DeploymentController.selectDeployment(context, deploymentId, false, true).map(dep -> {
String endpoint = endpointGetter.apply(dep);
context.setDeployment(dep);
context.getRequest().body()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Future<?> handle(String deploymentId, String deploymentApi) {
}

private Future<?> handleDeployment(String deploymentId, String deploymentApi) {
return DeploymentController.selectDeployment(context, deploymentId)
return DeploymentController.selectDeployment(context, deploymentId, false, true)
.map(dep -> {
if (dep.getEndpoint() == null) {
throw new HttpException(HttpStatus.SERVICE_UNAVAILABLE, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public LimitController(Proxy proxy, ProxyContext context) {
}

public Future<?> getLimits(String deploymentId) {
DeploymentController.selectDeployment(context, deploymentId)
DeploymentController.selectDeployment(context, deploymentId, false, true)
.compose(dep -> proxy.getRateLimiter().getLimitStats(dep, context))
.onSuccess(limitStats -> {
if (limitStats == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private Future<?> getResource(ResourceDescriptor descriptor, boolean hasWriteAcc

private Future<Pair<ResourceItemMetadata, String>> getApplicationData(ResourceDescriptor descriptor, boolean hasWriteAccess, EtagHeader etagHeader) {
return vertx.executeBlocking(() -> {
Pair<ResourceItemMetadata, Application> result = applicationService.getApplication(descriptor, etagHeader, context);
Pair<ResourceItemMetadata, Application> result = applicationService.getApplication(descriptor, etagHeader);
ResourceItemMetadata meta = result.getKey();

Application application = result.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.epam.aidial.core.server.util.CustomApplicationUtils;
import com.epam.aidial.core.server.util.ProxyUtil;
import com.epam.aidial.core.storage.http.HttpStatus;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.vertx.core.buffer.Buffer;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -45,7 +44,8 @@ private static boolean appendCustomProperties(ProxyContext context, ObjectNode t
Map<String, Object> props = CustomApplicationUtils.getCustomServerProperties(context.getConfig(), application);
ObjectNode customAppPropertiesNode = ProxyUtil.MAPPER.createObjectNode();
for (Map.Entry<String, Object> entry : props.entrySet()) {
customAppPropertiesNode.put(entry.getKey(), entry.getValue().toString());
customAppPropertiesNode.putPOJO(entry.getKey(), entry.getValue());
appended = true;
}
tree.set("custom_application_properties", customAppPropertiesNode);
return appended;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public boolean hasReadAccess(ResourceDescriptor resource, ProxyContext context)
}

public boolean hasWriteAccess(ResourceDescriptor resource, ProxyContext context) {
if (hasAdminAccess(context)) {
return true;
}
Map<ResourceDescriptor, Set<ResourceAccessType>> permissions =
lookupPermissions(Set.of(resource), context, Set.of(ResourceAccessType.WRITE));
return permissions.get(resource).contains(ResourceAccessType.WRITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public List<Application> getSharedApplications(ProxyContext context) {
ResourceDescriptor resource = ResourceDescriptorFactory.fromAnyUrl(meta.getUrl(), encryptionService);

if (meta instanceof ResourceItemMetadata) {
Application application = getApplication(resource, context).getValue();
Application application = getApplication(resource).getValue();
application = CustomApplicationUtils.filterCustomClientPropertiesWhenNoWriteAccess(context, resource, application);
list.add(application);
} else {
Expand All @@ -155,11 +155,11 @@ public List<Application> getPublicApplications(ProxyContext context) {
return getApplications(folder, page -> accessService.filterForbidden(context, folder, page), context);
}

public Pair<ResourceItemMetadata, Application> getApplication(ResourceDescriptor resource, ProxyContext context) {
return getApplication(resource, EtagHeader.ANY, context);
public Pair<ResourceItemMetadata, Application> getApplication(ResourceDescriptor resource) {
return getApplication(resource, EtagHeader.ANY);
}

public Pair<ResourceItemMetadata, Application> getApplication(ResourceDescriptor resource, EtagHeader etagHeader, ProxyContext context) {
public Pair<ResourceItemMetadata, Application> getApplication(ResourceDescriptor resource, EtagHeader etagHeader) {
verifyApplication(resource);
Pair<ResourceItemMetadata, String> result = resourceService.getResourceWithMetadata(resource, etagHeader);

Expand All @@ -174,10 +174,6 @@ public Pair<ResourceItemMetadata, Application> getApplication(ResourceDescriptor
throw new ResourceNotFoundException("Application is not found: " + resource.getUrl());
}

if (context != null) {
application = CustomApplicationUtils.modifyEndpointForCustomApplication(context.getConfig(), application);
}

return Pair.of(meta, application);
}

Expand Down Expand Up @@ -208,7 +204,7 @@ public List<Application> getApplications(ResourceDescriptor resource,
if (meta.getNodeType() == NodeType.ITEM && meta.getResourceType() == ResourceTypes.APPLICATION) {
try {
ResourceDescriptor item = ResourceDescriptorFactory.fromAnyUrl(meta.getUrl(), encryptionService);
Application application = getApplication(item, ctx).getValue();
Application application = getApplication(item).getValue();
application = CustomApplicationUtils.filterCustomClientPropertiesWhenNoWriteAccess(ctx, item, application);
applications.add(application);
} catch (ResourceNotFoundException ignore) {
Expand Down Expand Up @@ -311,7 +307,7 @@ public void copyApplication(ResourceDescriptor source, ResourceDescriptor destin
verifyApplication(source);
verifyApplication(destination);

Application application = getApplication(source, null).getValue();
Application application = getApplication(source).getValue();
Application.Function function = application.getFunction();

EtagHeader etag = overwrite ? EtagHeader.ANY : EtagHeader.NEW_ONLY;
Expand Down Expand Up @@ -434,11 +430,11 @@ public Application undeployApplication(ResourceDescriptor resource) {
return result.getValue();
}

public Application.Logs getApplicationLogs(ResourceDescriptor resource, ProxyContext context) {
public Application.Logs getApplicationLogs(ResourceDescriptor resource) {
verifyApplication(resource);
controller.verifyActive();

Application application = getApplication(resource, context).getValue();
Application application = getApplication(resource).getValue();

if (application.getFunction() == null || application.getFunction().getStatus() != Application.Function.Status.DEPLOYED) {
throw new HttpException(HttpStatus.CONFLICT, "Application is not started: " + resource.getUrl());
Expand Down Expand Up @@ -544,7 +540,7 @@ private Void launchApplication(ProxyContext context, ResourceDescriptor resource
throw new IllegalStateException("Application function is locked");
}

Application application = getApplication(resource, context).getValue();
Application application = getApplication(resource).getValue();
Application.Function function = application.getFunction();

if (function == null) {
Expand Down Expand Up @@ -598,7 +594,7 @@ private Void terminateApplication(ResourceDescriptor resource, String error) {
Application application;

try {
application = getApplication(resource, null).getValue();
application = getApplication(resource).getValue();
} catch (ResourceNotFoundException e) {
application = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private void addCustomApplicationRelatedFiles(ProxyContext context, Publication
if (source.getType() != ResourceTypes.APPLICATION) {
return Stream.empty();
}
Application application = applicationService.getApplication(source, context).getValue();
Application application = applicationService.getApplication(source).getValue();
if (application.getCustomAppSchemaId() == null) {
return Stream.empty();
}
Expand Down Expand Up @@ -505,7 +505,7 @@ private void validateResourceForDeletion(Publication.Resource resource, String t
}

if (target.getType() == ResourceTypes.APPLICATION && !isAdmin) {
Application application = applicationService.getApplication(target, null).getValue();
Application application = applicationService.getApplication(target).getValue();
if (application.getFunction() != null && !application.getFunction().getAuthorBucket().equals(bucketName)) {
throw new IllegalArgumentException("Target application has a different author: " + targetUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void addCustomApplicationRelatedFiles(ShareResourcesRequest request) {
for (SharedResource sharedResource : request.getResources()) {
ResourceDescriptor resource = getResourceFromLink(sharedResource.url());
if (resource.getType() == ResourceTypes.APPLICATION) {
Application application = applicationService.getApplication(resource, null).getValue();
Application application = applicationService.getApplication(resource).getValue();
List<ResourceDescriptor> files = CustomApplicationUtils.getFiles(config, application, encryptionService, resourceService);
for (ResourceDescriptor file : files) {
if (!filesFromRequest.contains(file.getUrl())) {
Expand Down

0 comments on commit aeecbf5

Please sign in to comment.