Skip to content

Commit 8f912a1

Browse files
committed
Baseline commit of security group module definitions.
0 parents  commit 8f912a1

File tree

4 files changed

+360
-0
lines changed

4 files changed

+360
-0
lines changed

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
# Rules Terraform Modules | Creates Security Group | Adds Rules
3+
4+
This security-group module **adds ingress and egress rules** to **either the default or a new** security group within a given VPC.
5+
6+
## Simple Module Usage Example
7+
8+
To use this module simply declare it like below supplying it with a mandatory VPC id. If you omit **in_ingress** a default ssh rule is created. A default **all traffic egress rule** is also created but you can override this behaviour if you so wish.
9+
10+
module security_group_module
11+
{
12+
source = "rules"
13+
in_vpc_id = "${module.vpc.vpc_id}"
14+
in_ingress = [ "ssh", "http", "https" ]
15+
}
16+
17+
This module defines two **list outputs** called **out_default_security_group_ids** and **out_new_security_group_ids**. Use the first after creating rules against the VPC's default security group and the second after a new security group is created (see variable in_use_default).
18+
19+
vpc_security_group_ids = [ "${module.security_group_module.out_default_security_group_ids}" ]
20+
21+
## Security Group Module Inputs
22+
23+
The security group's input variables are vital to achieving the desired behaviour.
24+
25+
| Imported | Type | Default | Comment |
26+
|:-------- |:---- |:------- |:------- |
27+
**in_vpc_id** | String | vpc-1234567890 | create security group/s under VPC with this id
28+
**in_use_default** | Boolean | [ true ] | use the default security group if true else create one
29+
**in_ingress** | List | [ "postgres", "https"] | identigy the ports to allow for inbound traffic
30+
**in_egress** | List | [ "all-traffic" ] | identigy the ports to allow for outbound traffic
31+
**in_ingress_cidr_blocks** | List | [ "0.0.0.0/0"] | list of source incoming traffic addresses to allow
32+
**in_egress_cidr_blocks** | List | [ "0.0.0.0/0"] | list of VPC source outgoing traffic addresses to allow
33+
**in_ecosystem_id** | String | kube-19188-2306 | the ecosystem's identifier including a timestamp
34+
35+
## Alternate Module Inputs
36+
37+
This security group module is simple but flexible as it needs to cater to many different tastes. Now follows a number of **overloading** facilities to craft your security group's behaviour.
38+
39+
### Specify the Creation of a Security Group
40+
41+
Passing **false** to the **in_use_default** flag causes the **creation of a security group**.
42+
43+
module security_group_module
44+
{
45+
source = "security"
46+
in_vpc_id = "${module.xyz.out_vpc_id}"
47+
in_ingress = [ "ssh", "http", "https" ]
48+
in_egress = [ "all-traffic" ]
49+
50+
in_use_default = false
51+
}
52+
53+
Note that this module only creates one security group at a time. To create two or more simply repeat the module declaration using a different name each time.
54+
55+
### Specify Ingress and Egress Cidr Blocks
56+
57+
Most security group **source cidr blocks** allow traffic originating from anywhere ( 0.0.0.0/0 ).
58+
59+
This is true for both inress (incoming) and egress (outgoing) traffic. You can alter this by specifying these extra module inputs. The below allows traffic **in only from a given VPC** and **out only from a subnet** within the security group's VPC.
60+
61+
in_ingress_cidr_blocks = [ "172.30.0.0/16" ]
62+
in_egress_cidr_blocks = [ "10.2.0.4/24" ]
63+
64+
The cidr blocks are lists of strings so you can also allow traffic from more than one source block.
65+
66+
in_ingress_cidr_blocks = [ "172.30.0.0/0", "82.9.72.144/31" ]
67+
68+
### Specify Other Ingress and Egress Rules
69+
70+
Clearly you will want to allow ingress and egress traffic for various middleware services.
71+
72+
> @todo Author then link to sister page containing a table full of rule classifications.
73+
74+
Note that if you create an all traffic egress rule and you have an **IPV6 cidr block**, AWS will create an extra **::/0** egress rule in addition to the 0.0.0.0/0 (IPV4) rule.
75+
76+
## Running the Module's Tests
77+
78+
Visit the README within the **ztest-security** folder for instructions on running this module's tests.
79+
80+
## Creating New Rule Groups
81+
82+
The outer list has no size restrictions but the inner list is expected to contain 4 elements.
83+
84+
- the port [from] which [inbound] traffic should be allowed
85+
- the port [to] which [inbound] traffic should be allowed
86+
- the ICMA protocol that the traffic obeys
87+
- the (name) description of the traffic rule

security.groups-main.tf

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
### ####################################### ###
3+
### [[resource]] aws_default_security_group ###
4+
### ####################################### ###
5+
/*
6+
resource aws_security_group sgroup-new
7+
{
8+
count = "${var.in_use_default == false ? 1 : 0}"
9+
10+
name = "security-group-${var.in_ecosystem_id}"
11+
description = "This security group ${var.in_history_note}"
12+
vpc_id = "${var.in_vpc_id}"
13+
14+
tags
15+
{
16+
Name = "security-group-${var.in_ecosystem_id}"
17+
Group = "eco-system-${var.in_ecosystem_id}"
18+
Desc = "This security group ${var.in_history_note}"
19+
}
20+
}
21+
*/
22+
23+
24+
### ####################################### ###
25+
### [[resource]] aws_default_security_group ###
26+
### ####################################### ###
27+
28+
resource aws_default_security_group default
29+
{
30+
vpc_id = "${var.in_vpc_id}"
31+
32+
tags
33+
{
34+
Name = "default-sg-${var.in_ecosystem_id}"
35+
Group = "eco-system-${var.in_ecosystem_id}"
36+
Desc = "This default VPC security group ${var.in_history_note}"
37+
}
38+
39+
}
40+
41+
42+
### #################################### ###
43+
### [[resource]] aws_security_group_rule ###
44+
### #################################### ###
45+
46+
resource aws_security_group_rule ingress
47+
{
48+
count = "${length(var.in_ingress)}"
49+
50+
# ---@----@--> security_group_id = "${var.in_use_default == true ? aws_default_security_group.default.id : aws_security_group.sgroup-new.id}"
51+
security_group_id = "${aws_default_security_group.default.id}"
52+
53+
type = "ingress"
54+
cidr_blocks = ["${var.in_ingress_cidr_blocks}"]
55+
description = "${element(var.rules[var.in_ingress[count.index]], 3)}"
56+
57+
from_port = "${element(var.rules[var.in_ingress[count.index]], 0)}"
58+
to_port = "${element(var.rules[var.in_ingress[count.index]], 1)}"
59+
protocol = "${element(var.rules[var.in_ingress[count.index]], 2)}"
60+
}
61+
62+
### #################################### ###
63+
### [[resource]] aws_security_group_rule ###
64+
### #################################### ###
65+
66+
resource aws_security_group_rule egress
67+
{
68+
count = "${length(var.in_egress)}"
69+
70+
# ---@----@--> security_group_id = "${var.in_use_default == true ? aws_default_security_group.default.id : aws_security_group.sgroup-new.id}"
71+
security_group_id = "${aws_default_security_group.default.id}"
72+
73+
type = "egress"
74+
cidr_blocks = ["${var.in_egress_cidr_blocks}"]
75+
description = "${element(var.rules[var.in_egress[count.index]], 3)}"
76+
77+
from_port = "${element(var.rules[var.in_egress[count.index]], 0)}"
78+
to_port = "${element(var.rules[var.in_egress[count.index]], 1)}"
79+
protocol = "${element(var.rules[var.in_egress[count.index]], 2)}"
80+
}

security.groups-rules.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
# -- ###################################################################################
3+
# -- ###################################################################################
4+
5+
# -- ############################ -- #
6+
# -- How to Add New Traffic Rules -- #
7+
# -- ############################ -- #
8+
9+
# -- In order to add new rules be informed that
10+
# --
11+
# -- - lists are in the form [ port-from, port-to, protocol, description ]
12+
# -- - the first two elements are integers and the final two are strings
13+
# -- - it pays to be conservative with the description characters and length
14+
# -- - ports can range from 0 to one less than 2^16 (which is 65,535)
15+
# -- - a -1 port signals that all ports are to be allowed (disallowed)
16+
# -- - the protocol can be one of [ tcp, udp, icmp, all ]
17+
# -- - the protocol can also be one of a small set of numbers
18+
19+
# -- Note that if you create an all traffic egress rule and you have an
20+
# -- IPV6 Cidr block another will be created to ::/0 in addition to the
21+
# -- one with the 0.0.0.0/0 (IPV4) notation.
22+
23+
variable "rules"
24+
{
25+
description = "Modular rules allowing either TCP or UDP traffic."
26+
type = "map"
27+
28+
default
29+
{
30+
31+
# < ~~~ ssh secure shell ~~~ >
32+
ssh = [ 22, 22, "tcp", "secure shell" ]
33+
34+
# < ~~~ http(s) - hyper text transfer protocol ~~~ >
35+
http = [ 80, 80, "tcp", "http plaintext" ]
36+
https = [ 443, 443, "tcp", "http secured" ]
37+
38+
# < ~~~ gollum's webrick http server ~~~ >
39+
gollum = [ 4567, 4567, "tcp", "gollum wiki" ]
40+
41+
# < ~~~ Kubernetes Services Suite ~~~ >
42+
kubernetes = [ 6443, 6443, "tcp", "kubernetes api" ]
43+
kubelet-api = [ 10250, 10250, "tcp", "kubelet api" ]
44+
kube-sched = [ 10251, 10251, "tcp", "kube scheduler" ]
45+
kube-control = [ 10252, 10252, "tcp", "kube controller" ]
46+
kube-read = [ 10255, 10255, "tcp", "kube read only" ]
47+
48+
# < ~~~ etcd client server api ~~~ >
49+
etcd-1 = [ 2379, 2379, "tcp", "etcd services 1" ]
50+
etcd-2 = [ 2380, 2380, "tcp", "etcd services 2" ]
51+
52+
53+
# -- ElasticSearch (ELK) Stack Rules
54+
# -- Remember that ElasticSearch (ELK stack) can require up
55+
# -- to 3 extra inbound ports for the JAVA API (9300), then
56+
# -- the HTTP (80) and HTTPS (443) for the Kibana UI.
57+
58+
elasticsearch = [ 9200, 9200, "tcp", "elasticsearch" ]
59+
60+
61+
# -- Java services traditionally employ port 8080
62+
# -- (tomcat, jenkins, nexus, jserve ...)
63+
64+
java = [ 8080, 8080, "tcp", "HTTP" ]
65+
66+
67+
# Open all ports & protocols
68+
all-traffic = [ -1, -1, "-1", "All protocols" ]
69+
all-tcp = [ 0, 65535, "tcp", "All TCP ports" ]
70+
all-udp = [ 0, 65535, "udp", "All UDP ports" ]
71+
all-icmp = [ -1, -1, "icmp", "All IPV4 ICMP" ]
72+
all-ipv6-icmp = [ -1, -1, 58, "All IPV6 ICMP" ]
73+
74+
}
75+
76+
}
77+
78+
# -- ###################################################################################
79+
# -- ###################################################################################

security.groups-vars.tf

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
################ ######################################## ########
3+
################ Module [[[rules]]] Input Variables List. ########
4+
################ ######################################## ########
5+
6+
### ####################### ###
7+
### [[variable]] in_ingress ###
8+
### ####################### ###
9+
10+
variable in_ingress
11+
{
12+
description = "4 element list defining traffic to allow in (see traffic-rules.tf)"
13+
type = "list"
14+
default = [ "ssh" ]
15+
}
16+
17+
18+
### ###################### ###
19+
### [[variable]] in_egress ###
20+
### ###################### ###
21+
22+
variable in_egress
23+
{
24+
description = "4 element list defining traffic to allow out (see traffic-rules.tf)"
25+
type = "list"
26+
default = [ "all-traffic" ]
27+
}
28+
29+
30+
### ################################### ###
31+
### [[variable]] in_ingress_cidr_blocks ###
32+
### ################################### ###
33+
34+
variable in_ingress_cidr_blocks
35+
{
36+
description = "The IPv4 CIDR ranges from which traffic is allowed to originate."
37+
type = "list"
38+
default = [ "0.0.0.0/0" ]
39+
}
40+
41+
42+
### ################################## ###
43+
### [[variable]] in_egress_cidr_blocks ###
44+
### ################################## ###
45+
46+
variable in_egress_cidr_blocks
47+
{
48+
description = "List of IPv4 CIDR ranges to use on all egress rules"
49+
type = "list"
50+
default = [ "0.0.0.0/0" ]
51+
}
52+
53+
54+
### ###################### ###
55+
### [[variable]] in_vpc_id ###
56+
### ###################### ###
57+
58+
variable in_vpc_id
59+
{
60+
description = "Mandatory VPC ID to create the security group under."
61+
}
62+
63+
64+
### ########################### ###
65+
### [[variable]] in_use_default ###
66+
### ########################### ###
67+
68+
variable in_use_default
69+
{
70+
default = true
71+
}
72+
73+
74+
### ############################ ###
75+
### [[variable]] in_ecosystem_id ###
76+
### ############################ ###
77+
78+
variable in_ecosystem_id
79+
{
80+
description = "Identifier binding all infrastructure components created for this ecosystem instance."
81+
}
82+
83+
84+
### ############################ ###
85+
### [[variable]] in_history_note ###
86+
### ############################ ###
87+
88+
variable in_history_note
89+
{
90+
description = "Note describing the whys and wherefores of this creation."
91+
}
92+
93+
94+
### ######################################### ###
95+
### [[output]] out_default_security_group_ids ###
96+
### ######################################### ###
97+
98+
output out_default_security_group_ids
99+
{
100+
description = "If in_use_default is true this output variable will be set."
101+
# ---@----@--> value = "${aws_default_security_group.default.*.id}"
102+
value = "${aws_default_security_group.default.id}"
103+
}
104+
105+
106+
# ---@----@-->### ############################# ###
107+
# ---@----@-->### [[output]] out_new_security_group_ids ###
108+
# ---@----@-->### ############################# ###
109+
# ---@----@-->output "out_new_security_group_ids"
110+
# ---@----@-->{
111+
# ---@----@--> description = "If in_use_default is false this output variable will be set."
112+
# ---@----@--> value = "${aws_security_group.sgroup-new.*.id}"
113+
# ---@----@-->}
114+

0 commit comments

Comments
 (0)