-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_policy_document.go
213 lines (204 loc) · 6.85 KB
/
data_source_policy_document.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
package border0
import (
"context"
"encoding/json"
"hash/crc32"
"sort"
"strconv"
border0client "github.com/borderzero/border0-go/client"
"github.com/borderzero/terraform-provider-border0/internal/schemautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourcePolicyDocument() *schema.Resource {
return &schema.Resource{
Description: "`border0_policy_document` data source can be used to generate a policy document in JSON format for use with `border0_policy` resource.",
ReadContext: dataSourcePolicyDocumentRead,
Schema: map[string]*schema.Schema{
"json": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Required: true,
Description: "The policy language version.",
},
"action": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
Description: "The actions that you want to allow.",
},
"condition": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"who": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The email address of the user who is allowed to perform the actions.",
},
"domain": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The domain of the user who is allowed to perform the actions.",
},
},
},
Description: "Who is allowed to perform the actions.",
},
"where": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_ip": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The IP address that the request must originate from to be allowed to perform the actions.",
},
"country": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The country that the request must originate from to be allowed to perform the actions.",
},
"country_not": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The country that the request must _NOT_ originate from to be allowed to perform the actions.",
},
},
},
Description: "Where the request must originate from to be allowed to perform the actions.",
},
"when": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"after": {
Type: schema.TypeString,
Optional: true,
Description: "When the request must be made after to be allowed to perform the actions.",
},
"before": {
Type: schema.TypeString,
Optional: true,
Description: "When the request must be made before to be allowed to perform the actions.",
},
"time_of_day_after": {
Type: schema.TypeString,
Optional: true,
Description: "When the request must be made after to be allowed to perform the actions.",
},
"time_of_day_before": {
Type: schema.TypeString,
Optional: true,
Description: "When the request must be made before to be allowed to perform the actions.",
},
},
},
Description: "When the request must be made to be allowed to perform the actions.",
},
},
},
Description: "The conditions under which you want to allow the actions.",
},
},
}
}
func dataSourcePolicyDocumentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var policyData border0client.PolicyData
if v, ok := d.GetOk("version"); ok {
policyData.Version = v.(string)
}
if v, ok := d.GetOk("action"); ok {
policyData.Action = policyDecodeStringList(v.(*schema.Set).List())
}
if v, ok := d.GetOk("condition"); ok {
if conditions := v.(*schema.Set).List(); len(conditions) > 0 {
condition := conditions[0].(map[string]interface{})
if v, ok := condition["who"]; ok {
if whos := v.(*schema.Set).List(); len(whos) > 0 {
who := whos[0].(map[string]interface{})
if v, ok := who["email"]; ok {
policyData.Condition.Who.Email = policyDecodeStringList(v.(*schema.Set).List())
}
if v, ok := who["domain"]; ok {
policyData.Condition.Who.Domain = policyDecodeStringList(v.(*schema.Set).List())
}
}
}
if v, ok := condition["where"]; ok {
if wheres := v.(*schema.Set).List(); len(wheres) > 0 {
where := wheres[0].(map[string]interface{})
if v, ok := where["allowed_ip"]; ok {
policyData.Condition.Where.AllowedIP = policyDecodeStringList(v.(*schema.Set).List())
}
if v, ok := where["country"]; ok {
policyData.Condition.Where.Country = policyDecodeStringList(v.(*schema.Set).List())
}
if v, ok := where["country_not"]; ok {
policyData.Condition.Where.CountryNot = policyDecodeStringList(v.(*schema.Set).List())
}
}
}
if v, ok := condition["when"]; ok {
if whens := v.(*schema.Set).List(); len(whens) > 0 {
when := whens[0].(map[string]interface{})
if v, ok := when["after"]; ok {
policyData.Condition.When.After = v.(string)
}
if v, ok := when["before"]; ok {
policyData.Condition.When.Before = v.(string)
}
if v, ok := when["time_of_day_after"]; ok {
policyData.Condition.When.TimeOfDayAfter = v.(string)
}
if v, ok := when["time_of_day_before"]; ok {
policyData.Condition.When.TimeOfDayBefore = v.(string)
}
}
}
}
}
jsonPolicyData, err := json.MarshalIndent(policyData, "", " ")
if err != nil {
return schemautil.DiagnosticsError(err, "Failed to marshal policy data")
}
jsonString := string(jsonPolicyData)
d.Set("json", jsonString)
d.SetId(strconv.Itoa(stringHashcode(jsonString)))
return nil
}
func policyDecodeStringList(list []interface{}) []string {
ret := make([]string, len(list))
for i, value := range list {
ret[i] = value.(string)
}
sort.Sort(sort.Reverse(sort.StringSlice(ret)))
return ret
}
func stringHashcode(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}