Class: TargetIO::TrainCompat::Etc

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/target_io/train/etc.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Class Method Details

.__getgr(&block) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



87
88
89
90
91
92
93
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Array

Parse /etc/passwd files. Courtesy of InSpec

Parameters:

  • content (String)

    the raw content of /etc/passwd

Returns:

  • (Array)

    Collection of passwd entries



47
48
49
50
51
52
53
# 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) ⇒ Hash

Parse a line of /etc/passwd

Parameters:

  • line (String)

    a line of /etc/passwd

Returns:

  • (Hash)

    Map of entries in this line



59
60
61
62
63
64
65
66
67
68
69
70
71
# 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_connectionObject



106
107
108
# File 'lib/chef/target_io/train/etc.rb', line 106

def __transport_connection
  Chef.run_context&.transport_connection
end

.getgrgid(gid) ⇒ Object



21
22
23
# File 'lib/chef/target_io/train/etc.rb', line 21

def getgrgid(gid)
  __getgr { |entry| entry["gid"] == gid.to_i }
end

.getgrnam(name) ⇒ Object



17
18
19
# File 'lib/chef/target_io/train/etc.rb', line 17

def getgrnam(name)
  __getgr { |entry| entry["name"] == name }
end

.getpwnam(name) ⇒ Object



9
10
11
# File 'lib/chef/target_io/train/etc.rb', line 9

def getpwnam(name)
  __getpw { |entry| entry["user"] == name }
end

.getpwuid(uid) ⇒ Object



13
14
15
# File 'lib/chef/target_io/train/etc.rb', line 13

def getpwuid(uid)
  __getpw { |entry| entry["uid"] == uid.to_i }
end