-
Notifications
You must be signed in to change notification settings - Fork 645
/
resource_ibm_kms_kmip_client_cert.go
244 lines (230 loc) · 7.63 KB
/
resource_ibm_kms_kmip_client_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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"fmt"
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
kp "github.com/IBM/keyprotect-go-client"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceIBMKmsKMIPClientCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceIBMKmsKMIPClientCertCreate,
Read: resourceIBMKmsKMIPClientCertRead,
Delete: resourceIBMKmsKMIPClientCertDelete,
Exists: resourceIBMKmsKMIPClientCertExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
},
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Key protect Instance GUID",
ForceNew: true,
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"adapter_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name or UUID of the KMIP adapter that contains the cert",
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The name of the KMIP client certificate",
},
"cert_id": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of the KMIP cert",
},
"certificate": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
Description: "The PEM-encoded contents of the certificate",
},
"created_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier that is associated with the entity that created the adapter.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when a resource was created. The date format follows RFC 3339.",
},
},
}
}
func resourceIBMKmsKMIPClientCertCreate(d *schema.ResourceData, meta interface{}) error {
certToCreate, adapterID, instanceID, err := ExtractAndValidateKMIPClientCertDataFromSchema(d)
if err != nil {
return err
}
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
if strings.Contains((kpAPI.URL).String(), "private") || strings.Contains(kpAPI.Config.BaseURL, "private") {
d.Set("endpoint_type", "private")
} else {
d.Set("endpoint_type", "public")
}
ctx := context.Background()
cert, err := kpAPI.CreateKMIPClientCertificate(ctx,
adapterID,
certToCreate.Certificate,
kp.WithKMIPClientCertName(certToCreate.Name),
)
if err != nil {
return fmt.Errorf("[ERROR] Error while creating KMIP client certificate: %s", err)
}
return populateKMIPClientCertSchemaDataFromStruct(d, *cert, adapterID, instanceID)
}
func resourceIBMKmsKMIPClientCertRead(d *schema.ResourceData, meta interface{}) error {
instanceID := d.Get("instance_id").(string)
adapterID := d.Get("adapter_id").(string)
// use instanceID and adapterID here to support terraform import case
instanceID, adapterID, certID, err := splitCertID(d.Id())
if err != nil {
return err
}
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
if strings.Contains((kpAPI.URL).String(), "private") || strings.Contains(kpAPI.Config.BaseURL, "private") {
d.Set("endpoint_type", "private")
} else {
d.Set("endpoint_type", "public")
}
ctx := context.Background()
cert, err := kpAPI.GetKMIPClientCertificate(ctx, adapterID, certID)
if err != nil {
return err
}
err = populateKMIPClientCertSchemaDataFromStruct(d, *cert, adapterID, instanceID)
if err != nil {
return err
}
return nil
}
func resourceIBMKmsKMIPClientCertDelete(d *schema.ResourceData, meta interface{}) error {
instanceID := d.Get("instance_id").(string)
adapterID := d.Get("adapter_id").(string)
_, _, certID, err := splitCertID(d.Id())
if err != nil {
return err
}
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
err = kpAPI.DeleteKMIPClientCertificate(context.Background(), adapterID, certID)
return err
}
func resourceIBMKmsKMIPClientCertExists(d *schema.ResourceData, meta interface{}) (bool, error) {
instanceID := d.Get("instance_id").(string)
adapterID := d.Get("adapter_id").(string)
// use instanceID and adapterID here to support terraform import case
instanceID, adapterID, certID, err := splitCertID(d.Id())
if err != nil {
return false, err
}
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return false, err
}
ctx := context.Background()
_, err = kpAPI.GetKMIPClientCertificate(ctx, adapterID, certID)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 {
return false, nil
}
return false, wrapError(err, "Error checking KMIP Client Certificate existence")
}
return true, nil
}
func ExtractAndValidateKMIPClientCertDataFromSchema(d *schema.ResourceData) (cert kp.KMIPClientCertificate, adapterIDStr string, instanceID string, err error) {
err = nil
instanceID = getInstanceIDFromResourceData(d, "instance_id")
cert = kp.KMIPClientCertificate{}
if name, ok := d.GetOk("name"); ok {
nameStr, ok2 := name.(string)
if !ok2 {
err = fmt.Errorf("[ERROR] Error converting name to string")
return
}
cert.Name = nameStr
}
if certPayload, ok := d.GetOk("certificate"); ok {
certStr, ok2 := certPayload.(string)
if !ok2 {
err = fmt.Errorf("[ERROR] Error converting certificate to string")
return
}
cert.Certificate = certStr
}
adapterID := d.Get("adapter_id")
adapterIDStr = adapterID.(string)
return
}
func populateKMIPClientCertSchemaDataFromStruct(d *schema.ResourceData, cert kp.KMIPClientCertificate, adapterID string, instanceID string) (err error) {
d.SetId(fmt.Sprintf("%s/%s/%s", instanceID, adapterID, cert.ID))
if err = d.Set("name", cert.Name); err != nil {
return fmt.Errorf("[ERROR] Error setting name: %s", err)
}
if err = d.Set("adapter_id", adapterID); err != nil {
return fmt.Errorf("[ERROR] Error setting adapter_id: %s", err)
}
if err = d.Set("instance_id", instanceID); err != nil {
return fmt.Errorf("[ERROR] Error setting instance_id: %s", err)
}
if err = d.Set("cert_id", cert.ID); err != nil {
return fmt.Errorf("[ERROR] Error setting cert_id: %s", err)
}
if err = d.Set("certificate", cert.Certificate); err != nil {
return fmt.Errorf("[ERROR] Error setting certificate: %s", err)
}
if cert.CreatedAt != nil {
if err = d.Set("created_at", cert.CreatedAt.String()); err != nil {
return fmt.Errorf("[ERROR] Error setting created_at: %s", err)
}
if err = d.Set("created_by", cert.CreatedBy); err != nil {
return fmt.Errorf("[ERROR] Error setting created_by: %s", err)
}
}
return nil
}
func splitCertID(terraformId string) (instanceID, adapterID, certID string, err error) {
split, err := flex.SepIdParts(terraformId, "/")
if err != nil {
return "", "", "", err
}
if len(split) != 3 {
return "", "", "", fmt.Errorf("[ERROR] The given id %s does not contain all expected sections, should be of format instance_id/adapter_id/cert_id", terraformId)
}
for index, id := range split {
if uuid.Validate(id) != nil {
return "", "", "", fmt.Errorf("[ERROR] The given id %s at index %d of instance_id/adapter_id/cert_id is not a valid UUID", id, index)
}
}
return split[0], split[1], split[2], nil
}