Sha256: 2836bcb345e8ac9c6ab321bc6e0800bc1c4c730ec0b1a6c8992e83242096b507

Contents?: true

Size: 1.87 KB

Versions: 11

Compression:

Stored size: 1.87 KB

Contents

module Inspec::Resources
  class Lines
    attr_reader :output

    def initialize(raw, desc)
      @output = raw
      @desc = desc
    end

    def to_s
      @desc
    end
  end

  class CassandradbSession < Inspec.resource(1)
    name "cassandradb_session"
    supports platform: "unix"
    supports platform: "windows"
    desc "Use the cassandradb_session InSpec resource to test commands against an Cassandra database"
    example <<~EXAMPLE
      cql = cassandradb_session(user: 'my_user', password: 'password', host: 'host', port: 'port')
      describe cql.query("SELECT cluster_name FROM system.local") do
        its('output') { should match /Test Cluster/ }
      end
    EXAMPLE

    attr_reader :user, :password, :host, :port

    def initialize(opts = {})
      @user = opts[:user] || "cassandra"
      @password = opts[:password] || "cassandra"
      @host = opts[:host]
      @port = opts[:port]
    end

    def query(q)
      cassandra_cmd = create_cassandra_cmd(q)
      cmd = inspec.command(cassandra_cmd)
      out = cmd.stdout + "\n" + cmd.stderr
      if cmd.exit_status != 0 || out =~ /Unable to connect to any servers/ || out.downcase =~ /^error:.*/
        raise Inspec::Exceptions::ResourceFailed, "Cassandra query with errors: #{out}"
      else
        Lines.new(cmd.stdout.strip, "Cassandra query: #{q}")
      end
    end

    def to_s
      "Cassandra DB Session"
    end

    private

    def create_cassandra_cmd(q)
      # TODO: simple escape, must be handled by a library
      # that does this securely
      escaped_query = q.gsub(/\\/, "\\\\").gsub(/"/, '\\"').gsub(/\$/, '\\$')

      # construct the query
      command = "cqlsh"
      command += " #{@host}" unless @host.nil?
      command += " #{@port}" unless @port.nil?
      command += " -u #{@user}"
      command += " -p #{@password}"
      command += " --execute '#{escaped_query}'"
      command
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
inspec-core-5.17.4 lib/inspec/resources/cassandradb_session.rb
inspec-core-5.14.0 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.56.20 lib/inspec/resources/cassandradb_session.rb
inspec-core-5.12.2 lib/inspec/resources/cassandradb_session.rb
inspec-core-5.10.5 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.56.19 lib/inspec/resources/cassandradb_session.rb
inspec-core-5.7.9 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.56.17 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.52.9 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.50.3 lib/inspec/resources/cassandradb_session.rb
inspec-core-4.49.0 lib/inspec/resources/cassandradb_session.rb