-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
181 lines (150 loc) · 5.08 KB
/
main.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
181
provider "vault" {
address = "https://10.1.132.21:8200"
skip_tls_verify = true
}
resource "vault_mount" "root" {
path = "root"
type = "pki"
}
resource "vault_pki_secret_backend_config_urls" "root" {
backend = vault_mount.root.path
issuing_certificates = [
"https://10.1.132.21:8200/v1/${vault_mount.root.path}/ca",
]
}
resource "vault_mount" "intermediate_one" {
path = "intermediate/playground"
type = "pki"
}
resource "vault_pki_secret_backend_config_urls" "intermediate_one" {
backend = vault_mount.intermediate_one.path
issuing_certificates = [
"https://10.1.132.21:8200/v1/${vault_mount.intermediate_one.path}/ca",
]
}
resource "vault_mount" "intermediate_two" {
path = "intermediate/two"
type = "pki"
}
resource "vault_pki_secret_backend_config_urls" "intermediate_two" {
backend = vault_mount.intermediate_two.path
issuing_certificates = [
"https://10.1.132.21:8200/v1/${vault_mount.intermediate_two.path}/ca",
]
}
resource "vault_pki_secret_backend_root_cert" "this" {
backend = vault_mount.root.path
type = "internal"
common_name = "Root CA"
ttl = "315360000"
format = "pem"
key_type = "rsa"
key_bits = 4096
exclude_cn_from_sans = true
ou = "My OU"
organization = "My organization"
depends_on = [vault_mount.root]
}
# Intermediate Level 1
# Will be recreated whenever `vault_mount.intermediate_one` is recreated.
# This will in turn force `vault_pki_secret_backend_intermediate_cert_request`
# to also be recreated, which will not happen otherwise.
#
# If the cert request isn't recreated when the PKI mount is, the issuer ref will
# not be valid, and certs cannot be issued.
resource "null_resource" "intermediate_one_tracker" {
triggers = {
accessor = vault_mount.intermediate_one.accessor
}
}
resource "vault_pki_secret_backend_intermediate_cert_request" "one" {
backend = vault_mount.intermediate_one.path
type = "internal"
common_name = "Intermediate Level 1"
depends_on = [vault_mount.intermediate_one]
lifecycle {
replace_triggered_by = [
null_resource.intermediate_one_tracker,
]
}
}
resource "vault_pki_secret_backend_root_sign_intermediate" "intermediate_one" {
backend = vault_mount.root.path
csr = vault_pki_secret_backend_intermediate_cert_request.one.csr
common_name = "Intermediate Level 1"
exclude_cn_from_sans = true
ou = "My OU"
organization = "My organization"
depends_on = [vault_pki_secret_backend_intermediate_cert_request.one]
}
resource "vault_pki_secret_backend_intermediate_set_signed" "one" {
backend = vault_mount.intermediate_one.path
certificate = vault_pki_secret_backend_root_sign_intermediate.intermediate_one.certificate
}
# Intermediate Level 2
# Same explanation with the `null_resource` hop as with Intermediate Level 1.
resource "null_resource" "intermediate_two_tracker" {
triggers = {
accessor = vault_mount.intermediate_two.accessor
}
}
resource "vault_pki_secret_backend_intermediate_cert_request" "two" {
backend = vault_mount.intermediate_two.path
type = "internal"
common_name = "Intermediate Authority Level 2"
depends_on = [
vault_mount.intermediate_two,
vault_pki_secret_backend_intermediate_set_signed.one,
]
lifecycle {
replace_triggered_by = [
null_resource.intermediate_two_tracker,
]
}
}
resource "vault_pki_secret_backend_root_sign_intermediate" "two" {
backend = vault_mount.intermediate_one.path
csr = vault_pki_secret_backend_intermediate_cert_request.two.csr
common_name = "Intermediate Authority Level 2"
exclude_cn_from_sans = true
ou = "My OU"
organization = "My organization"
depends_on = [
vault_mount.intermediate_one,
vault_pki_secret_backend_intermediate_cert_request.one,
]
}
resource "vault_pki_secret_backend_intermediate_set_signed" "two" {
backend = vault_mount.intermediate_two.path
certificate = vault_pki_secret_backend_root_sign_intermediate.two.certificate
}
resource "vault_pki_secret_backend_role" "this" {
backend = vault_mount.intermediate_two.path
name = "server"
ttl = 3600
allow_ip_sans = true
key_type = "rsa"
key_bits = 4096
allow_subdomains = true
allow_any_name = true
depends_on = [vault_pki_secret_backend_intermediate_set_signed.two]
}
resource "vault_pki_secret_backend_cert" "this" {
backend = vault_mount.intermediate_two.path
name = vault_pki_secret_backend_role.this.name
common_name = "some.name.here"
depends_on = [
vault_pki_secret_backend_role.this,
vault_pki_secret_backend_root_cert.this
]
}
output "intermediate_two_cert" {
value = vault_pki_secret_backend_root_sign_intermediate.two.certificate
}
output "leaf_cert" {
value = vault_pki_secret_backend_cert.this.certificate
}
output "leaf_key" {
value = vault_pki_secret_backend_cert.this.private_key
sensitive = true
}