Module: Utils

Defined in:
lib/simp/cli/lib/utils.rb

Constant Summary collapse

DEFAULT_PASSWORD_LENGTH =
32

Class Method Summary collapse

Class Method Details

.generate_certificates(hostname) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/simp/cli/lib/utils.rb', line 100

def generate_certificates(hostname)
  Dir.chdir('/etc/puppet/Config/FakeCA') do
    file = File.open('togen', 'w')
    file.puts hostname
    file.close

    passphrase = `cat cacertkey`.chomp
    system('./gencerts_nopass.sh auto')
  end
end

.generate_password(length = DEFAULT_PASSWORD_LENGTH, default_is_autogenerate = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simp/cli/lib/utils.rb', line 46

def generate_password(length = DEFAULT_PASSWORD_LENGTH, default_is_autogenerate = true)
  password = ''
  if Utils.yes_or_no('Do you want to autogenerate the password?', default_is_autogenerate )
    special_chars = ['#','%','&','*','+','-','.',':','@']
    symbols = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
    Integer(length).times { |i| password += (symbols + special_chars)[rand((symbols.length-1 + special_chars.length-1))] }
    # Ensure that the password does not start or end with a special
    # character.
    special_chars.include?(password[0].chr) and password[0] = symbols[rand(symbols.length-1)]
    special_chars.include?(password[password.length-1].chr) and password[password.length-1] = symbols[rand(symbols.length-1)]
    puts "Your password is:\n#{password}"
    print 'Push [ENTER] to continue.'
    $stdout.flush
    $stdin.gets
  else
    password = Utils.get_password
  end
  password
end

.get_passwordObject



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
# File 'lib/simp/cli/lib/utils.rb', line 20

def get_password
  print 'Enter password: '

  system('/bin/stty', '-echo')
  password1 = STDIN.gets.strip
  system('/bin/stty', 'echo')
  puts

  print 'Re-enter password: '
  system('/bin/stty', '-echo')
  password2 = STDIN.gets.strip
  system('/bin/stty', 'echo')
  puts

  if password1 == password2
    if validate_password(password1)
      password1
    else
      get_password
    end
  else
    puts "  Passwords do not match! Please try again."
    get_password
  end
end

.get_value(default_value = '') ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simp/cli/lib/utils.rb', line 83

def get_value(default_value = '')
  case default_value
  when /\d+\.\d+\.\d+\.\d+/
    print "Enter a new IP: "
    value = STDIN.gets.strip
    while !valid_ip?(value)
      puts "INVALID! Try again..."
      print "Enter a new IP: "
      value = STDIN.gets.strip
    end
  else
    print "Enter a value: "
    value = STDIN.gets.strip
  end
  value
end

.valid_ip?(value) ⇒ Boolean



111
112
113
# File 'lib/simp/cli/lib/utils.rb', line 111

def valid_ip?(value)
  value.to_s =~ /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
end

.validate_password(password) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simp/cli/lib/utils.rb', line 66

def validate_password(password)
  require 'shellwords'

  if password.length < 8
    puts "  Invalid Password: Password must be at least 8 characters long"
    false
  else
    pass_result = `echo #{Shellwords.escape(password)} | cracklib-check`.split(':').last.strip
    if pass_result == "OK"
      true
    else
      puts "  Invalid Password: #{pass_result}"
      false
    end
  end
end

.yes_or_no(prompt, default_yes) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/simp/cli/lib/utils.rb', line 6

def yes_or_no(prompt, default_yes)
  print prompt + (default_yes ? ' [Y|n]: ' : ' [y|N]: ')
  case STDIN.gets
  when /^(y|Y)/
    true
  when /^(n|N)/
    false
  when /^\s*$/
    default_yes
  else
    yes_or_no(prompt, default_yes)
  end
end