A fetch runs under a 30-second per-source budget, but the retry transport slept without reference to it, so backoff could convert a reportable provider status into an opaque "context deadline exceeded". Three changes. The final attempt no longer sleeps: nothing follows it, so the wait only spent the caller's deadline to return a response already in hand — up to a second per failed request, across sixteen drivers. Retry-After is now honoured, in both the delta-seconds and HTTP-date forms; ignoring it meant retrying a 429 after 250ms and earning another 429, spending the whole retry budget in under a second. And a wait is skipped entirely when it exceeds the remaining deadline or a 5s cap, because a retry that lands after the deadline cannot succeed — the throttled response is surfaced instead so the caller reports what the provider actually said. The type moves from google_workspace.go to driver.go, which is where the other shared driver machinery lives; sixteen drivers construct it and none of them are Google Workspace. It had no tests, so it has them now. Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package drivers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.probo.inc/probo/pkg/coredata"
|
|
admin "google.golang.org/api/admin/directory/v1"
|
|
"google.golang.org/api/option"
|
|
)
|
|
|
|
// GoogleWorkspaceDriver fetches user accounts from Google Workspace
|
|
// using the Admin Directory API via an OAuth2-authenticated HTTP client.
|
|
type GoogleWorkspaceDriver struct {
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewGoogleWorkspaceDriver(httpClient *http.Client) *GoogleWorkspaceDriver {
|
|
return &GoogleWorkspaceDriver{
|
|
httpClient: &http.Client{
|
|
Transport: &retryRoundTripper{
|
|
next: httpClient.Transport,
|
|
maxRetries: 3,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (d *GoogleWorkspaceDriver) ListAccounts(ctx context.Context) ([]AccountRecord, error) {
|
|
adminService, err := admin.NewService(ctx, option.WithHTTPClient(d.httpClient))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot create google admin service: %w", err)
|
|
}
|
|
|
|
var records []AccountRecord
|
|
|
|
pageToken := ""
|
|
|
|
for range maxPaginationPages {
|
|
call := adminService.Users.List().
|
|
Customer("my_customer").
|
|
MaxResults(500).
|
|
Projection("full").
|
|
Context(ctx)
|
|
if pageToken != "" {
|
|
call = call.PageToken(pageToken)
|
|
}
|
|
|
|
resp, err := call.Do()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot list google workspace users: %w", err)
|
|
}
|
|
|
|
for _, u := range resp.Users {
|
|
rec := AccountRecord{
|
|
Email: u.PrimaryEmail,
|
|
FullName: u.Name.FullName,
|
|
Active: new(!u.Suspended && !u.Archived),
|
|
IsAdmin: u.IsAdmin,
|
|
ExternalID: u.Id,
|
|
MFAStatus: coredata.MFAStatusUnknown,
|
|
AuthMethod: coredata.AccessReviewEntryAuthMethodSSO,
|
|
AccountType: coredata.AccessReviewEntryAccountTypeUser,
|
|
}
|
|
|
|
if u.IsEnrolledIn2Sv {
|
|
rec.MFAStatus = coredata.MFAStatusEnabled
|
|
} else {
|
|
rec.MFAStatus = coredata.MFAStatusDisabled
|
|
}
|
|
|
|
if u.CreationTime != "" {
|
|
if t, err := time.Parse(time.RFC3339, u.CreationTime); err == nil {
|
|
rec.CreatedAt = &t
|
|
}
|
|
}
|
|
|
|
if u.LastLoginTime != "" {
|
|
if t, err := time.Parse(time.RFC3339, u.LastLoginTime); err == nil {
|
|
rec.LastLogin = &t
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case u.IsAdmin:
|
|
rec.Roles = []string{"Super Admin"}
|
|
case u.IsDelegatedAdmin:
|
|
rec.Roles = []string{"Delegated Admin"}
|
|
default:
|
|
rec.Roles = []string{"User"}
|
|
}
|
|
|
|
records = append(records, rec)
|
|
}
|
|
|
|
pageToken = resp.NextPageToken
|
|
if pageToken == "" {
|
|
return records, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("cannot list all google workspace accounts: %w", ErrPaginationLimitReached)
|
|
}
|