-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathresource_certificate_discovery.go
More file actions
173 lines (160 loc) · 5.49 KB
/
Copy pathresource_certificate_discovery.go
File metadata and controls
173 lines (160 loc) · 5.49 KB
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
// generated file
package akeyless
import (
"context"
"fmt"
"strings"
akeyless_api "github.com/akeylesslabs/akeyless-go/v5"
"github.com/akeylesslabs/terraform-provider-akeyless/akeyless/common"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCertificateDiscovery() *schema.Resource {
return &schema.Resource{
Description: "Certificate Discovery resource. Starts a certificate discovery scan and saves results under the target location. Destroying this resource removes it from Terraform state only and does not delete discovered certificates.",
Create: resourceCertificateDiscoveryCreate,
Read: resourceCertificateDiscoveryRead,
Delete: resourceCertificateDiscoveryDelete,
Importer: &schema.ResourceImporter{
State: resourceCertificateDiscoveryImport,
},
Schema: map[string]*schema.Schema{
"hosts": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A comma separated list of IPs, CIDR ranges, or DNS names to scan",
},
"port_ranges": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "443",
Description: "A comma separated list of port ranges. Example: 80,8080-8085",
},
"target_location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The results will be saved in this folder",
DiffSuppressFunc: common.DiffSuppressOnLeadingSlash,
},
"expiration_event_in": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Description: "How many days before the expiration of the certificate would you like to be notified. To specify multiple events, repeat this argument.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"protection_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of the key that protects the certificate value (if empty, the account default key will be used)",
},
"count_new": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of new certificates discovered",
},
"count_existing": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of existing certificates updated",
},
"count_hosts": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of hosts scanned",
},
"count_failed": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of failed scan targets",
},
"item_names": {
Type: schema.TypeList,
Computed: true,
Description: "Names of certificate items created or updated by the discovery",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceCertificateDiscoveryCreate(d *schema.ResourceData, m interface{}) error {
provider := m.(*providerMeta)
client := *provider.client
token := *provider.token
ctx := context.Background()
hosts := strings.ReplaceAll(d.Get("hosts").(string), " ", "")
portRanges := strings.ReplaceAll(d.Get("port_ranges").(string), " ", "")
targetLocation := d.Get("target_location").(string)
expirationEventInSet := d.Get("expiration_event_in").(*schema.Set)
expirationEventIn := common.ExpandStringList(expirationEventInSet.List())
protectionKey := d.Get("protection_key").(string)
if hosts == "" {
return fmt.Errorf("hosts cannot be empty")
}
body := akeyless_api.CertificateDiscovery{
Hosts: hosts,
TargetLocation: targetLocation,
Token: &token,
}
common.GetAkeylessPtr(&body.PortRanges, portRanges)
common.GetAkeylessPtr(&body.ExpirationEventIn, expirationEventIn)
common.GetAkeylessPtr(&body.ProtectionKey, protectionKey)
out, resp, err := client.CertificateDiscovery(ctx).Body(body).Execute()
if err != nil {
return common.HandleError("can't run certificate discovery", resp, err)
}
countNew, countExisting, countHosts, countFailed := 0, 0, 0, 0
itemNames := []string{}
if out != nil && out.Results != nil {
results := out.Results
if results.CountNew != nil {
countNew = int(*results.CountNew)
}
if results.CountExisting != nil {
countExisting = int(*results.CountExisting)
}
if results.CountHosts != nil {
countHosts = int(*results.CountHosts)
}
if results.CountFailed != nil {
countFailed = int(*results.CountFailed)
}
if results.ItemNames != nil {
itemNames = results.ItemNames
}
}
if err := d.Set("count_new", countNew); err != nil {
return err
}
if err := d.Set("count_existing", countExisting); err != nil {
return err
}
if err := d.Set("count_hosts", countHosts); err != nil {
return err
}
if err := d.Set("count_failed", countFailed); err != nil {
return err
}
if err := d.Set("item_names", itemNames); err != nil {
return err
}
d.SetId(uuid.New().String())
return nil
}
func resourceCertificateDiscoveryRead(d *schema.ResourceData, m interface{}) error {
// Discovery is a one-shot scan with no get/describe API; retain Terraform state.
return nil
}
func resourceCertificateDiscoveryDelete(d *schema.ResourceData, m interface{}) error {
// Destroying the resource only removes it from Terraform state.
// Discovered certificate items are left intact.
d.SetId("")
return nil
}
func resourceCertificateDiscoveryImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
return nil, fmt.Errorf("certificate discovery cannot be imported because it is a one-shot scan with no remote identity")
}