/
resource_pki_secret_backend_root_cert.go
316 lines (287 loc) · 8.94 KB
/
resource_pki_secret_backend_root_cert.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package vault
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/hashicorp/vault/api"
)
func pkiSecretBackendRootCertResource() *schema.Resource {
return &schema.Resource{
Create: pkiSecretBackendRootCertCreate,
Read: pkiSecretBackendRootCertRead,
Update: pkiSecretBackendRootCertUpdate,
Delete: pkiSecretBackendRootCertDelete,
Schema: map[string]*schema.Schema{
"backend": {
Type: schema.TypeString,
Required: true,
Description: "The PKI secret backend the resource belongs to.",
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Type of intermediate to create. Must be either \"exported\" or \"internal\".",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"exported", "internal"}, false),
},
"common_name": {
Type: schema.TypeString,
Required: true,
Description: "CN of intermediate to create.",
ForceNew: true,
},
"alt_names": {
Type: schema.TypeList,
Optional: true,
Description: "List of alternative names.",
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ip_sans": {
Type: schema.TypeList,
Optional: true,
Description: "List of alternative IPs.",
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"uri_sans": {
Type: schema.TypeList,
Optional: true,
Description: "List of alterative URIs.",
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"other_sans": {
Type: schema.TypeList,
Optional: true,
Description: "List of other SANs.",
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ttl": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Description: "Time to leave.",
},
"format": {
Type: schema.TypeString,
Optional: true,
Description: "The format of data.",
ForceNew: true,
Default: "pem",
ValidateFunc: validation.StringInSlice([]string{"pem", "der", "pem_bundle"}, false),
},
"private_key_format": {
Type: schema.TypeString,
Optional: true,
Description: "The private key format.",
ForceNew: true,
Default: "der",
ValidateFunc: validation.StringInSlice([]string{"der", "pkcs8"}, false),
},
"key_type": {
Type: schema.TypeString,
Optional: true,
Description: "The desired key type.",
ForceNew: true,
Default: "rsa",
ValidateFunc: validation.StringInSlice([]string{"rsa", "ec"}, false),
},
"key_bits": {
Type: schema.TypeInt,
Optional: true,
Description: "The number of bits to use.",
ForceNew: true,
Default: 2048,
},
"max_path_length": {
Type: schema.TypeInt,
Optional: true,
Description: "The maximum path length to encode in the generated certificate.",
ForceNew: true,
Default: -1,
},
"exclude_cn_from_sans": {
Type: schema.TypeBool,
Optional: true,
Description: "Flag to exclude CN from SANs.",
ForceNew: true,
},
"permitted_dns_domains": {
Type: schema.TypeList,
Optional: true,
Description: "List of domains for which certificates are allowed to be issued.",
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ou": {
Type: schema.TypeString,
Optional: true,
Description: "The organization unit.",
ForceNew: true,
},
"organization": {
Type: schema.TypeString,
Optional: true,
Description: "The organization.",
ForceNew: true,
},
"country": {
Type: schema.TypeString,
Optional: true,
Description: "The country.",
ForceNew: true,
},
"locality": {
Type: schema.TypeString,
Optional: true,
Description: "The locality.",
ForceNew: true,
},
"province": {
Type: schema.TypeString,
Optional: true,
Description: "The province.",
ForceNew: true,
},
"street_address": {
Type: schema.TypeString,
Optional: true,
Description: "The street address.",
ForceNew: true,
},
"postal_code": {
Type: schema.TypeString,
Optional: true,
Description: "The postal code.",
ForceNew: true,
},
"certificate": {
Type: schema.TypeString,
Computed: true,
Description: "The certicate.",
},
"issuing_ca": {
Type: schema.TypeString,
Computed: true,
Description: "The issuing CA.",
},
"serial": {
Type: schema.TypeString,
Computed: true,
Description: "The serial number.",
},
},
}
}
func pkiSecretBackendRootCertCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
backend := d.Get("backend").(string)
rootType := d.Get("type").(string)
path := pkiSecretBackendIntermediateSetSignedReadPath(backend, rootType)
iAltNames := d.Get("alt_names").([]interface{})
altNames := make([]string, 0, len(iAltNames))
for _, iAltName := range iAltNames {
altNames = append(altNames, iAltName.(string))
}
iIPSans := d.Get("ip_sans").([]interface{})
ipSans := make([]string, 0, len(iIPSans))
for _, iIpSan := range iIPSans {
ipSans = append(ipSans, iIpSan.(string))
}
iURISans := d.Get("uri_sans").([]interface{})
uriSans := make([]string, 0, len(iURISans))
for _, iUriSan := range iURISans {
uriSans = append(uriSans, iUriSan.(string))
}
iOtherSans := d.Get("other_sans").([]interface{})
otherSans := make([]string, 0, len(iOtherSans))
for _, iOtherSan := range iOtherSans {
otherSans = append(otherSans, iOtherSan.(string))
}
iPermittedDNSDomains := d.Get("permitted_dns_domains").([]interface{})
permittedDNSDomains := make([]string, 0, len(iPermittedDNSDomains))
for _, iPermittedDNSDomain := range iPermittedDNSDomains {
permittedDNSDomains = append(permittedDNSDomains, iPermittedDNSDomain.(string))
}
data := map[string]interface{}{
"common_name": d.Get("common_name").(string),
"ttl": d.Get("ttl").(string),
"format": d.Get("format").(string),
"private_key_format": d.Get("private_key_format").(string),
"key_type": d.Get("key_type").(string),
"key_bits": d.Get("key_bits").(int),
"max_path_length": d.Get("max_path_length").(int),
"exclude_cn_from_sans": d.Get("exclude_cn_from_sans").(bool),
"ou": d.Get("ou").(string),
"organization": d.Get("organization").(string),
"country": d.Get("country").(string),
"locality": d.Get("locality").(string),
"province": d.Get("province").(string),
"street_address": d.Get("street_address").(string),
"postal_code": d.Get("postal_code").(string),
}
if len(altNames) > 0 {
data["alt_names"] = strings.Join(altNames, ",")
}
if len(ipSans) > 0 {
data["ip_sans"] = strings.Join(ipSans, ",")
}
if len(uriSans) > 0 {
data["uri_sans"] = strings.Join(uriSans, ",")
}
if len(otherSans) > 0 {
data["other_sans"] = strings.Join(otherSans, ",")
}
if len(permittedDNSDomains) > 0 {
data["permitted_dns_domains"] = strings.Join(permittedDNSDomains, ",")
}
log.Printf("[DEBUG] Creating root cert on PKI secret backend %q", backend)
resp, err := client.Logical().Write(path, data)
if err != nil {
return fmt.Errorf("error creating root cert for PKI secret backend %q: %s", backend, err)
}
log.Printf("[DEBUG] Created root cert on PKI secret backend %q", backend)
d.Set("certificate", resp.Data["certificate"])
d.Set("issuing_ca", resp.Data["issuing_ca"])
d.Set("serial", resp.Data["serial"])
d.SetId(path)
return pkiSecretBackendRootCertRead(d, meta)
}
func pkiSecretBackendRootCertRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func pkiSecretBackendRootCertUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}
func pkiSecretBackendRootCertDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
backend := d.Get("backend").(string)
path := pkiSecretBackendIntermediateSetSignedDeletePath(backend)
log.Printf("[DEBUG] Deleting root cert from PKI secret backend %q", path)
if _, err := client.Logical().Delete(path); err != nil {
return fmt.Errorf("error deleting root cert from PKI secret backend %q: %s", path, err)
}
log.Printf("[DEBUG] Deleted root cert from PKI secret backend %q", path)
return nil
}
func pkiSecretBackendIntermediateSetSignedReadPath(backend string, rootType string) string {
return strings.Trim(backend, "/") + "/root/generate/" + strings.Trim(rootType, "/")
}
func pkiSecretBackendIntermediateSetSignedDeletePath(backend string) string {
return strings.Trim(backend, "/") + "/root"
}