Sha256: 8097dbd0aa3c0d936ed052a8f0141887175985ff39ebadaba49d4a9613a54532

Contents?: true

Size: 1005 Bytes

Versions: 6

Compression:

Stored size: 1005 Bytes

Contents

require "openssl" unless defined?(OpenSSL)

module Berkshelf
  class SSLPolicy

    # @return [Store]
    #   Holds trusted CA certificates used to verify peer certificates
    attr_reader :store

    def initialize
      @store = OpenSSL::X509::Store.new.tap(&:set_default_paths)

      set_custom_certs if ::File.exist?(trusted_certs_dir)
    end

    def add_trusted_cert(cert)
      @store.add_cert(cert)
    rescue OpenSSL::X509::StoreError => e
      raise e unless e.message == "cert already in hash table"
    end

    def trusted_certs_dir
      config_dir = Berkshelf.config.chef.trusted_certs_dir.to_s.tr("\\", "/")
      if config_dir.empty? || !::File.exist?(config_dir)
        File.join(ENV["HOME"], ".chef", "trusted_certs")
      else
        config_dir
      end
    end

    def set_custom_certs
      ::Dir.glob("#{trusted_certs_dir}/{*.crt,*.pem}").each do |cert|
        cert = OpenSSL::X509::Certificate.new(IO.read(cert))
        add_trusted_cert(cert)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
berkshelf-8.0.7 lib/berkshelf/ssl_policies.rb
berkshelf-8.0.5 lib/berkshelf/ssl_policies.rb
berkshelf-8.0.2 lib/berkshelf/ssl_policies.rb
berkshelf-8.0.1 lib/berkshelf/ssl_policies.rb
berkshelf-8.0.0 lib/berkshelf/ssl_policies.rb
berkshelf-7.2.2 lib/berkshelf/ssl_policies.rb