/
resource_illumio_syslog_destination.go
364 lines (318 loc) · 11.9 KB
/
resource_illumio_syslog_destination.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
// Copyright 2021 Illumio, Inc. All Rights Reserved.
package illumiocore
import (
"context"
"fmt"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/illumio/terraform-provider-illumio-core/models"
)
var (
syslogDestAELMinSeverityValidValues = []string{"error", "warning", "informational"}
syslogDestTypeValidValues = []string{"local_syslog", "remote_syslog"}
syslogDestProtocolValidValues = []int{6, 17}
)
func resourceIllumioSyslogDestination() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIllumioSyslogDestinationCreate,
ReadContext: resourceIllumioSyslogDestinationRead,
UpdateContext: resourceIllumioSyslogDestinationUpdate,
DeleteContext: resourceIllumioSyslogDestinationDelete,
SchemaVersion: 1,
Description: "Manages Illumio Syslog Destination",
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "URI of the destination",
},
"pce_scope": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Description: "pce_scope for destination",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(syslogDestTypeValidValues, false)),
Description: `Destination type. Allowed values are "local_syslog" and "remote_syslog"`,
},
"description": {
Type: schema.TypeString,
Required: true,
Description: "Description of the destination",
},
"audit_event_logger": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "audit_event_logger details for destination",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"configuration_event_included": {
Type: schema.TypeBool,
Required: true,
Description: "Configuration (Northbound) auditable events",
},
"system_event_included": {
Type: schema.TypeBool,
Required: true,
Description: "System (PCE) auditable events",
},
"min_severity": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(syslogDestAELMinSeverityValidValues, false)),
Description: `Minimum severity level of audit event messages. Allowed values are "error", "warning" and "informational"`,
},
},
},
},
"traffic_event_logger": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: `traffic_event_logger details for destination`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"traffic_flow_allowed_event_included": {
Type: schema.TypeBool,
Required: true,
Description: "Set to enable traffic flow events",
},
"traffic_flow_potentially_blocked_event_included": {
Type: schema.TypeBool,
Required: true,
Description: "Set to enable traffic flow events",
},
"traffic_flow_blocked_event_included": {
Type: schema.TypeBool,
Required: true,
Description: "Set to enable traffic flow events",
},
},
},
},
"node_status_logger": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "node_status_logger details for destination",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"node_status_included": {
Type: schema.TypeBool,
Required: true,
Description: "Syslog messages regarding status of the nodes",
},
},
},
},
"remote_syslog": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "remote_syslog details for destination. Required when type is set to \"remote_syslog\"",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
Description: "The remote syslog IP or DNS address",
},
"port": {
Type: schema.TypeInt,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IsPortNumberOrZero),
Description: "The remote syslog port",
},
"protocol": {
Type: schema.TypeInt,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntInSlice(syslogDestProtocolValidValues)),
Description: "The protocol for streaming syslog messages. Allowed values are 6 and 17",
},
"tls_enabled": {
Type: schema.TypeBool,
Required: true,
Description: "To enable TLS",
},
"tls_ca_bundle": {
Type: schema.TypeString,
Optional: true,
Description: "Trustee CA bundle",
},
"tls_verify_cert": {
Type: schema.TypeBool,
Required: true,
Description: "Perform TLS verification",
},
},
},
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceIllumioSyslogDestinationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
orgID := illumioClient.OrgID
syslogDest := expandIllumioSyslogDestination(d, &diags)
diags = append(diags, syslogValidation(*syslogDest.Type, syslogDest.RemoteSyslog != nil)...)
if diags.HasError() {
return diags
}
_, data, err := illumioClient.Create(fmt.Sprintf("/orgs/%v/settings/syslog/destinations", orgID), syslogDest)
if err != nil {
return diag.FromErr(err)
}
d.SetId(data.S("href").Data().(string))
return resourceIllumioSyslogDestinationRead(ctx, d, m)
}
func expandIllumioSyslogDestination(d *schema.ResourceData, diags *diag.Diagnostics) *models.SyslogDestination {
return &models.SyslogDestination{
PceScope: PtrTo(getStringList(d.Get("pce_scope").(*schema.Set).List())),
Description: PtrTo(d.Get("description").(string)),
Type: PtrTo(d.Get("type").(string)),
AuditEventLogger: expandIllumioSyslogDestinationAuditEventLogger(d, diags),
TrafficEventLogger: expandIllumioSyslogDestinationTrafficEventLogger(d, diags),
NodeStatusLogger: expandIllumioSyslogDestinationNodeStatusLogger(d, diags),
RemoteSyslog: expandIllumioSyslogDestinationRemoteSyslog(d, diags),
}
}
func expandIllumioSyslogDestinationAuditEventLogger(d *schema.ResourceData, diags *diag.Diagnostics) *models.SyslogDestinationAuditEventLogger {
v := d.Get("audit_event_logger").([]interface{})[0].(map[string]interface{})
return &models.SyslogDestinationAuditEventLogger{
MinSeverity: PtrTo(v["min_severity"].(string)),
ConfigurationEventIncluded: PtrTo(v["configuration_event_included"].(bool)),
SystemEventIncluded: PtrTo(v["system_event_included"].(bool)),
}
}
func expandIllumioSyslogDestinationTrafficEventLogger(d *schema.ResourceData, diags *diag.Diagnostics) *models.SyslogDestinationTrafficEventLogger {
v := d.Get("traffic_event_logger").([]interface{})[0].(map[string]interface{})
return &models.SyslogDestinationTrafficEventLogger{
TrafficFlowAllowedEventIncluded: PtrTo(v["traffic_flow_allowed_event_included"].(bool)),
TrafficFlowPotentiallyBlockedEventIncluded: PtrTo(v["traffic_flow_potentially_blocked_event_included"].(bool)),
TrafficFlowBlockedEventIncluded: PtrTo(v["traffic_flow_blocked_event_included"].(bool)),
}
}
func expandIllumioSyslogDestinationNodeStatusLogger(d *schema.ResourceData, diags *diag.Diagnostics) *models.SyslogDestinationNodeStatusLogger {
v := d.Get("node_status_logger").([]interface{})[0].(map[string]interface{})
return &models.SyslogDestinationNodeStatusLogger{
NodeStatusIncluded: PtrTo(v["node_status_included"].(bool)),
}
}
func expandIllumioSyslogDestinationRemoteSyslog(d *schema.ResourceData, diags *diag.Diagnostics) *models.SyslogDestinationRemoteSyslog {
o, ok := d.GetOk("remote_syslog")
if !ok {
return nil
}
v := o.([]interface{})[0].(map[string]interface{})
return &models.SyslogDestinationRemoteSyslog{
Address: PtrTo(v["address"].(string)),
Port: PtrTo(v["port"].(int)),
Protocol: PtrTo(v["protocol"].(int)),
TLSEnabled: PtrTo(v["tls_enabled"].(bool)),
TLSCaBundle: PtrTo(v["tls_ca_bundle"].(string)),
TLSVerifyCert: PtrTo(v["tls_verify_cert"].(bool)),
}
}
func resourceIllumioSyslogDestinationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
href := d.Id()
_, data, err := illumioClient.Get(href, nil)
if err != nil {
return diag.FromErr(err)
}
setIllumioSyslogDestinationState(data, d)
return diags
}
func resourceIllumioSyslogDestinationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
if diags.HasError() {
return diags
}
syslogDest := &models.SyslogDestination{
Description: PtrTo(d.Get("description").(string)),
}
if d.HasChange("pce_scope") {
syslogDest.PceScope = PtrTo(getStringList(d.Get("pce_scope").(*schema.Set).List()))
}
if d.HasChange("type") {
syslogDest.Type = PtrTo(d.Get("type").(string))
}
if d.HasChange("audit_event_logger") {
syslogDest.AuditEventLogger = expandIllumioSyslogDestinationAuditEventLogger(d, &diags)
}
if d.HasChange("traffic_event_logger") {
syslogDest.TrafficEventLogger = expandIllumioSyslogDestinationTrafficEventLogger(d, &diags)
}
if d.HasChange("node_status_logger") {
syslogDest.NodeStatusLogger = expandIllumioSyslogDestinationNodeStatusLogger(d, &diags)
}
if d.HasChange("remote_syslog") {
syslogDest.RemoteSyslog = expandIllumioSyslogDestinationRemoteSyslog(d, &diags)
diags = append(diags, syslogValidation(d.Get("type").(string), syslogDest.RemoteSyslog != nil)...)
}
if d.HasChange("type") && !d.HasChange("remote_syslog") {
_, ok := d.GetOk("remote_syslog")
diags = append(diags, syslogValidation(d.Get("type").(string), ok)...)
}
if diags.HasError() {
return diags
}
_, err := illumioClient.Update(d.Id(), syslogDest)
if err != nil {
return diag.FromErr(err)
}
return resourceIllumioSyslogDestinationRead(ctx, d, m)
}
func syslogValidation(destType string, isRemoteSyslogSet bool) diag.Diagnostics {
var diags diag.Diagnostics
if destType == "remote_syslog" {
if !isRemoteSyslogSet {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "[illumio-core_syslog_destination] remote_syslog block expected",
Detail: `When type is set to "remote_syslog", remote_syslog block is required`,
AttributePath: cty.Path{cty.GetAttrStep{Name: "type"}},
})
}
} else {
if isRemoteSyslogSet {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "[illumio-core_syslog_destination] remote_syslog block not allowed",
Detail: `When type is set to "local_syslog", remote_syslog block is not allowed`,
AttributePath: cty.Path{cty.GetAttrStep{Name: "type"}},
})
}
}
return diags
}
func resourceIllumioSyslogDestinationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
_, err := illumioClient.Delete(d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return diags
}