Skip to content

Commit

Permalink
Remove Google Admin SDK (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreda authored Jan 26, 2024
1 parent 5e49176 commit 48ee7a4
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 170 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Hermes was created and is currently maintained by HashiCorp Labs, a small team i

1. Enable the following APIs for [Google Workspace APIs](https://developers.google.com/workspace/guides/enable-apis)

- Admin SDK API
- Google Docs API
- Google Drive API
- Gmail API
Expand Down
44 changes: 0 additions & 44 deletions internal/api/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package api

import (
"encoding/json"
"errors"
"fmt"
"net/http"

gw "github.com/hashicorp-forge/hermes/pkg/googleworkspace"
"github.com/hashicorp/go-hclog"
"google.golang.org/api/people/v1"
)

// MeGetResponse mimics the response from Google's `userinfo/me` API
Expand Down Expand Up @@ -113,22 +111,6 @@ func MeHandler(
return
}

// Replace the names in the People API result with data from the Admin
// Directory API.
// TODO: remove this when the bug in the People API is fixed:
// https://issuetracker.google.com/issues/196235775
if err := replaceNamesWithAdminAPIResponse(
p, s,
); err != nil {
errResp(
http.StatusInternalServerError,
"Error getting user information",
"error replacing names with Admin API response",
err,
)
return
}

// Verify other required values are set.
if len(p.Names) == 0 {
errResp(
Expand Down Expand Up @@ -172,29 +154,3 @@ func MeHandler(
}
})
}

// Replace the names in the People API result with data from the Admin Directory
// API.
// TODO: remove this when the bug in the People API is fixed:
// https://issuetracker.google.com/issues/196235775
func replaceNamesWithAdminAPIResponse(
p *people.Person, s *gw.Service,
) error {
if len(p.EmailAddresses) == 0 {
return errors.New("email address not found")
}
u, err := s.GetUser(p.EmailAddresses[0].Value)
if err != nil {
return fmt.Errorf("error getting user: %w", err)
}

p.Names = []*people.Name{
{
DisplayName: u.Name.FullName,
FamilyName: u.Name.FamilyName,
GivenName: u.Name.GivenName,
},
}

return nil
}
45 changes: 0 additions & 45 deletions internal/api/v2/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package api

import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/hashicorp-forge/hermes/internal/server"
gw "github.com/hashicorp-forge/hermes/pkg/googleworkspace"
"google.golang.org/api/people/v1"
)

// MeGetResponse mimics the response from Google's `userinfo/me` API
Expand Down Expand Up @@ -110,22 +107,6 @@ func MeHandler(srv server.Server) http.Handler {
return
}

// Replace the names in the People API result with data from the Admin
// Directory API.
// TODO: remove this when the bug in the People API is fixed:
// https://issuetracker.google.com/issues/196235775
if err := replaceNamesWithAdminAPIResponse(
p, srv.GWService,
); err != nil {
errResp(
http.StatusInternalServerError,
"Error getting user information",
"error replacing names with Admin API response",
err,
)
return
}

// Verify other required values are set.
if len(p.Names) == 0 {
errResp(
Expand Down Expand Up @@ -169,29 +150,3 @@ func MeHandler(srv server.Server) http.Handler {
}
})
}

// Replace the names in the People API result with data from the Admin Directory
// API.
// TODO: remove this when the bug in the People API is fixed:
// https://issuetracker.google.com/issues/196235775
func replaceNamesWithAdminAPIResponse(
p *people.Person, s *gw.Service,
) error {
if len(p.EmailAddresses) == 0 {
return errors.New("email address not found")
}
u, err := s.GetUser(p.EmailAddresses[0].Value)
if err != nil {
return fmt.Errorf("error getting user: %w", err)
}

p.Names = []*people.Name{
{
DisplayName: u.Name.FullName,
FamilyName: u.Name.FamilyName,
GivenName: u.Name.GivenName,
},
}

return nil
}
64 changes: 0 additions & 64 deletions pkg/googleworkspace/admin_helpers.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/googleworkspace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"golang.org/x/oauth2/jwt"

"github.com/pkg/browser"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/docs/v1"
"google.golang.org/api/drive/v3"
"google.golang.org/api/gmail/v1"
Expand All @@ -25,7 +24,6 @@ import (

// Service provides access to the Google Workspace API.
type Service struct {
Admin *admin.Service
Docs *docs.Service
Drive *drive.Service
Gmail *gmail.Service
Expand All @@ -52,8 +50,6 @@ func NewFromConfig(cfg *Config) *Service {
Email: cfg.ClientEmail,
PrivateKey: []byte(cfg.PrivateKey),
Scopes: []string{
// "https://www.googleapis.com/auth/admin.directory.group",
"https://www.googleapis.com/auth/admin.directory.user.readonly",
"https://www.googleapis.com/auth/directory.readonly",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive",
Expand All @@ -64,10 +60,6 @@ func NewFromConfig(cfg *Config) *Service {
}
client := conf.Client(context.TODO())

adminSrv, err := admin.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Admin client: %v", err)
}
docSrv, err := docs.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Docs client: %v", err)
Expand All @@ -91,7 +83,6 @@ func NewFromConfig(cfg *Config) *Service {
peoplePeopleSrv := people.NewPeopleService(peopleSrv)

return &Service{
Admin: adminSrv,
Docs: docSrv,
Drive: driveSrv,
Gmail: gmailSrv,
Expand All @@ -114,8 +105,6 @@ func New() *Service {

// If modifying these scopes, delete your previously saved token.json.
gc, err := google.ConfigFromJSON(b,
// "https://www.googleapis.com/auth/admin.directory.group",
"https://www.googleapis.com/auth/admin.directory.user.readonly",
"https://www.googleapis.com/auth/directory.readonly",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive",
Expand All @@ -125,10 +114,6 @@ func New() *Service {
}
client := getClient(gc)

adminSrv, err := admin.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Admin client: %v", err)
}
docSrv, err := docs.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Google Docs client: %v", err)
Expand All @@ -152,7 +137,6 @@ func New() *Service {
peoplePeopleSrv := people.NewPeopleService(peopleSrv)

return &Service{
Admin: adminSrv,
Docs: docSrv,
Drive: driveSrv,
Gmail: gmailSrv,
Expand Down

0 comments on commit 48ee7a4

Please sign in to comment.