-
Notifications
You must be signed in to change notification settings - Fork 102
/
resource_artifactory_permission_target.go
348 lines (291 loc) · 9.81 KB
/
resource_artifactory_permission_target.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
package artifactory
import (
"fmt"
"net/http"
"strings"
"github.com/go-resty/resty/v2"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
const permissionsEndPoint = "artifactory/api/v2/security/permissions/"
const (
PERM_READ = "read"
PERM_WRITE = "write"
PERM_ANNOTATE = "annotate"
PERM_DELETE = "delete"
PERM_MANAGE = "manage"
PERMISSION_SCHEMA = "application/vnd.org.jfrog.artifactory.security.PermissionTargetV2+json"
)
func resourceArtifactoryPermissionTargets() *schema.Resource {
target := resourceArtifactoryPermissionTarget()
return target
}
func resourceArtifactoryPermissionTarget() *schema.Resource {
actionSchema := schema.Schema{
Type: schema.TypeSet,
Optional: true,
Set: hashPrincipal,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"permissions": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
PERM_READ,
PERM_ANNOTATE,
PERM_WRITE,
PERM_DELETE,
PERM_MANAGE,
// v2.PERM_MANAGE_XRAY_METADATA,
"managedXrayMeta",
}, false),
},
Set: schema.HashString,
Required: true,
},
},
},
}
principalSchema := schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"includes_pattern": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
Description: `The default value will be [""] if nothing is supplied`,
},
"excludes_pattern": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
Description: `The default value will be [] if nothing is supplied`,
},
"repositories": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Required: true,
},
"actions": {
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"users": &actionSchema,
"groups": &actionSchema,
},
},
Optional: true,
},
},
},
}
buildSchema := principalSchema
buildSchema.Elem.(*schema.Resource).Schema["repositories"].Description = `This can only be 1 value: "artifactory-build-info", and currently, validation of sets/lists is not allowed. Artifactory will reject the request if you change this`
return &schema.Resource{
Create: resourcePermissionTargetCreate,
Read: resourcePermissionTargetRead,
Update: resourcePermissionTargetUpdate,
Delete: resourcePermissionTargetDelete,
Exists: resourcePermissionTargetExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"repo": &principalSchema,
"build": &buildSchema,
"release_bundle": &principalSchema,
},
}
}
func hashPrincipal(o interface{}) int {
p := o.(map[string]interface{})
part1 := schema.HashString(p["name"].(string)) + 31
permissions := castToStringArr(p["permissions"].(*schema.Set).List())
part3 := schema.HashString(strings.Join(permissions, ""))
return part1 * part3
}
func unpackPermissionTarget(s *schema.ResourceData) *services.PermissionTargetParams {
d := &ResourceData{s}
unpackPermission := func(rawPermissionData interface{}) *services.PermissionTargetSection {
unpackEntity := func(rawEntityData interface{}) *services.Actions {
unpackPermMap := func(rawPermSet interface{}) map[string][]string {
permList := rawPermSet.(*schema.Set).List()
if len(permList) == 0 {
return nil
}
permissions := make(map[string][]string)
for _, v := range permList {
id := v.(map[string]interface{})
permissions[id["name"].(string)] = castToStringArr(id["permissions"].(*schema.Set).List())
}
return permissions
}
entityDataList := rawEntityData.([]interface{})
if len(entityDataList) == 0 {
return nil
}
entityData := entityDataList[0].(map[string]interface{})
return &services.Actions{
Users: unpackPermMap(entityData["users"]),
Groups: unpackPermMap(entityData["groups"]),
}
}
if rawPermissionData == nil || rawPermissionData.([]interface{})[0] == nil {
return nil
}
// It is safe to unpack the zeroth element immediately since permission targets have min size of 1
permissionData := rawPermissionData.([]interface{})[0].(map[string]interface{})
permission := new(services.PermissionTargetSection)
// This will always exist
{
tmp := castToStringArr(permissionData["repositories"].(*schema.Set).List())
permission.Repositories = tmp
}
// Handle optionals
if v, ok := permissionData["includes_pattern"]; ok {
// It is not possible to set default values for sets. Because the data type between moving from
// atlassian to jfrog went from a *[]string to a []string, and both have json attributes of 'on empty omit'
// when the * version was used, this would have cause an [] array to be sent, which artifactory would accept
// now that the data type is changed, and [] is ommitted and so when artifactory see the key missing entirely
// it responds with "[**]" which messes us the test. This hack seems to line them up
tmp := castToStringArr(v.(*schema.Set).List())
if len(tmp) == 0 {
tmp = []string{""}
}
permission.IncludePatterns = tmp
}
if v, ok := permissionData["excludes_pattern"]; ok {
tmp := castToStringArr(v.(*schema.Set).List())
permission.ExcludePatterns = tmp
}
if v, ok := permissionData["actions"]; ok {
permission.Actions = unpackEntity(v)
}
return permission
}
pTarget := new(services.PermissionTargetParams)
pTarget.Name = d.getString("name", false)
if v, ok := d.GetOk("repo"); ok {
pTarget.Repo = unpackPermission(v)
}
if v, ok := d.GetOk("build"); ok {
pTarget.Build = unpackPermission(v)
}
return pTarget
}
func packPermissionTarget(permissionTarget *services.PermissionTargetParams, d *schema.ResourceData) error {
packPermission := func(p *services.PermissionTargetSection) []interface{} {
packPermMap := func(e map[string][]string) []interface{} {
perm := make([]interface{}, len(e))
count := 0
for k, v := range e {
perm[count] = map[string]interface{}{
"name": k,
"permissions": schema.NewSet(schema.HashString, castToInterfaceArr(v)),
}
count++
}
return perm
}
s := map[string]interface{}{}
if p != nil {
if p.IncludePatterns != nil {
s["includes_pattern"] = schema.NewSet(schema.HashString, castToInterfaceArr(p.IncludePatterns))
}
if p.ExcludePatterns != nil {
s["excludes_pattern"] = schema.NewSet(schema.HashString, castToInterfaceArr(p.ExcludePatterns))
}
if p.Repositories != nil {
s["repositories"] = schema.NewSet(schema.HashString, castToInterfaceArr(p.Repositories))
}
if p.Actions != nil {
perms := make(map[string]interface{})
if p.Actions.Users != nil {
perms["users"] = schema.NewSet(hashPrincipal, packPermMap(p.Actions.Users))
}
if p.Actions.Groups != nil {
perms["groups"] = schema.NewSet(hashPrincipal, packPermMap(p.Actions.Groups))
}
if len(perms) > 0 {
s["actions"] = []interface{}{perms}
}
}
}
return []interface{}{s}
}
setValue := mkLens(d)
errors := setValue("name", permissionTarget.Name)
if permissionTarget.Repo != nil {
errors = setValue("repo", packPermission(permissionTarget.Repo))
}
if permissionTarget.Build != nil {
errors = setValue("build", packPermission(permissionTarget.Build))
}
if errors != nil && len(errors) > 0 {
return fmt.Errorf("failed to marshal permission target %q", errors)
}
return nil
}
func resourcePermissionTargetCreate(d *schema.ResourceData, m interface{}) error {
permissionTarget := unpackPermissionTarget(d)
if _, err := m.(*resty.Client).R().AddRetryCondition(retry400).SetBody(permissionTarget).Post(permissionsEndPoint + permissionTarget.Name); err != nil {
return err
}
d.SetId(permissionTarget.Name)
return nil
}
func resourcePermissionTargetRead(d *schema.ResourceData, m interface{}) error {
permissionTarget := new(services.PermissionTargetParams)
resp, err := m.(*resty.Client).R().SetResult(permissionTarget).Get(permissionsEndPoint + d.Id())
if err != nil {
if resp != nil && resp.StatusCode() == http.StatusNotFound {
d.SetId("")
return nil
}
return err
}
return packPermissionTarget(permissionTarget, d)
}
func resourcePermissionTargetUpdate(d *schema.ResourceData, m interface{}) error {
permissionTarget := unpackPermissionTarget(d)
if _, err := m.(*resty.Client).R().SetBody(permissionTarget).Put(permissionsEndPoint + d.Id()); err != nil {
return err
}
d.SetId(permissionTarget.Name)
return resourcePermissionTargetRead(d, m)
}
func resourcePermissionTargetDelete(d *schema.ResourceData, m interface{}) error {
_, err := m.(*resty.Client).R().Delete(permissionsEndPoint + d.Id())
return err
}
func resourcePermissionTargetExists(d *schema.ResourceData, m interface{}) (bool, error) {
return permTargetExists(d.Id(), m)
}
func permTargetExists(id string, m interface{}) (bool, error) {
resp, err := m.(*resty.Client).R().Head(permissionsEndPoint + id)
if err != nil && resp != nil && resp.StatusCode() == http.StatusNotFound {
// Do not error on 404s as this causes errors when the upstream permission has been manually removed
return false, nil
}
return err == nil, err
}