/
cognito.go
271 lines (248 loc) · 7.7 KB
/
cognito.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
package identitysync
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
"github.com/common-fate/common-fate/pkg/cfaws"
"github.com/common-fate/common-fate/pkg/gconfig"
"github.com/common-fate/common-fate/pkg/identity"
"golang.org/x/sync/errgroup"
)
type CognitoSync struct {
client *cognitoidentityprovider.Client
userPoolID gconfig.StringValue
}
func (s *CognitoSync) Config() gconfig.Config {
return gconfig.Config{
gconfig.StringField("userPoolId", &s.userPoolID, "the Cognito user pool ID"),
}
}
func (s *CognitoSync) Init(ctx context.Context) error {
awsconfig, err := cfaws.ConfigFromContextOrDefault(ctx)
if err != nil {
return err
}
s.client = cognitoidentityprovider.NewFromConfig(awsconfig)
return nil
}
// idpUserFromCognitoUser converts a cognito user to an idp user after fetching the users groups
func (c *CognitoSync) idpUserFromCognitoUser(ctx context.Context, cognitoUser types.UserType) (identity.IDPUser, error) {
var u identity.IDPUser
for _, a := range cognitoUser.Attributes {
switch aws.ToString(a.Name) {
case "sub":
u.ID = aws.ToString(a.Value)
case "email":
u.Email = aws.ToString(a.Value)
case "given_name":
u.FirstName = aws.ToString(a.Value)
case "family_name":
u.LastName = aws.ToString(a.Value)
}
}
groups, err := c.listUserGroups(ctx, u.ID)
if err != nil {
return identity.IDPUser{}, err
}
u.Groups = groups
return u, nil
}
// groupFromCognitoGroup converts a cognito group to the identityprovider interface group type
func groupFromCognitoGroup(cognitoGroup types.GroupType) identity.IDPGroup {
return identity.IDPGroup{
ID: aws.ToString(cognitoGroup.GroupName),
Name: aws.ToString(cognitoGroup.GroupName),
Description: aws.ToString(cognitoGroup.Description),
}
}
func (c *CognitoSync) ListUsers(ctx context.Context) ([]identity.IDPUser, error) {
//get all users
users := []identity.IDPUser{}
hasMore := true
var paginationToken *string
for hasMore {
userRes, err := c.client.ListUsers(ctx, &cognitoidentityprovider.ListUsersInput{UserPoolId: aws.String(c.userPoolID.Get()), PaginationToken: paginationToken})
if err != nil {
return nil, err
}
for _, u := range userRes.Users {
// We skip syncing external users in cognito because this causes errors and is not what we need from our cognito sync.
// errors can happen in dev when we switch between providers
if u.UserStatus != "EXTERNAL_PROVIDER" {
user, err := c.idpUserFromCognitoUser(ctx, u)
if err != nil {
return nil, err
}
users = append(users, user)
}
}
paginationToken = userRes.PaginationToken
//Check that the next token is not nil so we don't need any more polling
hasMore = paginationToken != nil
}
return users, nil
}
func (c *CognitoSync) ListGroups(ctx context.Context) ([]identity.IDPGroup, error) {
groups := []identity.IDPGroup{}
hasMore := true
var paginationToken *string
for hasMore {
groupsRes, err := c.client.ListGroups(ctx, &cognitoidentityprovider.ListGroupsInput{UserPoolId: aws.String(c.userPoolID.Get()), NextToken: paginationToken})
if err != nil {
return nil, err
}
for _, u := range groupsRes.Groups {
groups = append(groups, groupFromCognitoGroup(u))
}
paginationToken = groupsRes.NextToken
//Check that the next token is not nil so we don't need any more polling
hasMore = paginationToken != nil
}
return groups, nil
}
func (c *CognitoSync) listUserGroups(ctx context.Context, id string) ([]string, error) {
groups := []string{}
hasMore := true
var paginationToken *string
for hasMore {
userGroupsRes, err := c.client.AdminListGroupsForUser(ctx, &cognitoidentityprovider.AdminListGroupsForUserInput{UserPoolId: aws.String(c.userPoolID.Get()), Username: aws.String(id), NextToken: paginationToken})
if err != nil {
return nil, err
}
for _, g := range userGroupsRes.Groups {
// group name is the id in cognito
groups = append(groups, aws.ToString(g.GroupName))
}
paginationToken = userGroupsRes.NextToken
//Check that the next token is not nil so we don't need any more polling
hasMore = paginationToken != nil
}
return groups, nil
}
type CreateUserOpts struct {
FirstName string
LastName string
Email string
}
func (c *CognitoSync) AdminCreateUser(ctx context.Context, in CreateUserOpts) (identity.IDPUser, error) {
res, err := c.client.AdminCreateUser(ctx, &cognitoidentityprovider.AdminCreateUserInput{
UserPoolId: aws.String(c.userPoolID.Get()),
Username: &in.Email,
UserAttributes: []types.AttributeType{
{
Name: aws.String("given_name"),
Value: &in.FirstName,
},
{
Name: aws.String("family_name"),
Value: &in.LastName,
},
},
})
if err != nil {
return identity.IDPUser{}, err
}
if res.User == nil {
return identity.IDPUser{}, errors.New("user was nil from cognito")
}
return c.idpUserFromCognitoUser(ctx, *res.User)
}
type CreateGroupOpts struct {
Name string
Description string
}
func (c *CognitoSync) AdminCreateGroup(ctx context.Context, in CreateGroupOpts) (identity.IDPGroup, error) {
res, err := c.client.CreateGroup(ctx, &cognitoidentityprovider.CreateGroupInput{
UserPoolId: aws.String(c.userPoolID.Get()),
GroupName: aws.String(in.Name),
Description: aws.String(in.Description),
})
if err != nil {
return identity.IDPGroup{}, err
}
if res.Group == nil {
return identity.IDPGroup{}, errors.New("group was nil from cognito")
}
return groupFromCognitoGroup(*res.Group), nil
}
type AddUserToGroupOpts struct {
UserID string
GroupID string
}
func (c *CognitoSync) AddUserToGroup(ctx context.Context, in AddUserToGroupOpts) error {
_, err := c.client.AdminAddUserToGroup(ctx, &cognitoidentityprovider.AdminAddUserToGroupInput{
UserPoolId: aws.String(c.userPoolID.Get()),
GroupName: aws.String(in.GroupID),
Username: aws.String(in.UserID),
})
return err
}
type RemoveUserFromGroupOpts struct {
UserID string
GroupID string
}
func (c *CognitoSync) RemoveUserFromGroup(ctx context.Context, in RemoveUserFromGroupOpts) error {
_, err := c.client.AdminRemoveUserFromGroup(ctx, &cognitoidentityprovider.AdminRemoveUserFromGroupInput{
UserPoolId: aws.String(c.userPoolID.Get()),
GroupName: aws.String(in.GroupID),
Username: aws.String(in.UserID),
})
return err
}
type UpdateUserGroupsOpts struct {
UserID string
Groups []string
}
func (c *CognitoSync) AdminUpdateUserGroups(ctx context.Context, in UpdateUserGroupsOpts) error {
existingGroups, err := c.listUserGroups(ctx, in.UserID)
if err != nil {
return err
}
// Using maps to do set intersections to work out which groups to add and remove
inMap := make(map[string]bool)
existingMap := make(map[string]bool)
for _, g := range in.Groups {
inMap[g] = true
}
for _, g := range existingGroups {
existingMap[g] = true
}
remove := make(map[string]bool)
add := make(map[string]bool)
for k := range inMap {
if !existingMap[k] {
add[k] = true
}
}
for k := range existingMap {
if !inMap[k] {
remove[k] = true
}
}
g, gctx := errgroup.WithContext(ctx)
for k := range remove {
kCopy := k
g.Go(func() error {
_, err := c.client.AdminRemoveUserFromGroup(gctx, &cognitoidentityprovider.AdminRemoveUserFromGroupInput{
GroupName: aws.String(kCopy),
UserPoolId: aws.String(c.userPoolID.Get()),
Username: aws.String(in.UserID),
})
return err
})
}
for k := range add {
kCopy := k
g.Go(func() error {
_, err := c.client.AdminAddUserToGroup(gctx, &cognitoidentityprovider.AdminAddUserToGroupInput{
GroupName: aws.String(kCopy),
UserPoolId: aws.String(c.userPoolID.Get()),
Username: aws.String(in.UserID),
})
return err
})
}
return g.Wait()
}