/
data_source_illumio_rule_sets.go
438 lines (408 loc) · 12.1 KB
/
data_source_illumio_rule_sets.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
// Copyright 2021 Illumio, Inc. All Rights Reserved.
package illumiocore
import (
"context"
"fmt"
"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"
)
func datasourceIllumioRuleSets() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceIllumioRuleSetsRead,
SchemaVersion: 1,
Description: "Represents Illumio Rulesets",
Schema: map[string]*schema.Schema{
"items": {
Type: schema.TypeList,
Computed: true,
Description: "list of Rulesets",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "URI of Ruleset",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this ruleset was first created",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this ruleset was last updated",
},
"deleted_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this ruleset was deleted",
},
"created_by": {
Type: schema.TypeMap,
Computed: true,
Description: "User who created this resource",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"updated_by": {
Type: schema.TypeMap,
Computed: true,
Description: "User who last updated this resource",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"deleted_by": {
Type: schema.TypeMap,
Computed: true,
Description: "User who deleted this resource",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"update_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of update",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of Ruleset",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of Ruleset",
},
"external_data_set": {
Type: schema.TypeString,
Computed: true,
Description: "External data set identifier",
},
"external_data_reference": {
Type: schema.TypeString,
Computed: true,
Description: "External data reference identifier",
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Enabled flag. Determines whether the Ruleset is enabled or not",
},
"scopes": {
Type: schema.TypeList,
Computed: true,
Description: "scopes for Ruleset",
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeMap,
Computed: true,
Description: "Href of Label",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"label_group": {
Type: schema.TypeMap,
Computed: true,
Description: "Href of Label Group",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
"rules": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of Security Rules",
Elem: &schema.Resource{
Schema: securityRuleDatasourceSchema(false),
},
},
"ip_tables_rules": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of IP Tables Rules",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "URI of the Ip Tables Rule",
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Enabled flag. Determines whether this IP Tables Rule is enabled or not",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the Ip Tables Rule",
},
"statements": {
Type: schema.TypeList,
Computed: true,
Description: "statements for IP Tables Rule",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"table_name": {
Type: schema.TypeString,
Computed: true,
Description: "table name of statement",
},
"chain_name": {
Type: schema.TypeString,
Computed: true,
Description: "chain name of statement",
},
"parameters": {
Type: schema.TypeString,
Computed: true,
Description: "parameters of statement",
},
},
},
},
"actors": {
Type: schema.TypeList,
Computed: true,
Description: "actors for IP Table Rule",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actors": {
Type: schema.TypeString,
Computed: true,
Description: "actors for IP table Rule actors",
},
"label": {
Type: schema.TypeMap,
Computed: true,
Description: "Href of Label",
Elem: &schema.Schema{Type: schema.TypeString},
},
"label_group": {
Type: schema.TypeMap,
Computed: true,
Description: "Href of Label Group",
Elem: &schema.Schema{Type: schema.TypeString},
},
"workload": {
Type: schema.TypeMap,
Computed: true,
Description: "Href of Workload",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"ip_version": {
Type: schema.TypeString,
Computed: true,
Description: "IP version for the rules to be applied to",
},
"update_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of update for IP Table Rule",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this IP Table Rule was first created",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this IP Table Rule was last updated",
},
"deleted_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when this IP Table Rule was deleted",
},
"created_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who created this IP Table Rule",
},
"updated_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who last updated this IP Table Rule",
},
"deleted_by": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "User who deleted this IP Table Rule",
},
},
},
},
},
},
},
"pversion": {
Type: schema.TypeString,
Optional: true,
Default: "draft",
ValidateDiagFunc: isValidPversion(),
Description: `Security policy version. Allowed values are "draft", "active", and numbers greater than 0. Default value: "draft"`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Description of Ruleset(s) to return. Supports partial matches",
},
"external_data_reference": {
Type: schema.TypeString,
Optional: true,
Description: "A unique identifier within the external data source",
},
"external_data_set": {
Type: schema.TypeString,
Optional: true,
Description: "The data source from which a resource originates",
},
"labels": {
Type: schema.TypeString,
Optional: true,
Description: "List of lists of label URIs, encoded as a JSON string",
},
"max_results": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: isStringGreaterThanZero(),
Description: "Maximum number of Rulesets to return. The integer should be a non-zero positive integer",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of Ruleset(s) to return. Matches based on match_type",
},
"match_type": {
Type: schema.TypeString,
Optional: true,
Default: PARTIAL_MATCH,
ValidateFunc: validation.StringInSlice([]string{PARTIAL_MATCH, EXACT_MATCH}, true),
Description: `Indicates whether to return all partially-matching names or only exact matches. Allowed values are "partial" and "exact". Default value: "partial"`,
},
},
}
}
func datasourceIllumioRuleSetsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diagnostics diag.Diagnostics
pConfig, _ := m.(Config)
illumioClient := pConfig.IllumioClient
pversion := d.Get("pversion").(string)
href := fmt.Sprintf("/orgs/%v/sec_policy/%v/rule_sets", illumioClient.OrgID, pversion)
paramKeys := []string{
"description",
"external_data_reference",
"external_data_set",
"labels",
"max_results",
"name",
}
params := resourceDataToMap(d, paramKeys)
_, data, err := illumioClient.Get(href, ¶ms)
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%v", hashcode(paramsString(params))))
rsKeys := []string{
"href",
"created_at",
"updated_at",
"deleted_at",
"created_by",
"updated_by",
"deleted_by",
"update_type",
"name",
"description",
"external_data_set",
"external_data_reference",
"enabled",
}
rss := []map[string]interface{}{}
for _, ruleSet := range data.Children() {
// if exact matching is enabled, skip the object if it's a partial match
if d.Get("match_type").(string) == EXACT_MATCH {
if !isExactMatch("name", d, ruleSet) {
continue
}
}
rs := extractMap(ruleSet, rsKeys)
key := "rules"
if ruleSet.Exists(key) {
rs[key] = extractRules(data.S(key))
}
key = "scopes"
if ruleSet.Exists(key) {
scps := []interface{}{}
scopeKeys := []string{"label", "label_group"}
for _, scope := range ruleSet.S(key).Children() {
scps = append(scps, extractMapArray(scope, scopeKeys))
}
rs[key] = scps
}
key = "ip_table_rules"
if ruleSet.Exists(key) {
iptrKeys := []string{
"href",
"enabled",
"description",
"ip_version",
"update_type",
"created_at",
"updated_at",
"deleted_at",
"created_by",
"updated_by",
"deleted_by",
}
statKey := "statements"
statKeys := []string{
"table_name",
"chain_name",
"parameters",
}
actorsKey := "actors"
iptrs := []map[string]interface{}{}
for _, iptRule := range ruleSet.S(key).Children() {
iptr := extractMap(iptRule, iptrKeys)
if iptRule.Exists(statKey) {
iptr[statKey] = extractMapArray(iptRule.S(statKey), statKeys)
}
if iptRule.Exists(actorsKey) {
iptr[actorsKey] = extractRuleActors(iptRule.S(actorsKey))
}
iptrs = append(iptrs, iptr)
}
rs[key] = iptrs
}
rss = append(rss, rs)
}
d.Set("items", rss)
return diagnostics
}