turbot/steampipe-mod-terraform-aws-compliance

Control: CloudFront distributions should have origin access identity enabled

Description

This control checks whether an Amazon CloudFront distribution with Amazon S3 Origin type has Origin Access Identity (OAI) configured. The control fails if OAI is not configured.

Usage

Run the control in your terminal:

powerpipe control run terraform_aws_compliance.control.cloudfront_distribution_origin_access_identity_enabled

Snapshot and share results via Turbot Pipes:

powerpipe login
powerpipe control run terraform_aws_compliance.control.cloudfront_distribution_origin_access_identity_enabled --share

SQL

This control uses a named query:

with cloudfront_distribution as (
select
*
from
terraform_resource
where
type = 'aws_cloudfront_distribution'
), origin_type as (
select
distinct address
from
cloudfront_distribution,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'origin')
when 'array' then (attributes_std -> 'origin')
else null end
) as o
where
(o ->> 'domain_name' ) like '%aws_s3_bucket%'
group by address
),origins as (
select
count(*),
address
from
cloudfront_distribution,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'origin')
when 'array' then (attributes_std -> 'origin')
else null end
) as o
where
(o ->> 'domain_name' ) like '%aws_s3_bucket%'
and(
(o -> 's3_origin_config' ->> 'origin_access_identity') = ''
or (o -> 's3_origin_config' ) is null
)
group by address
)
select
a.address as resource,
case
when (attributes_std -> 'origin') is null then 'alarm'
when (attributes_std -> 'origin' ->> 'domain_name' ) like '%aws_s3_bucket%' and (( not((attributes_std -> 'origin' -> 's3_origin_config' ->> 'origin_access_identity') = '')) and (attributes_std -> 'origin' -> 's3_origin_config' -> 'origin_access_identity') is not null) then 'ok'
when (attributes_std -> 'origin' ->> 'domain_name' ) like '%aws_s3_bucket%' and (((attributes_std -> 'origin' -> 's3_origin_config' ->> 'origin_access_identity') = '') or ((attributes_std -> 'origin' -> 's3_origin_config') is null)) then 'alarm'
when b.address is not null then 'alarm'
when (t.address is null ) and ((attributes_std -> 'origin' ->> 'domain_name') not like '%aws_s3_bucket%') then 'skip'
else 'ok'
end as status,
split_part(a.address, '.', 2) || case
when (attributes_std -> 'origin') is null then ' origins not defined'
when (attributes_std -> 'origin' ->> 'domain_name' ) like '%aws_s3_bucket%' and (( not((attributes_std -> 'origin' -> 's3_origin_config' ->> 'origin_access_identity') = '')) and (attributes_std -> 'origin' -> 's3_origin_config' -> 'origin_access_identity') is not null) then ' origin access identity configured'
when (attributes_std -> 'origin' ->> 'domain_name' ) like '%aws_s3_bucket%' and (((attributes_std -> 'origin' -> 's3_origin_config' ->> 'origin_access_identity') = '') or ((attributes_std -> 'origin' -> 's3_origin_config') is null)) then ' origin access identity not configured'
when b.address is not null then ' origin access identity not configured'
when (t.address is null ) and ((attributes_std -> 'origin' ->> 'domain_name') not like '%aws_s3_bucket%') then ' origin type is not S3'
else ' origin access identity configured'
end || '.' reason
, path || ':' || start_line
from
cloudfront_distribution as a
left join origin_type as t on a.address = t.address
left join origins as b on a.address = b.address;

Tags