forked from mondoohq/cnspec-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mondoo-terraform-gcp-security.mql.yaml
2224 lines (1943 loc) · 82.1 KB
/
mondoo-terraform-gcp-security.mql.yaml
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
# Copyright (c) Mondoo, Inc.
# SPDX-License-Identifier: BUSL-1.1
policies:
- uid: mondoo-terraform-gcp-security
name: Terraform HCL Security Static Analysis for Google Cloud
version: 1.2.1
license: BUSL-1.1
tags:
mondoo.com/category: security
mondoo.com/platform: gcp,cloud,terraform
authors:
- name: Mondoo, Inc
email: [email protected]
docs:
desc: |
## Overview
This checks for security misconfigurations in Terraform HCL for Google Cloud.
## Local scan
Local scan refer to scans of files and operating systems where cnspec is installed.
### Scan a Terraform project
Open a terminal and run this command:
```bash
cnspec scan terraform /path/to/terraform/directory
```
## Join the community!
Our goal is to build policies that are simple to deploy, accurate, and actionable.
If you have any suggestions for how to improve this policy, or if you need support, [join the community](https://github.com/orgs/mondoohq/discussions) in GitHub Discussions.
groups:
- title: GCP BigQuery
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-bigquery-no-public-access
- title: GCP Identity and Access Management (IAM)
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-iam-no-folder-level-default-service-account-assignment
- uid: terraform-gcp-security-iam-no-folder-level-service-account-impersonation
- uid: terraform-gcp-security-iam-no-privileged-service-accounts
- title: GCP Cloud Storage
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-storage-enable-ubla
- uid: terraform-gcp-security-storage-no-public-access
- title: GCP Compute
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-compute-disk-encryption-customer-key
- uid: terraform-gcp-security-compute-disk-encryption-required
- uid: terraform-gcp-security-compute-enable-shielded-vm
- uid: terraform-gcp-security-compute-enable-vpc-flow-logs
- uid: terraform-gcp-security-compute-no-default-service-account
- uid: terraform-gcp-security-compute-no-ip-forwarding
- uid: terraform-gcp-security-compute-no-plaintext-vm-disk-keys
- uid: terraform-gcp-security-compute-no-public-ip
- title: GCP DNS
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-dns-enable-dnssec
- uid: terraform-gcp-security-dns-no-rsa-sha1
- title: GCP Google Kubernetes Engine (GKE)
filters: |
asset.platform == "terraform" || asset.platform == "terraform-hcl"
terraform.providers.any( nameLabel == "google" )
checks:
- uid: terraform-gcp-security-gke-enable-auto-repair
- uid: terraform-gcp-security-gke-enable-auto-upgrade
- uid: terraform-gcp-security-gke-enable-ip-aliasing
- uid: terraform-gcp-security-gke-enable-master-networks
- uid: terraform-gcp-security-gke-enable-network-policy
- uid: terraform-gcp-security-gke-enable-private-cluster
- uid: terraform-gcp-security-gke-enable-stackdriver-logging
- uid: terraform-gcp-security-gke-enable-stackdriver-monitoring
- uid: terraform-gcp-security-gke-metadata-endpoints-disabled
- uid: terraform-gcp-security-gke-no-basic-authentication
- uid: terraform-gcp-security-gke-no-client-cert-authentication
- uid: terraform-gcp-security-gke-no-public-control-plane
- uid: terraform-gcp-security-gke-node-metadata-security
- uid: terraform-gcp-security-gke-node-pool-uses-cos
- uid: terraform-gcp-security-gke-node-shielding-enabled
- uid: terraform-gcp-security-gke-use-cluster-labels
- uid: terraform-gcp-security-gke-use-rbac-permissions
- uid: terraform-gcp-security-gke-use-service-account
queries:
- uid: terraform-gcp-security-iam-no-folder-level-default-service-account-assignment
title: Roles should not be assigned to default service accounts
mql: |
terraform.resources.where( nameLabel == "google_folder_iam_member") {
arguments['member'] != /.+@appspot\.gserviceaccount\.com/ &&
arguments['member'] != /.+-compute@developer\.gserviceaccount\.com/ &&
arguments['member'] != /data\.google_compute_default_account/
}
docs:
desc: |
Default service accounts should not be used when granting access to folders as this can violate least privilege. It is recommended to use specialized service accounts instead.
Some Google Cloud services create default service accounts when you first enable the API in a Google Cloud project. By default, these service accounts are granted the Editor role (roles/editor) on the Cloud project, which allows them to read and modify all resources in the Cloud project. This amount of access isn't essential for the services to work: To access resources in your Cloud project, Google Cloud services use service agents, not the default service accounts.
audit: |
Check if `member` is configured to use default service accounts `[email protected]`, `appspot.gserviceaccount.com`, or if a `data.google_compute_default_service_account` is being used
```hcl
resource "google_folder_iam_member" "folder-123" {
folder = "folder-123"
role = "roles/my-role"
member = "[email protected]"
}
resource "google_folder_iam_member" "folder-456" {
folder = "folder-456"
role = "roles/my-role"
member = "[email protected]"
}
data "google_compute_default_service_account" "default" {
}
resource "google_folder_iam_member" "folder-789" {
folder = "folder-789"
role = "roles/my-role"
member = data.google_compute_default_service_account.default.id
}
```
remediation: |
Define a service account with least privilege for the role
```hcl
resource "google_service_account" "limited" {
account_id = "account123"
display_name = "account123"
}
resource "google_folder_iam_member" "folder-123" {
folder = "folder-123"
role = "roles/my-role"
member = "serviceAccount:${google_service_account.limited.email}"
}
```
- uid: terraform-gcp-security-iam-no-folder-level-service-account-impersonation
title: Users should not be granted service account access at the folder level
mql: |
terraform.resources.where( nameLabel == "google_folder_iam_binding") {
arguments['role'] != /iam\.serviceAccountUser/
}
docs:
desc: |
Users with service account access at the folder level can impersonate any service account. Instead, they should be given access to particular service accounts as required.
audit: |
Check if `role` is configured with `roles/iam.serviceAccountUser`
```hcl
resource "google_folder_iam_binding" "folder-123" {
folder = "folder-123"
role = "roles/iam.serviceAccountUser"
}
```
remediation: |
Define a custom role with least privilege
```hcl
resource "google_folder_iam_binding" "folder-123" {
folder = "folder-123"
role = "roles/custom-role"
}
```
- uid: terraform-gcp-security-iam-no-privileged-service-accounts
title: Service accounts should not have roles assigned with excessive privileges
mql: |
terraform.resources.where( nameLabel == "google_project_iam_member") {
arguments['role'] != /roles\/owner/ &&
arguments['role'] != /roles\/editor/
}
docs:
desc: |
Service accounts should have a minimal set of permissions assigned to accomplish their job. They should never have excessive access because if compromised, an attacker can escalate privileges and take over the entire account.
audit: |
Check if `role` is configured with basic roles: `roles/editor`, `roles/owner`
```hcl
resource "google_service_account" "test" {
account_id = "account123"
display_name = "account123"
}
resource "google_project_iam_member" "project" {
project = "your-project-id"
role = "roles/owner"
member = "serviceAccount:${google_service_account.test.email}"
}
```
remediation: |
Define a custom role with least privilege
```hcl
resource "google_service_account" "test" {
account_id = "account123"
display_name = "account123"
}
resource "google_project_iam_member" "project" {
project = "your-project-id"
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.test.email}"
}
```
- uid: terraform-gcp-security-storage-no-public-access
title: Ensure that Cloud Storage bucket is not publicly accessible
mql: |
terraform.resources.where( nameLabel == "google_storage_bucket_iam_binding") {
attributes['members']['value'] { _ != /allUsers/ && _ != /allAuthenticatedUsers/}
}
docs:
desc: |
Google Cloud Storage buckets that define 'allUsers' or 'allAuthenticatedUsers' as members in an IAM member/binding causes data to be exposed outside of the organization. This can lead to exposure of sensitive data. The recommended approach is to restrict public access.
audit: |
Check if `members` is configured with `allAuthenticatedUsers` or `allUsers`
```hcl
resource "google_storage_bucket_iam_binding" "allAuthenticatedUsers" {
bucket = google_storage_bucket.default.name
role = "roles/storage.admin"
members = [
"allAuthenticatedUsers",
]
}
resource "google_storage_bucket_iam_binding" "allUsers" {
bucket = google_storage_bucket.default.name
role = "roles/storage.admin"
members = [
"allUsers",
]
}
```
remediation: |
Restrict public access to the bucket.
```hcl
resource "google_storage_bucket_iam_binding" "binding" {
bucket = google_storage_bucket.default.name
role = "roles/storage.admin"
members = [
"user:[email protected]",
]
}
```
- uid: terraform-gcp-security-storage-enable-ubla
title: Ensure that Cloud Storage buckets have uniform bucket-level access enabled
mql: |
terraform.resources.where( nameLabel == "google_storage_bucket") {
arguments['uniform_bucket_level_access'] == true
}
docs:
desc: |
Google Cloud Storage buckets should be configured with uniform bucket-level access.
When you enable uniform bucket-level access on a bucket, Access Control Lists (ACLs) are disabled, and only bucket-level Identity and Access Management (IAM) permissions grant access to that bucket and the objects it contains. You revoke all access granted by object ACLs and the ability to administrate permissions using bucket ACLs.
audit: |
Check if `uniform_bucket_level_access` is set to `true`
```hcl
resource "google_storage_bucket" "static-site" {
name = "image-store.com"
location = "EU"
force_destroy = true
uniform_bucket_level_access = false
website {
main_page_suffix = "index.html"
not_found_page = "404.html"
}
cors {
origin = ["http://image-store.com"]
method = ["GET", "HEAD", "PUT", "POST", "DELETE"]
response_header = ["*"]
max_age_seconds = 3600
}
}
```
remediation: |
Configure `uniform_bucket_level_access` to `true`
```hcl
resource "google_storage_bucket" "static-site" {
name = "image-store.com"
location = "EU"
force_destroy = true
uniform_bucket_level_access = true
website {
main_page_suffix = "index.html"
not_found_page = "404.html"
}
cors {
origin = ["http://image-store.com"]
method = ["GET", "HEAD", "PUT", "POST", "DELETE"]
response_header = ["*"]
max_age_seconds = 3600
}
}
```
- uid: terraform-gcp-security-compute-no-public-ip
title: Compute instances should not be publicly exposed to the internet
mql: |
terraform.resources.where( nameLabel == "google_compute_instance") {
blocks.where( type == "network_interface") {
blocks.where( type == "access_config") {
arguments.values.length != 0
}
}
}
docs:
desc: |
Google Cloud compute instances that have a public IP address are exposed on the internet and are at risk to attack.
audit: |
Check if the `access_config` is empty.
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
}
```
remediation: |
Configure compute instance without empty `access_config`
```hcl
resource "google_compute_instance" "good_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
network_interface {
network = "default"
}
}
```
- uid: terraform-gcp-security-compute-disk-encryption-customer-key
title: Disks should be encrypted with Customer Supplied Encryption Keys
mql: |
terraform.resources.where( nameLabel == "google_compute_disk" ) {
blocks.one( type == "disk_encryption_key")
}
terraform.resources.where( nameLabel == "google_compute_disk" && blocks.one( type == "disk_encryption_key") ) {
blocks.where( type == "disk_encryption_key") {
arguments != ""
}
}
docs:
desc: |
Google Cloud compute instances should use disk encryption using a customer-supplied encryption key. If you do not provide an encryption key when creating the disk, then the disk will be encrypted using an automatically generated key, and you do not need to provide the key to use the disk later.
audit: |
Check if `disk_encryption_key` key is defined and that the arguments are not empty strings.
```hcl
resource "google_compute_disk" "bad_example" {
name = "test-disk"
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-9-stretch-v20200805"
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
}
```
remediation: |
Configure compute instance with `disk_encryption_key` and `kms_key_self_link` defined.
```hcl
resource "google_compute_disk" "good_example" {
name = "test-disk"
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-9-stretch-v20200805"
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
disk_encryption_key {
kms_key_self_link = "something"
}
}
```
- uid: terraform-gcp-security-compute-disk-encryption-required
title: Disk encryption Keys should not be passed as plaintext
mql: |
terraform.resources.where( nameLabel == "google_compute_disk" && blocks.one( type == "disk_encryption_key") ) {
blocks.where( type == "disk_encryption_key") {
arguments.keys[0] != "raw_key"
}
}
docs:
desc: |
Google Cloud compute instances should use disk encryption using a customer-supplied encryption key. One of the options is for the `disk_encryption_key` is `raw_key`, which is the key in plaintext.
Sensitive values such as raw encryption keys should not be included in your Terraform code and should be stored securely by a secrets manager.
audit: |
Check if the `access_config` is empty
```hcl
resource "google_compute_disk" "good_example" {
disk_encryption_key {
raw_key="b2ggbm8gdGhpcyBpcyBiYWQ="
}
}
```
remediation: |
Configure compute instance with `disk_encryption_key` and `kms_key_self_link` defined
```hcl
resource "google_compute_disk" "good_example" {
disk_encryption_key {
kms_key_self_link = google_kms_crypto_key.my_crypto_key.id
}
}
```
- uid: terraform-gcp-security-compute-enable-shielded-vm
title: Verify shielded VM is enabled on compute instances
mql: |
terraform.resources.where( nameLabel == "google_compute_instance" ) {
blocks.one( type == "shielded_instance_config" )
}
terraform.resources.where( nameLabel == "google_compute_instance" && blocks.one( type == "shielded_instance_config" )) {
blocks.where( type == "shielded_instance_config") {
attributes['enable_vtpm'] == null || attributes['enable_vtpm']['value'] == true
}
}
terraform.resources.where( nameLabel == "google_compute_instance" && blocks.one( type == "shielded_instance_config" )) {
blocks.where( type == "shielded_instance_config") {
attributes['enable_integrity_monitoring'] == null || attributes['enable_integrity_monitoring']['value'] == true
}
}
docs:
desc: |
Shielded VMs are virtual machines (VMs) on Google Cloud hardened by a set of security controls that help defend against rootkits and bootkits. Using Shielded VMs helps protect enterprise workloads from threats like remote attacks, privilege escalation, and malicious insiders. Shielded VMs leverage advanced platform security capabilities such as secure and measured boot, a virtual trusted platform module (vTPM), UEFI firmware, and integrity monitoring.
**Secure Boot** helps ensure that the system only runs authentic software by verifying the digital signature of all boot components, and halting the boot process if signature verification fails.
**Integrity monitoring** helps you understand and make decisions about the state of your VM instances. Integrity monitoring compares the most recent boot measurements to the integrity policy baseline and returns a pair of pass/fail results depending on whether they match or not, one for the early boot sequence and one for the late boot sequence.
audit: |
Check if the `shielded_instance_config` is configured on the instance, and if `enable_vtpm` and `enable_integrity_monitoring` are set to `false`
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
shielded_instance_config {
enable_vtpm = false
enable_integrity_monitoring = false
}
}
```
remediation: |
Configure `shielded_instance_config` without `enable_vtpm` and `enable_integrity_monitoring`, or configure `enable_vtpm` and `enable_integrity_monitoring` explicitly to `true`
```hcl
resource "google_compute_instance" "good_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
shielded_instance_config {
enable_vtpm = true
enable_integrity_monitoring = true
}
}
```
- uid: terraform-gcp-security-compute-enable-vpc-flow-logs
title: Verify VPC flow logs enabled on compute instances
mql: |
terraform.resources.where( nameLabel == "google_compute_subnetwork" && arguments['purpose'] != "INTERNAL_HTTPS_LOAD_BALANCER" ) {
blocks.one( type == "log_config")
}
docs:
desc: |
VPC flow logs record information about all traffic, which is a vital tool in reviewing anomalous traffic. Google Compute Engine subnetworks that do not have VPC flow logs enabled have limited information for auditing and awareness.
Note: Google Compute Engine subnets configured as INTERNAL_HTTPS_LOAD_BALANCER do not support VPC flow logs. Compute subnetworks with `purpose INTERNAL_HTTPS_LOAD_BALANCER` attribute will not be evaluated.
audit: |
The following example will fail:
```hcl
resource "google_compute_subnetwork" "bad_example" {
name = "test-subnetwork"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.custom-test.id
secondary_ip_range {
range_name = "tf-test-secondary-range-update1"
ip_cidr_range = "192.168.10.0/24"
}
}
resource "google_compute_network" "custom-test" {
name = "test-network"
auto_create_subnetworks = false
}
```
remediation: |
The following example will pass:
```hcl
resource "google_compute_subnetwork" "good_example" {
name = "test-subnetwork"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.custom-test.id
secondary_ip_range {
range_name = "tf-test-secondary-range-update1"
ip_cidr_range = "192.168.10.0/24"
}
log_config {
aggregation_interval = "INTERVAL_10_MIN"
flow_sampling = 0.5
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_network" "custom-test" {
name = "test-network"
auto_create_subnetworks = false
}
```
- uid: terraform-gcp-security-compute-no-default-service-account
title: Compute instances should not use the default service account
mql: |
terraform.resources.where( nameLabel == "google_compute_instance" && blocks.one( type == "service_account") ) {
blocks.where( type == "service_account" ) {
attributes['email'] != empty
}
}
terraform.resources.where( nameLabel == "google_compute_instance" && blocks.one( type == "service_account") ) {
blocks.where( type == "service_account" ) {
attributes['email'] != /.+-compute@developer\.gserviceaccount.com/
}
}
docs:
desc: |
The default service account has full project access. Provisioning instances using the default service account gives the instance full access to the project. Compute instances should instead be assigned the minimal access they need.
audit: |
The following example will fail:
```hcl
resource "google_compute_instance" "default" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = "[email protected]"
scopes = ["cloud-platform"]
}
}
```
remediation: |
The following example will pass:
```hcl
resource "google_service_account" "default" {
account_id = "service_account_id"
display_name = "Service Account"
}
resource "google_compute_instance" "default" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata = {
foo = "bar"
}
metadata_startup_script = "echo hi > /test.txt"
service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
```
- uid: terraform-gcp-security-compute-no-ip-forwarding
title: Compute instances should be configured with IP forwarding
mql: |
terraform.resources.where( nameLabel == "google_compute_instance" && attributes['can_ip_forward']) {
attributes['can_ip_forward']['value'] == false
}
docs:
desc: |
Disabling IP forwarding ensures the instance can only receive packets addressed to the instance and can only send packets with a source address of the instance.
The attribute `can_ip_forward` is optional on `google_compute_instance` and defaults to `false`. Instances with `can_ip_forward = true` will fail.
audit: |
The following example will fail:
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
can_ip_forward = false
}
```
remediation: |
The following example will pass:
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
// Local SSD disk
scratch_disk {
interface = "SCSI"
}
can_ip_forward = false
}
```
- uid: terraform-gcp-security-compute-no-plaintext-vm-disk-keys
title: VM disk encryption keys should not be provided in plaintext
mql: |
terraform.resources.where( nameLabel == "google_compute_instance" ) {
blocks { arguments.keys { _ != 'disk_encryption_key_raw' } }
}
docs:
desc: |
Providing your encryption key in plaintext format means anyone with access to the source code also has access to the key.
When encrypting a `boot_disk`, it is not recommended to use the `disk_encryption_key_raw` argument as this passes the key in plaintext, which is not secure. Consider using `kms_key_self_link` or a secrets manager instead.
audit: |
The following example will fail:
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
disk_encryption_key_raw = "something"
}
}
```
remediation: |
The following example will pass:
```hcl
resource "google_compute_instance" "bad_example" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
kms_key_self_link = "kmsKeyName"
}
}
```
- uid: terraform-gcp-security-bigquery-no-public-access
title: BigQuery datasets should only be accessible within the organization
mql: |
terraform.resources.where( nameLabel == "google_bigquery_dataset" ) {
blocks { arguments.values.none("allAuthenticatedUsers") }
}
docs:
desc: |
BigQuery datasets should not be configured to provide access to `allAuthenticatedUsers` as this provides any authenticated GCP user, even those outside of your organization, access to your BigQuery dataset. This can lead to exposure of sensitive data to the public internet.
Configure access permissions with higher granularity and least privilege principles.
audit: |
The following example will fail:
```hcl
resource "google_bigquery_dataset" "bad_example" {
dataset_id = "example_dataset"
friendly_name = "test"
description = "This is a test description"
location = "EU"
default_table_expiration_ms = 3600000
labels = {
env = "default"
}
access {
role = "OWNER"
special_group = "allAuthenticatedUsers"
}
access {
role = "READER"
domain = "hashicorp.com"
}
}
```
remediation: |
The following example will pass:
```hcl
resource "google_bigquery_dataset" "good_example" {
dataset_id = "example_dataset"
friendly_name = "test"
description = "This is a test description"
location = "EU"
default_table_expiration_ms = 3600000
labels = {
env = "default"
}
access {
role = "OWNER"
user_by_email = google_service_account.bqowner.email
}
access {
role = "READER"
domain = "hashicorp.com"
}
}
resource "google_service_account" "bqowner" {
account_id = "bqowner"
}
```
- uid: terraform-gcp-security-dns-enable-dnssec
title: Cloud DNS should use DNSSEC
mql: |
terraform.resources.where( nameLabel == "google_dns_managed_zone" ) {
blocks.where( type == "dnssec_config" ) {
attributes['state']['value'] != "off"
}
}
docs:
desc: |
DNSSEC authenticates DNS responses, preventing MITM attacks and impersonation. Unverified DNS responses could lead to man-in-the-middle attacks.
audit: |
The following example will fail:
```hcl
resource "google_dns_managed_zone" "bad_example" {
name = "example-zone"
dns_name = "example-${random_id.rnd.hex}.com."
description = "Example DNS zone"
labels = {
foo = "bar"
}
dnssec_config {
state = "off"
}
}
resource "random_id" "rnd" {
byte_length = 4
}
```
remediation: |
The following example will pass:
```hcl
resource "google_dns_managed_zone" "good_example" {
name = "example-zone"
dns_name = "example-${random_id.rnd.hex}.com."
description = "Example DNS zone"
labels = {
foo = "bar"
}
dnssec_config {
state = "on"
}
}
resource "random_id" "rnd" {
byte_length = 4
}
```
- uid: terraform-gcp-security-dns-no-rsa-sha1
title: Zone signing should not use RSA SHA1
mql: |
terraform.datasources.where( nameLabel == "google_dns_keys" ) {
blocks { attributes['algorithm']['value'] != "rsasha1" }
}
docs:
desc: |
RSA SHA1 is a weaker algorithm than SHA2-based algorithms such as RSA SHA256/512.
audit: |
The following example will fail:
```hcl
resource "google_dns_managed_zone" "foo" {
name = "foobar"
dns_name = "foo.bar."
dnssec_config {
state = "on"
non_existence = "nsec3"
}
}
data "google_dns_keys" "foo_dns_keys" {
managed_zone = google_dns_managed_zone.foo.id
zone_signing_keys {
algorithm = "rsasha1"
}
}
output "foo_dns_ds_record" {
description = "DS record of the foo subdomain."
value = data.google_dns_keys.foo_dns_keys.key_signing_keys[0].ds_record
}
```
remediation: |
The following example will pass:
```hcl
resource "google_dns_managed_zone" "foo" {
name = "foobar"
dns_name = "foo.bar."
dnssec_config {
state = "on"
non_existence = "nsec3"
}
}
data "google_dns_keys" "foo_dns_keys" {
managed_zone = google_dns_managed_zone.foo.id
zone_signing_keys {
algorithm = "rsasha512"
}
}
output "foo_dns_ds_record" {
description = "DS record of the foo subdomain."
value = data.google_dns_keys.foo_dns_keys.key_signing_keys[0].ds_record
}
```
- uid: terraform-gcp-security-gke-enable-auto-repair
title: Kubernetes should have 'Automatic repair' enabled
mql: |
terraform.resources.where( nameLabel == "google_container_node_pool" ) {
blocks.where( type == "management") {
arguments['auto_repair'] != false
}
}
docs:
desc: |
Automatic repair will monitor nodes and attempt repair when a node fails multiple subsequent health checks. Failing nodes will require manual repair.
audit: |