Skip to content

Commit

Permalink
google: Option to filter groups by regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrabaute committed Jan 20, 2020
1 parent aca67b0 commit 7c6c279
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Documentation/connectors/google.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ connectors:
#
#serviceAccountFilePath: googleAuth.json
#adminEmail: [email protected]

# When fetching groups, this field allows to add a regexp to filter the groups
# that will be returned and used.
# This can be useful when the user is registered on a lot of groups because
# he is part of multiple clusters. In this case, usually, the groups are named
# with a pattern and a regexp can be used to filter the groups per cluster,
# allowing to have a smaller group list retrieved for each cluster.
# Default to empty string, and no filtering will be done in this case.
#
# groupsFilter: '^regexp.*'
```

## Fetching groups from Google
Expand Down
23 changes: 21 additions & 2 deletions connector/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"

"github.com/coreos/go-oidc"
Expand Down Expand Up @@ -39,6 +40,9 @@ type Config struct {
// If this field is nonempty, only users from a listed group will be allowed to log in
Groups []string `json:"groups"`

// Optional field to filter groups by regexp
GroupsFilter string `json:"groupsFilter"`

// Optional path to service account json
// If nonempty, and groups claim is made, will use authentication from file to
// check groups with the admin directory api
Expand Down Expand Up @@ -73,6 +77,18 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
return nil, fmt.Errorf("could not create directory service: %v", err)
}

var groupsFilter *regexp.Regexp
if c.GroupsFilter != "" {
var err error
groupsFilter, err = regexp.Compile(c.GroupsFilter)
if err != nil {
cancel()
return nil, fmt.Errorf("unable to compile the groupsFilter regexp. "+
"Input string: %q; error: %v", c.GroupsFilter, err,
)
}
}

clientID := c.ClientID
return &googleConnector{
redirectURI: c.RedirectURI,
Expand All @@ -90,6 +106,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
cancel: cancel,
hostedDomains: c.HostedDomains,
groups: c.Groups,
groupsFilter: groupsFilter,
serviceAccountFilePath: c.ServiceAccountFilePath,
adminEmail: c.AdminEmail,
adminSrv: srv,
Expand All @@ -109,6 +126,7 @@ type googleConnector struct {
logger log.Logger
hostedDomains []string
groups []string
groupsFilter *regexp.Regexp
serviceAccountFilePath string
adminEmail string
adminSrv *admin.Service
Expand Down Expand Up @@ -251,8 +269,9 @@ func (c *googleConnector) getGroups(email string) ([]string, error) {
}

for _, group := range groupsList.Groups {
// TODO (joelspeed): Make desried group key configurable
userGroups = append(userGroups, group.Email)
if c.groupsFilter == nil || c.groupsFilter.MatchString(group.Email) {
userGroups = append(userGroups, group.Email)
}
}

if groupsList.NextPageToken == "" {
Expand Down

0 comments on commit 7c6c279

Please sign in to comment.