2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "resources/aws/aws_iam_access_keys"
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws"
|
|
|
|
require "resources/aws/aws_iam_access_keys"
|
2019-05-21 00:19:38 +00:00
|
|
|
|
2017-11-22 19:17:36 +00:00
|
|
|
#==========================================================#
|
|
|
|
# Constructor Tests #
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
class AwsIamAccessKeysConstructorTest < Minitest::Test
|
|
|
|
def setup
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(AlwaysEmptyMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bare_constructor_does_not_explode
|
|
|
|
AwsIamAccessKeys.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
# Filtering Tests #
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
class AwsIamAccessKeysFilterTest < Minitest::Test
|
|
|
|
|
|
|
|
def test_filter_methods_should_exist
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(AlwaysEmptyMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
resource = AwsIamAccessKeys.new
|
|
|
|
[:where, :'exists?'].each do |meth|
|
|
|
|
assert_respond_to(resource, meth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_method_where_should_be_chainable
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(AlwaysEmptyMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
resource = AwsIamAccessKeys.new
|
|
|
|
assert_respond_to(resource.where, :where)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_method_exists_should_probe_empty_when_empty
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(AlwaysEmptyMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
resource = AwsIamAccessKeys.new
|
|
|
|
refute(resource.exists?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_method_exists_should_probe_present_when_present
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(BasicMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
resource = AwsIamAccessKeys.new
|
|
|
|
assert(resource.exists?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
# Filter Criteria Tests #
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
class AwsIamAccessKeysFilterCriteriaTest < Minitest::Test
|
|
|
|
def setup
|
2017-12-15 05:07:28 +00:00
|
|
|
# Here we always want no results.
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(AlwaysEmptyMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
@valued_criteria = {
|
2019-06-11 22:24:35 +00:00
|
|
|
username: "bob",
|
|
|
|
id: "AKIA1234567890ABCDEF",
|
|
|
|
access_key_id: "AKIA1234567890ABCDEF",
|
2017-11-22 19:17:36 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_criteria_when_used_in_constructor_with_value
|
|
|
|
@valued_criteria.each do |criterion, value|
|
|
|
|
AwsIamAccessKeys.new(criterion => value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_criteria_when_used_in_where_with_value
|
|
|
|
@valued_criteria.each do |criterion, value|
|
|
|
|
AwsIamAccessKeys.new.where(criterion => value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Negative cases
|
|
|
|
def test_criteria_when_used_in_constructor_with_bad_criterion
|
2018-02-08 04:26:37 +00:00
|
|
|
assert_raises(ArgumentError) do
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsIamAccessKeys.new(nope: "some_val")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_criteria_when_used_in_where_with_bad_criterion
|
2018-02-08 04:26:37 +00:00
|
|
|
assert_raises(ArgumentError) do
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsIamAccessKeys.new(nope: "some_val")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Identity criterion is allowed based on regex
|
|
|
|
def test_identity_criterion_when_used_in_constructor_positive
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsIamAccessKeys.new("AKIA1234567890ABCDEF")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Permitted by FilterTable?
|
|
|
|
def test_identity_criterion_when_used_in_where_positive
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsIamAccessKeys.new.where("AKIA1234567890ABCDEF")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_identity_criterion_when_used_in_constructor_negative
|
|
|
|
assert_raises(RuntimeError) do
|
2019-06-11 22:24:35 +00:00
|
|
|
AwsIamAccessKeys.new("NopeAKIA1234567890ABCDEF")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Permitted by FilterTable?
|
|
|
|
# def test_identity_criterion_when_used_in_where_negative
|
|
|
|
# assert_raises(RuntimeError) do
|
|
|
|
# AwsIamAccessKeys.new.where('NopeAKIA1234567890ABCDEF')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
# Property Tests #
|
|
|
|
#==========================================================#
|
|
|
|
class AwsIamAccessKeysPropertiesTest < Minitest::Test
|
|
|
|
def setup
|
|
|
|
# Reset back to the basic kit each time.
|
2018-02-08 04:26:37 +00:00
|
|
|
AwsIamAccessKeys::BackendFactory.select(BasicMAKP)
|
2017-11-22 19:17:36 +00:00
|
|
|
@all_basic = AwsIamAccessKeys.new
|
|
|
|
end
|
|
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
# created_date / created_days_ago / created_hours_ago #
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
def test_property_created_date
|
2018-02-01 03:16:30 +00:00
|
|
|
assert_kind_of(DateTime, @all_basic.entries.first.create_date)
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
arg_filtered = @all_basic.where(create_date: DateTime.parse("2017-10-27T17:58:00Z"))
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("BOB")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2018-02-01 03:16:30 +00:00
|
|
|
block_filtered = @all_basic.where { create_date.friday? }
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("BOB")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_created_days_ago
|
|
|
|
assert_kind_of(Integer, @all_basic.entries.first.created_days_ago)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(created_days_ago: 9)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
|
|
|
block_filtered = @all_basic.where { created_days_ago > 2 }
|
|
|
|
assert_equal(2, block_filtered.entries.count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_created_hours_ago
|
|
|
|
assert_kind_of(Integer, @all_basic.entries.first.created_hours_ago)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(created_hours_ago: 222)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
|
|
|
block_filtered = @all_basic.where { created_hours_ago > 100 }
|
|
|
|
assert_equal(2, block_filtered.entries.count)
|
|
|
|
end
|
|
|
|
|
2017-12-15 05:07:28 +00:00
|
|
|
#----------------------------------------------------------#
|
|
|
|
# created_with_user #
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
def test_property_created_with_user
|
|
|
|
assert_kind_of(TrueClass, @all_basic.entries[0].created_with_user)
|
|
|
|
assert_kind_of(FalseClass, @all_basic.entries[1].created_with_user)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(created_with_user: true)
|
|
|
|
assert_equal(2, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("BOB")
|
2017-12-15 05:07:28 +00:00
|
|
|
|
|
|
|
block_filtered = @all_basic.where { created_with_user }
|
|
|
|
assert_equal(2, block_filtered.entries.count)
|
|
|
|
end
|
|
|
|
|
2017-11-22 19:17:36 +00:00
|
|
|
#----------------------------------------------------------#
|
|
|
|
# active / inactive #
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
def test_property_active
|
|
|
|
assert_kind_of(TrueClass, @all_basic.entries.first.active)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(active: true)
|
|
|
|
assert_equal(2, arg_filtered.entries.count)
|
|
|
|
|
|
|
|
block_filtered = @all_basic.where { active }
|
|
|
|
assert_equal(2, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("BOB")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_inactive
|
|
|
|
assert_kind_of(FalseClass, @all_basic.entries.first.inactive)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(inactive: true)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
|
|
|
|
|
|
|
block_filtered = @all_basic.where { inactive }
|
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("ROBIN")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#-----------------------------------------------------------#
|
|
|
|
# last_used_date / last_used_days_ago / last_used_hours_ago #
|
|
|
|
#-----------------------------------------------------------#
|
|
|
|
def test_property_last_used_date
|
|
|
|
assert_kind_of(NilClass, @all_basic.entries[0].last_used_date)
|
|
|
|
assert_kind_of(DateTime, @all_basic.entries[1].last_used_date)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
arg_filtered = @all_basic.where(last_used_date: DateTime.parse("2017-10-27T17:58:00Z"))
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
block_filtered = @all_basic.where { last_used_date && last_used_date.friday? }
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_last_used_days_ago
|
|
|
|
assert_kind_of(NilClass, @all_basic.entries[0].last_used_days_ago)
|
|
|
|
assert_kind_of(Integer, @all_basic.entries[1].last_used_days_ago)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(last_used_days_ago: 4)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
block_filtered = @all_basic.where { last_used_days_ago && (last_used_days_ago < 2) }
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("ROBIN")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_last_used_hours_ago
|
|
|
|
assert_kind_of(NilClass, @all_basic.entries[0].last_used_hours_ago)
|
|
|
|
assert_kind_of(Integer, @all_basic.entries[1].last_used_hours_ago)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(last_used_hours_ago: 102)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
block_filtered = @all_basic.where { last_used_hours_ago && (last_used_hours_ago < 10) }
|
2017-11-22 19:17:36 +00:00
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("ROBIN")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#-----------------------------------------------------------#
|
|
|
|
# ever_used / never_used #
|
|
|
|
#-----------------------------------------------------------#
|
|
|
|
def test_property_ever_used
|
|
|
|
assert_kind_of(FalseClass, @all_basic.entries[0].ever_used)
|
|
|
|
assert_kind_of(TrueClass, @all_basic.entries[1].ever_used)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(ever_used: true)
|
|
|
|
assert_equal(2, arg_filtered.entries.count)
|
|
|
|
|
|
|
|
block_filtered = @all_basic.where { ever_used }
|
|
|
|
assert_equal(2, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_property_never_used
|
|
|
|
assert_kind_of(TrueClass, @all_basic.entries[0].never_used)
|
|
|
|
assert_kind_of(FalseClass, @all_basic.entries[1].never_used)
|
|
|
|
|
|
|
|
arg_filtered = @all_basic.where(never_used: true)
|
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
|
|
|
|
|
|
|
block_filtered = @all_basic.where { never_used }
|
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("BOB")
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
2017-12-15 05:07:28 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
# user_created_date #
|
|
|
|
#----------------------------------------------------------#
|
|
|
|
def test_property_user_created_date
|
|
|
|
assert_kind_of(DateTime, @all_basic.entries.first.user_created_date)
|
2019-06-11 22:24:35 +00:00
|
|
|
arg_filtered = @all_basic.where(user_created_date: DateTime.parse("2017-10-21T17:58:00Z"))
|
2017-12-15 05:07:28 +00:00
|
|
|
assert_equal(1, arg_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert arg_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-12-15 05:07:28 +00:00
|
|
|
|
|
|
|
block_filtered = @all_basic.where { user_created_date.saturday? }
|
|
|
|
assert_equal(1, block_filtered.entries.count)
|
2019-06-11 22:24:35 +00:00
|
|
|
assert block_filtered.access_key_ids.first.end_with?("SALLY")
|
2017-12-15 05:07:28 +00:00
|
|
|
end
|
2017-11-22 19:17:36 +00:00
|
|
|
end
|
|
|
|
#==========================================================#
|
|
|
|
# Mock Support Classes #
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
# MAKP = MockAccessKeyProvider. Abbreviation not used
|
|
|
|
# outside this file.
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class AlwaysEmptyMAKP < AwsBackendBase
|
2017-11-22 19:17:36 +00:00
|
|
|
def fetch(_filter_criteria)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
class BasicMAKP < AwsBackendBase
|
|
|
|
def fetch(_filter_criteria)
|
2017-11-22 19:17:36 +00:00
|
|
|
[
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
username: "bob",
|
|
|
|
access_key_id: "AKIA1234567890123BOB",
|
|
|
|
id: "AKIA1234567890123BOB",
|
|
|
|
create_date: DateTime.parse("2017-10-27T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
created_days_ago: 4,
|
|
|
|
created_hours_ago: 102,
|
2017-12-15 05:07:28 +00:00
|
|
|
created_with_user: true,
|
2019-06-11 22:24:35 +00:00
|
|
|
status: "Active",
|
2017-11-22 19:17:36 +00:00
|
|
|
active: true,
|
|
|
|
inactive: false,
|
|
|
|
last_used_date: nil,
|
|
|
|
last_used_days_ago: nil,
|
|
|
|
last_used_hours_ago: nil,
|
|
|
|
ever_used: false,
|
|
|
|
never_used: true,
|
2019-06-11 22:24:35 +00:00
|
|
|
user_created_date: DateTime.parse("2017-10-27T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
username: "sally",
|
|
|
|
access_key_id: "AKIA12345678901SALLY",
|
|
|
|
id: "AKIA12345678901SALLY",
|
|
|
|
create_date: DateTime.parse("2017-10-22T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
created_days_ago: 9,
|
|
|
|
created_hours_ago: 222,
|
2019-06-11 22:24:35 +00:00
|
|
|
created_with_user: false,
|
|
|
|
status: "Active",
|
2017-11-22 19:17:36 +00:00
|
|
|
active: true,
|
|
|
|
inactive: false,
|
2019-06-11 22:24:35 +00:00
|
|
|
last_used_date: DateTime.parse("2017-10-27T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
last_used_days_ago: 4,
|
|
|
|
last_used_hours_ago: 102,
|
|
|
|
ever_used: true,
|
|
|
|
never_used: false,
|
2019-06-11 22:24:35 +00:00
|
|
|
user_created_date: DateTime.parse("2017-10-21T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
username: "robin",
|
|
|
|
access_key_id: "AKIA12345678901ROBIN",
|
|
|
|
id: "AKIA12345678901ROBIN",
|
|
|
|
create_date: DateTime.parse("2017-10-31T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
created_days_ago: 1,
|
|
|
|
created_hours_ago: 12,
|
2019-06-11 22:24:35 +00:00
|
|
|
created_with_user: true,
|
|
|
|
status: "Inactive",
|
2017-11-22 19:17:36 +00:00
|
|
|
active: false,
|
|
|
|
inactive: true,
|
2019-06-11 22:24:35 +00:00
|
|
|
last_used_date: DateTime.parse("2017-10-31T20:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
last_used_days_ago: 0,
|
|
|
|
last_used_hours_ago: 5,
|
|
|
|
ever_used: true,
|
|
|
|
never_used: false,
|
2019-06-11 22:24:35 +00:00
|
|
|
user_created_date: DateTime.parse("2017-10-31T17:58:00Z"),
|
2017-11-22 19:17:36 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|