Class: VagrantPlugins::Certificates::Action::InstallCertificates

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-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-certificates/action/install_certificates.rb', line 11

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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/vagrant-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-certificates/action/install_certificates.rb', line 17

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

  create_certificates_directory
  @machine.ui.info(I18n.t('vagrant_certificates.certificate.upload.message'))
  @machine.config.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)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-certificates/action/install_certificates.rb', line 91

def certificate_matches?(from, to)
  md5sum = Digest::MD5.file(from)
  @logger.debug("Verifying #{from} md5sum in guest...")
  @machine.communicate.tap do |sh|
    case @machine.guest.name
    when :windows
      if sh.test("if(-not((Get-Filehash -path '#{to}' -Algorithm MD5) | Select-Object -ExpandProperty Hash) -eq '#{md5sum}'){Exit 1}")
        @logger.debug('Certificate md5sum in guest matches!')
        return true
      end
    else
      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
  end
  false
end

#certs_pathObject



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

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

#create_certificates_directoryObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant-certificates/action/install_certificates.rb', line 52

def create_certificates_directory
  @logger.debug('Checking if private certificate directory is created...')
  @machine.communicate.tap do |sh|
    case @machine.guest.name
    when :windows
      return if sh.test("$ProgressPreference=\"SilentlyContinue\";if(-not(Test-Path -Path #{certs_path})){Exit 1}")
      @logger.info("Creating Windows #{certs_path} for private certificates.")
      sh.sudo("New-Item -Path #{certs_path} -ItemType Directory")
    else
      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
end

#modify_etc_environmentObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-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|
    case @machine.guest.name
    when :windows
      sh.sudo("[Environment]::SetEnvironmentVariable('SSL_CERT_FILE','#{bundle_path}','Machine')")
    else
      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
end

#upload_certificate(from, to) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vagrant-certificates/action/install_certificates.rb', line 68

def upload_certificate(from, to)
  @logger.debug("Uploading certificates #{from} -> #{to}")
  if from =~ /^http[s]?/
    remote = Tempfile.new('vagrant-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_certificates.certificate.upload.file', from: from, to: to))
      sh.upload(from.to_s, tmp_to.to_s) # remote.path will build a "C:\" URI on windows, cp to ~ and move.
      case @machine.guest.name
      when :windows
        sh.sudo("Move-Item -path #{tmp_to}/* -Destination #{to} -Force")
      else
        sh.sudo("mv #{tmp_to} #{to} && chown root: #{to} && chmod 0644 #{to}")
      end
    end
  end
end