Skip to content

Commit

Permalink
Fetch groups using pagination when fetching Gsuite groups (#566)
Browse files Browse the repository at this point in the history
* Fetch groups using pagination when fetching Gsuite groups
* remove indexer from default plugin list to enable
  • Loading branch information
sandromello authored Nov 22, 2024
1 parent a212c47 commit dd27bba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion gateway/pgrest/plugins/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

var DefaultPluginNames = []string{
plugintypes.PluginAuditName,
plugintypes.PluginIndexName,
plugintypes.PluginEditorName,
plugintypes.PluginSlackName,
plugintypes.PluginRunbooksName,
Expand Down
40 changes: 33 additions & 7 deletions gateway/security/idp/gsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (
"net/http"
)

const gSuiteGroupsURL = "https://www.googleapis.com/admin/directory/v1/groups"
const (
// https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list
gSuiteGroupsURL = "https://www.googleapis.com/admin/directory/v1/groups"
defaultMaxPages = 3
defaultMaxResults = 200
)

type gsuiteGroups struct {
NextPageToken string `json:"nextPageToken"`
Expand All @@ -19,7 +24,32 @@ type gsuiteGroupEntry struct {
}

func (p *Provider) fetchGsuiteGroups(accessToken, email string) ([]string, error) {
apiURL := fmt.Sprintf("%s?userKey=%s", gSuiteGroupsURL, email)
var groups []string
var nextPageToken string

for count := 0; ; count++ {
if count > defaultMaxPages {
return nil, fmt.Errorf("reached max pagination (%v) fetching Gsuite Groups", defaultMaxPages)
}
response, err := p.fetchGroupsPage(accessToken, email, nextPageToken)
if err != nil {
return nil, fmt.Errorf("page=%v, %v", count, err)
}
for _, entry := range response.Groups {
groups = append(groups, entry.Email)
}
if response.NextPageToken != "" {
nextPageToken = response.NextPageToken
continue
}
break
}
return groups, nil
}

func (p *Provider) fetchGroupsPage(accessToken, email, pageToken string) (*gsuiteGroups, error) {
apiURL := fmt.Sprintf("%s?userKey=%s&pageToken=%s&maxResults=%v",
gSuiteGroupsURL, email, pageToken, defaultMaxResults)
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, fmt.Errorf("failed creating request to gsuite, reason=%v", err)
Expand All @@ -39,9 +69,5 @@ func (p *Provider) fetchGsuiteGroups(accessToken, email string) ([]string, error
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed decoding gsuite response, reason=%v", err)
}
var groups []string
for _, group := range response.Groups {
groups = append(groups, group.Email)
}
return groups, nil
return &response, nil
}

0 comments on commit dd27bba

Please sign in to comment.