Class: VagrantPlugins::RekeySSH::ActionSecureBox

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/vagrant-rekey-ssh/actions/secure_box.rb

Instance Method Summary collapse

Methods included from Helpers

#generate_ssh_key, #rekey_sentinel_file, #ssh_key_path, #ssh_pubkey_path, #ssh_public_key

Constructor Details

#initialize(app, env) ⇒ ActionSecureBox

Returns a new instance of ActionSecureBox.



9
10
11
12
13
14
# File 'lib/vagrant-rekey-ssh/actions/secure_box.rb', line 9

def initialize(app, env)
  @app = app
  @env = env
  @machine = env[:machine]
  @vagrant_insecure_public_key = "AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ=="
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-rekey-ssh/actions/secure_box.rb', line 16

def call(env)
  @app.call(env)
  return unless @machine.communicate.ready?
  
  if @machine.config.rekey_ssh.enable
      
    generate_ssh_key
    
    key = ssh_public_key()
    unless key.length > 0
      raise Errors::ErrorInvalidGeneratedSshKey
    end
    
    # remove the vagrant insecure public key
    cmd = "grep #{@vagrant_insecure_public_key} ~/.ssh/authorized_keys|| exit 1"
    cmd += ";sed -i 's/^.*#{@vagrant_insecure_public_key}.*$/#{key.gsub("/","\\/")}  vagrant less insecure private key/' ~/.ssh/authorized_keys || exit 2"
    cmd += ";exit 3"
    
    result = @machine.communicate.execute(cmd, sudo: false, error_check: false)
    
    if result == 1
      @machine.ui.info(I18n.t("vagrant_rekey_ssh.info.no_key"))
    elsif result == 2
      raise Errors::ErrorReplacingInsecureKey
    elsif result == 3
      # store an indicator that the key has been replaced
      FileUtils.touch(rekey_sentinel_file)
      
      @machine.ui.info(I18n.t("vagrant_rekey_ssh.info.key_replaced"))
      
      # if the insecure key *was* there, disable the user's and root's password too
      # -> this prevents the user from logging in using 'vagrant'
      # -> we only do this if the insecure key was found, because if it
      #    wasn't then perhaps it's a custom box and we shouldn't mess with it
      @machine.communicate.execute("sudo passwd -d $USER; sudo passwd -d root", sudo: false)
      
      
      
    end
    
  end
end