forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_locally_signed_cert.go
79 lines (68 loc) · 1.89 KB
/
resource_locally_signed_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
package tls
import (
"crypto/x509"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceLocallySignedCert() *schema.Resource {
s := resourceCertificateCommonSchema()
s["cert_request_pem"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "PEM-encoded certificate request",
ForceNew: true,
StateFunc: func(v interface{}) string {
return hashForState(v.(string))
},
}
s["ca_key_algorithm"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Name of the algorithm used to generate the certificate's private key",
ForceNew: true,
}
s["ca_private_key_pem"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "PEM-encoded CA private key used to sign the certificate",
ForceNew: true,
StateFunc: func(v interface{}) string {
return hashForState(v.(string))
},
}
s["ca_cert_pem"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "PEM-encoded CA certificate",
ForceNew: true,
StateFunc: func(v interface{}) string {
return hashForState(v.(string))
},
}
return &schema.Resource{
Create: CreateLocallySignedCert,
Delete: DeleteCertificate,
Read: ReadCertificate,
Schema: s,
}
}
func CreateLocallySignedCert(d *schema.ResourceData, meta interface{}) error {
certReq, err := parseCertificateRequest(d, "cert_request_pem")
if err != nil {
return err
}
caKey, err := parsePrivateKey(d, "ca_private_key_pem", "ca_key_algorithm")
if err != nil {
return err
}
caCert, err := parseCertificate(d, "ca_cert_pem")
if err != nil {
return err
}
cert := x509.Certificate{
Subject: certReq.Subject,
DNSNames: certReq.DNSNames,
IPAddresses: certReq.IPAddresses,
BasicConstraintsValid: true,
}
return createCertificate(d, &cert, caCert, certReq.PublicKey, caKey)
}