-
Notifications
You must be signed in to change notification settings - Fork 10
/
assertion.go
381 lines (358 loc) · 11 KB
/
assertion.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
package athenz
import (
"fmt"
"github.com/AthenZ/athenz/clients/go/msd"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/AthenZ/athenz/clients/go/zms"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceConditionSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"operator": {
Type: schema.TypeInt,
Computed: true,
},
"value": {
Computed: true,
Type: schema.TypeString,
},
},
}
}
func dataSourceAssertionSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"effect": {
Type: schema.TypeString,
Required: true,
},
"action": {
Type: schema.TypeString,
Required: true,
},
"role": {
Type: schema.TypeString,
Required: true,
},
"resource": {
Type: schema.TypeString,
Required: true,
},
"case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"condition": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instances": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
"id": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
"enforcementstate": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
"scopeonprem": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
"scopeaws": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
"scopeall": {
Type: schema.TypeSet,
Computed: true,
Elem: dataSourceConditionSchema(),
},
},
},
},
},
},
}
}
func resourceConditionSchema(validateDiagFuncForScope schema.SchemaValidateDiagFunc) *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"operator": {
Type: schema.TypeInt,
Optional: true,
Default: 1, // "EQUALS"
},
"value": {
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validateDiagFuncForScope,
},
},
}
}
func validateDiagFuncForScope(v any, p cty.Path) diag.Diagnostics {
value := v.(string)
return validation.ToDiagFunc(validation.StringInSlice([]string{"false", "true"}, false))(value, p)
}
func resourceAssertionSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"effect": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ALLOW",
"DENY",
}, false),
StateFunc: func(v interface{}) string {
return strings.ToUpper(v.(string))
},
},
"action": {
Type: schema.TypeString,
Required: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(i interface{}, s string) (ws []string, errors []error) {
err := validateRoleNameWithinAssertion(i.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"resource": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(i interface{}, s string) (ws []string, errors []error) {
err := validateResourceNameWithinAssertion(i.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"id": {
Type: schema.TypeInt,
Computed: true,
},
"condition": {
Type: schema.TypeSet,
MaxItems: 2, /* each assertion represent acl policy. Since for a given service,
you can make the acl policy enforced on some hosts and not on others,
therefore, to apply more than 2 conditions per assertion doesn't make any sense */
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeInt,
},
"instances": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: resourceConditionSchema(validatePatternFunc(ASSERTION_CONDITION_VALUE)),
},
"enforcementstate": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: resourceConditionSchema(
validation.ToDiagFunc(
validation.StringInSlice([]string{strings.ToLower(msd.REPORT.String()), strings.ToLower(msd.ENFORCE.String())}, false)),
),
},
"scopeonprem": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: resourceConditionSchema(validateDiagFuncForScope),
},
"scopeaws": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: resourceConditionSchema(validateDiagFuncForScope),
},
"scopeall": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: resourceConditionSchema(validateDiagFuncForScope),
},
},
},
},
},
},
}
}
func expandAssertionConditions(configured []interface{}) *zms.AssertionConditions {
conditionsList := make([]*zms.AssertionCondition, 0, len(configured))
keys := []string{Instances, EnforcementState, ScopeONPREM, ScopeAWS, ScopeALL}
for _, cRaw := range configured {
data := cRaw.(map[string]interface{})
conditionsMap := make(map[zms.AssertionConditionKey]*zms.AssertionConditionData, len(keys))
for _, key := range keys {
conditionsData := (data[key].(*schema.Set).List())[0].(map[string]interface{})
operator := conditionsData["operator"].(int)
value := conditionsData["value"].(string)
conditionsMap[zms.AssertionConditionKey(key)] = &zms.AssertionConditionData{
Operator: zms.AssertionConditionOperator(operator),
Value: zms.AssertionConditionValue(value),
}
}
assertionCondition := &zms.AssertionCondition{
ConditionsMap: conditionsMap,
}
conditionsList = append(conditionsList, assertionCondition)
}
return &zms.AssertionConditions{
ConditionsList: conditionsList,
}
}
func expandPolicyAssertions(dn string, configured []interface{}) []*zms.Assertion {
assertions := make([]*zms.Assertion, 0, len(configured))
for _, aRaw := range configured {
data := aRaw.(map[string]interface{})
role := dn + ROLE_SEPARATOR + data["role"].(string)
resource := data["resource"].(string)
effect := zms.NewAssertionEffect(strings.ToUpper(data["effect"].(string)))
action := data["action"].(string)
caseSensitive := data["case_sensitive"].(bool)
a := &zms.Assertion{
Role: role,
Resource: resource,
Action: action,
Effect: &effect,
CaseSensitive: &caseSensitive,
}
if conditions, ok := data["condition"]; ok {
a.Conditions = expandAssertionConditions(conditions.(*schema.Set).List())
}
assertions = append(assertions, a)
}
return assertions
}
func flattenAssertionConditions(list []*zms.AssertionCondition) []interface{} {
assertionConditions := make([]interface{}, 0, len(list))
for _, condition := range list {
keys := []string{Instances, EnforcementState, ScopeONPREM, ScopeAWS, ScopeALL}
c := make(map[string]interface{}, len(keys))
c["id"] = (int)(*condition.Id)
for _, key := range keys {
c[key] = []map[string]interface{}{
{
"operator": (int)(condition.ConditionsMap[zms.AssertionConditionKey(key)].Operator),
"value": (string)(condition.ConditionsMap[zms.AssertionConditionKey(key)].Value),
},
}
}
assertionConditions = append(assertionConditions, c)
}
return assertionConditions
}
func flattenPolicyAssertion(list []*zms.Assertion) []interface{} {
policyAssertions := make([]interface{}, 0, len(list))
for _, assertion := range list {
role := strings.Split(assertion.Role, ROLE_SEPARATOR)[1]
resource := assertion.Resource
effect := assertion.Effect.String()
action := assertion.Action
caseSensitive := inferCaseSensitiveValue(action, resource)
a := map[string]interface{}{
"role": role,
"resource": resource,
"action": action,
"effect": effect,
"case_sensitive": caseSensitive,
"id": (int)(*assertion.Id),
}
if assertion.Conditions != nil {
a["condition"] = flattenAssertionConditions(assertion.Conditions.ConditionsList)
}
policyAssertions = append(policyAssertions, a)
}
return policyAssertions
}
// enabling case_sensitive flag is allowed if and only if action or resource has capital letters
func validateCaseSensitiveValue(caseSensitive bool, action string, resourceName string) error {
if caseSensitive {
if strings.ToLower(resourceName) == resourceName && action == strings.ToLower(action) {
return fmt.Errorf("enabling case_sensitive flag is allowed only if action or resource has capital letters")
}
} else {
if strings.ToLower(resourceName) != resourceName || action != strings.ToLower(action) {
return fmt.Errorf("capitalized action or resource allowed only when enabling case_sensitive flag")
}
}
return nil
}
// case-sensitive value is inferred by analyzing assertion action and assertion resource
func inferCaseSensitiveValue(action, resourceName string) bool {
return strings.ToLower(resourceName) != resourceName || strings.ToLower(action) != action
}
func validateAssertion(assertions []interface{}) error {
for _, aRaw := range assertions {
data := aRaw.(map[string]interface{})
resource := data["resource"].(string)
action := data["action"].(string)
caseSensitive := data["case_sensitive"].(bool)
if err := validateCaseSensitiveValue(caseSensitive, action, resource); err != nil {
return err
}
}
return nil
}
func validateAssertionConditions(assertionConditions interface{}) error {
conditions := assertionConditions.(*schema.Set).List()
if len(conditions) <= 1 {
return nil
}
c1 := conditions[0].(map[string]interface{})
c2 := conditions[1].(map[string]interface{})
enforcementState1 := getTheValueFromCondition(c1, EnforcementState)
enforcementState2 := getTheValueFromCondition(c2, EnforcementState)
if enforcementState1 == enforcementState2 {
return fmt.Errorf("enforcement state can't be same for different conditions in a msd policy")
}
instances1 := getTheValueFromCondition(c1, Instances)
instances2 := getTheValueFromCondition(c2, Instances)
if isSharedHostsBetweenConditionInstances(instances1, instances2) {
return fmt.Errorf("the same host can not exist in both \"report\" and \"enforce\" modes")
}
return nil
}