Class: VagrantPlugins::CaCertificates::Action::InstallCertificates

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-ca-certificates/action/install_certificates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ InstallCertificates

Returns a new instance of InstallCertificates.



11
12
13
14
15
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 11

def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @logger = Log4r::Logger.new('vagrant::ca-certificates')
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 9

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 17

def call(env)
  @app.call(env)
  return unless @machine.config.ca_certificates.enabled?

  create_certificates_directory
  @machine.ui.info(I18n.t('vagrant_ca_certificates.certificate.upload.message'))
  @machine.config.ca_certificates.certs.each do |file|
    to = File.join(certs_path, File.basename(file))
    upload_certificate(file, to)
  end
  @machine.guest.capability(:update_certificate_bundle)
  modify_etc_environment
end

#certificate_matches?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 74

def certificate_matches?(from, to)
  md5sum = Digest::MD5.file(from)
  @logger.debug("Verifying #{from} md5sum in guest...")
  @machine.communicate.tap do |sh|
    return false unless sh.test("test -f #{from}")
    if sh.test(%{test '#{md5sum}' = '$(md5sum "#{to}")'}, shell: '/bin/bash')
      @logger.debug('Certificate md5sum in guest matches!')
      return true
    end
  end
  false
end

#certs_pathObject



31
32
33
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 31

def certs_path
  @machine.guest.capability(:certificate_upload_path)
end

#create_certificates_directoryObject



47
48
49
50
51
52
53
54
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 47

def create_certificates_directory
  @logger.debug('Checking if private certificate directory is created...')
  @machine.communicate.tap do |sh|
    return if sh.test("test -d #{certs_path}")
    @logger.info("Creating #{certs_path} for private certificates.")
    sh.sudo("mkdir -p #{certs_path} && chmod 0744 #{certs_path}")
  end
end

#modify_etc_environmentObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 35

def modify_etc_environment
  bundle_path = @machine.guest.capability(:certificate_file_bundle)
  @logger.debug("Private certificate path: <#{bundle_path}>")
  @machine.communicate.tap do |sh|
    if sh.test("grep -q 'SSL_CERT_FILE' /etc/environment", shell: '/bin/bash')
      sh.sudo(%{sed "s#^SSL_CERT_FILE=.*#SSL_CERT_FILE=#{bundle_path}#" -i /etc/environment})
    else
      sh.sudo(%{echo "SSL_CERT_FILE=#{bundle_path}" >> /etc/environment})
    end
  end
end

#upload_certificate(from, to) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant-ca-certificates/action/install_certificates.rb', line 56

def upload_certificate(from, to)
  @logger.debug("Uploading certificates #{from} -> #{to}")
  if from =~ /^http[s]?/
    remote = Tempfile.new('vagrant-ca-certificates')
    Vagrant::Util::Downloader.new(from, remote.path).download!
    from = remote.path
  end

  @machine.communicate.tap do |sh|
    unless certificate_matches?(from, to)
      tmp_to = Pathname.new(Tempfile.new('vagrant').path).basename
      @machine.ui.info(I18n.t('vagrant_ca_certificates.certificate.upload.file', from: from, to: to))
      sh.upload(from, tmp_to) # remote.path will build a "C:\" URI on windows, cp to ~ and move
      sh.sudo("mv #{tmp_to} #{to} && chown root: #{to} && chmod 0644 #{to}")
    end
  end
end