Search in sources :

Example 6 with JupyterProject

use of io.hops.hopsworks.persistence.entity.jupyter.JupyterProject in project hopsworks by logicalclocks.

the class JupyterJWTManager method recover.

protected void recover() {
    LOG.log(INFO, "Starting Jupyter JWT manager recovery");
    List<MaterializedJWT> failed2recover = new ArrayList<>();
    // Get state from the database
    for (MaterializedJWT materializedJWT : materializedJWTFacade.findAll4Jupyter()) {
        LOG.log(Level.FINEST, "Recovering Jupyter JWT " + materializedJWT.getIdentifier());
        // First lookup project and user in db
        Project project = projectFacade.find(materializedJWT.getIdentifier().getProjectId());
        Users user = userFacade.find(materializedJWT.getIdentifier().getUserId());
        if (project == null || user == null) {
            LOG.log(Level.WARNING, "Tried to recover " + materializedJWT.getIdentifier() + " but could not find " + "either Project or User");
            failed2recover.add(materializedJWT);
            continue;
        }
        // Get Jupyter configuration from db
        String hdfsUsername = hdfsUsersController.getHdfsUserName(project, user);
        JupyterProject jupyterProject = jupyterFacade.findByUser(hdfsUsername);
        if (jupyterProject == null) {
            LOG.log(Level.FINEST, "There is no Jupyter configuration persisted for " + materializedJWT.getIdentifier());
            failed2recover.add(materializedJWT);
            continue;
        }
        // Check if Jupyter is still running
        if (!jupyterManager.ping(jupyterProject)) {
            LOG.log(Level.FINEST, "Jupyter server is not running for " + materializedJWT.getIdentifier() + " Skip recovering...");
            failed2recover.add(materializedJWT);
            continue;
        }
        JupyterSettings jupyterSettings = jupyterSettingsFacade.findByProjectUser(project, user.getEmail());
        Path tokenFile = constructTokenFilePath(jupyterSettings);
        String token = null;
        JupyterJWT jupyterJWT = null;
        CidAndPort pidAndPort = new CidAndPort(jupyterProject.getCid(), jupyterProject.getPort());
        try {
            token = FileUtils.readFileToString(tokenFile.toFile());
            DecodedJWT decodedJWT = jwtController.verifyToken(token, settings.getJWTIssuer());
            jupyterJWT = new JupyterJWT(project, user, DateUtils.date2LocalDateTime(decodedJWT.getExpiresAt()), pidAndPort);
            jupyterJWT.token = token;
            jupyterJWT.tokenFile = tokenFile;
            LOG.log(Level.FINE, "Successfully read existing JWT from local filesystem");
        } catch (IOException | JWTException | JWTDecodeException ex) {
            LOG.log(Level.FINE, "Could not recover Jupyter JWT from local filesystem, generating new!", ex);
            // JWT does not exist or it is not valid any longer
            // We should create a new one
            String[] audience = new String[] { "api" };
            LocalDateTime expirationDate = LocalDateTime.now().plus(settings.getJWTLifetimeMs(), ChronoUnit.MILLIS);
            String[] userRoles = usersController.getUserRoles(user).toArray(new String[1]);
            try {
                Map<String, Object> claims = new HashMap<>(3);
                claims.put(Constants.RENEWABLE, false);
                claims.put(Constants.EXPIRY_LEEWAY, settings.getJWTExpLeewaySec());
                claims.put(Constants.ROLES, userRoles);
                token = jwtController.createToken(settings.getJWTSigningKeyName(), false, settings.getJWTIssuer(), audience, DateUtils.localDateTime2Date(expirationDate), DateUtils.localDateTime2Date(DateUtils.getNow()), user.getUsername(), claims, SignatureAlgorithm.valueOf(settings.getJWTSignatureAlg()));
                jupyterJWT = new JupyterJWT(project, user, expirationDate, pidAndPort);
                jupyterJWT.token = token;
                jupyterJWT.tokenFile = tokenFile;
                jwtTokenWriter.writeToken(settings, jupyterJWT);
                LOG.log(Level.FINE, "Generated new Jupyter JWT cause could not recover existing");
            } catch (IOException recIOEx) {
                LOG.log(Level.WARNING, "Failed to recover Jupyter JWT for " + materializedJWT.getIdentifier() + ", generated new valid JWT but failed to write to local filesystem. Invalidating new token!" + " Continue recovering...");
                if (token != null) {
                    try {
                        jwtController.invalidate(token);
                    } catch (InvalidationException jwtInvEx) {
                    // NO-OP
                    }
                }
                failed2recover.add(materializedJWT);
                continue;
            } catch (GeneralSecurityException | JWTException jwtEx) {
                LOG.log(Level.WARNING, "Failed to recover Jupyter JWT for " + materializedJWT.getIdentifier() + ", tried to generate new token and it failed as well. Could not recover! Continue recovering...");
                // Did our best, it's good to know when you should give up
                failed2recover.add(materializedJWT);
                continue;
            }
        }
        addToken(jupyterJWT);
    }
    // Remove from the database entries that we failed to recover
    for (MaterializedJWT failedRecovery : failed2recover) {
        materializedJWTFacade.delete(failedRecovery.getIdentifier());
    }
    LOG.log(INFO, "Finished Jupyter JWT recovery");
}
Also used : Path(java.nio.file.Path) LocalDateTime(java.time.LocalDateTime) JWTException(io.hops.hopsworks.jwt.exception.JWTException) ArrayList(java.util.ArrayList) JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) Users(io.hops.hopsworks.persistence.entity.user.Users) IOException(java.io.IOException) MaterializedJWT(io.hops.hopsworks.persistence.entity.airflow.MaterializedJWT) JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) Project(io.hops.hopsworks.persistence.entity.project.Project) JWTDecodeException(com.auth0.jwt.exceptions.JWTDecodeException) JupyterSettings(io.hops.hopsworks.persistence.entity.jupyter.JupyterSettings) InvalidationException(io.hops.hopsworks.jwt.exception.InvalidationException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with JupyterProject

use of io.hops.hopsworks.persistence.entity.jupyter.JupyterProject in project hopsworks by logicalclocks.

the class JupyterFacade method remove.

public boolean remove(String hdfsUsername, int port) {
    if (hdfsUsername == null || hdfsUsername.isEmpty()) {
        return false;
    }
    JupyterProject jp = findByUserAndPort(hdfsUsername, port);
    if (jp == null) {
        return false;
    }
    try {
        em.remove(jp);
        em.flush();
    } catch (Exception ex) {
        logger.warning("Problem removing jupyter notebook entry from hopsworks DB");
        logger.warning(ex.getMessage());
        return false;
    }
    return true;
}
Also used : JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) NoResultException(javax.persistence.NoResultException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 8 with JupyterProject

use of io.hops.hopsworks.persistence.entity.jupyter.JupyterProject in project hopsworks by logicalclocks.

the class JupyterFacade method findByUserAndPort.

public JupyterProject findByUserAndPort(String hdfsUser, int port) {
    HdfsUsers res = null;
    TypedQuery<HdfsUsers> query = em.createNamedQuery("HdfsUsers.findByName", HdfsUsers.class);
    query.setParameter("name", hdfsUser);
    try {
        res = query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
    TypedQuery<JupyterProject> jupyterProjectQuery = em.createNamedQuery("JupyterProject.findByHdfsUserIdAndPort", JupyterProject.class);
    jupyterProjectQuery.setParameter("hdfsUserId", res.getId());
    jupyterProjectQuery.setParameter("port", port);
    try {
        return jupyterProjectQuery.getSingleResult();
    } catch (NoResultException e) {
        Logger.getLogger(JupyterFacade.class.getName()).log(Level.FINE, null, e);
    }
    return null;
}
Also used : JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) NoResultException(javax.persistence.NoResultException) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)

Example 9 with JupyterProject

use of io.hops.hopsworks.persistence.entity.jupyter.JupyterProject in project hopsworks by logicalclocks.

the class JupyterService method stopNotebookServer.

@GET
@Path("/stop")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response stopNotebookServer(@Context SecurityContext sc) throws ProjectException, ServiceException {
    Users user = jWTHelper.getUserPrincipal(sc);
    String hdfsUsername = hdfsUsersController.getHdfsUserName(project, user);
    JupyterProject jp = jupyterFacade.findByUser(hdfsUsername);
    if (jp == null) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.JUPYTER_SERVER_NOT_FOUND, Level.FINE, "hdfsUser: " + hdfsUsername);
    }
    jupyterController.shutdown(project, hdfsUsername, user, jp.getSecret(), jp.getCid(), jp.getPort());
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).build();
}
Also used : ProjectException(io.hops.hopsworks.exceptions.ProjectException) JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) Users(io.hops.hopsworks.persistence.entity.user.Users) Path(javax.ws.rs.Path) 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 10 with JupyterProject

use of io.hops.hopsworks.persistence.entity.jupyter.JupyterProject in project hopsworks by logicalclocks.

the class JupyterFacade method findByUser.

public JupyterProject findByUser(String hdfsUser) {
    HdfsUsers res = null;
    TypedQuery<HdfsUsers> hdfsUsersQuery = em.createNamedQuery("HdfsUsers.findByName", HdfsUsers.class);
    hdfsUsersQuery.setParameter("name", hdfsUser);
    try {
        res = hdfsUsersQuery.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
    TypedQuery<JupyterProject> jupyterProjectQuery = em.createNamedQuery("JupyterProject.findByHdfsUserId", JupyterProject.class);
    jupyterProjectQuery.setParameter("hdfsUserId", res.getId());
    try {
        return jupyterProjectQuery.getSingleResult();
    } catch (NoResultException e) {
        Logger.getLogger(JupyterFacade.class.getName()).log(Level.FINE, null, e);
    }
    return null;
}
Also used : JupyterProject(io.hops.hopsworks.persistence.entity.jupyter.JupyterProject) NoResultException(javax.persistence.NoResultException) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)

Aggregations

JupyterProject (io.hops.hopsworks.persistence.entity.jupyter.JupyterProject)17 HdfsUsers (io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)7 ServiceException (io.hops.hopsworks.exceptions.ServiceException)6 Users (io.hops.hopsworks.persistence.entity.user.Users)6 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)4 ProjectException (io.hops.hopsworks.exceptions.ProjectException)4 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)4 IOException (java.io.IOException)4 Date (java.util.Date)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 JupyterSettings (io.hops.hopsworks.persistence.entity.jupyter.JupyterSettings)3 NoResultException (javax.persistence.NoResultException)3 GET (javax.ws.rs.GET)3 DistributedFileSystemOps (io.hops.hopsworks.common.hdfs.DistributedFileSystemOps)2 GenericException (io.hops.hopsworks.exceptions.GenericException)2 HopsSecurityException (io.hops.hopsworks.exceptions.HopsSecurityException)2 File (java.io.File)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2