cloudfoundry/cloud_controller_ng

View on GitHub
lib/cloud_controller/buildpacks/stack_name_extractor.rb

Summary

Maintainability
A
1 hr
Test Coverage
module VCAP::CloudController
  module Buildpacks
    class StackNameExtractor
      ONE_MEGABYTE = 1024 * 1024

      def self.extract_from_file(bits_file_path)
        bits_file_path = bits_file_path.path if bits_file_path.respond_to?(:path)
        Zip::File.open(bits_file_path) do |zip_file|
          zip_file.each do |entry|
            next unless entry.name == 'manifest.yml'
            raise CloudController::Errors::BuildpackError.new('buildpack manifest is too large') if entry.size > ONE_MEGABYTE

            return Psych.safe_load(entry.get_input_stream.read, permitted_classes: [Date], strict_integer: true)['stack']
          end
        end
        nil
      rescue Psych::Exception
        raise CloudController::Errors::BuildpackError.new('buildpack manifest is not valid')
      rescue Zip::Error
        raise CloudController::Errors::BuildpackError.new('buildpack zipfile is not valid')
      end
    end
  end
end