class Inspec::Resources::EtcGroup

Attributes

entries[RW]
gid[RW]

Public Class Methods

new(path = nil) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 43
def initialize(path = nil)
  @path = path || "/etc/group"
  @entries = parse_group(@path)
end

Public Instance Methods

gids(filter = nil) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 52
def gids(filter = nil)
  (filter || @entries)&.map { |x| x["gid"] }
end
groups(filter = nil) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 48
def groups(filter = nil)
  (filter || @entries)&.map { |x| x["name"] }
end
to_s() click to toggle source
# File lib/inspec/resources/etc_group.rb, line 94
def to_s
  "/etc/group"
end
users(filter = nil) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 56
def users(filter = nil)
  entries = filter || @entries
  return nil if entries.nil?

  # filter the user entry
  res = entries.map do |x|
    x["members"].split(",") if !x.nil? && !x["members"].nil?
  end.flatten
  # filter nil elements
  res.reject { |x| x.nil? || x.empty? }
end
where(conditions = {}) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 68
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

  unless res.nil?
    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
  end

  EtcGroupView.new(self, res)
end

Private Instance Methods

parse_group(path) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 100
def parse_group(path)
  @content = read_file_content(path, allow_empty: true)

  # iterate over each line and filter comments
  @content.split("\n").each_with_object([]) do |line, lines|
    grp_info = parse_group_line(line)
    lines.push(grp_info) if !grp_info.nil? && !grp_info.empty?
  end
end
parse_group_line(line) click to toggle source
# File lib/inspec/resources/etc_group.rb, line 110
def parse_group_line(line)
  opts = {
    comment_char: "#",
    standalone_comments: false,
  }
  line, _idx_nl = parse_comment_line(line, opts)
  x = line.split(":")
  # abort if we have an empty or comment line
  return nil if x.empty?

  # map data
  {
    "name" => x.at(0), # Name of the group.
    "password" => x.at(1), # Group's encrypted password.
    "gid" => convert_to_i(x.at(2)), # The group's decimal ID.
    "members" => x.at(3), # Group members.
  }
end