Skip to content

Commit b497a7d

Browse files
codrutpaneajbuck
authored andcommitted
feat(google): fetch groups in parallel
Signed-off-by: Codrut Panea <codrut@flowx.ai>
1 parent 8e96058 commit b497a7d

3 files changed

Lines changed: 168 additions & 40 deletions

File tree

‎connector/google/google.go‎

Lines changed: 100 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import (
88
"log/slog"
99
"net/http"
1010
"os"
11+
"sort"
1112
"strings"
13+
"sync"
1214
"time"
1315

1416
"cloud.google.com/go/compute/metadata"
1517
"github.com/coreos/go-oidc/v3/oidc"
1618
"golang.org/x/exp/slices"
1719
"golang.org/x/oauth2"
1820
"golang.org/x/oauth2/google"
21+
"golang.org/x/sync/errgroup"
1922
admin "google.golang.org/api/admin/directory/v1"
2023
"google.golang.org/api/impersonate"
2124
"google.golang.org/api/option"
@@ -27,6 +30,10 @@ import (
2730
const (
2831
issuerURL = "https://accounts.google.com"
2932
wildcardDomainToAdminEmail = "*"
33+
34+
// defaultConcurrentGroupLookups is the limit used when Config.MaxConcurrentGroupLookups
35+
// is zero or negative.
36+
defaultConcurrentGroupLookups = 10
3037
)
3138

3239
// Config holds configuration options for Google logins.
@@ -61,6 +68,10 @@ type Config struct {
6168
// If this field is true, fetch direct group membership and transitive group membership
6269
FetchTransitiveGroupMembership bool `json:"fetchTransitiveGroupMembership"`
6370

71+
// MaxConcurrentGroupLookups limits concurrent Admin Directory API calls when resolving
72+
// transitive group membership. If zero or negative, the connector default limit applies.
73+
MaxConcurrentGroupLookups int `json:"maxConcurrentGroupLookups"`
74+
6475
// Optional value for the prompt parameter, defaults to consent when offline_access
6576
// scope is requested
6677
PromptType *string `json:"promptType"`
@@ -119,6 +130,11 @@ func (c *Config) Open(id string, logger *slog.Logger) (conn connector.Connector,
119130
}
120131

121132
clientID := c.ClientID
133+
maxConcurrent := c.MaxConcurrentGroupLookups
134+
if maxConcurrent <= 0 {
135+
maxConcurrent = defaultConcurrentGroupLookups
136+
}
137+
122138
return &googleConnector{
123139
redirectURI: c.RedirectURI,
124140
oauth2Config: &oauth2.Config{
@@ -138,6 +154,7 @@ func (c *Config) Open(id string, logger *slog.Logger) (conn connector.Connector,
138154
serviceAccountFilePath: c.ServiceAccountFilePath,
139155
domainToAdminEmail: c.DomainToAdminEmail,
140156
fetchTransitiveGroupMembership: c.FetchTransitiveGroupMembership,
157+
maxConcurrentGroupLookups: maxConcurrent,
141158
adminSrv: adminSrv,
142159
promptType: promptType,
143160
}, nil
@@ -159,6 +176,7 @@ type googleConnector struct {
159176
serviceAccountFilePath string
160177
domainToAdminEmail map[string]string
161178
fetchTransitiveGroupMembership bool
179+
maxConcurrentGroupLookups int
162180
adminSrv map[string]*admin.Service
163181
promptType string
164182
}
@@ -263,8 +281,7 @@ func (c *googleConnector) createIdentity(ctx context.Context, identity connector
263281

264282
var groups []string
265283
if s.Groups && len(c.adminSrv) > 0 {
266-
checkedGroups := make(map[string]struct{})
267-
groups, err = c.getGroups(claims.Email, c.fetchTransitiveGroupMembership, checkedGroups)
284+
groups, err = c.getGroups(ctx, claims.Email, c.fetchTransitiveGroupMembership)
268285
if err != nil {
269286
return identity, fmt.Errorf("google: could not retrieve groups: %v", err)
270287
}
@@ -289,52 +306,107 @@ func (c *googleConnector) createIdentity(ctx context.Context, identity connector
289306
}
290307

291308
// getGroups creates a connection to the admin directory service and lists
292-
// all groups the user is a member of
293-
func (c *googleConnector) getGroups(email string, fetchTransitiveGroupMembership bool, checkedGroups map[string]struct{}) ([]string, error) {
294-
var userGroups []string
295-
var err error
296-
groupsList := &admin.Groups{}
297-
domain := c.extractDomainFromEmail(email)
298-
adminSrv, err := c.findAdminService(domain)
309+
// all groups the user is a member of.
310+
func (c *googleConnector) getGroups(ctx context.Context, email string, fetchTransitiveGroupMembership bool) ([]string, error) {
311+
directGroups, err := c.listGroupEmails(ctx, email)
299312
if err != nil {
300313
return nil, err
301314
}
302315

303-
for {
304-
groupsList, err = adminSrv.Groups.List().
305-
UserKey(email).PageToken(groupsList.NextPageToken).Do()
306-
if err != nil {
307-
return nil, fmt.Errorf("could not list groups: %v", err)
316+
var seenMu sync.Mutex
317+
seen := make(map[string]struct{})
318+
userGroups := make([]string, 0, len(directGroups))
319+
addGroup := func(groupEmail string) bool {
320+
seenMu.Lock()
321+
defer seenMu.Unlock()
322+
if _, exists := seen[groupEmail]; exists {
323+
return false
308324
}
325+
seen[groupEmail] = struct{}{}
326+
// TODO (joelspeed): Make desired group key configurable
327+
userGroups = append(userGroups, groupEmail)
328+
return true
329+
}
309330

310-
for _, group := range groupsList.Groups {
311-
if _, exists := checkedGroups[group.Email]; exists {
312-
continue
313-
}
331+
seeds := make([]string, 0, len(directGroups))
332+
for _, groupEmail := range directGroups {
333+
if addGroup(groupEmail) {
334+
seeds = append(seeds, groupEmail)
335+
}
336+
}
337+
338+
if !fetchTransitiveGroupMembership || len(seeds) == 0 {
339+
sort.Strings(userGroups)
340+
return userGroups, nil
341+
}
314342

315-
checkedGroups[group.Email] = struct{}{}
316-
// TODO (joelspeed): Make desired group key configurable
317-
userGroups = append(userGroups, group.Email)
343+
apiSem := make(chan struct{}, c.maxConcurrentGroupLookups)
344+
g, gctx := errgroup.WithContext(ctx)
318345

319-
if !fetchTransitiveGroupMembership {
320-
continue
346+
var enqueue func(string)
347+
enqueue = func(groupEmail string) {
348+
g.Go(func() error {
349+
if err := gctx.Err(); err != nil {
350+
return err
351+
}
352+
select {
353+
case <-gctx.Done():
354+
return gctx.Err()
355+
case apiSem <- struct{}{}:
321356
}
357+
defer func() { <-apiSem }()
322358

323-
// getGroups takes a user's email/alias as well as a group's email/alias
324-
transitiveGroups, err := c.getGroups(group.Email, fetchTransitiveGroupMembership, checkedGroups)
359+
parentGroups, err := c.listGroupEmails(gctx, groupEmail)
325360
if err != nil {
326-
return nil, fmt.Errorf("could not list transitive groups: %v", err)
361+
return fmt.Errorf("could not list transitive groups: %w", err)
362+
}
363+
for _, parent := range parentGroups {
364+
if addGroup(parent) {
365+
enqueue(parent)
366+
}
327367
}
368+
return nil
369+
})
370+
}
371+
372+
for _, groupEmail := range seeds {
373+
enqueue(groupEmail)
374+
}
375+
376+
if err := g.Wait(); err != nil {
377+
return nil, err
378+
}
328379

329-
userGroups = append(userGroups, transitiveGroups...)
380+
sort.Strings(userGroups)
381+
return userGroups, nil
382+
}
383+
384+
func (c *googleConnector) listGroupEmails(ctx context.Context, userKey string) ([]string, error) {
385+
domain := c.extractDomainFromEmail(userKey)
386+
adminSrv, err := c.findAdminService(domain)
387+
if err != nil {
388+
return nil, err
389+
}
390+
391+
groupEmails := []string{}
392+
groupsList := &admin.Groups{}
393+
for {
394+
groupsList, err = adminSrv.Groups.List().
395+
UserKey(userKey).PageToken(groupsList.NextPageToken).Context(ctx).Do()
396+
if err != nil {
397+
return nil, fmt.Errorf("could not list groups: %v", err)
398+
}
399+
400+
for _, group := range groupsList.Groups {
401+
groupEmails = append(groupEmails, group.Email)
330402
}
331403

332404
if groupsList.NextPageToken == "" {
333405
break
334406
}
335407
}
336408

337-
return userGroups, nil
409+
return groupEmails, nil
338410
}
339411

340412
func (c *googleConnector) findAdminService(domain string) (*admin.Service, error) {

‎connector/google/google_test.go‎

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"net/url"
1111
"os"
1212
"strings"
13+
"sync"
1314
"testing"
15+
"time"
1416

1517
"github.com/stretchr/testify/assert"
1618
admin "google.golang.org/api/admin/directory/v1"
@@ -32,7 +34,8 @@ var (
3234
"groups_2@dexidp.com": {{Email: "groups_0@dexidp.com"}},
3335
"groups_0@dexidp.com": {},
3436
}
35-
callCounter = make(map[string]int)
37+
callCounterMu sync.Mutex
38+
callCounter = make(map[string]int)
3639
)
3740

3841
func testSetup() *httptest.Server {
@@ -43,7 +46,9 @@ func testSetup() *httptest.Server {
4346
userKey := r.URL.Query().Get("userKey")
4447
if groups, ok := testGroups[userKey]; ok {
4548
json.NewEncoder(w).Encode(admin.Groups{Groups: groups})
49+
callCounterMu.Lock()
4650
callCounter[userKey]++
51+
callCounterMu.Unlock()
4752
}
4853
})
4954

@@ -224,23 +229,71 @@ func TestGetGroups(t *testing.T) {
224229
},
225230
} {
226231
testCase := testCase
227-
callCounter = map[string]int{}
232+
callCounterMu.Lock()
233+
callCounter = make(map[string]int)
234+
callCounterMu.Unlock()
228235
t.Run(name, func(t *testing.T) {
229236
assert := assert.New(t)
230-
lookup := make(map[string]struct{})
231237

232-
groups, err := conn.getGroups(testCase.userKey, testCase.fetchTransitiveGroupMembership, lookup)
238+
groups, err := conn.getGroups(context.Background(), testCase.userKey, testCase.fetchTransitiveGroupMembership)
233239
if testCase.shouldErr {
234240
assert.NotNil(err)
235241
} else {
236242
assert.Nil(err)
237243
}
238244
assert.ElementsMatch(testCase.expectedGroups, groups)
239-
t.Logf("[%s] Amount of API calls per userKey: %+v\n", t.Name(), callCounter)
245+
callCounterMu.Lock()
246+
s := fmt.Sprintf("%+v", callCounter)
247+
callCounterMu.Unlock()
248+
t.Logf("[%s] Amount of API calls per userKey: %s\n", t.Name(), s)
240249
})
241250
}
242251
}
243252

253+
// Regression test for MaxConcurrentGroupLookups=1 with a user -> A -> B membership chain.
254+
func TestGetGroups_transitiveNoDeadlockAtConcurrentLimitOne(t *testing.T) {
255+
chain := map[string][]*admin.Group{
256+
"user_chain@dexidp.com": {{Email: "group_a@dexidp.com"}},
257+
"group_a@dexidp.com": {{Email: "group_b@dexidp.com"}},
258+
"group_b@dexidp.com": {},
259+
}
260+
261+
mux := http.NewServeMux()
262+
mux.HandleFunc("/admin/directory/v1/groups/", func(w http.ResponseWriter, r *http.Request) {
263+
w.Header().Add("Content-Type", "application/json")
264+
userKey := r.URL.Query().Get("userKey")
265+
if groups, ok := chain[userKey]; ok {
266+
_ = json.NewEncoder(w).Encode(admin.Groups{Groups: groups})
267+
}
268+
})
269+
ts := httptest.NewServer(mux)
270+
defer ts.Close()
271+
272+
serviceAccountFilePath, err := tempServiceAccountKey()
273+
assert.Nil(t, err)
274+
275+
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", serviceAccountFilePath)
276+
conn, err := newConnector(&Config{
277+
ClientID: "testClient",
278+
ClientSecret: "testSecret",
279+
RedirectURI: ts.URL + "/callback",
280+
Scopes: []string{"openid", "groups"},
281+
DomainToAdminEmail: map[string]string{"*": "admin@dexidp.com"},
282+
MaxConcurrentGroupLookups: 1,
283+
})
284+
assert.Nil(t, err)
285+
286+
conn.adminSrv[wildcardDomainToAdminEmail], err = admin.NewService(context.Background(), option.WithoutAuthentication(), option.WithEndpoint(ts.URL))
287+
assert.Nil(t, err)
288+
289+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
290+
defer cancel()
291+
292+
groups, err := conn.getGroups(ctx, "user_chain@dexidp.com", true)
293+
assert.Nil(t, err)
294+
assert.Equal(t, []string{"group_a@dexidp.com", "group_b@dexidp.com"}, groups)
295+
}
296+
244297
func TestDomainToAdminEmailConfig(t *testing.T) {
245298
ts := testSetup()
246299
defer ts.Close()
@@ -280,18 +333,22 @@ func TestDomainToAdminEmailConfig(t *testing.T) {
280333
},
281334
} {
282335
testCase := testCase
283-
callCounter = map[string]int{}
336+
callCounterMu.Lock()
337+
callCounter = make(map[string]int)
338+
callCounterMu.Unlock()
284339
t.Run(name, func(t *testing.T) {
285340
assert := assert.New(t)
286-
lookup := make(map[string]struct{})
287341

288-
_, err := conn.getGroups(testCase.userKey, true, lookup)
342+
_, err := conn.getGroups(context.Background(), testCase.userKey, true)
289343
if testCase.expectedErr != "" {
290344
assert.ErrorContains(err, testCase.expectedErr)
291345
} else {
292346
assert.Nil(err)
293347
}
294-
t.Logf("[%s] Amount of API calls per userKey: %+v\n", t.Name(), callCounter)
348+
callCounterMu.Lock()
349+
s := fmt.Sprintf("%+v", callCounter)
350+
callCounterMu.Unlock()
351+
t.Logf("[%s] Amount of API calls per userKey: %s\n", t.Name(), s)
295352
})
296353
}
297354
}
@@ -381,9 +438,8 @@ func TestGCEWorkloadIdentity(t *testing.T) {
381438
} {
382439
t.Run(name, func(t *testing.T) {
383440
assert := assert.New(t)
384-
lookup := make(map[string]struct{})
385441

386-
_, err := conn.getGroups(testCase.userKey, true, lookup)
442+
_, err := conn.getGroups(context.Background(), testCase.userKey, true)
387443
if testCase.expectedErr != "" {
388444
assert.ErrorContains(err, testCase.expectedErr)
389445
} else {

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
golang.org/x/exp v0.0.0-20221004215720-b9f4876ce741
3737
golang.org/x/net v0.40.0
3838
golang.org/x/oauth2 v0.30.0
39+
golang.org/x/sync v0.14.0
3940
google.golang.org/api v0.233.0
4041
google.golang.org/grpc v1.72.1
4142
google.golang.org/protobuf v1.36.6
@@ -99,7 +100,6 @@ require (
99100
go.uber.org/multierr v1.11.0 // indirect
100101
go.uber.org/zap v1.27.0 // indirect
101102
golang.org/x/mod v0.23.0 // indirect
102-
golang.org/x/sync v0.14.0 // indirect
103103
golang.org/x/sys v0.33.0 // indirect
104104
golang.org/x/text v0.25.0 // indirect
105105
golang.org/x/tools v0.30.0 // indirect

0 commit comments

Comments
 (0)