Example usage for org.apache.commons.codec.digest DigestUtils sha256Hex

List of usage examples for org.apache.commons.codec.digest DigestUtils sha256Hex

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils sha256Hex.

Prototype

public static String sha256Hex(String data) 

Source Link

Usage

From source file:io.hops.hopsworks.api.tensorflow.TfServingService.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*  w  w  w.j a va 2s .c om*/
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
public Response createTfServing(TfServing tfServing, @Context SecurityContext sc,
        @Context HttpServletRequest req) throws AppException {

    if (projectId == null) {
        throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(), "Incomplete request!");
    }
    String hdfsUser = getHdfsUser(sc);
    if (hdfsUser == null) {
        throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
                "Could not find your username. Report a bug.");
    }

    try {
        HdfsUsers user = hdfsUsersFacade.findByName(hdfsUser);

        if (user == null) {
            throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
                    "Possible inconsistency - could not find your user.");
        }

        String modelPath = tfServing.getHdfsModelPath();

        if (modelPath.startsWith("hdfs://")) {
            int projectsIndex = modelPath.indexOf("/Projects");
            modelPath = modelPath.substring(projectsIndex, modelPath.length());
        }

        if (!inodes.existsPath(modelPath)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    "Could not find .pb file in the path " + modelPath);
        }

        if (modelPath.equals("")) {
            throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
                    "Select your .pb file corresponding to the model to be served in the Models dataset.");
        }

        tfServing.setHdfsUserId(user.getId());

        String secret = DigestUtils.sha256Hex(Integer.toString(ThreadLocalRandom.current().nextInt()));
        tfServing.setSecret(secret);

        tfServing.setModelName(getModelName(modelPath));
        int version = -1;
        String basePath = null;
        try {
            version = getVersion(modelPath);
            basePath = getModelBasePath(modelPath);
        } catch (Exception e) {
            throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
                    ".pb file should be located in Models/{model_name}/{version}");
        }

        String email = sc.getUserPrincipal().getName();

        tfServing.setVersion(version);
        tfServing.setProject(project);
        tfServing.setHdfsModelPath(basePath);
        tfServing.setStatus(TfServingStatusEnum.CREATED);
        tfServing.setCreator(userFacade.findByEmail(email));
        tfServingFacade.persist(tfServing);

    } catch (DatabaseException dbe) {
        throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), dbe.getMessage());
    }
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.CREATED).entity(tfServing).build();
}

From source file:io.hops.hopsworks.api.project.DataSetService.java

@GET
@Path("unzip/{path: .+}")
@Produces(MediaType.APPLICATION_JSON)/*from  www  .j  av a  2  s  .  c om*/
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
public Response unzip(@PathParam("path") String path, @Context SecurityContext sc)
        throws DatasetException, ProjectException {

    Response.Status resp = Response.Status.OK;
    DsPath dsPath = pathValidator.validatePath(this.project, path);
    String fullPath = dsPath.getFullPath().toString();

    String localDir = DigestUtils.sha256Hex(fullPath);
    String stagingDir = settings.getStagingDir() + File.separator + localDir;

    File unzipDir = new File(stagingDir);
    unzipDir.mkdirs();
    settings.addUnzippingState(fullPath);

    // HDFS_USERNAME is the next param to the bash script
    Users user = userFacade.findByEmail(sc.getUserPrincipal().getName());
    String hdfsUser = hdfsUsersBean.getHdfsUserName(project, user);

    List<String> commands = new ArrayList<>();

    commands.add(settings.getHopsworksDomainDir() + "/bin/unzip-background.sh");
    commands.add(stagingDir);
    commands.add(fullPath);
    commands.add(hdfsUser);

    SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands, false);
    String stdout = "", stderr = "";
    try {
        int result = commandExecutor.executeCommand();
        stdout = commandExecutor.getStandardOutputFromCommand();
        stderr = commandExecutor.getStandardErrorFromCommand();
        if (result == 2) {
            throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_SIZE_ERROR, Level.WARNING);
        }
        if (result != 0) {
            throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_ERROR, Level.WARNING,
                    "path: " + fullPath + ", result: " + result);
        }
    } catch (InterruptedException | IOException ex) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_ERROR, Level.SEVERE,
                "path: " + fullPath, ex.getMessage(), ex);
    }

    return noCacheResponse.getNoCacheResponseBuilder(resp).build();
}

From source file:io.hops.hopsworks.common.security.CertificatesMgmService.java

/**
 * Validates the provided password against the configured one
 * @param providedPassword Password to validate
 * @param userRequestedEmail User requested the password check
 * @throws IOException/*w  ww .  j  a v a  2s  .  c  o m*/
 * @throws EncryptionMasterPasswordException
 */
@Lock(LockType.READ)
@AccessTimeout(value = 3, unit = TimeUnit.SECONDS)
public void checkPassword(String providedPassword, String userRequestedEmail)
        throws IOException, EncryptionMasterPasswordException {
    String sha = DigestUtils.sha256Hex(providedPassword);
    if (!getMasterEncryptionPassword().equals(sha)) {
        Users user = userFacade.findByEmail(userRequestedEmail);
        String logMsg = "*** Attempt to change master encryption password with wrong credentials";
        if (user != null) {
            LOG.log(Level.INFO, logMsg + " by user <" + user.getUsername() + ">");
        } else {
            LOG.log(Level.INFO, logMsg);
        }
        throw new EncryptionMasterPasswordException("Provided password is incorrect");
    }
}

From source file:com.mweagle.tereus.commands.CreateCommand.java

protected void createStack(Optional<String> stackName, TereusInput tereusInput, JsonElement templateData,
        boolean logTemplate) throws UnsupportedEncodingException {
    if (tereusInput.dryRun) {
        tereusInput.logger.info("Dry run requested (-n/--noop). Stack creation bypassed.");
        if (logTemplate) {
            final String formattedTemplate = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
                    .create().toJson(templateData);
            tereusInput.logger.info("Stack Template:\n {}", formattedTemplate);
        }// w w w  .  j  a v a2 s. c  om
    } else {
        final String bucketName = tereusInput.params.get(CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME).toString();
        // Upload the template
        final String templateContent = new GsonBuilder().create().toJson(templateData);
        final byte[] templateBytes = templateContent.getBytes("UTF-8");
        final InputStream is = new ByteArrayInputStream(templateBytes);
        final String templateDigest = DigestUtils.sha256Hex(templateBytes);
        final String keyName = String.format("%s-tereus.cf.template", templateDigest);

        try (S3Resource resource = new S3Resource(bucketName, keyName, is,
                Optional.of(Long.valueOf(templateBytes.length)))) {
            resource.upload();
            final EstimateTemplateCostRequest costRequest = new EstimateTemplateCostRequest();
            costRequest.setParameters(toParameterList(tereusInput.params));
            costRequest.setTemplateURL(resource.getResourceURL().get());
            final AmazonCloudFormationClient awsClient = new AmazonCloudFormationClient(
                    tereusInput.awsCredentials);
            awsClient.setRegion(tereusInput.awsRegion);
            final EstimateTemplateCostResult costResult = awsClient.estimateTemplateCost(costRequest);
            tereusInput.logger.info("Cost Estimator: {}", costResult.getUrl());

            // Go ahead and create the stack.
            final String defaultTemplateName = String.format("Tereus-%s", System.currentTimeMillis());
            final CreateStackRequest request = new CreateStackRequest()
                    .withStackName(stackName.orElse(defaultTemplateName))
                    .withTemplateURL(resource.getResourceURL().get())
                    .withParameters(toParameterList(tereusInput.params)).withTags(toTagList(tereusInput.tags))
                    .withCapabilities("CAPABILITY_IAM");
            tereusInput.logger.debug("Creating stack: {}", stackName);
            tereusInput.logger.debug("Stack params: {}", request.getParameters());
            tereusInput.logger.debug("Stack tags: {}", request.getTags());
            final Optional<DescribeStacksResult> result = new CloudFormation().createStack(request,
                    tereusInput.awsRegion, tereusInput.logger);
            if (result.isPresent()) {
                tereusInput.logger.info("Stack successfully created");
                tereusInput.logger.info(result.get().toString());
                resource.setReleased(true);
            }
        }
    }
}

From source file:com.squid.kraken.v4.caching.redis.RedisCacheManager.java

public String buildCacheKey(String SQLQuery, List<String> dependencies) {
    String key = "";
    if (dependencies.size() > 0) {
        key += dependencies.get(0);/*  w  w  w  .  j  a  va2 s  .  co m*/
    }
    key += "-" + DigestUtils.sha256Hex(SQLQuery);
    //
    RedisKey rk = getKey(key, dependencies);
    return rk.getStringKey();
}

From source file:com.bloatit.framework.webprocessor.context.Session.java

public final synchronized String createWebProcess(final WebProcess process) {
    int length = 5;
    String key = null;/*w  w  w  .  j a  v  a2s .com*/
    while (key == null) {
        final String tempKey = DigestUtils.sha256Hex(UUID.randomUUID().toString()).substring(0, length);

        if (processes.containsKey(tempKey)) {
            if (length < SHA1_SIZE) {
                length++;
            }
        } else {
            key = tempKey;
        }

    }
    processes.put(key, process);
    return key;
}

From source file:eu.cloud4soa.tests.TestReqSec_Deploy.java

private ApplicationInstance createApplicationInstance(String applicationName) throws FileNotFoundException {
    URL fileURL = this.getClass().getClassLoader().getResource("SimpleWar.war");
    if (fileURL == null)
        throw new FileNotFoundException("SimpleWar.war");

    ByteArrayOutputStream bas = new ByteArrayOutputStream();

    File file = new File(fileURL.getPath());
    file.length();//  www.  j a v a2  s .  com
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);

    //Calculate digest from InputStream
    //        InputStream tempIs = new FileInputStream(file);
    String tempFileDigest = null;
    try {
        FileInputStream tempFis = new FileInputStream(file);
        tempFileDigest = DigestUtils.sha256Hex(tempFis);
    } catch (IOException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    ApplicationInstance applicationInstance = null;
    applicationInstance = new ApplicationInstance();
    applicationInstance.setAcronym(applicationName);
    applicationInstance.setApplicationcode(applicationName);
    applicationInstance.setDigest(tempFileDigest);
    applicationInstance.setProgramminglanguage("Java");
    applicationInstance.setProgramminglanguageVersion("1.6");
    applicationInstance.setSizeQuantity(new Float(file.length()));
    applicationInstance.setVersion("1.0");
    applicationInstance.setArchiveFileName("SimpleWar");
    applicationInstance.setArchiveExtensionName(".war");

    try {
        fis.close();
        bis.close();
        dis.close();
    } catch (IOException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }

    return applicationInstance;
}

From source file:io.hops.hopsworks.common.security.CertificatesMgmService.java

/**
 * Decrypt secrets using the old master password and encrypt them with the new
 * Both for project specific and project generic certificates
 * @param newMasterPasswd new master encryption password
 * @param userRequested User requested password change
 *///  ww w .  ja v a  2  s.  c  o m
@SuppressWarnings("unchecked")
@Asynchronous
@Lock(LockType.WRITE)
@AccessTimeout(value = 500)
public void resetMasterEncryptionPassword(String newMasterPasswd, String userRequested) {
    try {
        String newDigest = DigestUtils.sha256Hex(newMasterPasswd);
        List<String> updatedCertificates = callUpdateHandlers(newDigest);
        updateMasterEncryptionPassword(newDigest);
        sendSuccessfulMessage(updatedCertificates, userRequested);
        LOG.log(Level.INFO, "Master encryption password changed!");
    } catch (EncryptionMasterPasswordException ex) {
        String errorMsg = "*** Master encryption password update failed!!! Rolling back...";
        LOG.log(Level.SEVERE, errorMsg, ex);
        callRollbackHandlers();
        sendUnsuccessfulMessage(errorMsg + "\n" + ex.getMessage(), userRequested);
    } catch (IOException ex) {
        String errorMsg = "*** Failed to write new encryption password to file: "
                + masterPasswordFile.getAbsolutePath() + ". Rolling back...";
        LOG.log(Level.SEVERE, errorMsg, ex);
        callRollbackHandlers();
        sendUnsuccessfulMessage(errorMsg + "\n" + ex.getMessage(), userRequested);
    }
}

From source file:io.hops.hopsworks.common.user.AuthController.java

/**
 * Validate security question and update false login attempts
 *
 * @param user/*from w w w  .j a va2s.c  om*/
 * @param securityQ
 * @param securityAnswer
 * @param req
 * @return
 */
public boolean validateSecurityQA(Users user, String securityQ, String securityAnswer, HttpServletRequest req) {
    if (user == null) {
        throw new IllegalArgumentException("User not set.");
    }
    if (user.getMode().equals(UserAccountType.LDAP_ACCOUNT_TYPE)) {
        throw new IllegalArgumentException("Operation not allowed for LDAP account.");
    }
    if (securityQ == null || securityQ.isEmpty() || securityAnswer == null || securityAnswer.isEmpty()) {
        return false;
    }
    if (!user.getSecurityQuestion().getValue().equalsIgnoreCase(securityQ)
            || !user.getSecurityAnswer().equals(DigestUtils.sha256Hex(securityAnswer.toLowerCase()))) {
        registerFalseLogin(user, req);
        LOGGER.log(Level.WARNING, "False Security Question attempt by user: {0}", user.getEmail());
        return false;
    }
    return true;
}

From source file:io.hops.hopsworks.api.project.DataSetService.java

@GET
@Path("zip/{path: .+}")
@Produces(MediaType.APPLICATION_JSON)//  w w  w.j  a v a 2  s  .  co  m
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
public Response zip(@PathParam("path") String path, @Context SecurityContext sc)
        throws DatasetException, ProjectException {

    Response.Status resp = Response.Status.OK;
    DsPath dsPath = pathValidator.validatePath(this.project, path);
    String fullPath = dsPath.getFullPath().toString();

    String localDir = DigestUtils.sha256Hex(fullPath);
    String stagingDir = settings.getStagingDir() + File.separator + localDir;

    File zipDir = new File(stagingDir);
    zipDir.mkdirs();
    settings.addZippingState(fullPath);

    // HDFS_USERNAME is the next param to the bash script
    Users user = userFacade.findByEmail(sc.getUserPrincipal().getName());
    String hdfsUser = hdfsUsersBean.getHdfsUserName(project, user);

    List<String> commands = new ArrayList<>();

    commands.add(settings.getHopsworksDomainDir() + "/bin/zip-background.sh");
    commands.add(stagingDir);
    commands.add(fullPath);
    commands.add(hdfsUser);

    SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands, false);
    String stdout = "", stderr = "";
    try {
        int result = commandExecutor.executeCommand();
        stdout = commandExecutor.getStandardOutputFromCommand();
        stderr = commandExecutor.getStandardErrorFromCommand();
        if (result == 2) {
            throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_SIZE_ERROR, Level.WARNING);
        }
        if (result != 0) {
            throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_ERROR, Level.WARNING,
                    "path: " + fullPath + ", result: " + result);
        }
    } catch (InterruptedException | IOException ex) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.COMPRESSION_ERROR, Level.SEVERE,
                "path: " + fullPath, ex.getMessage(), ex);
    }

    return noCacheResponse.getNoCacheResponseBuilder(resp).build();
}