/
s3.sp
1154 lines (1090 loc) · 42.1 KB
/
s3.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
locals {
conformance_pack_s3_common_tags = merge(local.aws_compliance_common_tags, {
service = "AWS/S3"
})
}
control "s3_bucket_cross_region_replication_enabled" {
title = "S3 bucket cross-region replication should be enabled"
description = "Amazon Simple Storage Service (Amazon S3) Cross-Region Replication (CRR) supports maintaining adequate capacity and availability."
query = query.s3_bucket_cross_region_replication_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_default_encryption_enabled" {
title = "S3 bucket default encryption should be enabled"
description = "To help protect data at rest, ensure encryption is enabled for your Amazon Simple Storage Service (Amazon S3) buckets."
query = query.s3_bucket_default_encryption_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_moderate_rev_4 = "true"
gdpr = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_enforces_ssl" {
title = "S3 buckets should enforce SSL"
description = "To help protect data in transit, ensure that your Amazon Simple Storage Service (Amazon S3) buckets require requests to use Secure Socket Layer (SSL)."
query = query.s3_bucket_enforces_ssl
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gdpr = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_logging_enabled" {
title = "S3 bucket logging should be enabled"
description = "Amazon Simple Storage Service (Amazon S3) server access logging provides a method to monitor the network for potential cybersecurity events."
query = query.s3_bucket_logging_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_object_lock_enabled" {
title = "S3 bucket object lock should be enabled"
description = "Ensure that your Amazon Simple Storage Service (Amazon S3) bucket has lock enabled, by default."
query = query.s3_bucket_object_lock_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_csf = "true"
soc_2 = "true"
})
}
control "s3_bucket_restrict_public_read_access" {
title = "S3 buckets should prohibit public read access"
description = "Manage access to resources in the AWS Cloud by only allowing authorized users, processes, and devices access to Amazon Simple Storage Service (Amazon S3) buckets."
query = query.s3_bucket_restrict_public_read_access
tags = merge(local.conformance_pack_s3_common_tags, {
audit_manager_control_tower = "true"
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_restrict_public_write_access" {
title = "S3 buckets should prohibit public write access"
description = "Manage access to resources in the AWS Cloud by only allowing authorized users, processes, and devices access to Amazon Simple Storage Service (Amazon S3) buckets."
query = query.s3_bucket_restrict_public_write_access
tags = merge(local.conformance_pack_s3_common_tags, {
audit_manager_control_tower = "true"
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_versioning_enabled" {
title = "S3 bucket versioning should be enabled"
description = "Amazon Simple Storage Service (Amazon S3) bucket versioning helps keep multiple variants of an object in the same Amazon S3 bucket."
query = query.s3_bucket_versioning_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
audit_manager_control_tower = "true"
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
soc_2 = "true"
})
}
control "s3_bucket_static_website_hosting_disabled" {
title = "S3 buckets static website hosting should be disabled"
description = "Enabling static website on a S3 bucket requires to grant public read access to the bucket. There is a potential risk of exposure when you turn off block public access settings to make your bucket public. This is recommend to not configure static website on S3 bucket."
query = query.s3_bucket_static_website_hosting_disabled
tags = merge(local.conformance_pack_s3_common_tags, {
other_checks = "true"
})
}
control "s3_public_access_block_account" {
title = "S3 public access should be blocked at account level"
description = "Manage access to resources in the AWS Cloud by ensuring that Amazon Simple Storage Service (Amazon S3) buckets cannot be publicly accessed."
query = query.s3_public_access_block_account
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "s3_public_access_block_bucket_account" {
title = "S3 public access should be blocked at account and bucket levels"
description = "Manage access to resources in the AWS Cloud by ensuring that Amazon Simple Storage Service (Amazon S3) buckets cannot be publicly accessed."
query = query.s3_public_access_block_bucket_account
tags = merge(local.conformance_pack_s3_common_tags, {
fedramp_moderate_rev_4 = "true"
ffiec = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_4 = "true"
rbi_cyber_security = "true"
})
}
control "s3_bucket_default_encryption_enabled_kms" {
title = "S3 bucket default encryption should be enabled with KMS"
description = "To help protect data at rest, ensure encryption is enabled for your Amazon Simple Storage Service (Amazon S3) buckets."
query = query.s3_bucket_default_encryption_enabled_kms
tags = merge(local.conformance_pack_s3_common_tags, {
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gdpr = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
rbi_cyber_security = "true"
})
}
control "s3_public_access_block_bucket" {
title = "S3 public access should be blocked at bucket levels"
description = "Ensure if Amazon Simple Storage Service (Amazon S3) buckets are publicly accessible. This rule is non compliant if an Amazon S3 bucket is not listed in the excludedPublicBuckets parameter and bucket level settings are public."
query = query.s3_public_access_block_bucket
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
nist_800_53_rev_5 = "true"
nist_csf = "true"
})
}
control "s3_bucket_policy_restricts_cross_account_permission_changes" {
title = "Amazon S3 permissions granted to other AWS accounts in bucket policies should be restricted"
description = "This control checks whether the S3 bucket policy prevents principals from other AWS accounts from performing denied actions on resources in the S3 bucket."
query = query.s3_bucket_policy_restricts_cross_account_permission_changes
tags = merge(local.conformance_pack_s3_common_tags, {
cis_controls_v8_ig1 = "true"
nist_csf = "true"
})
}
control "s3_bucket_object_logging_enabled" {
title = "S3 buckets object logging should be enabled"
description = "Object-Level logging saves events in JSON format in CloudTrail. This is recommended from a security best practice perspective for buckets that contain sensitive data."
query = query.s3_bucket_object_logging_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
other_checks = "true"
})
}
control "s3_bucket_policy_restrict_public_access" {
title = "S3 bucket policy should prohibit public access"
description = "This control checks that the access granted by the S3 bucket is restricted by any of the principals, federated users, service principals, IP addresses, or VPCs that you provide. The rule is compliant if a bucket policy is not present."
query = query.s3_bucket_policy_restrict_public_access
tags = merge(local.conformance_pack_s3_common_tags, {
gxp_21_cfr_part_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_csf = "true"
soc_2 = "true"
})
}
control "s3_bucket_lifecycle_policy_enabled" {
title = "S3 buckets should have lifecycle policies configured"
description = "This control checks if Amazon Simple Storage Service (Amazon S3) buckets have lifecycle policy configured. This rule fails if Amazon S3 lifecycle policy is not enabled."
query = query.s3_bucket_lifecycle_policy_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
gxp_21_cfr_part_11 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "s3_bucket_versioning_and_lifecycle_policy_enabled" {
title = "S3 buckets with versioning enabled should have lifecycle policies configured"
description = "This control checks if Amazon Simple Storage Service (Amazon S3) version enabled buckets have lifecycle policy configured. This rule fails if Amazon S3 lifecycle policy is not enabled."
query = query.s3_bucket_versioning_and_lifecycle_policy_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
gxp_21_cfr_part_11 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "s3_bucket_event_notifications_enabled" {
title = "S3 buckets should have event notifications enabled"
description = "This control checks whether S3 Event Notifications are enabled on an Amazon S3 bucket. This control fails if S3 Event Notifications are not enabled on a bucket."
severity = "medium"
query = query.s3_bucket_event_notifications_enabled
tags = merge(local.conformance_pack_s3_common_tags, {
nist_csf = "true"
soc_2 = "true"
})
}
control "s3_bucket_acls_should_prohibit_user_access" {
title = "S3 buckets access control lists (ACLs) should not be used to manage user access to buckets"
description = "This control checks whether Amazon S3 buckets provide user permissions via ACLs. The control fails if ACLs are configured for managing user access on S3 buckets."
query = query.s3_bucket_acls_should_prohibit_user_access
tags = merge(local.conformance_pack_s3_common_tags, {
nist_csf = "true"
})
}
query "s3_bucket_cross_region_replication_enabled" {
sql = <<-EOQ
with bucket_with_replication as (
select
name,
r ->> 'Status' as rep_status
from
aws_s3_bucket,
jsonb_array_elements(replication -> 'Rules' ) as r
)
select
b.arn as resource,
case
when b.name = r.name and r.rep_status = 'Enabled' then 'ok'
else 'alarm'
end as status,
case
when b.name = r.name and r.rep_status = 'Enabled' then b.title || ' enabled with cross-region replication.'
else b.title || ' not enabled with cross-region replication.'
end as reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket b
left join bucket_with_replication r on b.name = r.name;
EOQ
}
query "s3_bucket_default_encryption_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when server_side_encryption_configuration is not null then 'ok'
else 'alarm'
end status,
case
when server_side_encryption_configuration is not null then name || ' default encryption enabled.'
else name || ' default encryption disabled.'
end reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_bucket_enforces_ssl" {
sql = <<-EOQ
with ssl_ok as (
select
distinct name,
arn,
'ok' as status
from
aws_s3_bucket,
jsonb_array_elements(policy_std -> 'Statement') as s,
jsonb_array_elements_text(s -> 'Principal' -> 'AWS') as p,
jsonb_array_elements_text(s -> 'Action') as a,
jsonb_array_elements_text(s -> 'Resource') as r,
jsonb_array_elements_text(
s -> 'Condition' -> 'Bool' -> 'aws:securetransport'
) as ssl
where
p = '*'
and s ->> 'Effect' = 'Deny'
and ssl :: bool = false
)
select
b.arn as resource,
case
when ok.status = 'ok' then 'ok'
else 'alarm'
end status,
case
when ok.status = 'ok' then b.name || ' bucket policy enforces HTTPS.'
else b.name || ' bucket policy does not enforce HTTPS.'
end reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join ssl_ok as ok on ok.name = b.name;
EOQ
}
query "s3_bucket_logging_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when logging -> 'TargetBucket' is null then 'alarm'
else 'ok'
end as status,
case
when logging -> 'TargetBucket' is null then title || ' logging disabled.'
else title || ' logging enabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_bucket_object_lock_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when object_lock_configuration is null then 'alarm'
else 'ok'
end as status,
case
when object_lock_configuration is null then title || ' object lock not enabled.'
else title || ' object lock enabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_bucket_restrict_public_read_access" {
sql = <<-EOQ
with public_acl as (
select
distinct name
from
aws_s3_bucket,
jsonb_array_elements(acl -> 'Grants') as grants
where
(grants -> 'Grantee' ->> 'URI' = 'http://acs.amazonaws.com/groups/global/AllUsers'
or grants -> 'Grantee' ->> 'URI' = 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
and (
grants ->> 'Permission' = 'FULL_CONTROL'
or grants ->> 'Permission' = 'READ_ACP'
or grants ->> 'Permission' = 'READ'
)
),read_access_policy as (
select
distinct name
from
aws_s3_bucket,
jsonb_array_elements(policy_std -> 'Statement') as s,
jsonb_array_elements_text(s -> 'Action') as action
where
s ->> 'Effect' = 'Allow'
and (
s -> 'Principal' -> 'AWS' = '["*"]'
or s ->> 'Principal' = '*'
)
and (
action = '*'
or action = '*:*'
or action = 's3:*'
or action ilike 's3:get%'
or action ilike 's3:list%'
)
)
select
b.arn as resource,
case
when (block_public_acls or a.name is null) and not bucket_policy_is_public then 'ok'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and block_public_policy) then 'ok'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and p.name is null) then 'ok'
else 'alarm'
end as status,
case
when (block_public_acls or a.name is null) and not bucket_policy_is_public then b.title || ' not publicly readable.'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and block_public_policy) then b.title || ' not publicly readable.'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and p.name is null) then b.title || ' not publicly readable.'
else b.title || ' publicly readable.'
end as reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join public_acl as a on b.name = a.name
left join read_access_policy as p on b.name = p.name;
EOQ
}
query "s3_bucket_restrict_public_write_access" {
sql = <<-EOQ
with public_acl as (
select
distinct name
from
aws_s3_bucket,
jsonb_array_elements(acl -> 'Grants') as grants
where
(grants -> 'Grantee' ->> 'URI' = 'http://acs.amazonaws.com/groups/global/AllUsers'
or grants -> 'Grantee' ->> 'URI' = 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers')
and (
grants ->> 'Permission' = 'FULL_CONTROL'
or grants ->> 'Permission' = 'WRITE_ACP'
or grants ->> 'Permission' = 'WRITE'
)
), write_access_policy as (
select
distinct name
from
aws_s3_bucket,
jsonb_array_elements(policy_std -> 'Statement') as s,
jsonb_array_elements_text(s -> 'Action') as action
where
s ->> 'Effect' = 'Allow'
and (
s -> 'Principal' -> 'AWS' = '["*"]'
or s ->> 'Principal' = '*'
)
and (
action = '*'
or action = '*:*'
or action = 's3:*'
or action ilike 's3:put%'
or action ilike 's3:delete%'
or action ilike 's3:create%'
or action ilike 's3:update%'
or action ilike 's3:replicate%'
or action ilike 's3:restore%'
)
)
select
b.arn as resource,
case
when (block_public_acls or a.name is null) and not bucket_policy_is_public then 'ok'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and block_public_policy) then 'ok'
when bucket_policy_is_public and p.name is null then 'ok'
else 'alarm'
end status,
case
when (block_public_acls or a.name is null ) and not bucket_policy_is_public then b.title || ' not publicly writable.'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and block_public_policy) then b.title || ' not publicly writable.'
when (block_public_acls or a.name is null) and (bucket_policy_is_public and p.name is null) then b.title || ' not publicly writable.'
else b.title || ' publicly writable.'
end reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join public_acl as a on b.name = a.name
left join write_access_policy as p on b.name = p.name;
EOQ
}
query "s3_bucket_versioning_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when versioning_enabled then 'ok'
else 'alarm'
end as status,
case
when versioning_enabled then name || ' versioning enabled.'
else name || ' versioning disabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_bucket_static_website_hosting_disabled" {
sql = <<-EOQ
select
arn as resource,
case
when website_configuration -> 'IndexDocument' ->> 'Suffix' is not null then 'alarm'
else 'ok'
end status,
case
when website_configuration -> 'IndexDocument' ->> 'Suffix' is not null then name || ' static website hosting enabled.'
else name || ' static website hosting disabled.'
end reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_public_access_block_account" {
sql = <<-EOQ
select
'arn' || ':' || 'aws' || ':::' || account_id as resource,
case
when block_public_acls
and block_public_policy
and ignore_public_acls
and restrict_public_buckets
then 'ok'
else 'alarm'
end as status,
case
when block_public_acls
and block_public_policy
and ignore_public_acls
and restrict_public_buckets
then 'Account level public access blocks enabled.'
else 'Account level public access blocks not enabled for: ' ||
concat_ws(', ',
case when not (block_public_acls ) then 'block_public_acls' end,
case when not (block_public_policy) then 'block_public_policy' end,
case when not (ignore_public_acls ) then 'ignore_public_acls' end,
case when not (restrict_public_buckets) then 'restrict_public_buckets' end
) || '.'
end as reason
${local.common_dimensions_global_sql}
from
aws_s3_account_settings;
EOQ
}
query "s3_public_access_block_bucket_account" {
sql = <<-EOQ
select
arn as resource,
case
when (bucket.block_public_acls or s3account.block_public_acls)
and (bucket.block_public_policy or s3account.block_public_policy)
and (bucket.ignore_public_acls or s3account.ignore_public_acls)
and (bucket.restrict_public_buckets or s3account.restrict_public_buckets)
then 'ok'
else 'alarm'
end as status,
case
when (bucket.block_public_acls or s3account.block_public_acls)
and (bucket.block_public_policy or s3account.block_public_policy)
and (bucket.ignore_public_acls or s3account.ignore_public_acls)
and (bucket.restrict_public_buckets or s3account.restrict_public_buckets)
then name || ' all public access blocks enabled.'
else name || ' not enabled for: ' ||
concat_ws(', ',
case when not (bucket.block_public_acls or s3account.block_public_acls) then 'block_public_acls' end,
case when not (bucket.block_public_policy or s3account.block_public_policy) then 'block_public_policy' end,
case when not (bucket.ignore_public_acls or s3account.ignore_public_acls) then 'ignore_public_acls' end,
case when not (bucket.restrict_public_buckets or s3account.restrict_public_buckets) then 'restrict_public_buckets' end
) || '.'
end as reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "bucket.")}
from
aws_s3_bucket as bucket,
aws_s3_account_settings as s3account
where
s3account.account_id = bucket.account_id;
EOQ
}
query "s3_bucket_default_encryption_enabled_kms" {
sql = <<-EOQ
with data as (
select
distinct name
from
aws_s3_bucket,
jsonb_array_elements(server_side_encryption_configuration -> 'Rules') as rules
where
rules -> 'ApplyServerSideEncryptionByDefault' ->> 'KMSMasterKeyID' is not null
)
select
b.arn as resource,
case
when d.name is not null then 'ok'
else 'alarm'
end status,
case
when d.name is not null then b.name || ' default encryption with KMS enabled.'
else b.name || ' default encryption with KMS disabled.'
end reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join data as d on b.name = d.name;
EOQ
}
query "s3_public_access_block_bucket" {
sql = <<-EOQ
select
arn as resource,
case
when block_public_acls
and block_public_policy
and ignore_public_acls
and restrict_public_buckets
then 'ok'
else 'alarm'
end as status,
case
when block_public_acls
and block_public_policy
and ignore_public_acls
and restrict_public_buckets
then name || ' all public access blocks enabled.'
else name || ' not enabled for: ' ||
concat_ws(', ',
case when not block_public_acls then 'block_public_acls' end,
case when not block_public_policy then 'block_public_policy' end,
case when not ignore_public_acls then 'ignore_public_acls' end,
case when not restrict_public_buckets then 'restrict_public_buckets' end
) || '.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket;
EOQ
}
query "s3_bucket_policy_restricts_cross_account_permission_changes" {
sql = <<-EOQ
with cross_account_buckets as (
select
distinct arn
from
aws_s3_bucket,
jsonb_array_elements(policy_std -> 'Statement') as s,
jsonb_array_elements_text(s -> 'Principal' -> 'AWS') as p,
string_to_array(p, ':') as pa,
jsonb_array_elements_text(s -> 'Action') as a
where
s ->> 'Effect' = 'Allow'
and (
pa [5] != account_id
or p = '*'
)
and a in (
's3:deletebucketpolicy',
's3:putbucketacl',
's3:putbucketpolicy',
's3:putencryptionconfiguration',
's3:putobjectacl'
)
)
select
a.arn as resource,
case
when b.arn is null then 'ok'
else 'alarm'
end as status,
case
when b.arn is null then title || ' restricts cross-account bucket access.'
else title || ' allows cross-account bucket access.'
end as reason
${local.tag_dimensions_sql}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "a.")}
from
aws_s3_bucket a
left join cross_account_buckets b on a.arn = b.arn;
EOQ
}
query "s3_bucket_object_logging_enabled" {
sql = <<-EOQ
with object_logging_cloudtrails as (
select
d ->> 'Type' as type,
replace(replace(v::text,'"',''),'/','') as bucket_arn
from
aws_cloudtrail_trail,
jsonb_array_elements(event_selectors) e,
jsonb_array_elements(e -> 'DataResources') as d,
jsonb_array_elements(d -> 'Values') v
where
d ->> 'Type' = 'AWS::S3::Object'
), object_logging_region as (
select
region as cloudtrail_region,
replace(replace(v::text,'"',''),'/','') as bucket_arn
from
aws_cloudtrail_trail,
jsonb_array_elements(event_selectors) e,
jsonb_array_elements(e -> 'DataResources') as d,
jsonb_array_elements(d -> 'Values') v
where
d ->> 'Type' = 'AWS::S3::Object'
and replace(replace(v::text,'"',''),'/','') = 'arn:aws:s3'
group by
region,
bucket_arn
),
object_logging_region_advance_es as (
select
region as cloudtrail_region
from
aws_cloudtrail_trail,
jsonb_array_elements(advanced_event_selectors) a,
jsonb_array_elements(a -> 'FieldSelectors') as f,
jsonb_array_elements_text(f -> 'Equals') e
where
e = 'AWS::S3::Object'
and f ->> 'Field' != 'eventCategory'
group by
region
)
select
distinct s.arn as resource,
case
when (s.arn = c.bucket_arn)
or (r.bucket_arn = 'arn:aws:s3' and r. cloudtrail_region = s.region )
or a. cloudtrail_region = s.region then 'ok'
else 'alarm'
end as status,
case
when (s.arn = c.bucket_arn)
or (r.bucket_arn = 'arn:aws:s3' and r. cloudtrail_region = s.region )
or a. cloudtrail_region = s.region then s.name || ' object logging enabled.'
else s.name || ' object logging not enabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket as s
left join object_logging_cloudtrails as c on s.arn = c.bucket_arn
left join object_logging_region as r on r. cloudtrail_region = s.region
left join object_logging_region_advance_es as a on a. cloudtrail_region = s.region;
EOQ
}
query "s3_bucket_policy_restrict_public_access" {
sql = <<-EOQ
with public_buckets as (
select
distinct arn as arn
from
aws_s3_bucket,
jsonb_array_elements(policy_std -> 'Statement') as s,
jsonb_array_elements_text(s -> 'Principal' -> 'AWS') as p
where
p = '*'
and s ->> 'Effect' = 'Allow'
)
select
b.arn as resource,
case
when b.policy_std is null then 'info'
when p.arn is not null then 'alarm'
else 'ok'
end as status,
case
when b.policy_std is null then title || ' does not have defined policy or insufficient access to the policy.'
when p.arn is not null then title || ' publicly accessible.'
else title || ' not publicly accessible.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_s3_bucket as b
left join public_buckets as p on p.arn = b.arn;
EOQ
}
query "s3_bucket_lifecycle_policy_enabled" {
sql = <<-EOQ
with lifecycle_rules_enabled as (
select
arn
from
aws_s3_bucket,
jsonb_array_elements(lifecycle_rules) as r
where
r ->> 'Status' = 'Enabled'
)
select
b.arn as resource,
case
when r.arn is not null then 'ok'
else 'alarm'
end status,
case
when r.arn is not null then name || ' lifecycle policy or rules configured.'
else name || ' lifecycle policy or rules not configured.'
end reason
${replace(local.tag_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join lifecycle_rules_enabled as r on r.arn = b.arn;
EOQ
}
query "s3_bucket_versioning_and_lifecycle_policy_enabled" {
sql = <<-EOQ
with lifecycle_rules_enabled as (
select
arn
from
aws_s3_bucket,
jsonb_array_elements(lifecycle_rules) as r
where
r ->> 'Status' = 'Enabled'
)
select
b.arn as resource,
case
when not versioning_enabled then 'alarm'
when versioning_enabled and r.arn is not null then 'ok'
else 'alarm'
end as status,
case
when not versioning_enabled then name || ' versioning diabled.'
when versioning_enabled and r.arn is not null then name || ' lifecycle policy configured.'
else name || ' lifecycle policy not configured.'
end as reason
${replace(local.tag_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "b.")}
from
aws_s3_bucket as b
left join lifecycle_rules_enabled as r on r.arn = b.arn;
EOQ
}
query "s3_bucket_event_notifications_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when
event_notification_configuration ->> 'EventBridgeConfiguration' is null
and event_notification_configuration ->> 'LambdaFunctionConfigurations' is null
and event_notification_configuration ->> 'QueueConfigurations' is null
and event_notification_configuration ->> 'TopicConfigurations' is null then 'alarm'
else 'ok'
end as status,