class Inspec::Resources::Crontab

Constants

DAILY
DEFAULT_TIMES

rubocop:disable Layout/AlignHash

HOURLY
MONTHLY
REBOOT
SYSTEM_COLUMNS
USER_COLUMNS
WEEKLY
YEARLY

Attributes

params[R]

Public Class Methods

new(opts = nil) click to toggle source
# File lib/inspec/resources/crontab.rb, line 37
def initialize(opts = nil)
  if opts.respond_to?(:fetch)
    Hash[opts.map { |k, v| [k.to_sym, v] }]
    @user = opts.fetch(:user, nil)
    @path = opts.fetch(:path, nil)
    raise Inspec::Exceptions::ResourceFailed, "A user or path must be supplied." if @user.nil? && @path.nil?
    raise Inspec::Exceptions::ResourceFailed, "Either user or path must be supplied, not both!" if !@user.nil? && !@path.nil?
  else
    @user = opts
    @path = nil
  end
  @params = read_crontab
end

Public Instance Methods

crontab_cmd() click to toggle source
# File lib/inspec/resources/crontab.rb, line 69
def crontab_cmd
  if @user.nil?
    "crontab -l"
  elsif inspec.os.aix?
    "crontab -l #{@user}"
  else
    # TODO: the -u scenario needs to be able to do sudo
    "crontab -l -u #{@user}"
  end
end
parse_crontab_line(l) click to toggle source
# File lib/inspec/resources/crontab.rb, line 62
def parse_crontab_line(l)
  data, = parse_comment_line(l, comment_char: "#", standalone_comments: false)
  return nil if data.nil? || data.empty?

  is_system_crontab? ? parse_system_crontab(data) : parse_user_crontab(data)
end
read_crontab() click to toggle source
# File lib/inspec/resources/crontab.rb, line 51
def read_crontab
  if is_system_crontab?
    raise Inspec::Exceptions::ResourceFailed, "Supplied crontab path '#{@path}' must exist!" unless inspec.file(@path).exist?

    ct = inspec.file(@path).content
  else
    ct = inspec.command(crontab_cmd).stdout
  end
  ct.lines.map { |l| parse_crontab_line(l) }.compact
end
to_s() click to toggle source
# File lib/inspec/resources/crontab.rb, line 98
def to_s
  if is_system_crontab?
    "crontab for path #{@path}"
  elsif is_user_crontab?
    "crontab for user #{@user}"
  else
    "crontab for current user"
  end
end

Private Instance Methods

is_system_crontab?() click to toggle source
# File lib/inspec/resources/crontab.rb, line 110
def is_system_crontab?
  !@path.nil?
end
is_user_crontab?() click to toggle source
# File lib/inspec/resources/crontab.rb, line 114
def is_user_crontab?
  !@user.nil?
end
merge_crontab(data, default) click to toggle source
# File lib/inspec/resources/crontab.rb, line 144
def merge_crontab(data, default)
  case data
  when /@hourly /
    default.merge(HOURLY)
  when /@(midnight|daily) /
    default.merge(DAILY)
  when /@weekly /
    default.merge(WEEKLY)
  when /@monthly /
    default.merge(MONTHLY)
  when /@(annually|yearly) /
    default.merge(YEARLY)
  when /@reboot /
    default.merge(REBOOT)
  end
end
parse_system_crontab(data) click to toggle source
# File lib/inspec/resources/crontab.rb, line 161
def parse_system_crontab(data)
  _, user, cmd = data.split(/\s+/, 3)
  default = DEFAULT_TIMES.merge("user"    => user,
                                "command" => cmd)

  merge_crontab(data, default) ||
    SYSTEM_COLUMNS.zip(data.split(/\s+/, 7)).to_h
end
parse_user_crontab(data) click to toggle source
# File lib/inspec/resources/crontab.rb, line 170
def parse_user_crontab(data)
  _, cmd = data.split(/\s+/, 2)
  default = DEFAULT_TIMES.merge("user"    => @user,
                                "command" => cmd)

  merge_crontab(data, default) ||
    USER_COLUMNS.zip(data.split(/\s+/, 6)).to_h.merge("user" => @user)
end