Skip to content

Commit

Permalink
Fix multiple CloudTrail, CloudWatch, Config, Lambda, and S3 queries t…
Browse files Browse the repository at this point in the history
…o work properly with multi-account connections fixes #250 (#247)

Co-authored-by: John Smyth <john@turbot.com>
  • Loading branch information
cbruno10 and johnsmyth authored Jul 23, 2021
1 parent 5149b43 commit 9b13d4b
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 44 deletions.
11 changes: 6 additions & 5 deletions query/cloudtrail/cloudtrail_multi_region_trail_enabled.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
with multi_region_trails as (
select
count(account_id),
string_agg(account_id, ',') as account_id
account_id,
count(account_id) as num_multregion_trails
from
aws_cloudtrail_trail
where
is_multi_region_trail
and is_logging
group by
account_id,
is_multi_region_trail
)
select
-- Required Columns
a.arn as resource,
case
when b.count < 1 then 'alarm'
when coalesce(num_multregion_trails, 0) < 1 then 'alarm'
else 'ok'
end as status,
a.title || ' has ' || coalesce(b.count, 0) || ' multi-region trail(s).' as reason,
a.title || ' has ' || coalesce(num_multregion_trails, 0) || ' multi-region trail(s).' as reason,
-- Additional Dimensions
a.account_id
from
aws_account as a
left join multi_region_trails as b on a.account_id = split_part(b.account_id, ',', 1);
left join multi_region_trails as b on a.account_id = b.account_id;
4 changes: 3 additions & 1 deletion query/cloudtrail/cloudtrail_s3_logging_enabled.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ select
t.account_id
from
aws_cloudtrail_trail t
inner join aws_s3_bucket b on t.s3_bucket_name = b.name;
inner join aws_s3_bucket b on t.s3_bucket_name = b.name
where
t.region = t.home_region;
4 changes: 3 additions & 1 deletion query/cloudtrail/cloudtrail_trail_integrated_with_logs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ select
region,
account_id
from
aws_cloudtrail_trail;
aws_cloudtrail_trail
where
region = home_region;
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ select
region,
account_id
from
aws_cloudtrail_trail;
aws_cloudtrail_trail
where
region = home_region;
4 changes: 3 additions & 1 deletion query/cloudtrail/cloudtrail_trail_validation_enabled.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ select
region,
account_id
from
aws_cloudtrail_trail;
aws_cloudtrail_trail
where
region = home_region;
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_bucket_policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_config_configuration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_console_login_mfa.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_disable_or_delete_cmk.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_iam_policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_network_acl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_network_gateway.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_organization.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
5 changes: 2 additions & 3 deletions query/cloudwatch/log_metric_filter_root_login.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -41,5 +41,4 @@ select
a.account_id
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
left join filter_data as f on a.account_id = f.account_id
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_route_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_security_group.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -42,4 +42,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_unauthorized_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -45,4 +45,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 1 addition & 2 deletions query/cloudwatch/log_metric_filter_vpc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with filter_data as (
)
select
-- Required Columns
'arn:' || a.partition || ':::' || a.account_id as resource,
distinct 'arn:' || a.partition || ':::' || a.account_id as resource,
case
when f.trail_name is null then 'alarm'
else 'ok'
Expand All @@ -43,4 +43,3 @@ select
from
aws_account as a
left join filter_data as f on a.account_id = f.account_id
limit 1
3 changes: 2 additions & 1 deletion query/config/config_enabled_all_regions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ select
a.account_id
from
aws_region as a
left join aws_config_configuration_recorder as r on r.region = a.name;
left join aws_config_configuration_recorder as r
on r.account_id = a.account_id and r.region = a.name;
4 changes: 2 additions & 2 deletions query/lambda/lambda_function_use_latest_runtime.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ select
else 'alarm'
end as status,
case
when runtime in ('nodejs14.x', 'nodejs12.x', 'nodejs10.x', 'python3.8', 'python3.7', 'python3.6', 'ruby2.5', 'ruby2.7', 'java11', 'java8', 'go1.x', 'dotnetcore2.1', 'dotnetcore3.1') then title || ' uses latest runtimes.'
else title || ' uses latest runtimes.'
when runtime in ('nodejs14.x', 'nodejs12.x', 'nodejs10.x', 'python3.8', 'python3.7', 'python3.6', 'ruby2.5', 'ruby2.7', 'java11', 'java8', 'go1.x', 'dotnetcore2.1', 'dotnetcore3.1') then title || ' uses latest runtime - ' || runtime || '.'
else title || ' uses ' || runtime || ' which is not the latest version.'
end as reason,
-- Additional Dimensions
region,
Expand Down
4 changes: 3 additions & 1 deletion query/s3/s3_public_access_block_bucket_account.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ select
bucket.account_id
from
aws_s3_bucket as bucket,
aws_s3_account_settings as s3account;
aws_s3_account_settings as s3account
where
s3account.account_id = bucket.account_id;

0 comments on commit 9b13d4b

Please sign in to comment.