Class: Inspec::Resources::DarwinGroup

Inherits:
GroupInfo
  • Object
show all
Defined in:
lib/inspec/resources/groups.rb

Overview

OSX uses opendirectory for groups, so ‘/etc/group` may not be fully accurate This uses `dscacheutil` to get the group info instead of `etc_group`

Instance Attribute Summary

Attributes inherited from GroupInfo

#inspec

Instance Method Summary collapse

Methods inherited from GroupInfo

#initialize

Constructor Details

This class inherits a constructor from Inspec::Resources::GroupInfo

Instance Method Details

#groupsObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/inspec/resources/groups.rb', line 175

def groups
  group_by_id = runmap("dscl . -list /Groups PrimaryGroupID")  { |l| name, id = l.split; [id.to_i, name] }
  userss      = runmap("dscl . -list /Users  PrimaryGroupID")  { |l| name, id = l.split; [name, id.to_i] }
  membership  = runmap("dscl . -list /Groups GroupMembership") { |l| key, *vs = l.split; [key, vs] }
  membership.default_proc = ->(h, k) { h[k] = [] }

  users_by_group = hashmap(userss.keys.group_by { |k| userss[k] }) { |k, vs| [group_by_id[k], vs] }
  users_by_group.each do |name, users|
    membership[name].concat users
  end

  group_info = inspec.command("dscacheutil -q group").stdout.split("\n\n").uniq

  regex = /^([^:]*?)\s*:\s(.*?)\s*$/
  groups = group_info.map do |data|
    inspec.parse_config(data, assignment_regex: regex).params
  end

  # Convert the `dscacheutil` groups to match `inspec.etc_group.entries`
  groups.each { |g| g["gid"] = g["gid"].to_i }
  groups.each do |g|
    users = g.delete("users") || ""
    users = users.split
    users += Array(users_by_group[g["name"]])
    g["members"] = users
    g["members"].sort.join ","
  end
end

#hashmap(enum, &blk) ⇒ Object



171
172
173
# File 'lib/inspec/resources/groups.rb', line 171

def hashmap(enum, &blk)
  enum.map(&blk).to_h
end

#runmap(cmd, &blk) ⇒ Object



167
168
169
# File 'lib/inspec/resources/groups.rb', line 167

def runmap(cmd, &blk)
  hashmap(inspec.command(cmd).stdout.lines, &blk)
end