2018-01-23 04:22:37 +00:00
|
|
|
require '_aws'
|
|
|
|
|
2017-08-08 13:50:35 +00:00
|
|
|
# author: Alex Bedley
|
|
|
|
# author: Steffanie Freeman
|
|
|
|
# author: Simon Varlow
|
|
|
|
# author: Chris Redekop
|
|
|
|
class AwsIamUsers < Inspec.resource(1)
|
|
|
|
name 'aws_iam_users'
|
|
|
|
desc 'Verifies settings for AWS IAM users'
|
|
|
|
example '
|
|
|
|
describe aws_iam_users.where(has_mfa_enabled?: false) do
|
|
|
|
it { should_not exist }
|
|
|
|
end
|
|
|
|
describe aws_iam_users.where(has_console_password?: true) do
|
|
|
|
it { should exist }
|
|
|
|
end
|
|
|
|
'
|
2018-02-08 04:26:37 +00:00
|
|
|
supports platform: 'aws'
|
|
|
|
|
|
|
|
include AwsPluralResourceMixin
|
2017-08-08 13:50:35 +00:00
|
|
|
|
|
|
|
filter = FilterTable.create
|
|
|
|
filter.add_accessor(:where)
|
|
|
|
.add_accessor(:entries)
|
|
|
|
.add(:exists?) { |x| !x.entries.empty? }
|
2017-12-08 18:34:09 +00:00
|
|
|
.add(:has_mfa_enabled?, field: :has_mfa_enabled)
|
|
|
|
.add(:has_console_password?, field: :has_console_password)
|
2018-02-01 16:23:25 +00:00
|
|
|
.add(:password_ever_used?, field: :password_ever_used?)
|
|
|
|
.add(:password_never_used?, field: :password_never_used?)
|
|
|
|
.add(:password_last_used_days_ago, field: :password_last_used_days_ago)
|
2017-12-08 18:34:09 +00:00
|
|
|
.add(:username, field: :user_name)
|
2018-02-08 04:26:37 +00:00
|
|
|
filter.connect(self, :table)
|
2017-08-08 13:50:35 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def validate_params(raw_params)
|
|
|
|
# No params yet
|
|
|
|
unless raw_params.empty?
|
|
|
|
raise ArgumentError, 'aws_iam_users does not accept resource parameters'
|
|
|
|
end
|
|
|
|
raw_params
|
|
|
|
end
|
2017-08-08 13:50:35 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def fetch_from_api
|
|
|
|
backend = BackendFactory.create(inspec_runner)
|
|
|
|
@table = backend.list_users.users.map(&:to_h)
|
2017-12-08 18:34:09 +00:00
|
|
|
|
|
|
|
# TODO: lazy columns - https://github.com/chef/inspec-aws/issues/100
|
2018-02-08 04:26:37 +00:00
|
|
|
@table.each do |user|
|
2017-12-08 18:34:09 +00:00
|
|
|
begin
|
|
|
|
_login_profile = backend.get_login_profile(user_name: user[:user_name])
|
|
|
|
user[:has_console_password] = true
|
|
|
|
rescue Aws::IAM::Errors::NoSuchEntity
|
|
|
|
user[:has_console_password] = false
|
|
|
|
end
|
|
|
|
user[:has_console_password?] = user[:has_console_password]
|
2017-08-08 13:50:35 +00:00
|
|
|
|
2017-12-08 18:34:09 +00:00
|
|
|
begin
|
|
|
|
aws_mfa_devices = backend.list_mfa_devices(user_name: user[:user_name])
|
|
|
|
user[:has_mfa_enabled] = !aws_mfa_devices.mfa_devices.empty?
|
|
|
|
rescue Aws::IAM::Errors::NoSuchEntity
|
|
|
|
user[:has_mfa_enabled] = false
|
|
|
|
end
|
|
|
|
user[:has_mfa_enabled?] = user[:has_mfa_enabled]
|
2018-02-01 16:23:25 +00:00
|
|
|
password_last_used = user[:password_last_used]
|
|
|
|
user[:password_ever_used?] = !password_last_used.nil?
|
|
|
|
user[:password_never_used?] = password_last_used.nil?
|
|
|
|
next unless user[:password_ever_used?]
|
|
|
|
user[:password_last_used_days_ago] = ((Time.now - password_last_used) / (24*60*60)).to_i
|
2017-12-08 18:34:09 +00:00
|
|
|
end
|
2018-02-08 04:26:37 +00:00
|
|
|
@table
|
2017-08-08 13:50:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'IAM Users'
|
|
|
|
end
|
|
|
|
|
2017-12-08 18:34:09 +00:00
|
|
|
#===========================================================================#
|
|
|
|
# Backend Implementation
|
|
|
|
#===========================================================================#
|
|
|
|
class Backend
|
2018-02-08 04:26:37 +00:00
|
|
|
class AwsClientApi < AwsBackendBase
|
|
|
|
BackendFactory.set_default_backend(self)
|
|
|
|
self.aws_client_class = Aws::IAM::Client
|
|
|
|
|
2017-12-08 18:34:09 +00:00
|
|
|
# TODO: delegate this out
|
|
|
|
def list_users(query = {})
|
2018-02-08 04:26:37 +00:00
|
|
|
aws_service_client.list_users(query)
|
2017-12-08 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_login_profile(query)
|
2018-02-08 04:26:37 +00:00
|
|
|
aws_service_client.get_login_profile(query)
|
2017-12-08 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_mfa_devices(query)
|
2018-02-08 04:26:37 +00:00
|
|
|
aws_service_client.list_mfa_devices(query)
|
2017-12-08 18:34:09 +00:00
|
|
|
end
|
|
|
|
end
|
2017-08-08 13:50:35 +00:00
|
|
|
end
|
|
|
|
end
|