-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitlab.tf.json.template.py
2281 lines (2234 loc) · 113 KB
/
gitlab.tf.json.template.py
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
from collections.abc import (
Iterable,
)
import ipaddress
from itertools import (
chain,
)
import json
from more_itertools import (
nth,
)
import yaml
from azul import (
config,
)
from azul.collections import (
dict_merge,
)
from azul.deployment import (
aws,
)
from azul.docker import (
resolve_docker_image_for_pull,
)
from azul.strings import (
departition,
double_quote as dq,
join_lines as jl,
join_words as jw,
single_quote as sq,
)
from azul.terraform import (
chalice,
emit_tf,
vpc,
)
# This Terraform config creates a single EC2 instance with a bunch of Docker
# containers running on it:
#
# ╔══════════════════════════════════════════════════════════════════════════════════════════════════╗
# ║ gitlab ║
# ║ ║
# ║ ┏━━━━━━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ║
# ║ ┃ gitlab ┃ ┃ gitlab-runner ┃ ┃ gitlab-dind ┃ ║
# ║ ┃ gitlab/gitlab-ce ┃ ┃ gitlab/gitlab-runner ┃ ┃ library/docker ┃ ║
# ║ ┃ ┃ ┃ ┃ ┃ ┃ ║
# ║ ┃ ┌──────────┐ ┃ ┃ ┌───────────────┐ ┃ ┃ ┌────────────┐ ┃ ║
# ║ ┃ │ rails │─ ╋ ▶┃ │ gitlab-runner │────╋──╋───▶ 2375│ dockerd │docker.sock ◀─┐ ┃ ║
# ╔═════╗ ║ ┃ └──────────┘ ┃ ┃ └───┬───────┬───┘ ┃ ┃ └────────────┘ │ ┃ ║
# ║ ALB ║ ║ ┃ ┌──────────┐ ┃ ┃ │ ┃ ┃ │ ┃ ║
# ┌──▶ 443╠ ─ ─ ╬──╬──╋───▶ 80│ nginx │ ┃ ┗━━━━━━━╋━━━━━━━╋━━━━━━━━┛ ┃ ┏━━━━━━━━━━━━━━━━━━┓ │ ┃ ║
# │ ╚═════╝ ║ ┃ └──────────┘ ┃ │ ▲ ┃ ┃ "executor" ┃ │ ┃ ║
# │ ╔═════╗ ║ ┃ ┌──────────┐ ┃ │ │ ┃ ┃ ucsc/azul/runner ┃ │ ┃ ║
# │ 22╠ ─ ─ ╬──╬──╋─▶ 2222│ gitaly │ ┃ │ │ ─ ─ ─ ─ ─ ─┃─ ─ ─▶┃ ┃ │ ┃ ║
# │ ║ ║ ║ ┃ └──────────┘ ┃◀─ ─ ─ │ ┃ ┃ ┌────────────┐ ┃ │ ┃ ║
# │ ║ ║ ║ ┃ ┃ │ │ │ ┃ ─ ╋ ─│ make │ ┃ │ ┃ ║
# │ ║ NLB ║ ║ ┗━━━━━━━━━━━━━━━━━━━━━┛ │ ┃ │ ┃ └────────────┘ ┃ │ ┃ ║
# │ ║ ║ ║ │ │ │ ┃ ┃ ┌────────────┐ ┃ │ ┃ ║
# │ ║ ║ ║ ┌─────────┐ │ ┃ │ ┃ │ docker │──╋───────────┤ ┃ ║
# │ 2222╠ ─ ─ ╬──╬──────────────▶ 22│ sshd │ │ │ │ ┃ ┃ └────────────┘ ┃ │ ┃ ║
# │ ╚═════╝ ║ └─────────┘ │ ┃ │ ┗━━━━━━━━━━━━━━━━━━┛ │ ┃ ║
# │ ║ ┌─────────┐ │ │ │ ┃ ┏━━━━━━━━━━━━━━━━━━┓ │ ┃ ║
# │ ║ ┌─▶ docker.sock│ dockerd │ │ ┃ │ ┃ "build" ┃ │ ┃ ║
# │ ║ │ └─────────┘ │ │ │ ┃ ┃ ucsc/azul/dev ┃ │ ┃ ║
# │ ║ │ ┌─────────┐ │ ┃ │ ┃ ┃ │ ┃ ║
# │ ║ └──────────────│ systemd │─ ┴ ─ ┼ ─ ┴ ─ ─ ─ ─ ─ ─ ─▶┃ ┃ ┌────────────┐ ┃ │ ┃ ║
# │ ║ └─────────┘ │ ┃ │ ┃ │ make │ ┃ │ ┃ ║
# │ ║ │ ┃ ─▶┃ └────────────┘ ┃ │ ┃ ║
# └────────────────╬───────────────────────────────────┘ ┃ ┃ ┌────────────┐ ┃ │ ┃ ║
# ║ ┃ ┃ │ ├──╋───────────┘ ┃ ║
# ║ ┃ ┌────╋──│ python │ ┃ ┃ ║
# ║ ┃ │ ┃ │ ├ ─┃─ ┐ ┃ ║
# ║ ┃ │ ┃ └────────────┘ ┃ ┃ ║
# ║ ┃ │ ┗━━━━━━━━━━━━━━━━━━┛ │ ┃ ║
# ║ ┃ │ ┏━━━━━━━━━━━━━━━━━━┓ ┃ ║
# ║ ┃ │ ┃ elasticsearch ┃ │ ┃ ║
# ║ ┃ │ ┃ ┌────────────┐ ┃ ┃ ║
# ║ ┃ └─▶ 9200│ java │ ┃◀ ┘ ┃ ║
# ║ ┃ ┃ └────────────┘ ┃ ┃ ║
# ║ ┃ ┗━━━━━━━━━━━━━━━━━━┛ ┃ ║
# ║ ┃ ┃ ║
# ║ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ║
# ║ ║
# ╚══════════════════════════════════════════════════════════════════════════════════════════════════╝
#
# ╔════════════╗ ┏━━━━━━━━━━━┓ ┌───────────┐
# Legend: ║ instance ║ ┃ container ┃ │ process │ ───interact──▶ ─ ─ run ─ ─ ▶
# ╚════════════╝ ┗━━━━━━━━━━━┛ └───────────┘
#
# The instance is fronted by two AWS load balancers:
#
# 1) an application load balancer (ALB) that terminates SSL and forwards to the
# Gitlab web UI
#
# 2) an network load balancer that forwards port 22 to an SSH daemon in the
# Gitlab container (for git+ssh://) and port 2222 to an SSH daemon for shell
# access in RancherOS' `console` container.
#
# The instance itself does not have a public IP and is only reachable from the
# internet through the load balancers.
#
# The NLB's public IP is bound to ssh.gitlab.
# {dev,prod}.singlecell.gi.ucsc.edu The ALB's public IP is bound to gitlab.
# {dev,prod}.singlecell.gi.ucsc.edu To log into the instance run `ssh
# [email protected] -p 2222`. Your SSH key must be
# mentioned in public_key or other_public_keys below.
#
# The Gitlab web UI is at https://gitlab.{dev,prod}.singlecell.gi.ucsc.edu/. It
# is safe to destroy all resources in this TF config. You can always build them
# up again. The only golden egg is the EBS volume that's attached to the
# instance. See below under ebs_volume_name. RancherOS was chosen for the AMI
# because it has Docker pre installed and supports cloud-init user data.
#
# The container wiring is fairly complicated as it involves docker-in-docker. It
# is inspired by
#
# https://medium.com/@tonywooster/docker-in-docker-in-gitlab-runners-220caeb708ca
#
# In this setup the build container is not privileged while allowing for image
# layer caching between builds. The `elasticsearch` and `dynamodb-local`
# containers are included as examples of test fixtures launched during test
# setup. This aspect may evolve over time. It's worth noting that these fixture
# containers are siblings of the build container. When the tests are run
# locally or on Travis, the tests run on the host. The above diagram also
# glosses over the fact that there are multiple separate bridge networks
# involved. The `gitlab-dind` and `gitlab-runner` containers are attached to a
# separate bridge network. The `gitlab` container is on the default bridge
# network. IMPORTANT: There is a bug in the Terraform AWS provider (I think
# it's conflating the listeners) which causes one of the NLB listeners to be
# missing after `terraform apply`.
# The name of an EBS volume to attach to the instance. This EBS volume must
# exist, be encrypted, and be formatted with ext4. We don't manage the volume in
# Terraform because that would require formatting it once after creation. That
# can only be one after attaching it to an EC2 instance but before mounting it.
# This turns out to be difficult and risks overwriting existing data on the
# volume. We'd also have to prevent the volume from being deleted during
# `terraform destroy`.
#
# If this EBS volume does not exist you must create it with the desired size
# before running Terraform. For example:
#
# aws ec2 create-volume \
# --encrypted \
# --size 100 \
# --availability-zone "${AWS_DEFAULT_REGION}a" \
# --tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=azul-gitlab},{Key=owner,[email protected]}]'
#
# To then format the volume, you can then either attach it to some other Linux
# instance and format it there or use `make terraform` to create the actual
# Gitlab instance and attach the volume. For the latter you would need to ssh
# into the Gitlab instance, format `/dev/xvdf` (`/dev/nvme1n1` on newer
# instance types) and reboot the instance. For example:
#
# docker stop gitlab-runner
# docker stop gitlab
# docker stop gitlab-dind
# sudo mv /mnt/gitlab /mnt/gitlab.deleteme
# sudo mkdir /mnt/gitlab
# sudo mkfs.ext4 /dev/nvme1n1
# sudo reboot
# sudo rm -rf /mnt/gitlab.deleteme
#
# The EBS volume should be backed up (EBS snapshot) periodically. Not only does
# it contain Gitlab's data but also its config.
#
ebs_volume_name = 'azul-gitlab'
num_zones = 2 # An ALB needs at least two availability zones
# List of port forwardings by the network load balancer (NLB). The first element
# in the tuple is the port on the external interface of the NLB, the second
# element is the port on the instance the NLB forwards to.
#
nlb_ports = [(22, 2222, 'git'), (2222, 22, 'ssh')]
# The Azul Gitlab instance uses one VPC. This variable specifies the IPv4
# address block to be used by that VPC.
#
# Be sure to avoid the default Docker address pool:
#
# https://github.com/docker/libnetwork/blob/a79d3687931697244b8e03485bf7b2042f8ec6b6/ipamutils/utils.go#L10
#
all_ipv4 = '0.0.0.0/0'
vpc_cidr = config.vpc_cidr
vpn_subnet = config.vpn_subnet
split_tunnel = not config.deployment.is_stable
# The public key of that keypair
#
administrator_key = (
'ssh-rsa'
' '
'AAAAB3NzaC1yc2EAAAADAQABAAABAQDhRBbejN2qT5+6nfpzxPTfTFuSDSiPrAyDKH+V/A9+Xw4ZT8Z3K4d0w0KlwjtRZ'
'7shmIxkN44DY8R8LGCiybYHHVHqRNoQYqY1BkfSSP8h+eTylo4kRE4hKzs97dsBKYN1iXYXxd9yJGf6u3iR51LFijNLNN'
'6QEsxC6PhBReye21X8KdrlOO1owG3D+BVF6Q8PxpBFTjwMLiJUe3hm/vNTrCJErtHAr6ok28BY7rj3UVbGscrnsMIpdsX'
'OFDl5NU7tB6H9HlQ46l/W70ZSpzx8FQel9kbxcjZLinmsujuILC2bI1ev4EcdTRXo9SHo5VLPnE9J2f6StlqbBYJpbdOl'
' '
)
operator_keys = [
(
'ssh-rsa'
' '
'AAAAB3NzaC1yc2EAAAADAQABAAABgQCrIU25zlzHBxIdEATJZsGXvatdWuen5zlOw1uE25spQ8eNnOUfbz5fR'
'yiQqyMNxE/dX2hCCDT1mr5Flke4uJ0FayC/l5ZC3bKYE2gnILbZBNsFuueZuDy9pRmZ+eTYs3vKXN361+loRi'
'6ag8h/pOQCvx6oO5NrVSBse0NcEn1tk1h7C1hOf8sblW17+OO9aDQJAA7G4PJw2kBRCYYEwDNLBRy3k1wBdcK'
'G2t2SuVh+PCpmMPA5/i/raDUqATO1H3bcRubtyGHNbAtihL5HLZK83O9fHVf/MD7il4N/9OwBNpOwvc2gi9zp'
'ChGpbl5jA2ZfoEDEOhX4ffOD1UwmkmkoUC82BvHyAwdnqgh3Nk4qCum53TsMhXVWMW/8tr/t+AxjE3/Acwj6H'
'VMz2j+67A0p1oaTbxBXdf00BmAYV2xPZNg8Fa2/AkQWPt4c4JJnktVjWM8/PU1h6FamyHfQ6pNmi+j6rHz9UZ'
'e1Zt6WybGr+Tt+KifhbCnZQkg74I1uT6M='
' '
),
(
'ssh-rsa'
' '
'AAAAB3NzaC1yc2EAAAADAQABAAABAQDDPUVio1tlAstsaM2Da7QfSIv0zMU7JwjO7a/BvsWg0tXES'
'gpL59i5QcycpYq6q7naF+N0co325e/OJ4lzi13T5xojSbh/kNETwiI+aJ9f0GxwnygcvVUpsTlH3X01fR+1xm'
'rlGWi8AhEfbFyAFaqb2i+Whbkt9/oa3EIv4l+OSH6VSRtKRE56IvJ06hnWQ3yR57wxRBnHjiUuEBQ5I0jsye3'
'0OE0USvjfbHqjbR9zyKCgnGuf/fY4aC+oimHu6/FSS3Q8+f5BtRrUjcYvddbAHnzrx08csztCx3s7iA5qUdhr'
'W07wIjyG7vfB9Y70CDNsfi1Zo/Ff+IMKSzPtasXx'
' '
)
]
# FIXME: Launch GitLab, DinD & runner images using image ID
# https://github.com/DataBiosphere/azul/issues/5960
clamav_image, _ = resolve_docker_image_for_pull('clamav')
dind_image, _ = resolve_docker_image_for_pull('dind')
gitlab_image, _ = resolve_docker_image_for_pull('gitlab')
runner_image, _ = resolve_docker_image_for_pull('gitlab_runner')
# For instructions on finding the latest CIS-hardened AMI, see
# OPERATOR.rst#upgrading-linux-ami
#
# CIS Amazon Linux 2 Kernel 4.14 Benchmark - Level 1 - v09 -4c096026-c6b0-440c-bd2f-6d34904e4fc6
#
ami_id = {
'us-east-1': 'ami-080cfaeb213b9f981'
}
gitlab_mount = '/mnt/gitlab'
vpc_dns_servers = [
# https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html#AmazonDNS
str(nth(ipaddress.ip_network(vpc_cidr).hosts(), 1)),
'169.254.169.253'
]
vpc_dns_docker_flags = [f'--dns {s}' for s in vpc_dns_servers]
aws_managed_buckets_for_ssm_agent = [
f'{aws.region_name}-birdwatcher-prod',
f'amazon-ssm-{aws.region_name}',
f'amazon-ssm-packages-{aws.region_name}',
f'aws-patchmanager-macos-{aws.region_name}',
f'aws-ssm-{aws.region_name}',
f'aws-ssm-document-attachments-{aws.region_name}',
f'aws-windows-downloads-{aws.region_name}',
f'patch-baseline-snapshot-{aws.region_name}'
]
def merge(sets: Iterable[Iterable[str]]) -> Iterable[str]:
return sorted(set(chain(*sets)))
emit_tf({} if config.terraform_component != 'gitlab' else {
'data': {
'aws_sns_topic': {
'monitoring': {
'name': aws.monitoring_topic_name
}
},
'aws_availability_zones': {
'available': {}
},
'aws_acm_certificate': {
'gitlab_vpn': {
'domain': 'azul-gitlab-vpn-server-' + config.deployment_stage
}
},
'aws_ebs_volume': {
'gitlab': {
'filter': [
{
'name': 'volume-type',
'values': ['gp2']
},
{
'name': 'tag:Name',
'values': [ebs_volume_name]
}
],
'most_recent': True
}
},
# This Route53 zone also has to exist.
'aws_route53_zone': {
'gitlab': {
'name': config.domain_name + '.',
'private_zone': False
}
},
'aws_s3_bucket': {
'logs': {
'bucket': aws.logs_bucket,
}
},
'aws_iam_policy_document': {
# This policy is really close to the policy size limit, if you get
# LimitExceeded: Cannot exceed quota for PolicySize: 6144, you need
# to strip the existing policy down by essentially replacing the
# calls to the helper functions like allow_service() with a
# hand-curated list of actions, potentially by starting from a copy
# of the template output.
'gitlab_boundary': {
'statement': [
{
'actions': [
's3:ListAllMyBuckets',
's3:GetAccountPublicAccessBlock',
's3:ListBucket'
],
'resources': [
'*'
]
},
{
'actions': [
's3:*'
],
'resources': merge(
[
f'arn:aws:s3:::{bucket_name}',
f'arn:aws:s3:::{bucket_name}/*'
] for bucket_name in [
aws.qualified_bucket_name('*'),
*aws_managed_buckets_for_ssm_agent
]
)
},
{
'actions': [
'kms:ListAliases',
'kms:ListKeys'
],
'resources': [
'*'
]
},
{
'actions': [
'kms:GetKeyRotationStatus',
'kms:ListRetirableGrants',
'kms:ListResourceTags',
'kms:ListAliases',
'kms:GetKeyPolicy',
'kms:ListKeys',
'kms:ListGrants',
'kms:ListKeyPolicies',
'kms:GetParametersForImport',
'kms:DescribeKey',
'kms:GenerateMac',
'kms:VerifyMac'
],
'resources': [
f'arn:aws:kms:{aws.region_name}:{aws.account}:key/*',
f'arn:aws:kms:{aws.region_name}:{aws.account}:alias/*'
]
},
{
'actions': [
'sqs:ListQueues'
],
'resources': [
'*'
]
},
{
'actions': [
'sqs:*'
],
'resources': [
f'arn:aws:sqs:{aws.region_name}:{aws.account}:azul-*'
]
},
# API Gateway ARNs refer to APIs by ID so we cannot restrict
# to name or prefix. Even though all API Gateway ARNs start
# with `arn:aws:apigateway:${Region}::`, using the pattern
# `arn:aws:apigateway:${Region}::*` in the policy causes the
# CreateDomainName action to return AccessDenied for unknown
# reasons. Other API Gateway actions succeed with that ARN
# pattern in the policy.
{
'actions': [
'apigateway:*'
],
'resources': [
'*'
]
},
{
'actions': [
'es:DescribeElasticsearchInstanceTypeLimits',
'es:ListElasticsearchInstanceTypes',
'es:ListElasticsearchVersions',
'es:DescribeReservedElasticsearchInstances',
'es:ListDomainNames',
'es:DescribeReservedElasticsearchInstanceOfferings'
],
'resources': [
'*'
]
},
{
'actions': [
'es:*'
],
'resources': [
f'arn:aws:es:{aws.region_name}:{aws.account}:domain/azul-*'
]
},
{
'actions': [
'es:ListTags'
],
'resources': [
f'arn:aws:es:{aws.region_name}:{aws.account}:domain/*'
]
},
{
'actions': [
'sts:GetCallerIdentity'
],
'resources': [
'*'
]
},
{
'actions': [
'sts:GetFederationToken',
'sts:GetCallerIdentity'
],
'resources': [
f'arn:aws:sts::{aws.account}:*',
f'arn:aws:iam::{aws.account}:*',
f'arn:aws:iam::{aws.account}:role/*',
f'arn:aws:iam::{aws.account}:user/*'
]
},
{
# ACM ARNs refer to certificates by ID so we
# cannot restrict to name or prefix
'actions': [
'acm:RequestCertificate',
'acm:ListTagsForCertificate',
'acm:ListCertificates'
],
'resources': [
'*'
]
},
# API Gateway certs must reside in us-east-1,
# so we'll always add that region
{
'actions': [
'acm:*'
],
'resources': [
f'arn:aws:acm:us-east-1:{aws.account}:certificate/*'
]
},
{
'actions': [
'dynamodb:ListTables',
'dynamodb:DescribeTimeToLive',
'dynamodb:DescribeReservedCapacityOfferings',
'dynamodb:ListBackups',
'dynamodb:ListStreams',
'dynamodb:ListTagsOfResource',
'dynamodb:DescribeLimits',
'dynamodb:ListGlobalTables',
'dynamodb:DescribeReservedCapacity'
],
'resources': [
'*'
]
},
{
'actions': [
'dynamodb:*'
],
'resources': [
f'arn:aws:dynamodb:{aws.region_name}:{aws.account}:table/azul-*',
f'arn:aws:dynamodb:{aws.region_name}:{aws.account}:table/azul-*/index/*'
]
},
# Lambda ARNs refer to event source mappings by UUID so we
# cannot restrict to name or prefix
{
'actions': [
'lambda:CreateEventSourceMapping',
'lambda:ListFunctions',
'lambda:ListLayers',
'lambda:GetAccountSettings',
'lambda:ListEventSourceMappings',
'lambda:GetEventSourceMapping',
'lambda:ListLayerVersions',
'lambda:UpdateEventSourceMapping'
],
'resources': [
'*'
]
},
{
'actions': [
'lambda:*'
],
'resources': [
f'arn:aws:lambda:{aws.region_name}:{aws.account}:event-source-mapping:*',
f'arn:aws:lambda:{aws.region_name}:{aws.account}:layer:azul-*',
f'arn:aws:lambda:{aws.region_name}:{aws.account}:function:azul-*',
f'arn:aws:lambda:{aws.region_name}:{aws.account}:layer:azul-*:*'
]
},
*chalice.vpc_lambda_iam_policy(for_tf=True),
# CloudWatch does not describe any resource-level
# permissions
{
'actions': [
'cloudwatch:*'
],
'resources': [
'*'
]
},
{
'actions': [
'events:DescribeEventBus',
'events:TestEventPattern'
],
'resources': [
'*'
]
},
{
'actions': [
'events:*'
],
'resources': [
f'arn:aws:events:{aws.region_name}:{aws.account}:rule/azul-*'
]
},
# Route 53 ARNs refer to resources by ID so we cannot
# restrict to name or prefix
#
# FIXME: this is obviously problematic
{
'actions': [
'route53:*'
],
'resources': [
'*'
]
},
# Secret Manager ARNs refer to secrets by UUID so we cannot
# restrict to name or prefix
#
# FIXME: this is obviously problematic
#
{
'actions': [
'secretsmanager:CreateSecret',
'secretsmanager:ListSecrets',
'secretsmanager:GetRandomPassword'
],
'resources': [
'*'
]
},
{
'actions': [
'secretsmanager:*'
],
'resources': [
f'arn:aws:secretsmanager:{aws.region_name}:{aws.account}:secret:*'
]
},
{
'actions': [
'ssm:GetParameter'
],
'resources': [
f'arn:aws:ssm:{aws.region_name}:{aws.account}:parameter/dcp/dss/*'
]
},
{
'actions': [
'states:*'
],
'resources': [
f'arn:aws:states:{aws.region_name}:{aws.account}:execution:azul-*:*',
f'arn:aws:states:{aws.region_name}:{aws.account}:stateMachine:azul-*'
]
},
{
'actions': [
'states:ListStateMachines',
'states:CreateStateMachine'
],
'resources': [
'*'
]
},
# CloudFront uses identifiers in most if its ARNs, not
# names. The identifiers are random so we can't easily use
# the ARNs that reference them in policies.
#
# FIXME: Tighten GitLab security boundary
# https://github.com/DataBiosphere/azul/issues/4207
{
'actions': [
'cloudfront:*'
],
'resources': [
'*'
]
},
# CloudWatch Logs
#
# FIXME: Tighten GitLab security boundary
# https://github.com/DataBiosphere/azul/issues/4207
{
'actions': [
'logs:*'
],
'resources': [
'*'
]
},
# WAFv2
{
'actions': [
'wafv2:*'
],
'resources': [
'*'
]
}
]
},
'gitlab_iam': {
'statement': [
# Let Gitlab manage roles as long as they specify the
# permissions boundary This prevents privilege escalation.
{
'actions': [
'iam:CreateRole',
'iam:TagRole',
'iam:UntagRole',
'iam:PutRolePolicy',
'iam:DeleteRolePolicy',
'iam:AttachRolePolicy',
'iam:DetachRolePolicy',
'iam:PutRolePermissionsBoundary'
],
'resources': [
f'arn:aws:iam::{aws.account}:role/azul-*'
],
'condition': {
'test': 'StringEquals',
'variable': 'iam:PermissionsBoundary',
'values': [
f'arn:aws:iam::{aws.account}:policy/azul-boundary'
]
}
},
{
'actions': [
'iam:CreateServiceLinkedRole'
],
'resources': [
f'arn:aws:iam::{aws.account}'
':role'
'/aws-service-role'
'/ops.apigateway.amazonaws.com'
'/AWSServiceRoleForAPIGateway',
]
},
{
'actions': [
'iam:UpdateAssumeRolePolicy',
'iam:TagRole',
'iam:DeleteRole',
'iam:PassRole' # FIXME: consider iam:PassedToService condition
],
'resources': [
f'arn:aws:iam::{aws.account}:role/azul-*'
]
},
{
'actions': [
'iam:GetServiceLinkedRoleDeletionStatus',
'iam:ListAttachedGroupPolicies',
'iam:ListSigningCertificates',
'iam:ListUsers',
'iam:GetPolicyVersion',
'iam:GetLoginProfile',
'iam:ListPolicies',
'iam:GetUser',
'iam:GetSSHPublicKey',
'iam:GetPolicy',
'iam:ListInstanceProfiles',
'iam:GenerateCredentialReport',
'iam:ListMFADevices',
'iam:ListRoles',
'iam:SimulateCustomPolicy',
'iam:ListUserPolicies',
'iam:GetContextKeysForCustomPolicy',
'iam:GetServiceLastAccessedDetails',
'iam:GetCredentialReport',
'iam:ListServerCertificates',
'iam:GetOpenIDConnectProvider',
'iam:ListVirtualMFADevices',
'iam:ListPolicyVersions',
'iam:GetInstanceProfile',
'iam:ListRolePolicies',
'iam:ListAttachedRolePolicies',
'iam:GetAccountAuthorizationDetails',
'iam:GetGroupPolicy',
'iam:GetRole',
'iam:SimulatePrincipalPolicy',
'iam:GetContextKeysForPrincipalPolicy',
'iam:GetServiceLastAccessedDetailsWithEntities',
'iam:GetAccessKeyLastUsed',
'iam:ListGroupPolicies',
'iam:GetGroup',
'iam:ListPoliciesGrantingServiceAccess',
'iam:GetUserPolicy',
'iam:ListAttachedUserPolicies',
'iam:ListRoleTags',
'iam:GenerateServiceLastAccessedDetails',
'iam:GetSAMLProvider',
'iam:ListGroups',
'iam:ListOpenIDConnectProviders',
'iam:ListServiceSpecificCredentials',
'iam:ListSSHPublicKeys',
'iam:ListGroupsForUser',
'iam:GetServerCertificate',
'iam:ListEntitiesForPolicy',
'iam:ListAccessKeys',
'iam:ListAccountAliases',
'iam:GetAccountPasswordPolicy',
'iam:ListUserTags',
'iam:ListInstanceProfilesForRole',
'iam:ListSAMLProviders',
'iam:GetAccountSummary',
'iam:GetRolePolicy'
],
'resources': [
'*'
]
},
*(
# Permissions required to deploy Data Browser and
# Portal
[
{
'actions': [
's3:*'
],
'resources': [
# Data Portal Dev
'arn:aws:s3:::dev.singlecell.gi.ucsc.edu/*',
'arn:aws:s3:::dev.singlecell.gi.ucsc.edu',
# Data Browser Dev
'arn:aws:s3:::dev.explore.singlecell.gi.ucsc.edu/*',
'arn:aws:s3:::dev.explore.singlecell.gi.ucsc.edu',
# Data Portal UX-Dev
'arn:aws:s3:::ux-dev.singlecell.gi.ucsc.edu/*',
'arn:aws:s3:::ux-dev.singlecell.gi.ucsc.edu',
# Data Browser UX-Dev
'arn:aws:s3:::ux-dev.explore.singlecell.gi.ucsc.edu/*',
'arn:aws:s3:::ux-dev.explore.singlecell.gi.ucsc.edu',
# Lungmap Data Portal Dev
'arn:aws:s3:::data-browser.dev.lungmap.net/*',
'arn:aws:s3:::data-browser.dev.lungmap.net',
# Lungmap Data Browser Dev
'arn:aws:s3:::dev.explore.lungmap.net/*',
'arn:aws:s3:::dev.explore.lungmap.net'
]
}
] if config.deployment_stage == 'dev' else [
{
'actions': [
's3:*'
],
'resources': [
# HCA Data Portal Prod
'arn:aws:s3:::org-humancellatlas-data-portal-dcp2-prod/*',
'arn:aws:s3:::org-humancellatlas-data-portal-dcp2-prod',
# HCA Data Browser Prod
'arn:aws:s3:::org-humancellatlas-data-browser-dcp2-prod/*',
'arn:aws:s3:::org-humancellatlas-data-browser-dcp2-prod',
# Lungmap Data Browser Prod
'arn:aws:s3:::data-browser.lungmap.net/*',
'arn:aws:s3:::data-browser.lungmap.net',
# Lungmap Data Portal Prod
'arn:aws:s3:::data-browser.explore.lungmap.net/*',
'arn:aws:s3:::data-browser.explore.lungmap.net'
]
}
] if config.deployment_stage == 'prod' else [
# anvildev and anvilprod already follow the bucket
# naming convention and is covered by the S3
# permissions in the boundary.
]
),
# Manage VPN infrastructure for private API
# FIXME: Tighten GitLab security boundary
# https://github.com/DataBiosphere/azul/issues/4207
{
'actions': [
'ec2:*'
],
'resources': [
'*'
]
},
{
'actions': [
'elasticloadbalancing:*'
],
'resources': [
'*'
]
},
# SNS
{
'actions': [
'sns:*'
],
'resources': [
f'arn:aws:sns:{aws.region_name}:{aws.account}:azul-*'
]
},
# Restricting the topic name prevents the SNS topic from
# being used in data blocks.
{
'actions': [
'sns:ListTopics'
],
'resources': [
f'arn:aws:sns:{aws.region_name}:{aws.account}:*'
]
},
# FedRAMP inventory
{
'actions': [
'config:ListDiscoveredResources',
'config:BatchGetResourceConfig'
],
'resources': [
'*'
]
},
{
'actions': [
'ecr:BatchCheckLayerAvailability',
'ecr:BatchGet*',
'ecr:Describe*',
'ecr:Get*',
'ecr:List*'
],
'resources': [
'*'
]
},
{
'actions': [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
'resources': [
'arn:aws:logs:*:*:*'
]
},
# KMS writes (reads are granted by boundary policy)
{
'actions': [
'kms:CreateKey'
],
'resources': [
'*'
]
},
{
'actions': [
'kms:CreateAlias',
'kms:UpdateAlias',
'kms:UpdateKeyDescription',
'kms:ScheduleKeyDeletion',
'kms:DeleteAlias',
'kms:TagResource',
'kms:UntagResource'
],
'resources': [
f'arn:aws:kms:{aws.region_name}:{aws.account}:key/*',
f'arn:aws:kms:{aws.region_name}:{aws.account}:alias/*'
]
},
# The SSM agent requires explicit permission to access
# certain AWS-managed buckets if S3 traffic is routed
# through a VPC gateway endpoint. See:
# https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent-minimum-s3-permissions.html
{
'actions': [
's3:GetObject',
's3:ListBucket'
],
'resources': merge(
[
f'arn:aws:s3:::{bucket_name}',
f'arn:aws:s3:::{bucket_name}/*'
] for bucket_name in aws_managed_buckets_for_ssm_agent
)
}
]
}
},
},
'resource': {
'aws_vpc': {
'gitlab': {
'cidr_block': vpc_cidr
}
},
'aws_subnet': {
# A public and a private subnet per availability zone
f'gitlab_{vpc.subnet_name(public)}_{zone}': {
'availability_zone': f'${{data.aws_availability_zones.available.names[{zone}]}}',
'cidr_block': f'${{cidrsubnet(aws_vpc.gitlab.cidr_block, 8, {vpc.subnet_number(zone, public)})}}',
'map_public_ip_on_launch': public,
'vpc_id': '${aws_vpc.gitlab.id}'
}
for public in (False, True)
for zone in range(num_zones)
},
'aws_internet_gateway': {
'gitlab': {
'vpc_id': '${aws_vpc.gitlab.id}'
}
},
'aws_route': {
'gitlab': {
'destination_cidr_block': all_ipv4,
'gateway_id': '${aws_internet_gateway.gitlab.id}',
'route_table_id': '${aws_vpc.gitlab.main_route_table_id}'
}
},
'aws_eip': {
f'gitlab_{zone}': {
'depends_on': [
'aws_internet_gateway.gitlab'
],
'domain': 'vpc'
}
for zone in range(num_zones)
},