Class: Inspec::Resources::EtcGroup

Inherits:
Object
  • Object
show all
Includes:
CommentParser, Converter
Defined in:
lib/resources/etc_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommentParser

#parse_comment_line

Methods included from Converter

#convert_to_i

Constructor Details

#initialize(path = nil) ⇒ EtcGroup

Returns a new instance of EtcGroup.



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

def initialize(path = nil)
  @path = path || '/etc/group'
  @entries = parse_group(@path)

  # skip resource if it is not supported on current OS
  return skip_resource 'The `etc_group` resource is not supported on your OS.' \
  unless inspec.os.unix?
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



40
41
42
# File 'lib/resources/etc_group.rb', line 40

def entries
  @entries
end

#gidObject

Returns the value of attribute gid.



40
41
42
# File 'lib/resources/etc_group.rb', line 40

def gid
  @gid
end

Instance Method Details

#gids(filter = nil) ⇒ Object



54
55
56
# File 'lib/resources/etc_group.rb', line 54

def gids(filter = nil)
  (filter || @entries)&.map { |x| x['gid'] }
end

#groups(filter = nil) ⇒ Object



50
51
52
# File 'lib/resources/etc_group.rb', line 50

def groups(filter = nil)
  (filter || @entries)&.map { |x| x['name'] }
end

#to_sObject



91
92
93
# File 'lib/resources/etc_group.rb', line 91

def to_s
  '/etc/group'
end

#users(filter = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/resources/etc_group.rb', line 58

def users(filter = nil)
  entries = filter || @entries
  return nil if entries.nil?
  # filter the user entry
  res = entries.map { |x|
    x['members'].split(',') if !x.nil? && !x['members'].nil?
  }.flatten
  # filter nil elements
  res.reject { |x| x.nil? || x.empty? }
end

#where(conditions = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resources/etc_group.rb', line 69

def where(conditions = {})
  return if conditions.empty?
  fields = {
    name: 'name',
    group_name: 'name',
    password: 'password',
    gid: 'gid',
    group_id: 'gid',
    users: 'members',
    members: 'members',
  }
  res = entries

  conditions.each do |k, v|
    idx = fields[k.to_sym]
    next if idx.nil?
    res = res.select { |x| x[idx].to_s == v.to_s }
  end

  EtcGroupView.new(self, res)
end