Skip to content

Commit 28fc31a

Browse files
authored
Add variable source_application_security_group_ids and destination_application_security_group_ids (#55)
1 parent 8c589ab commit 28fc31a

File tree

6 files changed

+162
-79
lines changed

6 files changed

+162
-79
lines changed

README.md

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ This module includes a a set of pre-defined rules for commonly used protocols (f
1616
The following example demonstrate how to use the network-security-group module with a combination of predefined and custom rules.
1717

1818
```hcl
19-
resource "azurerm_resource_group" "test" {
19+
provider "azurerm" {
20+
features {}
21+
}
22+
23+
resource "azurerm_resource_group" "example" {
2024
name = "my-resources"
2125
location = "West Europe"
2226
}
2327
2428
module "network-security-group" {
2529
source = "Azure/network-security-group/azurerm"
26-
resource_group_name = azurerm_resource_group.test.name
30+
resource_group_name = azurerm_resource_group.example.name
2731
location = "EastUS" # Optional; if not provided, will use Resource Group location
2832
security_group_name = "nsg"
2933
source_address_prefix = ["10.0.3.0/24"]
@@ -37,6 +41,7 @@ module "network-security-group" {
3741
source_port_range = "1024-1026"
3842
}
3943
]
44+
4045
custom_rules = [
4146
{
4247
name = "myhttp"
@@ -48,6 +53,65 @@ module "network-security-group" {
4853
description = "description-myhttp"
4954
}
5055
]
56+
57+
tags = {
58+
environment = "dev"
59+
costcenter = "it"
60+
}
61+
}
62+
```
63+
64+
## Usage with the Application Security Group module
65+
66+
The following example demonstrate how to use the network-security-group module with a combination of predefined and custom rules with ASG source or destination.
67+
68+
```hcl
69+
provider "azurerm" {
70+
features {}
71+
}
72+
73+
resource "azurerm_resource_group" "example" {
74+
name = "my-resources"
75+
location = "West Europe"
76+
}
77+
78+
resource "azurerm_application_security_group" "first" {
79+
name = "asg-first"
80+
location = "eastus"
81+
resource_group_name = azurerm_resource_group.example.name
82+
}
83+
84+
resource "azurerm_application_security_group" "second" {
85+
name = "asg-second"
86+
location = "eastus"
87+
resource_group_name = azurerm_resource_group.example.name
88+
}
89+
90+
module "network-security-group" {
91+
source = "Azure/network-security-group/azurerm"
92+
resource_group_name = azurerm_resource_group.example.name
93+
location = "eastus"
94+
security_group_name = "nsg"
95+
predefined_rules = [
96+
{
97+
name = "SSH"
98+
priority = "500"
99+
source_application_security_group_ids = [azurerm_application_security_group.first.id]
100+
}
101+
]
102+
103+
custom_rules = [
104+
{
105+
name = "myhttp"
106+
priority = "200"
107+
direction = "Inbound"
108+
access = "Allow"
109+
protocol = "tcp"
110+
destination_port_range = "8080"
111+
description = "description-myhttp"
112+
destination_application_security_group_ids = [azurerm_application_security_group.second.id]
113+
}
114+
]
51115
tags = {
52116
environment = "dev"
53117
costcenter = "it"
@@ -60,14 +124,14 @@ module "network-security-group" {
60124
The following example demonstrate how to use the pre-defined HTTP module with a custom rule for ssh.
61125

62126
```hcl
63-
resource "azurerm_resource_group" "test" {
127+
resource "azurerm_resource_group" "example" {
64128
name = "my-resources"
65129
location = "West Europe"
66130
}
67131
68132
module "network-security-group" {
69133
source = "Azure/network-security-group/azurerm//modules/HTTP"
70-
resource_group_name = azurerm_resource_group.test.name
134+
resource_group_name = azurerm_resource_group.example.name
71135
security_group_name = "nsg"
72136
custom_rules = [
73137
{
@@ -126,7 +190,7 @@ $ rake full
126190

127191
### Docker
128192

129-
We provide a Dockerfile to build a new image based `FROM` the `microsoft/terraform-test` Docker hub image which adds additional tools / packages specific for this module (see Custom Image section). Alternatively use only the `microsoft/terraform-test` Docker hub image [by using these instructions](https://github.com/Azure/terraform-test).
193+
We provide a Dockerfile to build a new image based `FROM` the `mcr.microsoft.com/terraform-test` Docker hub image which adds additional tools / packages specific for this module (see Custom Image section). Alternatively use only the `mcr.microsoft.com/terraform-test` Docker hub image [by using these instructions](https://github.com/Azure/terraform-test).
130194

131195
#### Prerequisites
132196

main.tf

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,41 @@ resource "azurerm_network_security_group" "nsg" {
1414
#############################
1515

1616
resource "azurerm_network_security_rule" "predefined_rules" {
17-
count = length(var.predefined_rules)
18-
name = lookup(var.predefined_rules[count.index], "name")
19-
priority = lookup(var.predefined_rules[count.index], "priority", 4096 - length(var.predefined_rules) + count.index)
20-
direction = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 0)
21-
access = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 1)
22-
protocol = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 2)
23-
source_port_ranges = split(",", replace(lookup(var.predefined_rules[count.index], "source_port_range", "*"), "*", "0-65535"))
24-
destination_port_range = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 4)
25-
description = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 5)
26-
source_address_prefix = join(",", var.source_address_prefix)
27-
destination_address_prefix = join(",", var.destination_address_prefix)
28-
resource_group_name = data.azurerm_resource_group.nsg.name
29-
network_security_group_name = azurerm_network_security_group.nsg.name
17+
count = length(var.predefined_rules)
18+
name = lookup(var.predefined_rules[count.index], "name")
19+
priority = lookup(var.predefined_rules[count.index], "priority", 4096 - length(var.predefined_rules) + count.index)
20+
direction = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 0)
21+
access = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 1)
22+
protocol = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 2)
23+
source_port_ranges = split(",", replace(lookup(var.predefined_rules[count.index], "source_port_range", "*"), "*", "0-65535"))
24+
destination_port_range = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 4)
25+
description = element(var.rules[lookup(var.predefined_rules[count.index], "name")], 5)
26+
source_address_prefix = length(lookup(var.predefined_rules[count.index], "source_application_security_group_ids", [])) == 0 ? join(",", var.source_address_prefix) : ""
27+
destination_address_prefix = length(lookup(var.predefined_rules[count.index], "destination_application_security_group_ids", [])) == 0 ? join(",", var.destination_address_prefix) : ""
28+
resource_group_name = data.azurerm_resource_group.nsg.name
29+
network_security_group_name = azurerm_network_security_group.nsg.name
30+
source_application_security_group_ids = lookup(var.predefined_rules[count.index], "source_application_security_group_ids", [])
31+
destination_application_security_group_ids = lookup(var.predefined_rules[count.index], "destination_application_security_group_ids", [])
3032
}
3133

3234
#############################
3335
# Detailed security rules #
3436
#############################
3537

3638
resource "azurerm_network_security_rule" "custom_rules" {
37-
count = length(var.custom_rules)
38-
name = lookup(var.custom_rules[count.index], "name", "default_rule_name")
39-
priority = lookup(var.custom_rules[count.index], "priority")
40-
direction = lookup(var.custom_rules[count.index], "direction", "Any")
41-
access = lookup(var.custom_rules[count.index], "access", "Allow")
42-
protocol = lookup(var.custom_rules[count.index], "protocol", "*")
43-
source_port_ranges = split(",", replace(lookup(var.custom_rules[count.index], "source_port_range", "*"), "*", "0-65535"))
44-
destination_port_ranges = split(",", replace(lookup(var.custom_rules[count.index], "destination_port_range", "*"), "*", "0-65535"))
45-
source_address_prefix = lookup(var.custom_rules[count.index], "source_address_prefix", "*")
46-
destination_address_prefix = lookup(var.custom_rules[count.index], "destination_address_prefix", "*")
47-
description = lookup(var.custom_rules[count.index], "description", "Security rule for ${lookup(var.custom_rules[count.index], "name", "default_rule_name")}")
48-
resource_group_name = data.azurerm_resource_group.nsg.name
49-
network_security_group_name = azurerm_network_security_group.nsg.name
39+
count = length(var.custom_rules)
40+
name = lookup(var.custom_rules[count.index], "name", "default_rule_name")
41+
priority = lookup(var.custom_rules[count.index], "priority")
42+
direction = lookup(var.custom_rules[count.index], "direction", "Any")
43+
access = lookup(var.custom_rules[count.index], "access", "Allow")
44+
protocol = lookup(var.custom_rules[count.index], "protocol", "*")
45+
source_port_ranges = split(",", replace(lookup(var.custom_rules[count.index], "source_port_range", "*"), "*", "0-65535"))
46+
destination_port_ranges = split(",", replace(lookup(var.custom_rules[count.index], "destination_port_range", "*"), "*", "0-65535"))
47+
source_address_prefix = length(lookup(var.custom_rules[count.index], "source_application_security_group_ids", [])) == 0 ? lookup(var.custom_rules[count.index], "source_address_prefix", "*") : ""
48+
destination_address_prefix = length(lookup(var.custom_rules[count.index], "destination_application_security_group_ids", [])) == 0 ? lookup(var.custom_rules[count.index], "destination_address_prefix", "*") : ""
49+
description = lookup(var.custom_rules[count.index], "description", "Security rule for ${lookup(var.custom_rules[count.index], "name", "default_rule_name")}")
50+
resource_group_name = data.azurerm_resource_group.nsg.name
51+
network_security_group_name = azurerm_network_security_group.nsg.name
52+
source_application_security_group_ids = lookup(var.custom_rules[count.index], "source_application_security_group_ids", [])
53+
destination_application_security_group_ids = lookup(var.custom_rules[count.index], "destination_application_security_group_ids", [])
5054
}

test/fixture/main.tf

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,62 @@ module "testPredefinedAD" {
2626
security_group_name = "nsg_testPredefinedAD"
2727
}
2828

29+
resource "azurerm_application_security_group" "first" {
30+
name = "acctest-first"
31+
location = azurerm_resource_group.test.location
32+
resource_group_name = azurerm_resource_group.test.name
33+
}
34+
35+
resource "azurerm_application_security_group" "second" {
36+
name = "acctest-second"
37+
location = azurerm_resource_group.test.location
38+
resource_group_name = azurerm_resource_group.test.name
39+
}
40+
2941
module "testPredefinedRuleWithCustom" {
3042
source = "../../"
3143
resource_group_name = azurerm_resource_group.test.name
3244
security_group_name = "nsg_testPredefinedWithCustom"
33-
custom_rules = var.custom_rules
34-
predefined_rules = var.predefined_rules
45+
predefined_rules = [
46+
{
47+
name = "HTTP"
48+
source_application_security_group_ids = [azurerm_application_security_group.first.id]
49+
},
50+
{
51+
name = "HTTPS"
52+
priority = 510
53+
},
54+
]
3555
}
3656

57+
58+
3759
module "testCustom" {
3860
source = "../../"
3961
resource_group_name = azurerm_resource_group.test.name
4062
security_group_name = "nsg_testCustom"
41-
custom_rules = var.custom_rules
63+
custom_rules = [
64+
{
65+
name = "myssh"
66+
priority = 201
67+
direction = "Inbound"
68+
access = "Allow"
69+
protocol = "tcp"
70+
source_port_range = "*"
71+
destination_port_range = "22"
72+
description = "description-myssh"
73+
source_application_security_group_ids = [azurerm_application_security_group.first.id]
74+
},
75+
{
76+
name = "myhttp"
77+
priority = 200
78+
direction = "Inbound"
79+
access = "Allow"
80+
protocol = "tcp"
81+
source_port_range = "*"
82+
destination_port_range = "8080"
83+
description = "description-http"
84+
destination_application_security_group_ids = [azurerm_application_security_group.second.id]
85+
},
86+
]
4287
}

test/fixture/terraform.tfvars

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1 @@
1-
location = "westus"
2-
3-
custom_rules = [
4-
{
5-
name = "myssh"
6-
priority = "101"
7-
direction = "Inbound"
8-
access = "Allow"
9-
protocol = "tcp"
10-
source_port_range = "1234"
11-
destination_port_range = "22"
12-
description = "description-myssh"
13-
},
14-
{
15-
name = "myhttp"
16-
priority = "200"
17-
direction = "Inbound"
18-
access = "Allow"
19-
protocol = "tcp"
20-
source_port_range = "666,4096-4098"
21-
destination_port_range = "8080"
22-
description = "description-http"
23-
},
24-
]
25-
26-
predefined_rules = [
27-
{
28-
name = "HTTP"
29-
source_port_range = "666,1024-1026"
30-
},
31-
{
32-
name = "HTTPS"
33-
priority = "510"
34-
},
35-
]
1+
location = "westus"

test/fixture/variables.tf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
variable "location" {}
2-
3-
variable "custom_rules" {
4-
type = list(any)
5-
}
6-
7-
variable "predefined_rules" {
8-
type = list(any)
9-
}

variables.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "location" {
2424

2525
# Predefined rules
2626
variable "predefined_rules" {
27-
type = list(any)
27+
type = any
2828
default = []
2929
}
3030

@@ -33,7 +33,7 @@ variable "predefined_rules" {
3333
# All the fields are required.
3434
variable "custom_rules" {
3535
description = "Security rules for the network security group using this format name = [priority, direction, access, protocol, source_port_range, destination_port_range, source_address_prefix, destination_address_prefix, description]"
36-
type = list(any)
36+
type = any
3737
default = []
3838
}
3939

@@ -52,3 +52,15 @@ variable "destination_address_prefix" {
5252

5353
# Example ["10.0.3.0/32","10.0.3.128/32"] or ["VirtualNetwork"]
5454
}
55+
56+
variable "source_application_security_group_ids" {
57+
description = "(Optional) A List of source Application Security Group IDs. Conflicted with `source_address_prefix`. Once assigned with `source_address_prefix`, it'll have a higher priority."
58+
type = set(string)
59+
default = []
60+
}
61+
62+
variable "destination_application_security_group_ids" {
63+
description = "(Optional) A List of destination Application Security Group IDs. Conflicted with `destination_address_prefix`. Once assigned with `destination_address_prefix`, it'll have a higher priority."
64+
type = set(string)
65+
default = []
66+
}

0 commit comments

Comments
 (0)