Skip to content

Commit 14cf9a4

Browse files
committed
feat(firewall): add a security list for SSH and VScode
1 parent 2157a3c commit 14cf9a4

File tree

5 files changed

+127
-31
lines changed

5 files changed

+127
-31
lines changed

.terraform.lock.hcl

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Terraform project that deploys VSCode Server on Oracle Cloud Infrastructure usin
1818
- [x] Create the instance on free tier (4 vCPU, 24GB memory)
1919
- [x] Configure the instance and install VSCode Server with Cloud Init
2020
- [x] Create automatically the SSH key pair
21-
- [x] Mount and format the block volume on `/data` (WIP)
21+
- [x] Mount and format the block volume on `/data`
22+
- [x] Restrict SSH and VS Code port access
2223
- [ ] Encrypt the block volume with a KMS key
2324
- [ ] Configure backups of the block volume only (WIP)
2425
- [ ] Configure Cloudflare Zero Trust to secure the instance access

firewall.tf

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
locals {
2+
security_list_ssh = "${var.namespace}-seclist-ssh-${var.stage}"
3+
security_list_vscode = "${var.namespace}-seclist-vscode-${var.stage}"
4+
}
5+
6+
# Security List | SSH
7+
resource "oci_core_security_list" "security_list_ssh" {
8+
9+
# Global
10+
compartment_id = var.compartment_ocid
11+
vcn_id = oci_core_vcn.vcn.id
12+
display_name = local.security_list_ssh
13+
14+
# Ingress
15+
dynamic "ingress_security_rules" {
16+
for_each = var.allowed_ingress_ssh
17+
18+
content {
19+
description = "Allow traffic only from the SSH allowed IPs"
20+
source = ingress_security_rules.value
21+
protocol = "6" # TCP
22+
23+
tcp_options {
24+
min = 22 # SSH
25+
max = 22 # SSH
26+
}
27+
}
28+
}
29+
30+
# Egress
31+
dynamic "egress_security_rules" {
32+
for_each = var.allowed_egress_ssh
33+
34+
content {
35+
description = "Allow all outbound traffic from the SSH allowed IPs"
36+
destination = egress_security_rules.value
37+
protocol = "all"
38+
}
39+
}
40+
}
41+
42+
# Security List | VSCode
43+
resource "oci_core_security_list" "security_list_vscode" {
44+
45+
# Global
46+
compartment_id = var.compartment_ocid
47+
vcn_id = oci_core_vcn.vcn.id
48+
display_name = local.security_list_vscode
49+
50+
# Ingress
51+
dynamic "ingress_security_rules" {
52+
for_each = var.allowed_ingress_vscode
53+
54+
content {
55+
description = "Allow traffic only from the VSCode allowed IPs"
56+
source = ingress_security_rules.value
57+
protocol = "6" # TCP
58+
59+
tcp_options {
60+
min = 443 # HTTPS
61+
max = 443 # HTTPS
62+
}
63+
}
64+
}
65+
66+
# Egress
67+
dynamic "egress_security_rules" {
68+
for_each = var.allowed_egress_vscode
69+
70+
content {
71+
description = "Allow all outbound traffic from the VSCode allowed IPs"
72+
destination = egress_security_rules.value
73+
protocol = "all"
74+
}
75+
}
76+
}

network.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ locals {
44
igw_name = "${var.namespace}-igw-${var.stage}"
55
default_rt_name = "${var.namespace}-default-rt-${var.stage}"
66
subnet_name = "${var.namespace}-subnet-${var.stage}"
7+
seclist_name = "${var.namespace}-seclist-${var.stage}"
78
}
89

910
# Virtual Cloud Network
@@ -69,7 +70,10 @@ resource "oci_core_subnet" "subnet" {
6970
dhcp_options_id = oci_core_vcn.vcn.default_dhcp_options_id
7071

7172
# Security
72-
security_list_ids = [oci_core_vcn.vcn.default_security_list_id]
73+
security_list_ids = [
74+
oci_core_security_list.security_list_ssh.id,
75+
oci_core_security_list.security_list_vscode.id,
76+
]
7377

7478
# Labels
7579
freeform_tags = local.common_labels

variables.tf

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ variable "region" {
5858
default = "uk-london-1"
5959
}
6060

61+
#############################
62+
# Security Lists
63+
#############################
64+
65+
variable "allowed_ingress_ssh" {
66+
type = list(string)
67+
description = "List of IPs allowed to SSH on the instance"
68+
default = []
69+
}
70+
71+
variable "allowed_egress_ssh" {
72+
type = list(string)
73+
description = "List of IPs the instance is allowed to connect"
74+
default = ["0.0.0.0/0"]
75+
}
76+
77+
variable "allowed_ingress_vscode" {
78+
type = list(string)
79+
description = "List of IPs allowed to access to VS Code Server"
80+
default = []
81+
}
82+
83+
variable "allowed_egress_vscode" {
84+
type = list(string)
85+
description = "List of IPs the instance is allowed to connect"
86+
default = ["0.0.0.0/0"]
87+
}
88+
6189
#############################
6290
# Instance
6391
#############################
@@ -80,18 +108,6 @@ variable "instance_shape_config_memory_in_gbs" {
80108
default = 24
81109
}
82110

83-
variable "block_volume_size" {
84-
type = string
85-
description = "Block Volume size in GBs (/data)"
86-
default = 100
87-
}
88-
89-
variable "block_volume_device_name" {
90-
type = string
91-
description = "Block Volume device name (/dev/oracleoci/oraclevdb)"
92-
default = "/dev/oracleoci/oraclevdb"
93-
}
94-
95111
variable "instance_os" {
96112
type = string
97113
description = "Instance OS"
@@ -110,6 +126,22 @@ variable "instance_os_user" {
110126
default = "ubuntu" # opc if Oracle Linux
111127
}
112128

129+
#############################
130+
# Block Volume (/data)
131+
#############################
132+
133+
variable "block_volume_size" {
134+
type = string
135+
description = "Block Volume size in GBs (/data)"
136+
default = 100
137+
}
138+
139+
variable "block_volume_device_name" {
140+
type = string
141+
description = "Block Volume device name (/dev/oracleoci/oraclevdb)"
142+
default = "/dev/oracleoci/oraclevdb"
143+
}
144+
113145
#############################
114146
# VS Code Server
115147
#############################

0 commit comments

Comments
 (0)