class TargetIO::TrainCompat::Etc

Public Class Methods

__getgr(&block) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 73
def __getgr(&block)
  content = ::TargetIO::File.read("/etc/group")
  entries = __parse_group(content)
  data    = entries.detect(&block)
  raise ArgumentError unless data

  ::Etc::Group.new(
    data["name"],
    data["password"],
    data["gid"].to_i,
    String(data["mem"]).split(",")
  )
end
__getpw(&block) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 25
def __getpw(&block)
  content = ::TargetIO::File.read("/etc/passwd")
  entries = __parse_passwd(content)
  data    = entries.detect(&block)
  raise ArgumentError unless data

  ::Etc::Passwd.new(
    data["user"],
    data["password"],
    data["uid"].to_i,
    data["gid"].to_i,
    data["desc"],
    data["home"],
    data["shell"]
  )
end
__parse_group(content) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 87
def __parse_group(content)
  content.to_s.split("\n").map do |line|
    next if line[0] == "#"

    __parse_group_line(line)
  end.compact
end
__parse_group_line(line) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 95
def __parse_group_line(line)
  x = line.split(":")
  {
    # rubocop:disable Layout/AlignHash
    "name"     => x.at(0),
    "password" => x.at(1),
    "gid"      => x.at(2),
    "mem"      => x.at(3),
  }
end
__parse_passwd(content) click to toggle source

Parse /etc/passwd files. Courtesy of InSpec

@param [String] content the raw content of /etc/passwd @return [Array] Collection of passwd entries

# File lib/chef/target_io/train/etc.rb, line 47
def __parse_passwd(content)
  content.to_s.split("\n").map do |line|
    next if line[0] == "#"

    __parse_passwd_line(line)
  end.compact
end
__parse_passwd_line(line) click to toggle source

Parse a line of /etc/passwd

@param [String] line a line of /etc/passwd @return [Hash] Map of entries in this line

# File lib/chef/target_io/train/etc.rb, line 59
def __parse_passwd_line(line)
  x = line.split(":")
  {
    # rubocop:disable Layout/AlignHash
    "user"     => x.at(0),
    "password" => x.at(1),
    "uid"      => x.at(2),
    "gid"      => x.at(3),
    "desc"     => x.at(4),
    "home"     => x.at(5),
    "shell"    => x.at(6),
  }
end
__transport_connection() click to toggle source
# File lib/chef/target_io/train/etc.rb, line 106
def __transport_connection
  Chef.run_context&.transport_connection
end
getgrgid(gid) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 21
def getgrgid(gid)
  __getgr { |entry| entry["gid"] == gid.to_i }
end
getgrnam(name) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 17
def getgrnam(name)
  __getgr { |entry| entry["name"] == name }
end
getpwnam(name) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 9
def getpwnam(name)
  __getpw { |entry| entry["user"] == name }
end
getpwuid(uid) click to toggle source
# File lib/chef/target_io/train/etc.rb, line 13
def getpwuid(uid)
  __getpw { |entry| entry["uid"] == uid.to_i }
end