Example #1
0
    public void updateBackingContent(String filename, String displayVersion) {
      File file = new File(filename);
      if (!file.exists()) {
        throw new IllegalArgumentException("File not found: " + file.getAbsolutePath());
      }
      if (file.isDirectory()) {
        throw new IllegalArgumentException(
            "File expected, found directory: " + file.getAbsolutePath());
      }

      byte[] fileContents = new ScriptUtil(remoteClient).getFileBytes(filename);
      String sha = null;
      try {
        sha =
            new MessageDigestGenerator(MessageDigestGenerator.SHA_256)
                .calcDigestString(fileContents);
      } catch (Exception e) {
        // do nothing because the sha will remain null.
        LOG.error("Message digest for the package bits failed.", e);
      }

      String packageVersion = "[sha256=" + sha + "]";

      InstalledPackage oldPackage = getBackingContent();

      PackageVersion pv =
          remoteClient
              .getContentManager()
              .createPackageVersionWithDisplayVersion(
                  remoteClient.getSubject(),
                  oldPackage.getPackageVersion().getGeneralPackage().getName(),
                  oldPackage.getPackageVersion().getGeneralPackage().getPackageType().getId(),
                  packageVersion,
                  displayVersion,
                  oldPackage.getPackageVersion().getArchitecture().getId(),
                  fileContents);

      remoteClient
          .getContentManager()
          .deployPackagesWithNote(
              remoteClient.getSubject(),
              new int[] {resourceClientProxy.getId()},
              new int[] {pv.getId()},
              "CLI deployment request");
    }
Example #2
0
  /**
   * Checks to see if the package file's hash matches that of the given package version. If the file
   * doesn't exist or the hash doesn't match, an exception is thrown. This method returns normally
   * if the hash matches the file. If there is no known hash in the package version, this method
   * returns normally.
   *
   * @param packageVersion contains the hash that is expected
   * @param packageFile the local file whose hash is to be checked
   * @throws Exception if the file does not match the hash or the file doesn't exist
   */
  private void verifyHash(PackageVersion packageVersion, File packageFile) throws Exception {
    if (!packageFile.exists()) {
      throw new Exception(
          "Package version [" + packageVersion + "] does not exist, cannot check hash");
    }

    String realHash;
    if (packageVersion.getMD5() != null) {
      realHash =
          new MessageDigestGenerator(MessageDigestGenerator.MD5).calcDigestString(packageFile);
      if (!packageVersion.getMD5().equals(realHash)) {
        throw new Exception(
            "Package version ["
                + packageVersion
                + "] failed MD5 check. expected=["
                + packageVersion.getMD5()
                + "], actual=["
                + realHash
                + "]");
      }
    } else if (packageVersion.getSHA256() != null) {
      realHash =
          new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(packageFile);
      if (!packageVersion.getSHA256().equals(realHash)) {
        throw new Exception(
            "Package version ["
                + packageVersion
                + "] failed SHA256 check. expected=["
                + packageVersion.getSHA256()
                + "], actual=["
                + realHash
                + "]");
      }
    } else {
      log.debug(
          "Package version [" + packageVersion + "] has no MD5/SHA256 hash - not verifying it");
    }

    return;
  }
Example #3
0
  /**
   * Downloads the bundle's files into the bundle plugin's tmp directory and returns that tmp
   * directory.
   *
   * @param resourceDeployment access to deployment information, including what bundle files need to
   *     be downloaded
   * @param downloadDir location where the bundle files should be downloaded
   * @return map of the package versions to their files that were downloaded
   * @throws Exception
   */
  private Map<PackageVersion, File> downloadBundleFiles(
      BundleResourceDeployment resourceDeployment, File downloadDir) throws Exception {

    BundleDeployment bundleDeployment = resourceDeployment.getBundleDeployment();
    BundleVersion bundleVersion = bundleDeployment.getBundleVersion();

    Map<PackageVersion, File> packageVersionFiles = new HashMap<PackageVersion, File>();
    List<PackageVersion> packageVersions = getAllBundleVersionPackageVersions(bundleVersion);
    for (PackageVersion packageVersion : packageVersions) {
      File packageFile = new File(downloadDir, packageVersion.getFileName());

      try {
        verifyHash(packageVersion, packageFile);
      } catch (Exception e) {

        // file either doesn't exist or it hash doesn't match, download a new copy
        packageFile.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(packageFile);
        try {
          auditDeployment(
              resourceDeployment,
              AUDIT_FILE_DOWNLOAD_STARTED,
              packageVersion.getDisplayName(),
              "Downloading [" + packageVersion + "]");

          long size = getFileContent(packageVersion, fos);

          if (packageVersion.getFileSize() != null
              && size != packageVersion.getFileSize().longValue()) {
            String message =
                "Downloaded bundle file ["
                    + packageVersion
                    + "] but its size was ["
                    + size
                    + "] when it was expected to be ["
                    + packageVersion.getFileSize()
                    + "].";
            log.warn(message);
            auditDeployment(
                resourceDeployment,
                AUDIT_FILE_DOWNLOAD_ENDED,
                packageVersion.getDisplayName(),
                null,
                BundleResourceDeploymentHistory.Status.WARN,
                message,
                null);
          } else {
            auditDeployment(
                resourceDeployment,
                AUDIT_FILE_DOWNLOAD_ENDED,
                packageVersion.getDisplayName(),
                "Download complete for [" + packageVersion + "]");
          }
        } catch (Exception e2) {
          String message = "Failed to downloaded bundle file [" + packageVersion + "] " + e2;
          log.warn(message);
          auditDeployment(
              resourceDeployment,
              AUDIT_FILE_DOWNLOAD_ENDED,
              packageVersion.getDisplayName(),
              null,
              BundleResourceDeploymentHistory.Status.FAILURE,
              message,
              null);
        } finally {
          fos.close();
        }

        // now try to verify it again, if this throws an exception, that is very bad and we need to
        // abort
        verifyHash(packageVersion, packageFile);
      }

      packageVersionFiles.put(packageVersion, packageFile);
    }

    return packageVersionFiles;
  }