-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_pkcs12_bundle.go
100 lines (79 loc) · 2.07 KB
/
resource_pkcs12_bundle.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
package main
import (
"encoding/base64"
"github.com/ephyrasoftware/terraform-provider-keystore/impl"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"io/ioutil"
"os"
"path"
)
func pkcsBundle() *schema.Resource {
return &schema.Resource{
Create: pkcsBundleCreate,
Read: pkcsBundleRead,
Update: pkcsBundleUpdate,
Delete: pkcsBundleDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"cert_pem": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"key_pem": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ca_certs": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"bundle": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func pkcsBundleCreate(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
outputPath := m.(KeystoreConfig).Path
certPEM := d.Get("cert_pem").(string)
keyPEM := d.Get("key_pem").(string)
caCerts := d.Get("ca_certs").(*schema.Set).List()
err := impl.CreateBundle(certPEM, keyPEM, impl.SliceOfString(caCerts), outputPath, name)
if err != nil {
return err
}
d.SetId(name)
return pkcsBundleRead(d, m)
}
func pkcsBundleRead(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
outputPath := m.(KeystoreConfig).Path
var outFile = path.Join(outputPath, name+".p12")
bundle, err := ioutil.ReadFile(outFile)
if err != nil {
d.SetId("")
return nil
}
err = d.Set("bundle", base64.StdEncoding.EncodeToString(bundle))
if err != nil {
return err
}
return nil
}
func pkcsBundleUpdate(d *schema.ResourceData, m interface{}) error {
return pkcsBundleRead(d, m)
}
func pkcsBundleDelete(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
outputPath := m.(KeystoreConfig).Path
var outFile = path.Join(outputPath, name+".p12")
err := os.Remove(outFile)
return err
}