Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions internal/googlews/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strings"

"golang.org/x/oauth2/google"
Expand Down Expand Up @@ -43,11 +44,24 @@ type membersListCaller interface {
// New builds a Service backed by the real Admin Directory API using JWT
// domain-wide delegation (impersonating the configured admin email address).
func New(ctx context.Context, cfg *config.Config) (*Service, error) {
jwtCfg, err := google.JWTConfigFromJSON(cfg.GoogleCredentialsJSON, userScope, memberScope)
scopes := scopesFor(cfg)
jwtCfg, err := google.JWTConfigFromJSON(cfg.GoogleCredentialsJSON, scopes...)
if err != nil {
return nil, fmt.Errorf("parsing google credentials: %w", err)
}
jwtCfg.Subject = cfg.GoogleEmailAddress
slog.Info("google directory auth",
"subject", jwtCfg.Subject,
"service_account", jwtCfg.Email,
"client_id", cfg.GoogleCredentials.ClientID,
"scopes", jwtCfg.Scopes,
)
if strings.TrimSpace(jwtCfg.Subject) == "" {
return nil, fmt.Errorf("google-email-address is empty; domain-wide delegation needs an admin to impersonate")
}
if jwtCfg.Subject != strings.TrimSpace(jwtCfg.Subject) {
slog.Warn("google-email-address has surrounding whitespace", "subject", jwtCfg.Subject)
}

svc, err := admin.NewService(ctx, option.WithHTTPClient(jwtCfg.Client(ctx)))
if err != nil {
Expand All @@ -60,13 +74,30 @@ func New(ctx context.Context, cfg *config.Config) (*Service, error) {

// GithubUsersFromGoogle returns the set of GitHub usernames linked across all
// Google Workspace users, paging through the directory.
// scopesFor is the JWT scope list. Group-member scope is only requested when
// a team map is configured; asking for it unconditionally makes domain-wide
// delegation reject the token for installs that only granted user.readonly.
func scopesFor(cfg *config.Config) []string {
if cfg != nil && len(cfg.TeamMap) > 0 {
return []string{userScope, memberScope}
}
return []string{userScope}
}

func (s *Service) subject() string {
if s.cfg == nil {
return ""
}
return s.cfg.GoogleEmailAddress
}

func (s *Service) GithubUsersFromGoogle(ctx context.Context) (set.Set, error) {
accounts := set.New()
pageToken := ""
for {
resp, err := s.users.List(ctx, pageToken)
if err != nil {
return nil, fmt.Errorf("listing google users: %w", err)
return nil, fmt.Errorf("listing google users as %q scopes %s: %w", s.subject(), strings.Join(scopesFor(s.cfg), " "), err)
}
for _, u := range formatUserList(resp.Users) {
accounts.Add(u)
Expand Down Expand Up @@ -97,7 +128,7 @@ func (s *Service) GithubUsersForGroups(ctx context.Context, groups []string) (ma
}
emails, err := s.groupMemberEmails(ctx, group, map[string]bool{})
if err != nil {
return nil, fmt.Errorf("listing members of group %s: %w", group, err)
return nil, fmt.Errorf("listing members of group %s as %q: %w", group, s.subject(), err)
}
accounts := set.New()
for _, email := range emails {
Expand All @@ -118,7 +149,7 @@ func (s *Service) emailToGithub(ctx context.Context) (map[string]set.Set, error)
for {
resp, err := s.users.List(ctx, pageToken)
if err != nil {
return nil, fmt.Errorf("listing google users: %w", err)
return nil, fmt.Errorf("listing google users as %q scopes %s: %w", s.subject(), strings.Join(scopesFor(s.cfg), " "), err)
}
for _, u := range resp.Users {
if u == nil || u.PrimaryEmail == "" {
Expand Down
27 changes: 27 additions & 0 deletions internal/googlews/google_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package googlews

import (
"context"
"fmt"
"strings"
"testing"

admin "google.golang.org/api/admin/directory/v1"
Expand Down Expand Up @@ -61,6 +63,31 @@ func (f *fakeUsers) List(_ context.Context, token string) (*admin.Users, error)
return f.pages[idx], nil
}

type errUsers struct{}

func (errUsers) List(context.Context, string) (*admin.Users, error) {
return nil, fmt.Errorf("boom")
}

func TestListErrorNamesSubject(t *testing.T) {
s := &Service{cfg: &config.Config{GoogleEmailAddress: "admin@example.com"}, users: errUsers{}}
_, err := s.GithubUsersFromGoogle(context.Background())
if err == nil || !strings.Contains(err.Error(), "admin@example.com") {
t.Fatalf("err = %v, want subject", err)
}
}

func TestScopesForTeamMap(t *testing.T) {
got := scopesFor(&config.Config{})
if len(got) != 1 || got[0] != userScope {
t.Fatalf("no team map: %v", got)
}
got = scopesFor(&config.Config{TeamMap: map[string]string{"eng@x": "eng"}})
if len(got) != 2 || got[0] != userScope || got[1] != memberScope {
t.Fatalf("team map: %v", got)
}
}

func TestGithubUsersFromGooglePaginates(t *testing.T) {
s := &Service{users: &fakeUsers{pages: []*admin.Users{
{Users: []*admin.User{user(`{"github":[{"value":"a"}]}`)}, NextPageToken: "p2"},
Expand Down
Loading