mirror of
https://github.com/inspec/inspec
synced 2024-11-26 14:40:26 +00:00
Merge pull request #6105 from inspec/vasundhara/group7_resource_ids
CFINSPEC-268 Adds resource_id group 7
This commit is contained in:
commit
ff8744d339
21 changed files with 103 additions and 2 deletions
|
@ -24,6 +24,14 @@ module Inspec::Resources
|
|||
@output = run_command
|
||||
end
|
||||
|
||||
def resource_id
|
||||
if inspec.os.platform?("windows")
|
||||
"ibmdb2_conf"
|
||||
else
|
||||
"ibmdb2_conf:DatabaseInstance:#{@db_instance}"
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
"IBM Db2 Conf"
|
||||
end
|
||||
|
|
|
@ -63,6 +63,14 @@ module Inspec::Resources
|
|||
end
|
||||
end
|
||||
|
||||
def resource_id
|
||||
if inspec.os.platform?("windows")
|
||||
"ibmdb2_session:DatabaseName#{@db_name}"
|
||||
else
|
||||
"ibmdb2_session:DatabaseInstance:#{@db_instance}:DatabaseName#{@db_name}"
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
"IBM Db2 Session"
|
||||
end
|
||||
|
|
|
@ -24,6 +24,11 @@ module Inspec::Resources
|
|||
super(@conf_path)
|
||||
end
|
||||
|
||||
# set resource_id to "" if system is not able to determine the @conf_path
|
||||
def resource_id
|
||||
@conf_path || "mongodb_conf"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse(content)
|
||||
|
|
|
@ -63,6 +63,10 @@ module Inspec::Resources
|
|||
raise Inspec::Exceptions::ResourceFailed, "Can't run MongoDB command Error: #{e.message}"
|
||||
end
|
||||
|
||||
def resource_id
|
||||
"mongodb_session:User:#{@user}:Host:#{@host}:Database:#{@database}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_session
|
||||
|
|
|
@ -51,6 +51,10 @@ module Inspec::Resources
|
|||
@mount_options[name]
|
||||
end
|
||||
|
||||
def resource_id
|
||||
@path || "mount"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Mount #{@path}"
|
||||
end
|
||||
|
|
|
@ -80,6 +80,10 @@ module Inspec::Resources
|
|||
end
|
||||
end
|
||||
|
||||
def resource_id
|
||||
"mssql_session:User:#{@user}:Host:#{@host}:Database:#{@db_name}:Instance:#{@instance}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"MSSQL session"
|
||||
end
|
||||
|
|
|
@ -121,6 +121,10 @@ module Inspec::Resources
|
|||
@files_contents[path] ||= read_file_content(path)
|
||||
end
|
||||
|
||||
def resource_id
|
||||
@conf_path || "mysql_conf"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"MySQL Configuration"
|
||||
end
|
||||
|
|
|
@ -43,6 +43,7 @@ module Inspec::Resources
|
|||
@host = host
|
||||
@port = port
|
||||
@socket = socket
|
||||
@db = nil
|
||||
init_fallback if user.nil? || pass.nil?
|
||||
raise Inspec::Exceptions::ResourceFailed, "Can't run MySQL SQL checks without authentication." if @user.nil? || @pass.nil?
|
||||
|
||||
|
@ -52,7 +53,9 @@ module Inspec::Resources
|
|||
def query(q, db = "")
|
||||
raise Inspec::Exceptions::ResourceFailed, "#{resource_exception_message}" if resource_failed?
|
||||
|
||||
mysql_cmd = create_mysql_cmd(q, db)
|
||||
@db = db
|
||||
mysql_cmd = create_mysql_cmd(q, @db)
|
||||
|
||||
cmd = if !@pass.nil?
|
||||
inspec.command(mysql_cmd, redact_regex: /(mysql -u\w+ -p).+(\s-(h|S).*)/)
|
||||
else
|
||||
|
@ -66,6 +69,10 @@ module Inspec::Resources
|
|||
end
|
||||
end
|
||||
|
||||
def resource_id
|
||||
"mysql_session:User:#{@user}:Host:#{@host}:Database:#{@db}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"MySQL Session"
|
||||
end
|
||||
|
|
|
@ -18,12 +18,13 @@ module Inspec::Resources
|
|||
its('modules') { should include 'my_module' }
|
||||
end
|
||||
EXAMPLE
|
||||
attr_reader :params, :bin_dir
|
||||
attr_reader :params, :bin_dir, :nginx_path
|
||||
|
||||
def initialize(nginx_path = "/usr/sbin/nginx")
|
||||
return skip_resource "The `nginx` resource is not yet available on your OS." if inspec.os.windows?
|
||||
return skip_resource "The `nginx` binary not found in the path provided." unless inspec.command(nginx_path).exist?
|
||||
|
||||
@nginx_path = nginx_path
|
||||
cmd = inspec.command("#{nginx_path} -V 2>&1")
|
||||
if cmd.exit_status != 0
|
||||
return skip_resource "Error using the command nginx -V"
|
||||
|
@ -59,6 +60,10 @@ module Inspec::Resources
|
|||
@data.scan(/--with-(\S+)_module/).flatten
|
||||
end
|
||||
|
||||
def resource_id
|
||||
nginx_path || "nginx"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Nginx Environment"
|
||||
end
|
||||
|
|
|
@ -50,6 +50,10 @@ module Inspec::Resources
|
|||
|
||||
def_delegators :http, :servers, :locations
|
||||
|
||||
def resource_id
|
||||
@conf_path || "nginx_conf"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"nginx_conf #{@conf_path}"
|
||||
end
|
||||
|
|
|
@ -2,6 +2,10 @@ module Inspec::Resources
|
|||
class Noop < Inspec.resource(1)
|
||||
name "noop"
|
||||
|
||||
def resource_id
|
||||
"No-op"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"No-op"
|
||||
end
|
||||
|
|
|
@ -3,6 +3,11 @@ require "inspec/resource"
|
|||
require "inspec/resources/ibmdb2_conf"
|
||||
|
||||
describe "Inspec::Resources::ibmdb2_conf" do
|
||||
it "generates the resource_id for the current resource" do
|
||||
resource = load_resource("ibmdb2_conf", db_instance: "db2inst1")
|
||||
_(resource.resource_id).must_equal "ibmdb2_conf:DatabaseInstance:db2inst1"
|
||||
end
|
||||
|
||||
it "fails when no IBM db2 executable path is provided" do
|
||||
resource = load_resource("ibmdb2_conf", db_instance: "db2inst1")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
|
@ -17,6 +22,7 @@ describe "Inspec::Resources::ibmdb2_conf" do
|
|||
|
||||
it "verify ibmdb2_conf on windows" do
|
||||
resource = MockLoader.new(:windows).load_resource("ibmdb2_conf")
|
||||
_(resource.resource_id).must_equal "ibmdb2_conf"
|
||||
_(resource.resource_failed?).must_equal false
|
||||
_(resource.output).must_be_kind_of Array
|
||||
end
|
||||
|
|
|
@ -3,6 +3,16 @@ require "inspec/resource"
|
|||
require "inspec/resources/ibmdb2_session"
|
||||
|
||||
describe "Inspec::Resources::ibmdb2_session" do
|
||||
it "generates the resource_id for the current resource" do
|
||||
resource = load_resource("ibmdb2_session", db_instance: "db2inst1", db_name: "sample")
|
||||
_(resource.resource_id).must_equal "ibmdb2_session:DatabaseInstance:db2inst1:DatabaseNamesample"
|
||||
end
|
||||
|
||||
it "generates the resource_id for the current resource when on Windows" do
|
||||
resource = MockLoader.new(:windows).load_resource("ibmdb2_session", db_name: "sample")
|
||||
_(resource.resource_id).must_equal "ibmdb2_session:DatabaseNamesample"
|
||||
end
|
||||
|
||||
it "fails when no IBM db2 instance name is provided" do
|
||||
resource = load_resource("ibmdb2_session", db_instance: "db2inst1", db_name: "sample")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
|
|
|
@ -5,6 +5,7 @@ require "inspec/resources/mongodb_conf"
|
|||
describe "Inspec::Resources::MongodbConf" do
|
||||
it "verify mongd.conf config parsing" do
|
||||
resource = load_resource("mongodb_conf", "/etc/mongod.conf")
|
||||
_(resource.resource_id).must_equal "/etc/mongod.conf"
|
||||
_(resource.params["storage"]["dbPath"]).must_equal "/var/lib/mongodb"
|
||||
_(resource.params["systemLog"]["path"]).must_equal "/var/log/mongodb/mongod.log"
|
||||
_(resource.params["net"]["port"]).must_equal 27017
|
||||
|
|
|
@ -5,12 +5,14 @@ require "inspec/resources/mongodb_session"
|
|||
describe "Inspec::Resources::MongodbSession" do
|
||||
it "fails when no user, password" do
|
||||
resource = load_resource("mongodb_session", host: "localhost", port: 27017, database: "test")
|
||||
_(resource.resource_id).must_equal("mongodb_session:User::Host:localhost:Database:test")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "Can't run MongoDB command. Error: Can't run MongoDB checks without authentication."
|
||||
end
|
||||
|
||||
it "fails when no database name is provided" do
|
||||
resource = load_resource("mongodb_session", user: "foo", password: "bar", host: "localhost", port: 27017)
|
||||
_(resource.resource_id).must_equal("mongodb_session:User:foo:Host:localhost:Database:")
|
||||
_(resource.resource_failed?).must_equal true
|
||||
_(resource.resource_exception_message).must_equal "Can't run MongoDB command. Error: You must provide a database name for the session."
|
||||
end
|
||||
|
|
|
@ -6,6 +6,10 @@ require "inspec/resources/mount"
|
|||
describe Inspec::Resources::FileResource do
|
||||
let(:root_resource) { load_resource("mount", "/") }
|
||||
|
||||
it "generates the resource_id for current resource" do
|
||||
_(root_resource.resource_id).must_equal "/"
|
||||
end
|
||||
|
||||
it "parses the mount data properly" do
|
||||
_(root_resource.send(:device)).must_equal("/dev/xvda1")
|
||||
_(root_resource.send(:type)).must_equal("ext4")
|
||||
|
@ -15,6 +19,10 @@ describe Inspec::Resources::FileResource do
|
|||
|
||||
let(:iso_resource) { load_resource("mount", "/mnt/iso-disk") }
|
||||
|
||||
it "generates resource_id for current resource" do
|
||||
_(iso_resource.resource_id).must_equal "/mnt/iso-disk"
|
||||
end
|
||||
|
||||
it "parses the mount data properly" do
|
||||
_(iso_resource.send(:device)).must_equal("/root/alpine-3.3.0-x86_64_2.iso")
|
||||
_(iso_resource.send(:type)).must_equal("iso9660")
|
||||
|
|
|
@ -5,6 +5,7 @@ require "inspec/resources/mssql_session"
|
|||
describe "Inspec::Resources::MssqlSession" do
|
||||
it "verify default mssql_session configuration" do
|
||||
resource = load_resource("mssql_session", user: "sa", password: "yourStrong(!)Password")
|
||||
_(resource.resource_id).must_equal "mssql_session:User:sa:Host:localhost:Database::Instance:"
|
||||
_(resource.user).must_equal "sa"
|
||||
_(resource.password).must_equal "yourStrong(!)Password"
|
||||
_(resource.host).must_equal "localhost"
|
||||
|
@ -27,6 +28,7 @@ describe "Inspec::Resources::MssqlSession" do
|
|||
|
||||
it "verify mssql_session configuration with custom instance and port" do
|
||||
resource = load_resource("mssql_session", user: "sa", password: "yourStrong(!)Password", instance: "SQL2012INSPEC", port: "1691")
|
||||
_(resource.resource_id).must_equal "mssql_session:User:sa:Host:localhost:Database::Instance:SQL2012INSPEC"
|
||||
_(resource.user).must_equal "sa"
|
||||
_(resource.password).must_equal "yourStrong(!)Password"
|
||||
_(resource.host).must_equal "localhost"
|
||||
|
|
|
@ -3,6 +3,11 @@ require "inspec/resource"
|
|||
require "inspec/resources/mysql_conf"
|
||||
|
||||
describe "Inspec::Resources::MysqlConf" do
|
||||
it "generates the resource_id for current resource" do
|
||||
resource = load_resource("mysql_conf", "/etc/mysql/my.cnf")
|
||||
_(resource.resource_id).must_equal "/etc/mysql/my.cnf"
|
||||
end
|
||||
|
||||
it "verify mysql.conf config parsing" do
|
||||
resource = load_resource("mysql_conf", "/etc/mysql/my.cnf")
|
||||
_(resource.client["port"]).must_equal "3306"
|
||||
|
|
|
@ -11,12 +11,14 @@ describe "Inspec::Resources::MysqlSession" do
|
|||
|
||||
_(resource.send(:create_mysql_cmd, "SELECT 1 FROM DUAL;"))
|
||||
.must_equal(%q{mysql -uroot -p\'\%\"\'\"\&\^\*\&\(\)\'\*\% -h localhost -s -e "SELECT 1 FROM DUAL;"})
|
||||
_(resource.resource_id).must_equal("mysql_session:User:root:Host:localhost:Database:")
|
||||
end
|
||||
it "verify mysql_session omits optional username and password" do
|
||||
resource = load_resource("mysql_session")
|
||||
|
||||
_(resource.send(:create_mysql_cmd, "SELECT 1 FROM DUAL;"))
|
||||
.must_equal('mysql -h localhost -s -e "SELECT 1 FROM DUAL;"')
|
||||
_(resource.resource_id).must_equal("mysql_session:User::Host:localhost:Database:")
|
||||
end
|
||||
it "verify mysql_session redacts output" do
|
||||
cmd = %q{mysql -uroot -p\'\%\"\'\"\&\^\*\&\(\)\'\*\% -h localhost -s -e "SELECT 1 FROM DUAL;"}
|
||||
|
|
|
@ -9,6 +9,10 @@ describe "Inspec::Resources::NginxConf" do
|
|||
|
||||
let(:nginx_conf) { MockLoader.new(:ubuntu).load_resource("nginx_conf") }
|
||||
|
||||
it "generates the resource_id for the current resource" do
|
||||
_(nginx_conf.resource_id).must_equal "/etc/nginx/nginx.conf"
|
||||
end
|
||||
|
||||
it "doesnt fail with a missing file" do
|
||||
# This path is not mocked because we cannot mock File.exist?
|
||||
# ...As far as I know
|
||||
|
|
|
@ -87,5 +87,9 @@ describe "Inspec::Resources::Nginx" do
|
|||
resource = load_resource("nginx")
|
||||
_(resource.http_scgi_temp_path).must_match "/var/cache/nginx/scgi_temp"
|
||||
end
|
||||
it "generates resource_id for the current_resource" do
|
||||
resource = load_resource("nginx")
|
||||
_(resource.resource_id).must_equal "/usr/sbin/nginx"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue