Class: Inspec::Resources::Shadow

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/resources/shadow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = '/etc/shadow', opts = nil) ⇒ Shadow

Returns a new instance of Shadow.



40
41
42
43
44
45
46
47
# File 'lib/resources/shadow.rb', line 40

def initialize(path = '/etc/shadow', opts = nil)
  opts ||= {}
  @path = path || '/etc/shadow'
  @content = opts[:content] || inspec.file(@path).content
  @lines = @content.to_s.split("\n")
  @filters = opts[:filters] || ''
  @params = @lines.map { |l| parse_shadow_line(l) }
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



37
38
39
# File 'lib/resources/shadow.rb', line 37

def content
  @content
end

#linesObject (readonly)

Returns the value of attribute lines.



38
39
40
# File 'lib/resources/shadow.rb', line 38

def lines
  @lines
end

#paramsObject (readonly)

Returns the value of attribute params.



36
37
38
# File 'lib/resources/shadow.rb', line 36

def params
  @params
end

Instance Method Details

#entriesObject



69
70
71
72
73
74
75
# File 'lib/resources/shadow.rb', line 69

def entries
  @lines.map do |line|
    params = parse_shadow_line(line)
    Shadow.new(@path, content: line,
               filters: "#{@filters} on entry user=#{params['user']}")
  end
end

#expiry_dates(filter_by = nil) ⇒ Object



105
106
107
# File 'lib/resources/shadow.rb', line 105

def expiry_dates(filter_by = nil)
  filter_by.nil? ? map_data('expiry_date') : filter(expiry_date: filter_by)
end

#filter(hm = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/resources/shadow.rb', line 49

def filter(hm = {})
  return self if hm.nil? || hm.empty?
  res = @params
  filters = ''
  hm.each do |attr, condition|
    condition = condition.to_s if condition.is_a? Integer
    filters += " #{attr} = #{condition.inspect}"
    res = res.find_all do |line|
      case line[attr.to_s]
      when condition
        true
      else
        false
      end
    end
  end
  content = res.map { |x| x.values.join(':') }.join("\n")
  Shadow.new(@path, content: content, filters: @filters + filters)
end

#inactive_days(filter_by = nil) ⇒ Object



101
102
103
# File 'lib/resources/shadow.rb', line 101

def inactive_days(filter_by = nil)
  filter_by.nil? ? map_data('inactive_days') : filter(inactive_days: filter_by)
end

#last_changes(filter_by = nil) ⇒ Object



85
86
87
# File 'lib/resources/shadow.rb', line 85

def last_changes(filter_by = nil)
  filter_by.nil? ? map_data('last_change') : filter(last_change: filter_by)
end

#max_days(filter_by = nil) ⇒ Object



93
94
95
# File 'lib/resources/shadow.rb', line 93

def max_days(filter_by = nil)
  filter_by.nil? ? map_data('max_days') : filter(max_days: filter_by)
end

#min_days(filter_by = nil) ⇒ Object



89
90
91
# File 'lib/resources/shadow.rb', line 89

def min_days(filter_by = nil)
  filter_by.nil? ? map_data('min_days') : filter(min_days: filter_by)
end

#passwords(password = nil) ⇒ Object



81
82
83
# File 'lib/resources/shadow.rb', line 81

def passwords(password = nil)
  password.nil? ? map_data('password') : filter(password: password)
end

#to_sObject



109
110
111
112
# File 'lib/resources/shadow.rb', line 109

def to_s
  f = @filters.empty? ? '' : ' with'+@filters
  "/etc/shadow#{f}"
end

#users(name = nil) ⇒ Object



77
78
79
# File 'lib/resources/shadow.rb', line 77

def users(name = nil)
  name.nil? ? map_data('user') : filter(user: name)
end

#warn_days(filter_by = nil) ⇒ Object



97
98
99
# File 'lib/resources/shadow.rb', line 97

def warn_days(filter_by = nil)
  filter_by.nil? ? map_data('warn_days') : filter(warn_days: filter_by)
end