-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_sm_public_certificate.go
593 lines (546 loc) · 22.1 KB
/
data_source_ibm_sm_public_certificate.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package secretsmanager
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/secrets-manager-go-sdk/v2/secretsmanagerv2"
)
func DataSourceIbmSmPublicCertificate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmSmPublicCertificateSecretRead,
Schema: map[string]*schema.Schema{
"secret_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"secret_id", "name"},
Description: "The ID of the secret.",
},
"created_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier that is associated with the entity that created the secret.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when a resource was created. The date format follows RFC 3339.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A CRN that uniquely identifies an IBM Cloud resource.",
},
"custom_metadata": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Description: "The secret metadata that a user can customize.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An extended description of your secret.To protect your privacy, do not use personal data, such as your name or location, as a description for your secret group.",
},
"downloaded": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the secret data that is associated with a secret version was retrieved in a call to the service API.",
},
"labels": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Labels that you can use to search for secrets in your instance.Up to 30 labels can be created.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"locks_total": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of locks of the secret.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"secret_id", "name"},
RequiredWith: []string{"secret_group_name"},
Description: "The human-readable name of your secret.",
},
"secret_group_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A v4 UUID identifier, or `default` secret group.",
},
"secret_group_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"name"},
Description: "The human-readable name of your secret group.",
},
"secret_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The secret type. Supported types are arbitrary, certificates (imported, public, and private), IAM credentials, key-value, and user credentials.",
},
"state": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The secret state that is based on NIST SP 800-57. States are integers and correspond to the `Pre-activation = 0`, `Active = 1`, `Suspended = 2`, `Deactivated = 3`, and `Destroyed = 5` values.",
},
"state_description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A text representation of the secret state.",
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when a resource was recently modified. The date format follows RFC 3339.",
},
"versions_total": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of versions of the secret.",
},
"signing_algorithm": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The identifier for the cryptographic algorithm that was used by the issuing certificate authority to sign a certificate.",
},
"alt_names": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "With the Subject Alternative Name field, you can specify additional host names to be protected by a single SSL certificate.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"common_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Common Name (AKA CN) represents the server name protected by the SSL certificate.",
},
"expiration_date": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date a secret is expired. The date format follows RFC 3339.",
},
"issuance_info": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Issuance information that is associated with your certificate.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_rotated": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the issued certificate is configured with an automatic rotation policy.",
},
"challenges": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The set of challenges. It is returned only when ordering public certificates by using manual DNS configuration.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The challenge domain.",
},
"expiration": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The challenge expiration date. The date format follows RFC 3339.",
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The challenge status.",
},
"txt_record_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The TXT record name.",
},
"txt_record_value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The TXT record value.",
},
},
},
},
"dns_challenge_validation_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date that a user requests to validate DNS challenges for certificates that are ordered with a manual DNS provider. The date format follows RFC 3339.",
},
"error_code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A code that identifies an issuance error.This field, along with `error_message`, is returned when Secrets Manager successfully processes your request, but the certificate authority is unable to issue a certificate.",
},
"error_message": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A human-readable message that provides details about the issuance error.",
},
"ordered_on": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when the certificate is ordered. The date format follows RFC 3339.",
},
"state": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The secret state that is based on NIST SP 800-57. States are integers and correspond to the `Pre-activation = 0`, `Active = 1`, `Suspended = 2`, `Deactivated = 3`, and `Destroyed = 5` values.",
},
"state_description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A text representation of the secret state.",
},
},
},
},
"issuer": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The distinguished name that identifies the entity that signed and issued the certificate.",
},
"key_algorithm": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The identifier for the cryptographic algorithm to be used to generate the public key that is associated with the certificate.The algorithm that you select determines the encryption algorithm (`RSA` or `ECDSA`) and key size to be used to generate keys and sign certificates. For longer living certificates, it is recommended to use longer keys to provide more encryption protection. Allowed values: RSA2048, RSA4096, EC256, EC384.",
},
"serial_number": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique serial number that was assigned to a certificate by the issuing certificate authority.",
},
"validity": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The date and time that the certificate validity period begins and ends.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"not_before": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date-time format follows RFC 3339.",
},
"not_after": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date-time format follows RFC 3339.",
},
},
},
},
"rotation": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Determines whether Secrets Manager rotates your secrets automatically.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_rotate": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Determines whether Secrets Manager rotates your secret automatically.Default is `false`. If `auto_rotate` is set to `true` the service rotates your secret based on the defined interval.",
},
"rotate_keys": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Determines whether Secrets Manager rotates the private key for your public certificate automatically.Default is `false`. If it is set to `true`, the service generates and stores a new private key for your rotated certificate.",
},
},
},
},
"bundle_certs": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the issued certificate is bundled with intermediate certificates.",
},
"ca": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the certificate authority configuration.",
},
"dns": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the DNS provider configuration.",
},
"certificate": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The PEM-encoded contents of your certificate.",
},
"intermediate": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "(Optional) The PEM-encoded intermediate certificate to associate with the root certificate.",
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "(Optional) The PEM-encoded private key to associate with the certificate.",
},
},
}
}
func dataSourceIbmSmPublicCertificateSecretRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
publicCertificateIntf, region, instanceId, diagError := getSecretByIdOrByName(context, d, meta, PublicCertSecretType)
if diagError != nil {
return diagError
}
publicCertificate := publicCertificateIntf.(*secretsmanagerv2.PublicCertificate)
d.SetId(fmt.Sprintf("%s/%s/%s", region, instanceId, *publicCertificate.ID))
var err error
if err = d.Set("region", region); err != nil {
return diag.FromErr(fmt.Errorf("Error setting region: %s", err))
}
if err = d.Set("created_by", publicCertificate.CreatedBy); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_by: %s", err))
}
if err = d.Set("created_at", DateTimeToRFC3339(publicCertificate.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("crn", publicCertificate.Crn); err != nil {
return diag.FromErr(fmt.Errorf("Error setting crn: %s", err))
}
if publicCertificate.CustomMetadata != nil {
convertedMap := make(map[string]interface{}, len(publicCertificate.CustomMetadata))
for k, v := range publicCertificate.CustomMetadata {
convertedMap[k] = v
}
if err = d.Set("custom_metadata", flex.Flatten(convertedMap)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting custom_metadata: %s", err))
}
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting custom_metadata %s", err))
}
}
if err = d.Set("description", publicCertificate.Description); err != nil {
return diag.FromErr(fmt.Errorf("Error setting description: %s", err))
}
if err = d.Set("downloaded", publicCertificate.Downloaded); err != nil {
return diag.FromErr(fmt.Errorf("Error setting downloaded: %s", err))
}
if publicCertificate.Labels != nil {
if err = d.Set("labels", publicCertificate.Labels); err != nil {
return diag.FromErr(fmt.Errorf("Error setting labels: %s", err))
}
}
if err = d.Set("locks_total", flex.IntValue(publicCertificate.LocksTotal)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting locks_total: %s", err))
}
if err = d.Set("name", publicCertificate.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("secret_group_id", publicCertificate.SecretGroupID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting secret_group_id: %s", err))
}
if err = d.Set("secret_type", publicCertificate.SecretType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting secret_type: %s", err))
}
if err = d.Set("state", flex.IntValue(publicCertificate.State)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting state: %s", err))
}
if err = d.Set("state_description", publicCertificate.StateDescription); err != nil {
return diag.FromErr(fmt.Errorf("Error setting state_description: %s", err))
}
if err = d.Set("updated_at", DateTimeToRFC3339(publicCertificate.UpdatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting updated_at: %s", err))
}
if err = d.Set("versions_total", flex.IntValue(publicCertificate.VersionsTotal)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting versions_total: %s", err))
}
if err = d.Set("signing_algorithm", publicCertificate.SigningAlgorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting signing_algorithm: %s", err))
}
if publicCertificate.AltNames != nil {
if err = d.Set("alt_names", publicCertificate.AltNames); err != nil {
return diag.FromErr(fmt.Errorf("Error setting alt_names: %s", err))
}
}
if err = d.Set("common_name", publicCertificate.CommonName); err != nil {
return diag.FromErr(fmt.Errorf("Error setting common_name: %s", err))
}
if err = d.Set("expiration_date", DateTimeToRFC3339(publicCertificate.ExpirationDate)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting expiration_date: %s", err))
}
issuanceInfo := []map[string]interface{}{}
if publicCertificate.IssuanceInfo != nil {
modelMap, err := dataSourceIbmSmPublicCertificateSecretCertificateIssuanceInfoToMap(publicCertificate.IssuanceInfo)
if err != nil {
return diag.FromErr(err)
}
issuanceInfo = append(issuanceInfo, modelMap)
}
if err = d.Set("issuance_info", issuanceInfo); err != nil {
return diag.FromErr(fmt.Errorf("Error setting issuance_info %s", err))
}
if err = d.Set("issuer", publicCertificate.Issuer); err != nil {
return diag.FromErr(fmt.Errorf("Error setting issuer: %s", err))
}
if err = d.Set("key_algorithm", publicCertificate.KeyAlgorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting key_algorithm: %s", err))
}
if err = d.Set("serial_number", publicCertificate.SerialNumber); err != nil {
return diag.FromErr(fmt.Errorf("Error setting serial_number: %s", err))
}
validity := []map[string]interface{}{}
if publicCertificate.Validity != nil {
modelMap, err := dataSourceIbmSmPublicCertificateSecretCertificateValidityToMap(publicCertificate.Validity)
if err != nil {
return diag.FromErr(err)
}
validity = append(validity, modelMap)
}
if err = d.Set("validity", validity); err != nil {
return diag.FromErr(fmt.Errorf("Error setting validity %s", err))
}
rotation := []map[string]interface{}{}
if publicCertificate.Rotation != nil {
modelMap, err := dataSourceIbmSmPublicCertificateSecretRotationPolicyToMap(publicCertificate.Rotation)
if err != nil {
return diag.FromErr(err)
}
rotation = append(rotation, modelMap)
}
if err = d.Set("rotation", rotation); err != nil {
return diag.FromErr(fmt.Errorf("Error setting rotation %s", err))
}
if err = d.Set("bundle_certs", publicCertificate.BundleCerts); err != nil {
return diag.FromErr(fmt.Errorf("Error setting bundle_certs: %s", err))
}
if err = d.Set("ca", publicCertificate.Ca); err != nil {
return diag.FromErr(fmt.Errorf("Error setting ca: %s", err))
}
if err = d.Set("dns", publicCertificate.Dns); err != nil {
return diag.FromErr(fmt.Errorf("Error setting dns: %s", err))
}
if err = d.Set("certificate", publicCertificate.Certificate); err != nil {
return diag.FromErr(fmt.Errorf("Error setting certificate: %s", err))
}
if err = d.Set("intermediate", publicCertificate.Intermediate); err != nil {
return diag.FromErr(fmt.Errorf("Error setting intermediate: %s", err))
}
if err = d.Set("private_key", publicCertificate.PrivateKey); err != nil {
return diag.FromErr(fmt.Errorf("Error setting private_key: %s", err))
}
return nil
}
func dataSourceIbmSmPublicCertificateSecretCertificateIssuanceInfoToMap(model *secretsmanagerv2.CertificateIssuanceInfo) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.AutoRotated != nil {
modelMap["auto_rotated"] = *model.AutoRotated
}
if model.Challenges != nil {
challenges := []map[string]interface{}{}
for _, challengesItem := range model.Challenges {
challengesItemMap, err := dataSourceIbmSmPublicCertificateSecretChallengeResourceToMap(&challengesItem)
if err != nil {
return modelMap, err
}
challenges = append(challenges, challengesItemMap)
}
modelMap["challenges"] = challenges
}
if model.DnsChallengeValidationTime != nil {
modelMap["dns_challenge_validation_time"] = model.DnsChallengeValidationTime.String()
}
if model.ErrorCode != nil {
modelMap["error_code"] = *model.ErrorCode
}
if model.ErrorMessage != nil {
modelMap["error_message"] = *model.ErrorMessage
}
if model.OrderedOn != nil {
modelMap["ordered_on"] = model.OrderedOn.String()
}
if model.State != nil {
modelMap["state"] = *model.State
}
if model.StateDescription != nil {
modelMap["state_description"] = *model.StateDescription
}
return modelMap, nil
}
func dataSourceIbmSmPublicCertificateSecretChallengeResourceToMap(model *secretsmanagerv2.ChallengeResource) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Domain != nil {
modelMap["domain"] = *model.Domain
}
if model.Expiration != nil {
modelMap["expiration"] = model.Expiration.String()
}
if model.Status != nil {
modelMap["status"] = *model.Status
}
if model.TxtRecordName != nil {
modelMap["txt_record_name"] = *model.TxtRecordName
}
if model.TxtRecordValue != nil {
modelMap["txt_record_value"] = *model.TxtRecordValue
}
return modelMap, nil
}
func dataSourceIbmSmPublicCertificateSecretCertificateValidityToMap(model *secretsmanagerv2.CertificateValidity) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.NotBefore != nil {
modelMap["not_before"] = model.NotBefore.String()
}
if model.NotAfter != nil {
modelMap["not_after"] = model.NotAfter.String()
}
return modelMap, nil
}
func dataSourceIbmSmPublicCertificateSecretRotationPolicyToMap(model secretsmanagerv2.RotationPolicyIntf) (map[string]interface{}, error) {
if _, ok := model.(*secretsmanagerv2.CommonRotationPolicy); ok {
return dataSourceIbmSmPublicCertificateSecretCommonRotationPolicyToMap(model.(*secretsmanagerv2.CommonRotationPolicy))
} else if _, ok := model.(*secretsmanagerv2.PublicCertificateRotationPolicy); ok {
return dataSourceIbmSmPublicCertificateSecretPublicCertificateRotationPolicyToMap(model.(*secretsmanagerv2.PublicCertificateRotationPolicy))
} else if _, ok := model.(*secretsmanagerv2.RotationPolicy); ok {
modelMap := make(map[string]interface{})
model := model.(*secretsmanagerv2.RotationPolicy)
if model.AutoRotate != nil {
modelMap["auto_rotate"] = *model.AutoRotate
}
if model.RotateKeys != nil {
modelMap["rotate_keys"] = *model.RotateKeys
}
return modelMap, nil
} else {
return nil, fmt.Errorf("Unrecognized secretsmanagerv2.RotationPolicyIntf subtype encountered")
}
}
func dataSourceIbmSmPublicCertificateSecretCommonRotationPolicyToMap(model *secretsmanagerv2.CommonRotationPolicy) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.AutoRotate != nil {
modelMap["auto_rotate"] = *model.AutoRotate
}
return modelMap, nil
}
func dataSourceIbmSmPublicCertificateSecretPublicCertificateRotationPolicyToMap(model *secretsmanagerv2.PublicCertificateRotationPolicy) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.AutoRotate != nil {
modelMap["auto_rotate"] = *model.AutoRotate
}
if model.RotateKeys != nil {
modelMap["rotate_keys"] = *model.RotateKeys
}
return modelMap, nil
}