2016-09-22 21:45:28 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'utils/filter'
|
|
|
|
|
|
|
|
module Inspec::Resources
|
|
|
|
# This file contains two resources, the `group` and `groups` resource.
|
|
|
|
# The `group` resource is optimized for requests that verify specific groups
|
|
|
|
# that you know upfront for testing. If you need to query all groups or search
|
|
|
|
# specific groups with certain properties, use the `groups` resource.
|
|
|
|
module GroupManagementSelector
|
|
|
|
# select group provider based on the operating system
|
|
|
|
# returns nil, if no group manager was found for the operating system
|
|
|
|
def select_group_manager(os)
|
2017-12-20 15:57:45 +00:00
|
|
|
@group_provider = if os.darwin?
|
|
|
|
DarwinGroup.new(inspec)
|
|
|
|
elsif os.unix?
|
|
|
|
UnixGroup.new(inspec)
|
|
|
|
elsif os.windows?
|
|
|
|
WindowsGroup.new(inspec)
|
|
|
|
end
|
2016-09-22 21:45:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Groups < Inspec.resource(1)
|
|
|
|
include GroupManagementSelector
|
|
|
|
|
|
|
|
name 'groups'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
|
|
|
supports platform: 'windows'
|
2016-09-22 21:45:28 +00:00
|
|
|
desc 'Use the group InSpec audit resource to test groups on the system. Groups can be filtered.'
|
|
|
|
example "
|
|
|
|
describe groups.where { name == 'root'} do
|
|
|
|
its('names') { should eq ['root'] }
|
|
|
|
its('gids') { should eq [0] }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe groups.where { name == 'Administrators'} do
|
|
|
|
its('names') { should eq ['Administrators'] }
|
|
|
|
its('gids') { should eq ['S-1-5-32-544'] }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
# select group manager
|
|
|
|
@group_provider = select_group_manager(inspec.os)
|
|
|
|
return skip_resource 'The `groups` resource is not supported on your OS yet.' if @group_provider.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
filter = FilterTable.create
|
2018-06-26 19:14:21 +00:00
|
|
|
filter.register_custom_matcher(:exists?) { |x| !x.entries.empty? }
|
|
|
|
filter.register_column(:names, field: 'name')
|
|
|
|
.register_column(:gids, field: 'gid')
|
|
|
|
.register_column(:domains, field: 'domain')
|
2018-10-18 20:48:30 +00:00
|
|
|
.register_column(:members, field: 'members', style: :simple)
|
2018-06-26 19:14:21 +00:00
|
|
|
filter.install_filter_methods_on_resource(self, :collect_group_details)
|
2016-09-22 21:45:28 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
'Groups'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# collects information about every group
|
|
|
|
def collect_group_details
|
|
|
|
return @groups_cache ||= @group_provider.groups unless @group_provider.nil?
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# describe group('root') do
|
|
|
|
# it { should exist }
|
|
|
|
# its('gid') { should eq 0 }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class Group < Inspec.resource(1)
|
|
|
|
include GroupManagementSelector
|
|
|
|
|
|
|
|
name 'group'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
|
|
|
supports platform: 'windows'
|
2016-09-22 21:45:28 +00:00
|
|
|
desc 'Use the group InSpec audit resource to test groups on the system.'
|
|
|
|
example "
|
|
|
|
describe group('root') do
|
|
|
|
it { should exist }
|
|
|
|
its('gid') { should eq 0 }
|
|
|
|
end
|
2018-05-31 17:37:44 +00:00
|
|
|
|
|
|
|
describe group('Administrators') do
|
|
|
|
its('members') { should include 'Administrator' }
|
|
|
|
end
|
2016-09-22 21:45:28 +00:00
|
|
|
"
|
|
|
|
|
|
|
|
def initialize(groupname)
|
|
|
|
@group = groupname
|
|
|
|
|
|
|
|
# select group manager
|
|
|
|
@group_provider = select_group_manager(inspec.os)
|
|
|
|
return skip_resource 'The `group` resource is not supported on your OS yet.' if @group_provider.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
# verifies if a group exists
|
|
|
|
def exists?
|
2017-02-08 22:49:16 +00:00
|
|
|
!group_info.entries.empty?
|
2016-09-22 21:45:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def gid
|
2018-05-31 17:37:44 +00:00
|
|
|
flatten_entry(group_info, 'gid')
|
2016-09-22 21:45:28 +00:00
|
|
|
end
|
|
|
|
|
2018-05-31 17:37:44 +00:00
|
|
|
def members
|
|
|
|
flatten_entry(group_info, 'members')
|
|
|
|
end
|
|
|
|
|
2016-09-22 21:45:28 +00:00
|
|
|
def local
|
|
|
|
# at this point the implementation only returns local groups
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"Group #{@group}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-05-31 17:37:44 +00:00
|
|
|
def flatten_entry(group_info, prop)
|
|
|
|
entries = group_info.entries
|
|
|
|
if entries.empty?
|
|
|
|
nil
|
|
|
|
elsif entries.size == 1
|
|
|
|
entries.first.send(prop)
|
|
|
|
else
|
|
|
|
raise 'found more than one group with the same name, please use `groups` resource'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 21:45:28 +00:00
|
|
|
def group_info
|
|
|
|
# we need a local copy for the block
|
|
|
|
group = @group.dup
|
|
|
|
@groups_cache ||= inspec.groups.where { name == group }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GroupInfo
|
|
|
|
attr_reader :inspec
|
|
|
|
def initialize(inspec)
|
|
|
|
@inspec = inspec
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups
|
2017-02-08 22:49:16 +00:00
|
|
|
raise 'group provider must implement the `groups` method'
|
2016-09-22 21:45:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# implements generic unix groups via /etc/group
|
|
|
|
class UnixGroup < GroupInfo
|
|
|
|
def groups
|
|
|
|
inspec.etc_group.entries
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-20 15:57:45 +00:00
|
|
|
# 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`
|
|
|
|
class DarwinGroup < GroupInfo
|
|
|
|
def groups
|
|
|
|
group_info = inspec.command('dscacheutil -q group').stdout.split("\n\n")
|
|
|
|
|
|
|
|
groups = []
|
|
|
|
regex = /^([^:]*?)\s*:\s(.*?)\s*$/
|
|
|
|
group_info.each do |data|
|
|
|
|
groups << 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|
|
|
|
|
next if g['users'].nil?
|
|
|
|
g['members'] = g.delete('users')
|
|
|
|
g['members'].tr!(' ', ',')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 21:45:28 +00:00
|
|
|
class WindowsGroup < GroupInfo
|
|
|
|
# returns all local groups
|
|
|
|
def groups
|
2018-05-31 17:37:44 +00:00
|
|
|
script = <<-EOH
|
|
|
|
Function ConvertTo-SID { Param([byte[]]$BinarySID)
|
|
|
|
(New-Object System.Security.Principal.SecurityIdentifier($BinarySID,0)).Value
|
2017-11-21 07:49:41 +00:00
|
|
|
}
|
2018-05-31 17:37:44 +00:00
|
|
|
$Computername = $Env:Computername
|
|
|
|
$adsi = [ADSI]"WinNT://$Computername"
|
|
|
|
$groups = $adsi.Children | where {$_.SchemaClassName -eq 'group'} | ForEach {
|
2017-11-21 07:49:41 +00:00
|
|
|
$name = $_.Name[0]
|
|
|
|
$sid = ConvertTo-SID -BinarySID $_.ObjectSID[0]
|
|
|
|
$group =[ADSI]$_.Path
|
2018-05-31 17:37:44 +00:00
|
|
|
$members = $_.Members() | Foreach-Object { $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null) }
|
|
|
|
# An empty collection of these objects isn't properly converted to an empty array by ConvertTo-Json
|
|
|
|
if(-not [bool]$members) {
|
|
|
|
$members = @()
|
|
|
|
}
|
|
|
|
new-object psobject -property @{name = $group.Name[0]; gid = $sid; domain = $Computername; members = $members}
|
2017-11-21 07:49:41 +00:00
|
|
|
}
|
|
|
|
$groups | ConvertTo-Json -Depth 3
|
2016-09-22 21:45:28 +00:00
|
|
|
EOH
|
|
|
|
cmd = inspec.powershell(script)
|
|
|
|
# cannot rely on exit code for now, successful command returns exit code 1
|
|
|
|
# return nil if cmd.exit_status != 0, try to parse json
|
|
|
|
begin
|
|
|
|
groups = JSON.parse(cmd.stdout)
|
|
|
|
rescue JSON::ParserError => _e
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
|
|
|
# ensure we have an array of groups
|
2018-10-18 20:48:30 +00:00
|
|
|
groups = [groups] unless groups.is_a?(Array)
|
2016-09-22 21:45:28 +00:00
|
|
|
groups
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|