Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementation of basic access control for Files API #91

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import io.vertx.core.Future;
import lombok.AllArgsConstructor;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@AllArgsConstructor
public abstract class AccessControlBaseController {

Expand All @@ -15,14 +18,16 @@ public abstract class AccessControlBaseController {


public Future<?> handle(String bucket, String filePath) {
String decodedBucket = URLDecoder.decode(bucket, StandardCharsets.UTF_8);
String decodedFilePath = URLDecoder.decode(filePath, StandardCharsets.UTF_8);
String expectedUserBucket = BlobStorageUtil.buildUserBucket(context);
String decryptedBucket = proxy.getEncryptionService().decrypt(bucket);
String decryptedBucket = proxy.getEncryptionService().decrypt(decodedBucket);

if (!expectedUserBucket.equals(decryptedBucket)) {
return context.respond(HttpStatus.FORBIDDEN, "You don't have an access to the bucket " + bucket);
return context.respond(HttpStatus.FORBIDDEN, "You don't have an access to the bucket " + decodedBucket);
}

return handle(bucket, decryptedBucket, filePath);
return handle(decodedBucket, decryptedBucket, decodedFilePath);
}

protected abstract Future<?> handle(String bucketName, String bucketLocation, String filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,108 +44,109 @@ public class ControllerSelector {
private static final Pattern PATTERN_TRUNCATE_PROMPT = Pattern.compile("/+v1/deployments/([-.@a-zA-Z0-9]+)/truncate_prompt");

public Controller select(Proxy proxy, ProxyContext context) {
String path = URLDecoder.decode(context.getRequest().path(), StandardCharsets.UTF_8);
String providedPath = context.getRequest().path();
String decodedPath = URLDecoder.decode(context.getRequest().path(), StandardCharsets.UTF_8);
HttpMethod method = context.getRequest().method();
Controller controller = null;

if (method == HttpMethod.GET) {
controller = selectGet(proxy, context, path);
controller = selectGet(proxy, context, providedPath, decodedPath);
} else if (method == HttpMethod.POST) {
controller = selectPost(proxy, context, path);
controller = selectPost(proxy, context, providedPath, decodedPath);
} else if (method == HttpMethod.DELETE) {
controller = selectDelete(proxy, context, path);
controller = selectDelete(proxy, context, providedPath, decodedPath);
} else if (method == HttpMethod.PUT) {
controller = selectPut(proxy, context, path);
controller = selectPut(proxy, context, providedPath, decodedPath);
}

return (controller == null) ? new RouteController(proxy, context) : controller;
}

private static Controller selectGet(Proxy proxy, ProxyContext context, String path) {
private static Controller selectGet(Proxy proxy, ProxyContext context, String providedPath, String decodedPath) {
Matcher match;

match = match(PATTERN_DEPLOYMENT, path);
match = match(PATTERN_DEPLOYMENT, decodedPath);
if (match != null) {
DeploymentController controller = new DeploymentController(context);
String deploymentId = match.group(1);
return () -> controller.getDeployment(deploymentId);
}

match = match(PATTERN_DEPLOYMENTS, path);
match = match(PATTERN_DEPLOYMENTS, decodedPath);
if (match != null) {
DeploymentController controller = new DeploymentController(context);
return controller::getDeployments;
}

match = match(PATTERN_MODEL, path);
match = match(PATTERN_MODEL, decodedPath);
if (match != null) {
ModelController controller = new ModelController(context);
String modelId = match.group(1);
return () -> controller.getModel(modelId);
}

match = match(PATTERN_MODELS, path);
match = match(PATTERN_MODELS, decodedPath);
if (match != null) {
ModelController controller = new ModelController(context);
return controller::getModels;
}

match = match(PATTERN_ADDON, path);
match = match(PATTERN_ADDON, decodedPath);
if (match != null) {
AddonController controller = new AddonController(context);
String addonId = match.group(1);
return () -> controller.getAddon(addonId);
}

match = match(PATTERN_ADDONS, path);
match = match(PATTERN_ADDONS, decodedPath);
if (match != null) {
AddonController controller = new AddonController(context);
return controller::getAddons;
}

match = match(PATTERN_ASSISTANT, path);
match = match(PATTERN_ASSISTANT, decodedPath);
if (match != null) {
AssistantController controller = new AssistantController(context);
String assistantId = match.group(1);
return () -> controller.getAssistant(assistantId);
}

match = match(PATTERN_ASSISTANTS, path);
match = match(PATTERN_ASSISTANTS, decodedPath);
if (match != null) {
AssistantController controller = new AssistantController(context);
return controller::getAssistants;
}

match = match(PATTERN_APPLICATION, path);
match = match(PATTERN_APPLICATION, decodedPath);
if (match != null) {
ApplicationController controller = new ApplicationController(context);
String application = match.group(1);
return () -> controller.getApplication(application);
}

match = match(PATTERN_APPLICATIONS, path);
match = match(PATTERN_APPLICATIONS, decodedPath);
if (match != null) {
ApplicationController controller = new ApplicationController(context);
return controller::getApplications;
}

match = match(PATTERN_FILES_METADATA, path);
match = match(PATTERN_FILES_METADATA, providedPath);
if (match != null) {
String bucket = match.group(1);
String filePath = match.group(2);
FileMetadataController controller = new FileMetadataController(proxy, context);
return () -> controller.handle(bucket, filePath);
}

match = match(PATTERN_FILES, path);
match = match(PATTERN_FILES, providedPath);
if (match != null) {
String bucket = match.group(1);
String filePath = match.group(2);
DownloadFileController controller = new DownloadFileController(proxy, context);
return () -> controller.handle(bucket, filePath);
}

match = match(PATTERN_BUCKET, path);
match = match(PATTERN_BUCKET, decodedPath);
if (match != null) {
BucketController controller = new BucketController(proxy, context);
return controller::getBucket;
Expand All @@ -154,16 +155,16 @@ private static Controller selectGet(Proxy proxy, ProxyContext context, String pa
return null;
}

private static Controller selectPost(Proxy proxy, ProxyContext context, String path) {
Matcher match = match(PATTERN_POST_DEPLOYMENT, path);
private static Controller selectPost(Proxy proxy, ProxyContext context, String providedPath, String decodedPath) {
Matcher match = match(PATTERN_POST_DEPLOYMENT, decodedPath);
if (match != null) {
String deploymentId = match.group(1);
String deploymentApi = match.group(2);
DeploymentPostController controller = new DeploymentPostController(proxy, context);
return () -> controller.handle(deploymentId, deploymentApi);
}

match = match(PATTERN_RATE_RESPONSE, path);
match = match(PATTERN_RATE_RESPONSE, decodedPath);
if (match != null) {
String deploymentId = match.group(1);

Expand All @@ -178,7 +179,7 @@ private static Controller selectPost(Proxy proxy, ProxyContext context, String p
return () -> controller.handle(deploymentId, getter, false);
}

match = match(PATTERN_TOKENIZE, path);
match = match(PATTERN_TOKENIZE, decodedPath);
if (match != null) {
String deploymentId = match.group(1);

Expand All @@ -193,7 +194,7 @@ private static Controller selectPost(Proxy proxy, ProxyContext context, String p
return () -> controller.handle(deploymentId, getter, true);
}

match = match(PATTERN_TRUNCATE_PROMPT, path);
match = match(PATTERN_TRUNCATE_PROMPT, decodedPath);
if (match != null) {
String deploymentId = match.group(1);

Expand All @@ -211,8 +212,8 @@ private static Controller selectPost(Proxy proxy, ProxyContext context, String p
return null;
}

private static Controller selectDelete(Proxy proxy, ProxyContext context, String path) {
Matcher match = match(PATTERN_FILES, path);
private static Controller selectDelete(Proxy proxy, ProxyContext context, String providedPath, String decodedPath) {
Matcher match = match(PATTERN_FILES, providedPath);
artsiomkorzun marked this conversation as resolved.
Show resolved Hide resolved
if (match != null) {
String bucket = match.group(1);
String filePath = match.group(2);
Expand All @@ -223,8 +224,8 @@ private static Controller selectDelete(Proxy proxy, ProxyContext context, String
return null;
}

private static Controller selectPut(Proxy proxy, ProxyContext context, String path) {
Matcher match = match(PATTERN_FILES, path);
private static Controller selectPut(Proxy proxy, ProxyContext context, String providedPath, String decodedPath) {
Matcher match = match(PATTERN_FILES, providedPath);
if (match != null) {
String bucket = match.group(1);
String filePath = match.group(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static FileMetadataBase buildFileMetadata(ResourceDescription resource,
}

private static ResourceDescription getResourceDescription(ResourceType resourceType, String bucketName, String bucketLocation, String absoluteFilePath) {
String relativeFilePath = absoluteFilePath.substring(bucketLocation.length() + resourceType.getFolder().length());
String relativeFilePath = absoluteFilePath.substring(bucketLocation.length() + resourceType.getFolder().length() + 1);
return ResourceDescription.from(resourceType, bucketName, bucketLocation, relativeFilePath);
}

Expand Down