Skip to content

Commit 239abe1

Browse files
author
Sean Sundberg
authored
Provisions a new base security group (#35)
- Base security group to be used instead of default security group which has outbound "0.0.0.0/0" Signed-off-by: Sean Sundberg <[email protected]>
1 parent 15f4463 commit 239abe1

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ if ! ibmcloud is vpc "${VPC_ID}"; then
3333
exit 1
3434
fi
3535

36+
SG_NAME="${VPC_NAME}-base"
37+
3638
echo "Testing security group rules"
37-
ibmcloud is security-groups --output JSON | \
38-
jq --arg VPC_NAME "${VPC_NAME}" '.[] | select(.vpc.name == $VPC_NAME) | .rules[]'
39-
OPEN_RULES=$(ibmcloud is security-groups --output JSON | jq -c --arg VPC_NAME "${VPC_NAME}" '.[] | select(.vpc.name == $VPC_NAME) | .rules[] | select(.remote.cidr == "0.0.0.0/0")')
40-
if [[ -n "${OPEN_RULES}" ]]; then
41-
echo "Rules found with public internet address"
42-
echo "${OPEN_RULES}"
39+
ibmcloud is security-groups --output JSON | jq '.[]'
40+
OPEN_RULES=$(ibmcloud is security-groups --output JSON | jq -c --arg SG_NAME "${SG_NAME}" '.[] | select(.name == $SG_NAME) | .rules[]')
41+
if [[ -z "${OPEN_RULES}" ]]; then
42+
echo "No rules found for '${SG_NAME}'"
4343
exit 1
4444
fi
4545

main.tf

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ locals {
66
prefix_name = var.name_prefix != "" ? var.name_prefix : var.resource_group_name
77
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
88
vpc_id = data.ibm_is_vpc.vpc.id
9-
security_group_id = data.ibm_is_vpc.vpc.default_security_group
9+
security_group_count = var.provision ? 2 : 0
10+
security_group_ids = var.provision ? [ data.ibm_is_vpc.vpc.default_security_group, data.ibm_is_security_group.base.id ] : []
1011
acl_id = data.ibm_is_vpc.vpc.default_network_acl
1112
crn = data.ibm_is_vpc.vpc.resource_crn
1213
ipv4_cidr_provided = var.address_prefix_count > 0 && length(var.address_prefixes) >= var.address_prefix_count
@@ -20,9 +21,9 @@ resource ibm_is_vpc vpc {
2021
name = local.vpc_name
2122
resource_group = var.resource_group_id
2223
address_prefix_management = local.ipv4_cidr_provided ? "manual" : "auto"
23-
default_security_group_name = "${local.vpc_name}-security-group"
24-
default_network_acl_name = "${local.vpc_name}-acl"
25-
default_routing_table_name = "${local.vpc_name}-routing"
24+
default_security_group_name = "${local.vpc_name}-default"
25+
default_network_acl_name = "${local.vpc_name}-default"
26+
default_routing_table_name = "${local.vpc_name}-default"
2627
}
2728

2829
data ibm_is_vpc vpc {
@@ -60,11 +61,33 @@ resource null_resource post_vpc_address_pfx_default {
6061
}
6162
}
6263

64+
resource ibm_is_security_group base {
65+
count = var.provision ? 1 : 0
66+
67+
name = "${local.vpc_name}-base"
68+
vpc = data.ibm_is_vpc.vpc.id
69+
resource_group = var.resource_group_id
70+
}
71+
72+
data ibm_is_security_group base {
73+
depends_on = [ibm_is_security_group.base]
74+
75+
name = "${local.vpc_name}-base"
76+
}
77+
78+
resource null_resource print_sg_name {
79+
depends_on = [data.ibm_is_security_group.base]
80+
81+
provisioner "local-exec" {
82+
command = "echo 'SG name: ${data.ibm_is_security_group.base.name}'"
83+
}
84+
}
85+
6386
# from https://cloud.ibm.com/docs/vpc?topic=vpc-service-endpoints-for-vpc
6487
resource ibm_is_security_group_rule "cse_dns_1" {
65-
count = var.provision ? 1 : 0
88+
count = local.security_group_count
6689

67-
group = local.security_group_id
90+
group = local.security_group_ids[count.index]
6891
direction = "outbound"
6992
remote = "161.26.0.10"
7093
udp {
@@ -74,9 +97,9 @@ resource ibm_is_security_group_rule "cse_dns_1" {
7497
}
7598

7699
resource ibm_is_security_group_rule cse_dns_2 {
77-
count = var.provision ? 1 : 0
100+
count = local.security_group_count
78101

79-
group = local.security_group_id
102+
group = local.security_group_ids[count.index]
80103
direction = "outbound"
81104
remote = "161.26.0.11"
82105
udp {
@@ -86,9 +109,9 @@ resource ibm_is_security_group_rule cse_dns_2 {
86109
}
87110

88111
resource ibm_is_security_group_rule private_dns_1 {
89-
count = var.provision ? 1 : 0
112+
count = local.security_group_count
90113

91-
group = local.security_group_id
114+
group = local.security_group_ids[count.index]
92115
direction = "outbound"
93116
remote = "161.26.0.7"
94117
udp {
@@ -98,9 +121,9 @@ resource ibm_is_security_group_rule private_dns_1 {
98121
}
99122

100123
resource ibm_is_security_group_rule private_dns_2 {
101-
count = var.provision ? 1 : 0
124+
count = local.security_group_count
102125

103-
group = local.security_group_id
126+
group = local.security_group_ids[count.index]
104127
direction = "outbound"
105128
remote = "161.26.0.8"
106129
udp {

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ output "ids" {
3838
depends_on = [null_resource.post_vpc_address_pfx_default, ibm_is_vpc.vpc]
3939
description = "The id of the vpc instance"
4040
}
41+
42+
output "base_security_group" {
43+
value = data.ibm_is_security_group.base.id
44+
description = "The id of the base security group to be shared by other resources. The base group is different from the default security group."
45+
}

0 commit comments

Comments
 (0)