summaryrefslogtreecommitdiff
path: root/lib/bundler/ssl_certs/certificate_manager.rb
blob: a5e5d84b645d0fa20ebb216b65bb3df666d41b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
require "fileutils"
require "net/https"
require "openssl"

module Bundler
  module SSLCerts
    class CertificateManager
      attr_reader :bundler_cert_path, :bundler_certs, :rubygems_certs

      def self.update_from!(rubygems_path)
        new(rubygems_path).update!
      end

      def initialize(rubygems_path = nil)
        if rubygems_path
          rubygems_cert_path = File.join(rubygems_path, "lib/rubygems/ssl_certs")
          @rubygems_certs = certificates_in(rubygems_cert_path)
        end

        @bundler_cert_path = File.expand_path("..", __FILE__)
        @bundler_certs = certificates_in(bundler_cert_path)
      end

      def up_to_date?
        rubygems_certs.all? do |rc|
          bundler_certs.find do |bc|
            File.basename(bc) == File.basename(rc) && FileUtils.compare_file(bc, rc)
          end
        end
      end

      def update!
        return if up_to_date?

        FileUtils.rm bundler_certs
        FileUtils.cp rubygems_certs, bundler_cert_path
      end

      def connect_to(host)
        http = Net::HTTP.new(host, 443)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.cert_store = store
        http.head("/")
      end

    private

      def certificates_in(path)
        Dir[File.join(path, "**/*.pem")].sort
      end

      def store
        @store ||= begin
          store = OpenSSL::X509::Store.new
          bundler_certs.each do |cert|
            store.add_file cert
          end
          store
        end
      end
    end
  end
end