Class: Simp::Cli::Config::Item::GrubPassword

Inherits:
PasswordItem show all
Includes:
SafeApplying
Defined in:
lib/simp/cli/config/item/grub_password.rb

Instance Attribute Summary

Attributes inherited from PasswordItem

#can_generate, #generate_by_default

Attributes inherited from Simp::Cli::Config::Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods included from SafeApplying

#safe_apply

Methods inherited from PasswordItem

#query_ask, #query_extras, #query_generate_password

Methods inherited from Simp::Cli::Config::Item

#default_value, #highline_question_type, #next_items, #not_valid_message, #os_value, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_extras, #query_status, #recommended_value, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s

Constructor Details

#initializeGrubPassword

Returns a new instance of GrubPassword.



15
16
17
18
19
# File 'lib/simp/cli/config/item/grub_password.rb', line 15

def initialize
  super
  @key         = 'grub::password'
  @description = %Q{The password to access GRUB}
end

Instance Method Details

#applyObject



41
42
43
44
45
46
47
48
49
# File 'lib/simp/cli/config/item/grub_password.rb', line 41

def apply
  if Facter.value('lsbmajdistrelease') > "6" then
    # TODO: beg team hercules to make a augeas provider for grub2 passwords?
    `sed -i 's/password_pbkdf2 root.*$/password_pbkdf2 root #{@value}/' /etc/grub.d/01_users`
    `grub2-mkconfig -o /etc/grub2.cfg`
  else
    `sed -i '/password/ c\password --encrypted #{@value}' /boot/grub/grub.conf`
  end
end

#encrypt(string) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simp/cli/config/item/grub_password.rb', line 27

def encrypt string
  result   = nil
  password = string
  if Facter.value('lsbmajdistrelease') > '6'
    result = `grub2-mkpasswd-pbkdf2 <<EOM\n#{password}\n#{password}\nEOM`.split.last
  else
    require 'digest/sha2'
    salt   = rand(36**8).to_s(36)
    result = password.crypt("$6$" + salt)
  end
  result
end

#validate(string) ⇒ Object



22
23
24
# File 'lib/simp/cli/config/item/grub_password.rb', line 22

def validate string
  !string.to_s.strip.empty? && super
end