/
groups.go
264 lines (226 loc) · 7.54 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
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
package connector
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/conductorone/baton-jumpcloud/pkg/jcapi2"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"github.com/conductorone/baton-sdk/pkg/pagination"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)
type groupResourceType struct {
resourceType *v2.ResourceType
client1 jc1Func
client2 jc2Func
}
func (o *groupResourceType) ResourceType(_ context.Context) *v2.ResourceType {
return o.resourceType
}
func (o *groupResourceType) List(
ctx context.Context,
resourceID *v2.ResourceId,
token *pagination.Token,
) ([]*v2.Resource, string, annotations.Annotations, error) {
ctx, client := o.client2(ctx)
skip, b, err := unmarshalSkipToken(token)
if err != nil {
return nil, "", nil, err
}
groups, resp, err := client.UserGroupsApi.GroupsUserList(ctx).Skip(skip).Execute()
if err != nil {
return nil, "", nil, err
}
defer resp.Body.Close()
var rv []*v2.Resource
for i := range groups {
ur, err := groupResource(ctx, &groups[i])
if err != nil {
return nil, "", nil, err
}
rv = append(rv, ur)
}
pageToken, err := marshalSkipToken(len(groups), skip, b)
if err != nil {
return nil, "", nil, err
}
return rv, pageToken, nil, nil
}
func groupResource(ctx context.Context, group *jcapi2.UserGroup) (*v2.Resource, error) {
trait, err := groupTrait(ctx, group)
if err != nil {
return nil, err
}
var annos annotations.Annotations
annos.Update(trait)
return &v2.Resource{
Id: fmtResourceId(resourceTypeGroup.Id, group.GetId()),
DisplayName: group.GetName(),
Annotations: annos,
Description: group.GetDescription(),
}, nil
}
func groupTrait(ctx context.Context, group *jcapi2.UserGroup) (*v2.GroupTrait, error) {
profile, err := structpb.NewStruct(map[string]interface{}{
"type": group.GetType(),
"email": group.GetEmail(),
})
if err != nil {
return nil, fmt.Errorf("baton-jumpcloud: failed to construct role profile for role trait: %w", err)
}
ret := &v2.GroupTrait{
Profile: profile,
}
return ret, nil
}
func (o *groupResourceType) Entitlements(
ctx context.Context,
resource *v2.Resource,
token *pagination.Token,
) ([]*v2.Entitlement, string, annotations.Annotations, error) {
var rv []*v2.Entitlement
rv = append(rv, groupEntitlement(ctx, resource))
return rv, "", nil, nil
}
func groupEntitlement(ctx context.Context, resource *v2.Resource) *v2.Entitlement {
return &v2.Entitlement{
Id: fmtResource(resource.Id, resource.Id.GetResource()),
Resource: resource,
DisplayName: fmt.Sprintf("%s Group Member", resource.DisplayName),
Description: fmt.Sprintf("Member of %s group", resource.DisplayName),
GrantableTo: []*v2.ResourceType{resourceTypeUser},
Purpose: v2.Entitlement_PURPOSE_VALUE_ASSIGNMENT,
Slug: "member",
}
}
func (o *groupResourceType) Grants(
ctx context.Context,
resource *v2.Resource,
token *pagination.Token,
) ([]*v2.Grant, string, annotations.Annotations, error) {
ctx, client := o.client2(ctx)
skip, b, err := unmarshalSkipToken(token)
if err != nil {
return nil, "", nil, err
}
members, resp, err := client.UserGroupMembersMembershipApi.GraphUserGroupMembersList(ctx, resource.Id.Resource).Skip(skip).Execute()
if err != nil {
return nil, "", nil, err
}
defer resp.Body.Close()
var rv []*v2.Grant
for i := range members {
member := &members[i]
switch member.To.GetType() {
case "user":
rv = append(rv, groupGrant(resource, resourceTypeUser.Id, member))
case "group":
rv = append(rv, groupGrant(resource, resourceTypeGroup.Id, member))
default:
continue
}
}
pt, err := marshalSkipToken(len(members), skip, b)
if err != nil {
return nil, "", nil, err
}
return rv, pt, nil, nil
}
func (o *groupResourceType) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
ctx, client := o.client2(ctx)
l := ctxzap.Extract(ctx)
if principal.Id.ResourceType != resourceTypeUser.Id {
l.Warn(
"baton-jumpcloud: only users can be granted group membership",
zap.String("principal_type", principal.Id.ResourceType),
zap.String("principal_id", principal.Id.Resource),
)
return nil, errors.New("baton-jumpcloud: only users can be granted group membership")
}
resp, err := client.UserGroupsApi.GraphUserGroupMembersPost(ctx, entitlement.Resource.Id.Resource).Body(jcapi2.GraphOperationUserGroupMember{
Id: principal.Id.Resource,
Op: "add",
Type: "user",
}).Execute()
if err != nil {
// If the user is already a member of the group, we get a 409 and we want to return success.
if resp == nil || resp.StatusCode != http.StatusConflict {
l.Error("failed to add user to group", zap.Error(err), zap.String("group", entitlement.Resource.Id.Resource), zap.String("user", principal.Id.Resource))
return nil, err
}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusConflict {
l.Error(
"failed to add user to group",
zap.Error(err),
zap.String("group", entitlement.Resource.Id.Resource),
zap.String("user", principal.Id.Resource),
zap.Int("status", resp.StatusCode),
)
return nil, errors.New("baton-jumpcloud: failed adding user to group")
}
return nil, nil
}
func (o *groupResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
ctx, client := o.client2(ctx)
l := ctxzap.Extract(ctx)
entitlement := grant.Entitlement
principal := grant.Principal
if principal.Id.ResourceType != resourceTypeUser.Id {
l.Warn(
"baton-jumpcloud: only users can have group membership revoked",
zap.String("principal_type", principal.Id.ResourceType),
zap.String("principal_id", principal.Id.Resource),
)
return nil, errors.New("baton-jumpcloud: only users can have group membership revoked")
}
resp, err := client.UserGroupsApi.GraphUserGroupMembersPost(ctx, entitlement.Resource.Id.Resource).Body(jcapi2.GraphOperationUserGroupMember{
Id: principal.Id.Resource,
Op: "remove",
Type: "user",
}).Execute()
if err != nil {
// If the user is already not a member of the group, we get a 404 and we want to return success.
if resp == nil || resp.StatusCode != http.StatusNotFound {
l.Error("failed to remove user from group", zap.Error(err), zap.String("group", entitlement.Resource.Id.Resource), zap.String("user", principal.Id.Resource))
return nil, err
}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
l.Error(
"failed to remove user from group",
zap.Error(err),
zap.String("group", entitlement.Resource.Id.Resource),
zap.String("user", principal.Id.Resource),
zap.Int("status", resp.StatusCode),
)
return nil, errors.New("baton-jumpcloud: failed removing user from group")
}
return nil, nil
}
func groupGrant(resource *v2.Resource, resoureTypeId string, member *jcapi2.GraphConnection) *v2.Grant {
groupID := resource.Id.GetResource()
ur := &v2.Resource{Id: &v2.ResourceId{ResourceType: resoureTypeId, Resource: member.To.Id}}
var annos annotations.Annotations
return &v2.Grant{
Id: fmtResourceGrant(resource.Id, ur.Id, groupID),
Entitlement: &v2.Entitlement{
Id: fmtResource(resource.Id, groupID),
Resource: resource,
},
Annotations: annos,
Principal: ur,
}
}
func newGroupBuilder(jc1 jc1Func, jc2 jc2Func) *groupResourceType {
return &groupResourceType{
resourceType: resourceTypeGroup,
client1: jc1,
client2: jc2,
}
}