Skip to content

Commit

Permalink
catalog: add resource checks, #TASK-6442
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Oct 17, 2024
1 parent 6629f3f commit 90e7280
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,17 @@ protected void check() throws Exception {
}

String userId = jwtPayload.getUserId(catalogFqn.getOrganizationId());
// Check write permissions over the path
catalogManager.getAuthorizationManager().checkFilePermission(catalogFqn.getOrganizationId(), study.getUid(),
parents.first().getUid(), userId, FilePermissions.WRITE);

boolean isResource = toolParams.getResource() != null && toolParams.getResource();
if (isResource) {
// Check it is a study admin
catalogManager.getAuthorizationManager().checkIsAtLeastStudyAdministrator(catalogFqn.getOrganizationId(), study.getUid(),
userId);
} else {
// Check write permissions over the path
catalogManager.getAuthorizationManager().checkFilePermission(catalogFqn.getOrganizationId(), study.getUid(),
parents.first().getUid(), userId, FilePermissions.WRITE);
}
} catch (CatalogException e) {
throw new ToolException(e);
}
Expand Down Expand Up @@ -122,7 +130,8 @@ protected void run() throws Exception {
step("register", () -> {
// Move downloaded file and register
try {
moveFile(studyFqn, getOutDir().resolve(fileName), null, toolParams.getPath(), token);
boolean isResource = toolParams.getResource() != null && toolParams.getResource();
moveFile(studyFqn, getOutDir().resolve(fileName), null, toolParams.getPath(), isResource, token);
} catch (Exception e) {
deleteTemporaryFile();
throw new CatalogException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,11 @@ protected final void addAttribute(String key, Object value) throws ToolException
erm.addAttribute(key, value);
}

protected final void moveFile(String study, Path source, Path destiny, String catalogDirectoryPath, String token) throws ToolException {
protected final void moveFile(String study, Path source, Path destiny, String catalogDirectoryPath, boolean isResource, String token)
throws ToolException {
File file;
try {
file = catalogManager.getFileManager().moveAndRegister(study, source, destiny, catalogDirectoryPath, token).first();
file = catalogManager.getFileManager().moveAndRegister(study, source, destiny, catalogDirectoryPath, isResource, token).first();
} catch (Exception e) {
throw new ToolException("Error moving file from " + source + " to " + destiny, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void fetch() throws ToolException {

Path outDir = Paths.get(options.outDir);

toolRunner.execute(FetchAndRegisterTask.class, new FileFetch(options.url, options.path), outDir, null, false, options.commonOptions.token);
toolRunner.execute(FetchAndRegisterTask.class, new FileFetch(options.url, options.path, options.resource), outDir, null, false,
options.commonOptions.token);
}

private void tsvLoad() throws ToolException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public class FetchCommandOptions {
description = "Folder path where the downloaded file will be registered", required = true, arity = 1)
public String path;

@Parameter(names = {"--resource"}, description = "File resource", arity = 1)
public Boolean resource;

@Parameter(names = {"--url"}, description = "External url where the file to be registered can be downloaded from", required = true,
arity = 1)
public String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class UploadCommandOptions {
+ "removed", arity = 0)
public boolean replace;

@Parameter(names = {"--resource"}, description = "File resource", arity = 1)
public Boolean resource;
// @Parameter(names = {"-ch", "--checksum"}, description = "[PENDING] Calculate checksum", arity = 0)
// public boolean checksum;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ private RestResponse<File> create() throws Exception {
putNestedIfNotEmpty(beanParams, "software.website", commandOptions.softwareWebsite, true);
putNestedMapIfNotEmpty(beanParams, "software.params", commandOptions.softwareParams, true);
putNestedIfNotNull(beanParams, "tags", commandOptions.tags, true);
putNestedIfNotNull(beanParams, "resource", commandOptions.resource, true);
putNestedIfNotEmpty(beanParams, "jobId", commandOptions.jobId, true);
putNestedIfNotEmpty(beanParams, "creationDate", commandOptions.creationDate, true);
putNestedIfNotEmpty(beanParams, "modificationDate", commandOptions.modificationDate, true);
Expand Down Expand Up @@ -372,6 +373,7 @@ private RestResponse<Job> fetch() throws Exception {
} else {
ObjectMap beanParams = new ObjectMap();
putNestedIfNotEmpty(beanParams, "url", commandOptions.url, true);
putNestedIfNotNull(beanParams, "resource", commandOptions.resource, true);
putNestedIfNotEmpty(beanParams, "path", commandOptions.path, true);

fileFetch = JacksonUtils.getDefaultObjectMapper().copy()
Expand Down Expand Up @@ -569,6 +571,7 @@ private RestResponse<File> upload() throws Exception {
queryParams.putIfNotNull("fileFormat", commandOptions.fileFormat);
queryParams.putIfNotNull("bioformat", commandOptions.bioformat);
queryParams.putIfNotEmpty("checksum", commandOptions.checksum);
queryParams.putIfNotNull("resource", commandOptions.resource);
queryParams.putIfNotEmpty("study", commandOptions.study);
queryParams.putIfNotEmpty("relativeFilePath", commandOptions.relativeFilePath);
queryParams.putIfNotEmpty("description", commandOptions.description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ public class CreateCommandOptions {
@Parameter(names = {"--tags"}, description = "The body web service tags parameter", required = false, arity = 1)
public String tags;

@Parameter(names = {"--resource"}, description = "Indicates the file is treated as a resource.", required = false, arity = 1)
public Boolean resource;

@Parameter(names = {"--job-id"}, description = "The body web service jobId parameter", required = false, arity = 1)
public String jobId;

Expand Down Expand Up @@ -378,6 +381,9 @@ public class FetchCommandOptions {
@Parameter(names = {"--url"}, description = "The body web service url parameter", required = true, arity = 1)
public String url;

@Parameter(names = {"--resource"}, description = "Indicates the file is treated as a resource.", required = false, arity = 1)
public Boolean resource;

@Parameter(names = {"--path"}, description = "The body web service path parameter", required = false, arity = 1)
public String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ protected void run() throws Exception {
int release = fileDoc.get("release", Number.class).intValue();

// Create the RESOURCES folder
File file = new File("RESOURCES", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "RESOURCES/",
Paths.get(studyUri).resolve("RESOURCES").toUri(), "Default resources folder", FileInternal.init(), 0,
release).setResource(true);
File file = new File("RESOURCES", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN,
"RESOURCES/", Paths.get(studyUri).resolve("RESOURCES").toUri(), "Default resources folder",
FileInternal.init(), true, 0, release);
try {
dbAdaptorFactory.getCatalogFileDBAdaptor(organizationId).insert(studyUid, file, Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(), QueryOptions.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum QueryParams implements QueryParam {
MODIFICATION_DATE("modificationDate", TEXT_ARRAY, ""),
DESCRIPTION("description", TEXT_ARRAY, ""),
EXTERNAL("external", BOOLEAN, ""),
RESOURCE("resource", BOOLEAN, ""),
RELEASE("release", INTEGER, ""),
STATUS("status", TEXT_ARRAY, ""),
STATUS_ID("status.id", TEXT, ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ Study insert(ClientSession clientSession, Project project, Study study)
// Create default folders for study
List<File> files = Arrays.asList(
new File(".", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "", study.getUri(),
"study root folder", FileInternal.init(), 0, project.getCurrentRelease()),
"study root folder", FileInternal.init(), false, 0, project.getCurrentRelease()),
new File("JOBS", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "JOBS/",
ioManager.getJobsUri(), "Default jobs folder", FileInternal.init(), 0, project.getCurrentRelease()),
ioManager.getJobsUri(), "Default jobs folder", FileInternal.init(), false, 0, project.getCurrentRelease()),
new File("RESOURCES", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "RESOURCES/",
Paths.get(study.getUri()).resolve("RESOURCES").toUri(), "Default resources folder", FileInternal.init(), 0,
project.getCurrentRelease()).setResource(true)
Paths.get(study.getUri()).resolve("RESOURCES").toUri(), "Default resources folder", FileInternal.init(), true, 0,
project.getCurrentRelease())
);

// Create default folders
Expand Down
Loading

0 comments on commit 90e7280

Please sign in to comment.