inspec/lib/resources/etc_group.rb

67 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
entries.map{|x| x[0]}
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +00:00
def gids
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
entries.map{|x| x[3].split(',') }.flatten
2015-07-26 10:30:12 +00:00
end
2015-07-15 13:16:10 +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
conditions.each do |k,v|
idx = fields[k.to_sym]
next if idx.nil?
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