-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_kms_keys.go
319 lines (300 loc) · 8.85 KB
/
data_source_ibm_kms_keys.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
317
318
319
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"fmt"
"log"
"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/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMKMSkeys() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMKMSKeysRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Key protect or hpcs instance GUID",
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"key_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the key to be fetched",
ConflictsWith: []string{"alias", "key_id"},
},
"limit": {
Type: schema.TypeInt,
Optional: true,
Description: "Limit till the keys to be fetched",
},
"alias": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the key to be fetched",
ConflictsWith: []string{"key_name", "key_id"},
},
"key_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"alias", "key_name"},
},
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
ForceNew: true,
Default: "public",
},
"keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aliases": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"crn": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"key_ring_id": {
Type: schema.TypeString,
Computed: true,
Description: "The key ring id of the key to be fetched",
},
"standard_key": {
Type: schema.TypeBool,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rotation": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud Resource Name (CRN) that uniquely identifies your cloud resources.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
},
"last_update_date": {
Type: schema.TypeString,
Computed: true,
},
"interval_month": {
Type: schema.TypeInt,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"dual_auth_delete": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud Resource Name (CRN) that uniquely identifies your cloud resources.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
},
"last_update_date": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMKMSKeysRead(d *schema.ResourceData, meta interface{}) error {
instanceID := getInstanceIDFromCRN(d.Get("instance_id").(string))
api, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
var totalKeys []kp.Key
if v, ok := d.GetOk("alias"); ok {
aliasName := v.(string)
key, err := api.GetKey(context.Background(), aliasName)
if err != nil {
return fmt.Errorf("[ERROR] Get Keys failed with error: %s", err)
}
keyMap := make([]map[string]interface{}, 0, 1)
keyInstance := make(map[string]interface{})
keyInstance["id"] = key.ID
keyInstance["name"] = key.Name
keyInstance["crn"] = key.CRN
keyInstance["standard_key"] = key.Extractable
keyInstance["aliases"] = key.Aliases
keyInstance["key_ring_id"] = key.KeyRingID
keyMap = append(keyMap, keyInstance)
d.Set("keys", keyMap)
} else if v, ok := d.GetOk("key_id"); ok {
key, err := api.GetKey(context.Background(), v.(string))
if err != nil {
return fmt.Errorf("[ERROR] Get Keys failed with error: %s", err)
}
keyMap := make([]map[string]interface{}, 0, 1)
keyInstance := make(map[string]interface{})
keyInstance["id"] = key.ID
keyInstance["name"] = key.Name
keyInstance["crn"] = key.CRN
keyInstance["standard_key"] = key.Extractable
keyInstance["aliases"] = key.Aliases
keyInstance["key_ring_id"] = key.KeyRingID
policies, err := api.GetPolicies(context.Background(), key.ID)
if err != nil {
return fmt.Errorf("[ERROR] Failed to read policies: %s", err)
}
if len(policies) == 0 {
log.Printf("No Policy Configurations read\n")
} else {
keyInstance["policies"] = flex.FlattenKeyPolicies(policies)
}
keyMap = append(keyMap, keyInstance)
d.SetId(instanceID)
d.Set("keys", keyMap)
d.Set("instance_id", instanceID)
} else {
limit := d.Get("limit")
limitVal := limit.(int)
offset := 0
//default page size of API is 200 as stated
pageSize := 200
// when the limit is not passed, the api works in default way to avoid backward compatibility issues
if limitVal == 0 {
{
keys, err := api.GetKeys(context.Background(), 0, offset)
if err != nil {
return fmt.Errorf("[ERROR] Get Keys failed with error: %s", err)
}
retreivedKeys := keys.Keys
totalKeys = append(totalKeys, retreivedKeys...)
}
} else {
// when the limit is passed by the user
for {
if offset < limitVal {
if (limitVal - offset) < pageSize {
keys, err := api.GetKeys(context.Background(), (limitVal - offset), offset)
if err != nil {
return fmt.Errorf("[ERROR] Get Keys failed with error: %s", err)
}
retreivedKeys := keys.Keys
totalKeys = append(totalKeys, retreivedKeys...)
break
} else {
keys, err := api.GetKeys(context.Background(), pageSize, offset)
if err != nil {
return fmt.Errorf("[ERROR] Get Keys failed with error: %s", err)
}
numOfKeysFetched := keys.Metadata.NumberOfKeys
retreivedKeys := keys.Keys
totalKeys = append(totalKeys, retreivedKeys...)
if numOfKeysFetched < pageSize || offset+pageSize == limitVal {
break
}
offset = offset + pageSize
}
}
}
}
var keyName string
var matchKeys []kp.Key
if v, ok := d.GetOk("key_name"); ok {
keyName = v.(string)
for _, keyData := range totalKeys {
if keyData.Name == keyName {
matchKeys = append(matchKeys, keyData)
}
}
} else {
matchKeys = totalKeys
}
if len(matchKeys) == 0 {
return fmt.Errorf("[ERROR] No keys with name %s in instance %s", keyName, instanceID)
}
keyMap := make([]map[string]interface{}, 0, len(matchKeys))
for _, key := range matchKeys {
keyInstance := make(map[string]interface{})
keyInstance["id"] = key.ID
keyInstance["name"] = key.Name
keyInstance["crn"] = key.CRN
keyInstance["standard_key"] = key.Extractable
keyInstance["aliases"] = key.Aliases
keyInstance["key_ring_id"] = key.KeyRingID
keyMap = append(keyMap, keyInstance)
}
d.Set("keys", keyMap)
}
d.SetId(instanceID)
d.Set("instance_id", instanceID)
return nil
}