Search in sources :

Example 11 with DatasetPath

use of io.hops.hopsworks.common.dataset.util.DatasetPath in project hopsworks by logicalclocks.

the class ProjectMembersService method getDatasetMembers.

@GET
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@Path("/dataset/{name}")
public Response getDatasetMembers(@PathParam("name") String dsName, @QueryParam("type") DatasetType datasetType) throws ProjectException, DatasetException {
    Project project = projectController.findProjectById(this.projectId);
    String path = Utils.getProjectPath(project.getName()) + dsName;
    DatasetPath dp = datasetHelper.getDatasetPath(project, path, datasetType);
    Collection<ProjectTeam> membersCol = accessCtrl.getExtendedMembers(dp.getDataset());
    GenericEntity<Collection<ProjectTeam>> members = new GenericEntity<Collection<ProjectTeam>>(membersCol) {
    };
    return Response.ok().entity(members).build();
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTeam(io.hops.hopsworks.persistence.entity.project.team.ProjectTeam) GenericEntity(javax.ws.rs.core.GenericEntity) Collection(java.util.Collection) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Path(javax.ws.rs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 12 with DatasetPath

use of io.hops.hopsworks.common.dataset.util.DatasetPath in project hopsworks by logicalclocks.

the class ProjectProvenanceResource method usage.

@GET
@Path("usage")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiKeyRequired(acceptedScopes = { ApiScope.PROJECT }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiOperation(value = "Artifact usage", response = ProvArtifactUsageParentDTO.class)
public Response usage(@QueryParam("artifact_id") String artifactId, @QueryParam("endpoint_id") Integer endpointId, @QueryParam("artifact_type") DatasetAccessType accessType, @BeanParam ProvUsageBeanParams params, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ProvenanceException, GenericException, DatasetException, MetadataException, SchematizedTagException {
    Users user = jWTHelper.getUserPrincipal(sc);
    if (artifactId == null) {
        throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "artifactId id cannot be null");
    }
    Project targetProject = project;
    if (endpointId != null) {
        targetProject = projectFacade.findById(endpointId).orElseThrow(() -> new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "target project not found"));
    }
    Dataset targetEndpoint;
    if (accessType != null) {
        try {
            switch(accessType) {
                case FEATUREGROUPS:
                    targetEndpoint = fsCtrl.getProjectFeaturestoreDataset(targetProject);
                    break;
                case TRAININGDATASETS:
                    String tdName = project.getName() + "_" + Settings.ServiceDataset.TRAININGDATASETS.getName();
                    targetEndpoint = datasetCtrl.getByName(targetProject, tdName);
                    break;
                case MODELS:
                    targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_MODELS_DATASET);
                    break;
                case EXPERIMENTS:
                    targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_EXPERIMENTS_DATASET);
                    break;
                default:
                    throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "access type not supports:" + accessType);
            }
        } catch (FeaturestoreException | DatasetException e) {
            throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "cannot access the dataset of the artifact");
        }
    } else {
        throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "access type not defined");
    }
    DatasetPath targetEndpointPath = datasetHelper.getTopLevelDatasetPath(project, targetEndpoint);
    ProvArtifactUsageParentDTO status = usageBuilder.buildAccessible(uriInfo, user, targetEndpointPath, artifactId, params.getUsageType());
    return Response.ok().entity(status).build();
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) ProvArtifactUsageParentDTO(io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO) Users(io.hops.hopsworks.persistence.entity.user.Users) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) FeaturestoreException(io.hops.hopsworks.exceptions.FeaturestoreException) GenericException(io.hops.hopsworks.exceptions.GenericException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) Path(javax.ws.rs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) ApiKeyRequired(io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 13 with DatasetPath

use of io.hops.hopsworks.common.dataset.util.DatasetPath in project hopsworks by logicalclocks.

the class DownloadService method downloadFromHDFS.

@GET
@javax.ws.rs.Path("with_token/{path: .+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@JWTNotRequired
@ApiOperation(value = "Download file.", response = StreamingOutput.class)
public Response downloadFromHDFS(@PathParam("path") String path, @QueryParam("token") String token, @QueryParam("type") DatasetType datasetType, @Context SecurityContext sc) throws DatasetException, SigningKeyNotFoundException, VerificationException, ProjectException {
    if (!settings.isDownloadAllowed()) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DOWNLOAD_NOT_ALLOWED, Level.FINEST);
    }
    Project project = this.getProject();
    DatasetPath datasetPath = datasetHelper.getDatasetPathIfFileExist(project, path, datasetType);
    String fullPath = datasetPath.getFullPath().toString();
    DecodedJWT djwt = jWTHelper.verifyOneTimeToken(token, fullPath);
    Users user = userFacade.findByUsername(djwt.getSubject());
    Pair<Path, StreamingOutput> pathStreamPair = downloadFromHDFS(project, datasetPath, user);
    Response.ResponseBuilder response = Response.ok(pathStreamPair.getValue1());
    response.header("Content-disposition", "attachment; filename=\"" + pathStreamPair.getValue0().getName() + "\"");
    return response.build();
}
Also used : Path(org.apache.hadoop.fs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Response(javax.ws.rs.core.Response) Project(io.hops.hopsworks.persistence.entity.project.Project) StreamingOutput(javax.ws.rs.core.StreamingOutput) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Users(io.hops.hopsworks.persistence.entity.user.Users) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) DatasetException(io.hops.hopsworks.exceptions.DatasetException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) JWTNotRequired(io.hops.hopsworks.api.filter.JWTNotRequired)

Example 14 with DatasetPath

use of io.hops.hopsworks.common.dataset.util.DatasetPath in project hopsworks by logicalclocks.

the class DownloadService method getDownloadToken.

@GET
@javax.ws.rs.Path("token/{path: .+}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get a one time download token.", response = RESTApiJsonResponse.class)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response getDownloadToken(@PathParam("path") String path, @QueryParam("type") DatasetType datasetType, @Context SecurityContext sc) throws DatasetException, ProjectException {
    if (!settings.isDownloadAllowed()) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DOWNLOAD_NOT_ALLOWED, Level.FINEST);
    }
    Users user = jWTHelper.getUserPrincipal(sc);
    DatasetPath datasetPath = datasetHelper.getDatasetPathIfFileExist(this.getProject(), path, datasetType);
    Project owningProject = datasetController.getOwningProject(datasetPath.getDataset());
    RESTApiJsonResponse response = new RESTApiJsonResponse();
    String username = hdfsUsersController.getHdfsUserName(this.getProject(), user);
    // For example, DS1 of project1 is shared with project2. User must be a member of project1 to download files
    if (owningProject.equals(this.getProject()) && datasetController.isDownloadAllowed(this.getProject(), user, datasetPath.getFullPath().toString())) {
        datasetController.checkFileExists(datasetPath.getFullPath(), username);
        String token = jWTHelper.createOneTimeToken(user, datasetPath.getFullPath().toString(), null);
        if (token != null && !token.isEmpty()) {
            response.setData(token);
            return Response.status(Response.Status.OK).entity(response).build();
        }
    }
    response.setErrorMsg(ResponseMessages.DOWNLOAD_PERMISSION_ERROR);
    return Response.status(Response.Status.FORBIDDEN).entity(response).build();
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) Users(io.hops.hopsworks.persistence.entity.user.Users) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) DatasetException(io.hops.hopsworks.exceptions.DatasetException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 15 with DatasetPath

use of io.hops.hopsworks.common.dataset.util.DatasetPath in project hopsworks by logicalclocks.

the class DownloadService method downloadFromHDFS.

/**
 * @param project
 * @param datasetPath
 * @param user
 * @return
 */
private Pair<Path, StreamingOutput> downloadFromHDFS(Project project, DatasetPath datasetPath, Users user) throws DatasetException {
    String fullPath = datasetPath.getFullPath().toString();
    String projectUsername = hdfsUsersController.getHdfsUserName(project, user);
    Dataset ds = datasetPath.getDataset();
    if (ds.isShared(project) && ds.getFilePermissions().equals(DatasetPermissions.OWNER_ONLY) && !ds.isPublicDs()) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DOWNLOAD_ERROR, Level.FINE);
    }
    FSDataInputStream stream;
    DistributedFileSystemOps udfso;
    try {
        if (projectUsername != null) {
            udfso = dfs.getDfsOps(projectUsername);
            Path p = new Path(fullPath);
            stream = udfso.open(p);
            return new Pair(p, buildOutputStream(stream, udfso));
        } else {
            throw new DatasetException(RESTCodes.DatasetErrorCode.DOWNLOAD_ERROR, Level.WARNING);
        }
    } catch (IOException ex) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DOWNLOAD_ERROR, Level.SEVERE, "path: " + fullPath, ex.getMessage(), ex);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) DistributedFileSystemOps(io.hops.hopsworks.common.hdfs.DistributedFileSystemOps) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) Pair(org.javatuples.Pair)

Aggregations

DatasetPath (io.hops.hopsworks.common.dataset.util.DatasetPath)26 Users (io.hops.hopsworks.persistence.entity.user.Users)15 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)14 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)13 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)13 ApiOperation (io.swagger.annotations.ApiOperation)13 ApiKeyRequired (io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired)10 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)9 DatasetException (io.hops.hopsworks.exceptions.DatasetException)9 Project (io.hops.hopsworks.persistence.entity.project.Project)9 Dataset (io.hops.hopsworks.persistence.entity.dataset.Dataset)8 GET (javax.ws.rs.GET)7 TagsDTO (io.hops.hopsworks.common.tags.TagsDTO)5 GenericException (io.hops.hopsworks.exceptions.GenericException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 InodeDTO (io.hops.hopsworks.api.dataset.inode.InodeDTO)3 DELETE (javax.ws.rs.DELETE)3 PUT (javax.ws.rs.PUT)3 ProvArtifactUsageParentDTO (io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO)2