Class: Inspec::Resources::MongodbSession

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MongodbSession

Returns a new instance of MongodbSession.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/inspec/resources/mongodb_session.rb', line 37

def initialize(opts = {})
  @user = opts[:user] || nil
  @password = opts[:password] || nil
  @host = opts[:host] || "127.0.0.1"
  @port = opts[:port] || "27017"
  @database = opts[:database] || nil
  @auth_mech = opts[:auth_mech] || :scram
  @auth_source = opts[:auth_source] || @database
  @ssl = opts[:ssl] || false
  @ssl_cert = opts[:ssl_cert] || nil
  @ssl_key = opts[:ssl_key] || nil
  @ssl_ca_cert = opts[:ssl_ca_cert] || nil
  @auth_mech_properties = opts[:auth_mech_properties] || {}
  @client = nil

  fail_resource "Can't run MongoDB checks without authentication." unless user && @password
  fail_resource "You must provide a database name for the session." unless database

  create_session
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



35
36
37
# File 'lib/inspec/resources/mongodb_session.rb', line 35

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



35
36
37
# File 'lib/inspec/resources/mongodb_session.rb', line 35

def host
  @host
end

#paramsObject (readonly)

Returns the value of attribute params.



35
36
37
# File 'lib/inspec/resources/mongodb_session.rb', line 35

def params
  @params
end

#portObject (readonly)

Returns the value of attribute port.



35
36
37
# File 'lib/inspec/resources/mongodb_session.rb', line 35

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



35
36
37
# File 'lib/inspec/resources/mongodb_session.rb', line 35

def user
  @user
end

Instance Method Details

#query(command) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/inspec/resources/mongodb_session.rb', line 58

def query(command)
  raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?

  Lines.new(@client.command(command).documents.first, "MongoDB query: #{command}")
rescue => e
  raise Inspec::Exceptions::ResourceFailed, "Can't run MongoDB command Error: #{e.message}"
end

#resource_idObject



66
67
68
# File 'lib/inspec/resources/mongodb_session.rb', line 66

def resource_id
  "mongodb_session:User:#{@user}:Host:#{@host}:Database:#{@database}"
end