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");
}
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;
}
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;
}
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();
}
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;
}
Aggregations