-
Notifications
You must be signed in to change notification settings - Fork 353
/
groups.go
178 lines (149 loc) · 5.57 KB
/
groups.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
package service
import (
"encoding/json"
"errors"
"fmt"
"github.com/databrickslabs/databricks-terraform/client/model"
"net/http"
"sort"
)
// GroupsAPI exposes the scim groups API
type GroupsAPI struct {
Client DBApiClient
}
// Create creates a scim group in the Databricks workspace
func (a GroupsAPI) Create(groupName string, members []string, roles []string, entitlements []string) (model.Group, error) {
var group model.Group
scimGroupRequest := struct {
Schemas []model.URN `json:"schemas,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Members []model.ValueListItem `json:"members,omitempty"`
Entitlements []model.ValueListItem `json:"entitlements,omitempty"`
Roles []model.ValueListItem `json:"roles,omitempty"`
}{}
scimGroupRequest.Schemas = []model.URN{model.GroupSchema}
scimGroupRequest.DisplayName = groupName
scimGroupRequest.Members = []model.ValueListItem{}
for _, member := range members {
scimGroupRequest.Members = append(scimGroupRequest.Members, model.ValueListItem{Value: member})
}
scimGroupRequest.Roles = []model.ValueListItem{}
for _, role := range roles {
scimGroupRequest.Roles = append(scimGroupRequest.Roles, model.ValueListItem{Value: role})
}
scimGroupRequest.Entitlements = []model.ValueListItem{}
for _, entitlement := range entitlements {
scimGroupRequest.Entitlements = append(scimGroupRequest.Entitlements, model.ValueListItem{Value: entitlement})
}
resp, err := a.Client.performQuery(http.MethodPost, "/preview/scim/v2/Groups", "2.0", scimHeaders, scimGroupRequest, nil)
if err != nil {
return group, err
}
err = json.Unmarshal(resp, &group)
return group, err
}
// Read reads and returns a Group object via SCIM api
func (a GroupsAPI) Read(groupID string) (model.Group, error) {
var group model.Group
groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID)
resp, err := a.Client.performQuery(http.MethodGet, groupPath, "2.0", scimHeaders, nil, nil)
if err != nil {
return group, err
}
err = json.Unmarshal(resp, &group)
if err != nil {
return group, err
}
//get inherited groups
var groups []model.Group
for _, inheritedGroup := range group.Groups {
inheritedGroupFull, err := a.Read(inheritedGroup.Value)
if err != nil {
return group, err
}
groups = append(groups, inheritedGroupFull)
}
inherited, unInherited, err := a.getInheritedAndNonInheritedRoles(group, groups)
group.InheritedRoles = inherited
group.UnInheritedRoles = unInherited
return group, err
}
// GetAdminGroup returns the admin group in a given workspace by fetching with query "displayName+eq+admins"
func (a GroupsAPI) GetAdminGroup() (model.Group, error) {
var group model.Group
var groups model.GroupList
adminsQuery := "/preview/scim/v2/Groups?filter=displayName+eq+admins"
resp, err := a.Client.performQuery(http.MethodGet, adminsQuery, "2.0", scimHeaders, nil, nil)
if err != nil {
return group, err
}
err = json.Unmarshal(resp, &groups)
resources := groups.Resources
if len(resources) == 1 {
return resources[0], err
}
return group, errors.New("Unable to identify the admin group! ")
}
// Patch applys a patch request for a group given a path attribute
func (a GroupsAPI) Patch(groupID string, addList []string, removeList []string, path model.GroupPathType) error {
groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID)
var addOperations model.GroupPatchOperations
var removeOperations model.GroupPatchOperations
groupPatchRequest := model.GroupPatchRequest{
Schemas: []model.URN{model.PatchOp},
Operations: []model.GroupPatchOperations{},
}
if addList == nil && removeList == nil {
return errors.New("empty members list to add or to remove")
}
if len(addList) > 0 {
addOperations = model.GroupPatchOperations{
Op: "add",
Path: path,
Value: []model.ValueListItem{},
}
for _, addItem := range addList {
addOperations.Value = append(addOperations.Value, model.ValueListItem{Value: addItem})
}
groupPatchRequest.Operations = append(groupPatchRequest.Operations, addOperations)
}
for _, removeItem := range removeList {
path := fmt.Sprintf("%s[value eq \"%s\"]", string(path), removeItem)
removeOperations = model.GroupPatchOperations{
Op: "remove",
Path: model.GroupPathType(path),
}
groupPatchRequest.Operations = append(groupPatchRequest.Operations, removeOperations)
}
_, err := a.Client.performQuery(http.MethodPatch, groupPath, "2.0", scimHeaders, groupPatchRequest, nil)
return err
}
// Delete deletes a group given a group id
func (a GroupsAPI) Delete(groupID string) error {
groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID)
_, err := a.Client.performQuery(http.MethodDelete, groupPath, "2.0", scimHeaders, nil, nil)
return err
}
func (a GroupsAPI) getInheritedAndNonInheritedRoles(group model.Group, groups []model.Group) (inherited []model.RoleListItem, unInherited []model.RoleListItem, err error) {
allRoles := group.Roles
var inheritedRoles []model.RoleListItem
inheritedRolesKeys := []string{}
inheritedRolesMap := map[string]model.RoleListItem{}
for _, group := range groups {
inheritedRoles = append(inheritedRoles, group.Roles...)
}
for _, role := range inheritedRoles {
inheritedRolesKeys = append(inheritedRolesKeys, role.Value)
inheritedRolesMap[role.Value] = role
}
sort.Strings(inheritedRolesKeys)
for _, roleKey := range inheritedRolesKeys {
inherited = append(inherited, inheritedRolesMap[roleKey])
}
for _, role := range allRoles {
if _, ok := inheritedRolesMap[role.Value]; !ok {
unInherited = append(unInherited, role)
}
}
return inherited, unInherited, nil
}