forked from openclarity/vmclarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VmClarity.cfn
1315 lines (1254 loc) · 51.2 KB
/
VmClarity.cfn
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
AWSTemplateFormatVersion: 2010-09-09
Description: |
VMClarity is a tool for agentless detection and management of Virtual Machine
Software Bill Of Materials (SBOM) and vulnerabilities
Resources:
# Create separate VPC to host the VMClarity components and scans so that we
# keep VM Clarity resources completely separate from the VMs being scanned.
VmClarityVPC:
Type: "AWS::EC2::VPC"
Properties:
EnableDnsSupport: "true"
EnableDnsHostnames: "true"
CidrBlock: 10.0.0.0/16
# Subnet for the VmClarityServer. Will be public.
VmClarityServerSubnet:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VmClarityVPC
CidrBlock: 10.0.0.0/24
# Subnet for the VmClarityScanners. Will be private.
VmClarityScannerSubnet:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VmClarityVPC
CidrBlock: 10.0.1.0/24
# Elastic IP address that will be used to serve the VMClarity UI, API and SSH
# access.
VmClarityServerElasticIp:
Type: "AWS::EC2::EIP"
Properties:
Domain: "vpc"
# Elastic IP address that will be used by the NAT gateway to allow the
# private scanner VMs to access the internet without requiring a public
# address themselves.
VmClarityScannerNatElasticIp:
Type: "AWS::EC2::EIP"
Properties:
Domain: "vpc"
# VmClarityServer network interface definition.
VmClarityServerNetworkInterface:
Type: 'AWS::EC2::NetworkInterface'
Properties:
GroupSet:
- !Ref VmClarityServerSecurityGroup
SubnetId: !Ref VmClarityServerSubnet
# Associate the VmClarityServer elastic IP address directly to the
# VMClarityServers private network interface for now. In the future we might
# want to replace this with a load balancer.
VmClarityServerEipAssociation:
Type: 'AWS::EC2::EIPAssociation'
Properties:
AllocationId: !GetAtt
- VmClarityServerElasticIp
- AllocationId
NetworkInterfaceId: !Ref VmClarityServerNetworkInterface
# VmClarityServer will host the VMClarity services which orchestrate the scans
# as well as serve the user interface and API
VmClarityServer:
Type: "AWS::EC2::Instance"
CreationPolicy:
ResourceSignal:
Timeout: PT7M
Count: "1"
Properties:
InstanceType: !Ref InstanceType
Tags:
- Key: Name
Value: "VMClarity Server"
- Key: Owner
Value: "VMClarity"
ImageId: !FindInMap
- AWSRegionArch2AMI
- !Ref "AWS::Region"
- !FindInMap
- AWSInstanceType2Arch
- !Ref InstanceType
- Arch
KeyName: !Ref KeyName
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeType: gp2
VolumeSize: '30'
DeleteOnTermination: 'true'
Encrypted: 'false'
IamInstanceProfile:
Ref: VmClarityServerInstanceProfile
NetworkInterfaces:
- NetworkInterfaceId: !Ref VmClarityServerNetworkInterface
DeviceIndex: "0"
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash -xe
apt-get update -y
apt-get -y install python3-pip
pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
ln -s /usr/local/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
/usr/local/bin/cfn-init -v --stack ${AWS::StackName} --resource VmClarityServer --configsets full_install --region ${AWS::Region}
/usr/local/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource VmClarityServer --region ${AWS::Region}
Metadata:
AWS::CloudFormation::Init:
configSets:
full_install:
- install_and_enable_cfn_hup
- install_vmclarity
install_and_enable_cfn_hup:
files:
"/etc/cfn/cfn-hup.conf":
content:
Fn::Sub: |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=5
mode: "000400"
owner: root
group: root
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content:
Fn::Sub: |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.VmClarityServer.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource VmClarityServer --configsets full_install --region ${AWS::Region}
runas=root
"/lib/systemd/system/cfn-hup.service":
content: |
[Unit]
Description=cfn-hup daemon
[Service]
Type=simple
ExecStart=/usr/local/bin/cfn-hup
Restart=always
[Install]
WantedBy=multi-user.target
commands:
01reload_systemctl:
command: systemctl daemon-reload
02enable_cfn_hup:
command: systemctl enable cfn-hup.service
03start_restart_cfn_hup:
command: systemctl restart cfn-hup.service
install_vmclarity:
files:
"/etc/vmclarity/deploy.sh":
content:
Fn::Sub: |
#!/bin/bash
set -euo pipefail
# Install the latest version of docker from the offical
# docker repository instead of the older version built into
# ubuntu, so that we can use docker compose v2.
#
# To install this we need to add the docker apt repo gpg key
# to the apt keyring, and then add the apt sources based on
# our version of ubuntu. Then we can finally apt install all
# the required docker components.
apt-get update
apt-get install -y ca-certificates curl gnupg
mkdir -p /etc/apt/keyrings
chmod 755 /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --yes --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
if [ "${DatabaseToUse}" == "Postgresql" ]; then
# Enable and start/restart postgres
echo "COMPOSE_PROFILES=postgres" >> /etc/vmclarity/service.env
# Configure the VMClarity backend to use the local postgres
# service
echo "DATABASE_DRIVER=POSTGRES" > /etc/vmclarity/apiserver.env
echo "DB_NAME=vmclarity" >> /etc/vmclarity/apiserver.env
echo "DB_USER=vmclarity" >> /etc/vmclarity/apiserver.env
echo "DB_PASS=${PostgresDBPassword}" >> /etc/vmclarity/apiserver.env
echo "DB_HOST=postgres.service" >> /etc/vmclarity/apiserver.env
echo "DB_PORT_NUMBER=5432" >> /etc/vmclarity/apiserver.env
elif [ "${DatabaseToUse}" == "External Postgresql" ]; then
# Configure the VMClarity backend to use the postgres
# database configured by the user.
echo "DATABASE_DRIVER=POSTGRES" > /etc/vmclarity/apiserver.env
echo "DB_NAME=${ExternalDBName}" >> /etc/vmclarity/apiserver.env
echo "DB_USER=${ExternalDBUsername}" >> /etc/vmclarity/apiserver.env
echo "DB_PASS=${ExternalDBPassword}" >> /etc/vmclarity/apiserver.env
echo "DB_HOST=${ExternalDBHost}" >> /etc/vmclarity/apiserver.env
echo "DB_PORT_NUMBER=${ExternalDBPort}" >> /etc/vmclarity/apiserver.env
elif [ "${DatabaseToUse}" == "SQLite" ]; then
# Configure the VMClarity backend to use the SQLite DB
# driver and configure the storage location so that it
# persists.
echo "DATABASE_DRIVER=LOCAL" > /etc/vmclarity/apiserver.env
echo "LOCAL_DB_PATH=/data/vmclarity.db" >> /etc/vmclarity/apiserver.env
fi
# Replace anywhere in the config.env __CONTROLPLANE_HOST__
# with the local ipv4 IP address of the VMClarity server.
local_ip_address="$(curl http://169.254.169.254/latest/meta-data/local-ipv4)"
sed -i "s/__CONTROLPLANE_HOST__/${!local_ip_address}/" /etc/vmclarity/orchestrator.env
# Reload the systemd daemon to ensure that the VMClarity unit
# has been detected.
systemctl daemon-reload
# Create directory required for grype-server
/usr/bin/mkdir -p /opt/grype-server
/usr/bin/chown -R 1000:1000 /opt/grype-server
# Create directory required for vmclarity apiserver
/usr/bin/mkdir -p /opt/vmclarity
# Create directory for exploit db server
/usr/bin/mkdir -p /opt/exploits
# Create directory for trivy server
/usr/bin/mkdir -p /opt/trivy-server
# Enable and start/restart VMClarity backend
systemctl enable vmclarity.service
systemctl restart vmclarity.service
mode: "000744"
"/etc/vmclarity/vmclarity.yaml":
content:
Fn::Sub:
- |
version: '3'
services:
apiserver:
image: ${APIServerContainerImage}
command:
- run
- --log-level
- info
ports:
- "8888:8888"
env_file: ./apiserver.env
volumes:
- type: bind
source: /opt/vmclarity
target: /data
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
orchestrator:
image: ${OrchestratorContainerImage}
command:
- run
- --log-level
- info
env_file: ./orchestrator.env
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
ui:
image: ${UIContainerImage}
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
uibackend:
image: ${UIBackendContainerImage}
command:
- run
- --log-level
- info
env_file: ./uibackend.env
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
gateway:
image: nginx
ports:
- "80:80"
configs:
- source: gateway_config
target: /etc/nginx/nginx.conf
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
exploit-db-server:
image: ${ExploitDBServerContainerImage}
ports:
- "1326:1326"
volumes:
- type: bind
source: /opt/exploits
target: /vuls
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
trivy-server:
image: ${TrivyServerContainerImage}
command:
- server
ports:
- "9992:9992"
env_file: ./trivy-server.env
volumes:
- type: bind
source: /opt/trivy-server
target: /home/scanner/.cache
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
grype-server:
image: ${GrypeServerContainerImage}
command:
- run
- --log-level
- warning
ports:
- "9991:9991"
env_file: ./grype-server.env
volumes:
- type: bind
source: /opt/grype-server
target: /opt/grype-server
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
freshclam-mirror:
image: ${FreshclamMirrorContainerImage}
ports:
- "1000:80"
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
postgresql:
image: ${PostgresqlContainerImage}
env_file: ./postgres.env
ports:
- "5432:5432"
profiles:
- postgres
logging:
driver: journald
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
swagger-ui:
image: swaggerapi/swagger-ui:v5.3.1
environment:
CONFIG_URL: /apidocs/swagger-config.json
configs:
- source: swagger_config
target: /usr/share/nginx/html/swagger-config.json
configs:
gateway_config:
file: ./gateway.conf
swagger_config:
file: ./swagger-config.json
- APIServerContainerImage: !If [ APIServerContainerImageOverridden, !Ref APIServerContainerImageOverride, "ghcr.io/openclarity/vmclarity-apiserver:latest" ]
OrchestratorContainerImage: !If [ OrchestratorContainerImageOverridden, !Ref OrchestratorContainerImageOverride, "ghcr.io/openclarity/vmclarity-orchestrator:latest" ]
UIContainerImage: !If [ UIContainerImageOverridden, !Ref UIContainerImageOverride, "ghcr.io/openclarity/vmclarity-ui:latest" ]
UIBackendContainerImage: !If [ UIBackendContainerImageOverridden, !Ref UIBackendContainerImageOverride, "ghcr.io/openclarity/vmclarity-ui-backend:latest" ]
ExploitDBServerContainerImage: !If [ExploitDBServerContainerImageOverridden, !Ref ExploitDBServerContainerImageOverride, "ghcr.io/openclarity/exploit-db-server:v0.2.3"]
TrivyServerContainerImage: !If [TrivyServerContainerImageOverridden, !Ref TrivyServerContainerImageOverride, "docker.io/aquasec/trivy:0.41.0"]
GrypeServerContainerImage: !If [GrypeServerContainerImageOverridden, !Ref GrypeServerContainerImageOverride, "ghcr.io/openclarity/grype-server:v0.4.0"]
FreshclamMirrorContainerImage: !If [FreshclamMirrorContainerImageOverridden, !Ref FreshclamMirrorContainerImageOverride, "ghcr.io/openclarity/freshclam-mirror:v0.2.0"]
PostgresqlContainerImage: !If [PostgresqlContainerImageOverridden, !Ref PostgresqlContainerImageOverride, "bitnami/postgresql:12.14.0-debian-11-r28"]
"/etc/vmclarity/uibackend.env":
content: |
##
## UIBackend configuration
##
# Host for the VMClarity backend server
APISERVER_HOST=apiserver
# Port number for the VMClarity backend server
APISERVER_PORT=8888
"/etc/vmclarity/swagger-config.json":
content: |
{
"urls": [
{
"name": "VMClarity API",
"url": "/api/openapi.json"
}
]
}
"/etc/vmclarity/orchestrator.env":
content:
Fn::Sub:
- |
##
## Orchestrator configuration
##
# Host for the VMClarity backend server
APISERVER_HOST=apiserver
# Port number for the VMClarity backend server
APISERVER_PORT=8888
# Container image for Scanner instance
SCANNER_CONTAINER_IMAGE=${ScannerContainerImage}
# API Server Address for the Scanner to connect to
SCANNER_VMCLARITY_APISERVER_ADDRESS=http://__CONTROLPLANE_HOST__:8888
# Trivy server address
TRIVY_SERVER_ADDRESS=http://__CONTROLPLANE_HOST__:9992
# Grype server address
GRYPE_SERVER_ADDRESS=__CONTROLPLANE_HOST__:9991
# FreshClam mirror URL
ALTERNATIVE_FRESHCLAM_MIRROR_URL=http://__CONTROLPLANE_HOST__:1000/clamav
# Resource cleanup policy
DELETE_JOB_POLICY=${AssetScanDeletePolicy}
# Provider to use
PROVIDER=aws
##
## Provider configuration
##
# The AWS region where the provider is deployed
AWS_REGION=${AWS::Region}
# Region where the Scanner instance needs to be created
VMCLARITY_AWS_SCANNER_REGION=${AWS::Region}
# SubnetID where the Scanner instance needs to be created
VMCLARITY_AWS_SUBNET_ID=${VmClarityScannerSubnet}
# SecurityGroupId which needs to be attached to the Scanner instance
VMCLARITY_AWS_SECURITY_GROUP_ID=${VmClarityScannerSecurityGroup}
# Name of the SSH KeyPair to use for Scanner instance launch
VMCLARITY_AWS_KEYPAIR_NAME=${KeyName}
# The AMI image used for creating Scanner instance
VMCLARITY_AWS_SCANNER_AMI_ID=${ScannerImageID}
# The instance type used for Scanner instance
VMCLARITY_AWS_SCANNER_INSTANCE_TYPE=${ScannerInstanceType}
# Block device name used for attaching Scanner volume to the Scanner instance
#VMCLARITY_AWS_BLOCK_DEVICE_NAME=xvhd
- ScannerImageID: !FindInMap
- AWSRegionArch2AMI
- !Ref "AWS::Region"
- !FindInMap
- AWSInstanceType2Arch
- !Ref ScannerInstanceType
- Arch
ScannerContainerImage: !If [ScannerContainerImageOverridden, !Ref ScannerContainerImageOverride, "ghcr.io/openclarity/vmclarity-cli:latest"]
mode: "000644"
"/etc/vmclarity/trivy-server.env":
content: |
TRIVY_LISTEN=0.0.0.0:9992
TRIVY_CACHE_DIR=/home/scanner/.cache/trivy
mode: "000644"
"/etc/vmclarity/grype-server.env":
content: |
DB_ROOT_DIR=/opt/grype-server/db
mode: "000644"
"/etc/vmclarity/service.env":
content: |
# COMPOSE_PROFILES=
mode: "000644"
"/etc/vmclarity/postgres.env":
content:
Fn::Sub: |
POSTGRESQL_USERNAME=vmclarity
POSTGRESQL_PASSWORD=${PostgresDBPassword}
POSTGRESQL_DATABASE=vmclarity
mode: "000644"
"/etc/vmclarity/gateway.conf":
content: |
events {
worker_connections 1024;
}
http {
upstream ui {
server ui:80;
}
upstream uibackend {
server uibackend:8890;
}
upstream apiserver {
server apiserver:8888;
}
server {
listen 80;
absolute_redirect off;
location / {
proxy_pass http://ui/;
}
location /ui/api/ {
proxy_pass http://uibackend/;
}
location /api/ {
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Prefix /api;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://apiserver/;
}
location /apidocs/ {
proxy_pass http://swagger-ui:8080/;
}
}
}
mode: "000644"
"/lib/systemd/system/vmclarity.service":
content: |
[Unit]
Description=VmClarity
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Type=oneshot
RemainAfterExit=true
EnvironmentFile=/etc/vmclarity/service.env
ExecStart=/usr/bin/docker compose -p vmclarity -f /etc/vmclarity/vmclarity.yaml up -d --wait --remove-orphans
ExecStop=/usr/bin/docker compose -p vmclarity -f /etc/vmclarity/vmclarity.yaml down
[Install]
WantedBy=multi-user.target
mode: "000644"
commands:
01deploy_vmclarity:
command: /etc/vmclarity/deploy.sh
DependsOn:
- VmClarityServerPublicRoute
# Create a Security Group for the VMClarity server. Allow on the public
# address, SSH access can be restricted by source CIDR range during
# installation of the stack through the SSHLocation parameter.
# TODO(sambetts) Enable HTTP access from the public address for the UI and
# API
VmClarityServerSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
VpcId: !Ref VmClarityVPC
GroupDescription: Allow only required network traffic for VMClarity server
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref SSHLocation
# Create a Security Group for the VMClarity scanner VMs. Restrict all ingress
# except SSH access from the VMClarityServerSecurityGroup, the
# VMClarityServer can act as a bastion for debugging the scanners.
VmClarityScannerSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
VpcId: !Ref VmClarityVPC
GroupDescription: Allow only required network traffic for VMClarity scanners
# Allow the VMClarity Server in the VmClarityScannerSecurityGroup to access
# the Scanner VMs through SSH by adding an ingress rule to the
# VmClarityScannerSecurityGroup.
VmClarityScannerSecurityGroupServerIngressToSSH:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityScannerSecurityGroup
IpProtocol: tcp
FromPort: 22
ToPort: 22
SourceSecurityGroupId: !Ref VmClarityServerSecurityGroup
# Allow the Scanner VMs in the VmClarityScannerSecurityGroup to access the
# VMClarity server API on 8888 by adding an ingress rule to the
# VmClarityServerSecurityGroup.
VmClarityServerSecurityGroupScannerIngressToAPI:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityServerSecurityGroup
IpProtocol: tcp
FromPort: 8888
ToPort: 8888
SourceSecurityGroupId: !Ref VmClarityScannerSecurityGroup
# Allow the Scanner VMs in the VmClarityScannerSecurityGroup to access the
# Exploits DB server on port 1326 by adding an ingress rule to the
# VmClarityServerSecurityGroup.
VmClarityServerSecurityGroupScannerIngressToExploitDB:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityServerSecurityGroup
IpProtocol: tcp
FromPort: 1326
ToPort: 1326
SourceSecurityGroupId: !Ref VmClarityScannerSecurityGroup
# Allow the Scanner VMs in the VmClarityScannerSecurityGroup to access the
# Trivy Server on port 9992 by adding an ingress rule to the
# VmClarityServerSecurityGroup.
VmClarityServerSecurityGroupScannerIngressToTrivyServer:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityServerSecurityGroup
IpProtocol: tcp
FromPort: 9992
ToPort: 9992
SourceSecurityGroupId: !Ref VmClarityScannerSecurityGroup
# Allow the Scanner VMs in the VmClarityScannerSecurityGroup to access the
# Grype Server on port 9992 by adding an ingress rule to the
# VmClarityServerSecurityGroup.
VmClarityServerSecurityGroupScannerIngressToGrypeServer:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityServerSecurityGroup
IpProtocol: tcp
FromPort: 9991
ToPort: 9991
SourceSecurityGroupId: !Ref VmClarityScannerSecurityGroup
# Allow the Scanner VMs in the VmClarityScannerSecurityGroup to access the
# FreshClam mirror on port 1000 by adding an ingress rule to the
# VmClarityServerSecurityGroup.
VmClarityServerSecurityGroupScannerIngressToFreshClamMirror:
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
GroupId: !Ref VmClarityServerSecurityGroup
IpProtocol: tcp
FromPort: 1000
ToPort: 1000
SourceSecurityGroupId: !Ref VmClarityScannerSecurityGroup
# Create an Internet Gateway to allow VMClarityServer to talk to the internet
# and the internet to talk to it for SSH/HTTP.
VmClarityServerInternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties: {}
# Attach our VPC to the InternetGateway above
VmClarityServerInternetGatewayAttachment:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
VpcId: !Ref VmClarityVPC
InternetGatewayId: !Ref VmClarityServerInternetGateway
# Create a route table to host the routes required for our VPC.
VmClarityServerRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VmClarityVPC
# Associate the route table with our subnet so that VMs in that subnet get
# the routes from the route table.
VmClarityServerSubnetRouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref VmClarityServerSubnet
RouteTableId: !Ref VmClarityServerRouteTable
# Create a route with forwards all non-local traffic to the internet gateway
# for routing.
VmClarityServerPublicRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref VmClarityServerRouteTable
GatewayId: !Ref VmClarityServerInternetGateway
DestinationCidrBlock: 0.0.0.0/0
DependsOn:
- VmClarityServerInternetGatewayAttachment
# Create a NAT gateway to allow VMClarity Scanner instances to access the
# internet without needing an internet routable IP address. This goes into
# the VmClarityServerSubnet so that it has public access to the internet.
VmClarityScannerNatGateway:
Type: "AWS::EC2::NatGateway"
Properties:
AllocationId: !GetAtt
- VmClarityScannerNatElasticIp
- AllocationId
SubnetId: !Ref VmClarityServerSubnet
# Create route table for VMClarity Scanner instances to access the NAT
# gateway
VmClarityScannerNatRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VmClarityVPC
# Create route rule the pushes all non-local traffic to the NAT gateway for
# routing.
VMClarityScannerNatRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref VmClarityScannerNatRouteTable
NatGatewayId: !Ref VmClarityScannerNatGateway
DestinationCidrBlock: 0.0.0.0/0
# Associate the VMClarity Scanner subnet with the Scanner route table that
# goes to the NAT gateway.
VmClarityScannerSubnetRouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref VmClarityScannerSubnet
RouteTableId: !Ref VmClarityScannerNatRouteTable
# Create a IAM policy which allows the VMClarityServer to perform all the
# tasks required to discover instances running on the AWS account, snapshot
# their volumes, and then create the scanner instances with those volumes
# attached.
VmClarityServerPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: VmClarityServerPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
# ##########################
# Allow snapshots everywhere in the AWS account to ensure that we can
# snapshot all the VMs. Enforce that we tag those snapshots with the
# VMClarity Owner tag so that we can control deleting them.
- Effect: "Allow"
Action: "ec2:CreateSnapshot"
Resource: !Sub "arn:aws:ec2:*:${AWS::AccountId}:volume/*"
- Effect: "Allow"
Action: "ec2:CreateSnapshot"
Resource: !Sub "arn:${AWS::Partition}:ec2:*::snapshot/*"
Condition:
StringEquals:
"aws:RequestTag/Owner": "VMClarity"
"ForAllValues:StringEquals":
"aws:TagKeys":
- Owner
- Name
- VMClarity.ScanID
- VMClarity.AssetScanID
- VMClarity.AssetID
- VMClarity.AssetVolumeID
#
# ##########################
# ##########################
# Allow copying snapshots to the VMClarity Server region for
# scanning. Only allow copy if the copy will have the VMClarity Owner
# tag.
# TODO(sambetts) Only allow us to copy snapshots which have the
# OwnerVMClarity tag.
- Effect: "Allow"
Action: "ec2:CopySnapshot"
Resource: !Sub "arn:${AWS::Partition}:ec2:*::snapshot/*"
Condition:
StringEquals:
"aws:RequestTag/Owner": "VMClarity"
"ForAllValues:StringEquals":
"aws:TagKeys":
- Owner
- Name
- VMClarity.ScanID
- VMClarity.AssetScanID
- VMClarity.AssetID
- VMClarity.AssetVolumeID
#
# ##########################
- Effect: "Allow"
Action:
- "ec2:CreateVolume"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:volume/*"
Condition:
StringEquals:
"aws:RequestTag/Owner": "VMClarity"
"ForAllValues:StringEquals":
"aws:TagKeys":
- Owner
- Name
- VMClarity.ScanID
- VMClarity.AssetScanID
- VMClarity.AssetID
- VMClarity.AssetVolumeID
# ##########################
# Only allow RunInstances inside of the VMClarity VPC by enforcing
# that the Subnet the Instance is created in belongs to the VmClarity
# VPC.
- Effect: "Allow"
Action: "ec2:RunInstances"
Resource: !Sub "arn:aws:ec2:*:${AWS::AccountId}:subnet/*"
Condition:
ArnEquals:
"ec2:Vpc": !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${VmClarityVPC}"
# Force that we tag the instance when we create it, this is so that
# we can limit the instances we're allow to terminate. The only tag
# keys allowed are:
# * Owner
# * VMClarity.ScanID
# * VMClarity.AssetScanID
# * VMClarity.AssetID
# "Owner" must be set to "VmClarity".
- Effect: "Allow"
Action:
- "ec2:RunInstances"
- "ec2:CreateVolume"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:volume/*"
Condition:
StringEquals:
"aws:RequestTag/Owner": "VMClarity"
"ForAllValues:StringEquals":
"aws:TagKeys":
- Owner
- Name
- VMClarity.ScanID
- VMClarity.AssetScanID
- VMClarity.AssetID
# Allow instance to be created using snapshots, only if the snapshot
# has the Owner:VMClarity tag.
- Effect: "Allow"
Action: "ec2:RunInstances"
Resource:
!Sub "arn:${AWS::Partition}:ec2:*::snapshot/*"
Condition:
StringEquals:
"aws:ResourceTag/Owner": "VMClarity"
"ForAllValues:StringEquals":
"aws:TagKeys":
- Owner
- Name
- VMClarity.ScanID
- VMClarity.AssetScanID
- VMClarity.AssetID
# Allow the creation of network interfaces, and allow instances to be
# created with any security group and image in our account and
# region.
# Also allow creation with any key-pair from our account, so that it
# can be used to access the scanner VMs via SSH and debug them.
# TODO(sambetts) Add lock it down to just the scanner image and
# create a security group for the scanners
- Effect: "Allow"
Action: "ec2:RunInstances"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:network-interface/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:security-group/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:key-pair/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}::image/*"
#
# ##########################
# ##########################
# Limit CreateTags to just creating an instance, volume or snapshot
# otherwise we could modify existing resources to allow us to delete
# them.
- Effect: "Allow"
Action: "ec2:CreateTags"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:network-interface/*"
- !Sub "arn:${AWS::Partition}:ec2:*:${AWS::AccountId}:instance/*"
- !Sub "arn:aws:ec2:*:${AWS::AccountId}:volume/*"
- !Sub "arn:${AWS::Partition}:ec2:*::snapshot/*"
Condition:
StringEquals:
"ec2:CreateAction":
- RunInstances
- CreateVolume
- CreateSnapshot
- CreateSnapshots
- CopySnapshot
#
# ##########################
# ##########################
# Only allow to start, stop and terminate the instances, volumes and
# snapshots that we created using the tags to identify them.
- Effect: "Allow"
Action:
- "ec2:StartInstances"
- "ec2:StopInstances"
- "ec2:TerminateInstances"
- "ec2:DeleteVolume"
- "ec2:DeleteSnapshot"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:volume/*"
- !Sub "arn:${AWS::Partition}:ec2:*::snapshot/*"
Condition:
StringEquals:
"aws:ResourceTag/Owner": "VMClarity"
#
# ##########################
# ##########################
# Allow VMClarity to query everything
- Effect: "Allow"
Action:
- "ec2:DescribeImages"
- "ec2:DescribeInstances"
- "ec2:DescribeVolumeStatus"
- "ec2:DescribeVolumes"
- "ec2:DescribeVolumesModifications"
- "ec2:DescribeSnapshots"
- "ec2:DescribeInstanceStatus"
- "ec2:DescribeVolumeAttribute"
- "ec2:DescribeRegions"
- "ec2:DescribeVpcs"
- "ec2:DescribeSecurityGroups"
Resource: "*"
#
# ##########################
# ##########################
# Only allow to attach volumes to instances that we created
# using the tags to identify them.
- Effect: "Allow"
Action:
- "ec2:AttachVolume"
Resource:
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*"
- !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:volume/*"
Condition:
StringEquals:
"aws:ResourceTag/Owner": "VMClarity"
#
# ##########################
Roles:
- !Ref VmClarityServerRole
# Create a IAM role which will contain the policy above.
VmClarityServerRole:
Type: AWS::IAM::Role
Properties:
Path: "/"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
# Create an InstanceProfile which binds the role to the VmClarityServer.
VmClarityServerInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: /
Roles:
- !Ref VmClarityServerRole
Parameters:
# Provide some choice of instance type, these are all 2 VCPU 8GB RAM systems
# and should perform similarly.
InstanceType:
Description: VmClarity Server Instance Type
Type: String
Default: t2.large
AllowedValues:
- m6i.large