inspec/lib/resources/etc_group.rb

66 lines
1.1 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
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
2015-07-26 10:30:12 +00:00
def groups
2015-09-04 07:59:30 +00:00
entries.map { |x| x[0] }
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def gids
2015-09-04 07:59:30 +00:00
entries.map { |x| x[2] }
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
2015-07-26 10:30:12 +00:00
def users
2015-09-04 07:59:30 +00:00
entries.map { |x| x[3].split(',') }.flatten
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,
2015-09-04 07:59:30 +00:00
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?
2015-09-04 07:59:30 +00:00
res = res.map { |x| x[idx] == v.to_s }
end
@entries = res
self
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