class Serverspec::Type::SudoUser
Public Class Methods
new(name)
click to toggle source
Calls superclass method
# File lib/serverspec_extra_types/types/sudo_user.rb, line 10 def initialize(name) super @user = name end
Public Instance Methods
allowed_to_run_command?(command, user = nil, checkpw = false)
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 23 def allowed_to_run_command?(command, user = nil, checkpw = false) perm = permission(command) if user if checkpw perm[:user] == user && perm[:nopasswd] else perm[:user] == user end else checkpw ? perm && perm[:nopasswd] : perm end end
exist?()
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 15 def exist? if get_inspection.success? && /User #{@user} may run the following commands/.match(@get_inspection.stdout) true else false end end
has_sudo_disabled?()
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 44 def has_sudo_disabled? /User #{@user} is not allowed to run sudo/.match(@get_inspection.stdout) end
inspection()
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 48 def inspection @inspection ||= get_sudo_perms(get_inspection.stdout) end
permission(command)
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 36 def permission(command) permissions.find { |x| x[:command].include?(command) } end
permissions()
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 40 def permissions inspection[:permissions] end
Private Instance Methods
chunk(chunks, parts, perm, user)
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 68 def chunk(chunks, parts, perm, user) if user.include?(':') chunks[:user] = user.split(':')[0] chunks[:group] = user.split(':')[1] else chunks[:user] = user end if /NOPASSWD:/.match? perm chunks[:nopasswd] = true commands = parts[2..-1].join(' ').split(',').map(&:strip) chunks[:command] = commands.length > 1 ? commands : commands[0] else chunks[:nopasswd] = false commands = parts[1..-1].join(' ').split(',').map(&:strip) chunks[:command] = chunks[:command] = commands.length > 1 ? commands : commands[0] end end
chunk_permission(perm)
click to toggle source
rubocop:enable Naming/AccessorMethodName
# File lib/serverspec_extra_types/types/sudo_user.rb, line 60 def chunk_permission(perm) chunks = {} parts = perm.sub(' : ', ':').split(/\s+/).reject { |x| x == '' || x == "\n" } user = parts[0].sub('(', '').sub(')', '') chunk(chunks, parts, perm, user) chunks end
get_inspection()
click to toggle source
rubocop:disable Naming/AccessorMethodName
# File lib/serverspec_extra_types/types/sudo_user.rb, line 55 def get_inspection @get_inspection ||= @runner.run_command("sudo -l -U #{@user}") end
get_sudo_perms(output)
click to toggle source
# File lib/serverspec_extra_types/types/sudo_user.rb, line 86 def get_sudo_perms(output) matches = /Matching Defaults entries for #{@user} on .*\n(.*)\n/.match output defaults = matches ? matches[1].split(', ').map(&:strip) : {} matches = /User #{@user} may run the following commands on .*\n((\W.*\n)*)/.match output permissions = matches ? matches[1].split("\n").map { |x| chunk_permission(x.strip) } : {} { defaults: defaults, permissions: permissions } end