forked from Alexander-Babichuk/aws-cvpn-endpoints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.tf
180 lines (150 loc) · 5.29 KB
/
tls.tf
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
# ------------------------------------------------------------------------------
# Generate the root CA
# ------------------------------------------------------------------------------
# Create CA private key
resource "tls_private_key" "ca" {
algorithm = "RSA"
rsa_bits = var.rsa_bits
}
# Export the CA private key in a file.
resource "local_file" "ca_key" {
content = tls_private_key.ca.private_key_pem
filename = format("%s_ca.key", local.bundle)
file_permission = "0600"
}
# Create CA public cert
resource "tls_self_signed_cert" "ca" {
key_algorithm = tls_private_key.ca.algorithm
private_key_pem = tls_private_key.ca.private_key_pem
is_ca_certificate = true
validity_period_hours = var.validity_period_hours
# RFC5280 https://www.terraform.io/docs/providers/tls/r/self_signed_cert.html#allowed_uses."
allowed_uses = [
"cert_signing",
"crl_signing",
]
subject {
common_name = var.common_name
organization = var.organization
}
}
# Export the CA public cert in a file.
resource "local_file" "ca_cert" {
content = tls_self_signed_cert.ca.cert_pem
filename = format("%s_ca.crt", local.bundle)
file_permission = "0600"
}
# ------------------------------------------------------------------------------
# Generate server side side tls bundle
# ------------------------------------------------------------------------------
# Create server side private key
resource "tls_private_key" "server_side" {
algorithm = "RSA"
rsa_bits = var.rsa_bits
}
# Export the cert`s private key to a file.
resource "local_file" "server_key" {
content = tls_private_key.server_side.private_key_pem
filename = format("%s_server.key", local.bundle)
file_permission = "0600"
}
# Create server side public cert signing request(CSR)
resource "tls_cert_request" "server_side" {
key_algorithm = tls_private_key.server_side.algorithm
private_key_pem = tls_private_key.server_side.private_key_pem
subject {
common_name = format("server.%s", var.common_name)
organization = var.organization
}
}
# Create server side signed public cert
resource "tls_locally_signed_cert" "server_side" {
cert_request_pem = tls_cert_request.server_side.cert_request_pem
ca_key_algorithm = tls_private_key.ca.algorithm
ca_private_key_pem = tls_private_key.ca.private_key_pem
ca_cert_pem = tls_self_signed_cert.ca.cert_pem
validity_period_hours = var.validity_period_hours
# RFC5280 https://www.terraform.io/docs/providers/tls/r/self_signed_cert.html#allowed_uses."
allowed_uses = [
"digital_signature",
"key_encipherment",
"server_auth",
]
}
# Export the cert`s public key to a file.
resource "local_file" "server_cert" {
content = tls_locally_signed_cert.server_side.cert_pem
filename = format("%s_server.crt", local.bundle)
file_permission = "0600"
}
# Import server side certificates to ACM
resource "aws_acm_certificate" "server_side" {
private_key = tls_private_key.server_side.private_key_pem
certificate_body = tls_locally_signed_cert.server_side.cert_pem
certificate_chain = tls_self_signed_cert.ca.cert_pem
tags = merge(
var.tags,
{
Bundle = local.bundle
Terraform = "true"
Tier = "server-side"
}
)
}
# ------------------------------------------------------------------------------
# Generate client side tls bundle
# ------------------------------------------------------------------------------
# Create client side private key
resource "tls_private_key" "client_side" {
algorithm = "RSA"
rsa_bits = var.rsa_bits
}
# Export the cert`s private key to a file.
resource "local_file" "client_key" {
content = tls_private_key.client_side.private_key_pem
filename = format("%s_client.key", local.bundle)
file_permission = "0600"
}
# Create client side public cert signing request(CSR)
resource "tls_cert_request" "client_side" {
key_algorithm = tls_private_key.client_side.algorithm
private_key_pem = tls_private_key.client_side.private_key_pem
subject {
common_name = format("client.%s", var.common_name)
organization = var.organization
}
}
# Create client side signed public cert
resource "tls_locally_signed_cert" "client_side" {
cert_request_pem = tls_cert_request.client_side.cert_request_pem
ca_key_algorithm = tls_private_key.ca.algorithm
ca_private_key_pem = tls_private_key.ca.private_key_pem
ca_cert_pem = tls_self_signed_cert.ca.cert_pem
validity_period_hours = var.validity_period_hours
# RFC5280 https://www.terraform.io/docs/providers/tls/r/self_signed_cert.html#allowed_uses."
allowed_uses = [
"digital_signature",
"key_agreement",
"client_auth",
]
}
# Export the cert`s public key to a file.
resource "local_file" "client_cert" {
content = tls_locally_signed_cert.client_side.cert_pem
filename = format("%s_client.crt", local.bundle)
file_permission = "0600"
}
# Import client side certificates to ACM
resource "aws_acm_certificate" "client_side" {
private_key = tls_private_key.client_side.private_key_pem
certificate_body = tls_locally_signed_cert.client_side.cert_pem
certificate_chain = tls_self_signed_cert.ca.cert_pem
tags = merge(
var.tags,
{
Bundle = local.bundle
Terraform = "true"
Tier = "client-side"
}
)
}