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.



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

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.



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

def content
  @content
end

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#entriesObject



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

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



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

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

#filter(hm = {}) ⇒ Object



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

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



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

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



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

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



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

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



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

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

#passwords(password = nil) ⇒ Object



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

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

#to_sObject



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

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

#users(name = nil) ⇒ Object



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

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

#warn_days(filter_by = nil) ⇒ Object



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

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