raiseArgumentError,"Invalid security_identifier param '#{opts}'. Please pass a hash with these supported keys: #{supported_opt_keys}"unlessopts.respond_to?(:keys)
raiseArgumentError,'Specifying more than one of :user :group or :unspecified for security_identifier is not supported'unlessopts.keys&&(opts.keys&supported_opt_keys).length==1
ifopts[:user]
@type=:user
@name=opts[:user]
end
ifopts[:group]
@type=:group
@name=opts[:group]
end
ifopts[:unspecified]
@type=:unspecified
@name=opts[:unspecified]
end
raiseArgumentError,'Specify one of :user :group or :unspecified for security_identifier'unless@name
@sids=nil
end
defsid
fetch_sidsunless@sids
@sids[@name]# nil if not found
end
defexist?
fetch_sidsunless@sids
@sids.key?(@name)
end
private
deffetch_sids
@sids={}
case@type
when:group
sid_data=wmi_results(:group)
when:user
sid_data=wmi_results(:user)
when:unspecified
# try group first, then user
sid_data=wmi_results(:group)
ifsid_data.empty?
sid_data=wmi_results(:user)
end
else
raise"Unhandled entity type '#{@type}'"
end
sid_data.each{|sid|@sids[sid[1]]=sid[2]}
end
defwmi_results(type)
query='wmic '
casetype
when:group
query+='group'
when:user
query+='useraccount'
end
query+=" where 'Name=\"#{@name}\"' get Name\",\"SID /format:csv"
# Example output:
# inspec> command("wmic useraccount where 'Name=\"Administrator\"' get Name\",\"SID /format:csv").stdout