inspec/lib/resources/etc_group.rb

105 lines
2 KiB
Ruby
Raw Normal View History

2015-07-15 13:16:10 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
# license: All rights reserved
# The file format consists of
# - group name
# - password
# - gid
# - group list, comma seperated list
#
# Usage:
# describe etc_group do
# its('gids') { should_not contain_duplicates }
# its('groups') { should include 'my_user' }
# its('users') { should include 'my_user' }
# end
#
# describe etc_group.where(name: 'my_group') do
# its('users') { should include 'my_user' }
# end
2015-07-15 13:16:10 +00:00
class EtcGroup < Vulcano.resource(1)
name 'etc_group'
2015-07-15 13:16:10 +00:00
attr_accessor :gid, :entries
def initialize(path = nil)
@path = path || '/etc/group'
@entries = parse(@path)
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def to_s
@path
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def groups(filter = nil)
entries = filter || @entries
entries.map { |x| x[0] } if !entries.nil?
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def gids(filter = nil)
entries = filter || @entries
entries.map { |x| x[2] } if !entries.nil?
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def users(filter = nil)
entries = filter || @entries
return nil if entries.nil?
# filter the user entry
res = entries.map { |x|
x[3].split(',') if !x.nil? && !x[3].nil?
}.flatten
# filter nil elements
res.reject { |x| x.nil? || x.empty? }
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
2015-09-03 18:43:58 +00:00
def where(conditions = {})
return if conditions.empty?
fields = {
name: 0,
group_name: 0,
password: 1,
gid: 2,
group_id: 2,
group_list: 3,
users: 3,
}
res = entries
2015-09-04 07:59:30 +00:00
conditions.each do |k, v|
idx = fields[k.to_sym]
next if idx.nil?
res = res.select { |x| x[idx] == v.to_s }
end
EtcGroupView.new(self, res)
2015-07-15 13:16:10 +00:00
end
private
def parse(path)
@content = vulcano.file(path).content
@content.split("\n").map do |line|
line.split(':')
end
end
end
# object that hold a specifc view on etc group
class EtcGroupView
def initialize(parent, filter)
@parent = parent
@filter = filter
end
def groups
@parent.groups(@filter)
end
def gids
@parent.gids(@filter)
end
def users
@parent.users(@filter)
end
end