Files
probo/pkg/accessreview/drivers/anthropic.go
Aurélien Sibiril 50093c9d19 Add Anthropic access-review driver and resolver
The driver lists organization members through the Anthropic Admin API
(GET /v1/organizations/users) with cursor pagination, mapping the role
and the RFC 3339 added_at timestamp. The name resolver reads the
organization name from /v1/organizations/me; a non-2xx response (for
example a revoked key) yields no name rather than making the
source-name worker retry forever. Both send the required
anthropic-version header.

Add a VCR test helper that injects the key via x-api-key so the
cassette stays recordable, and strip x-api-key on save. The cassette
holds synthetic members covering the user, developer, and admin roles.

Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
2026-05-28 22:33:29 +02:00

160 lines
4.1 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package drivers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"go.probo.inc/probo/pkg/coredata"
)
type AnthropicDriver struct {
httpClient *http.Client
}
var _ Driver = (*AnthropicDriver)(nil)
const (
anthropicUsersEndpoint = "https://api.anthropic.com/v1/organizations/users"
// anthropicAPIVersion is the required anthropic-version header value
// sent on every Admin API request. Shared with the name resolver.
anthropicAPIVersion = "2023-06-01"
)
type anthropicUsersResponse struct {
Data []struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
AddedAt string `json:"added_at"`
} `json:"data"`
HasMore bool `json:"has_more"`
LastID string `json:"last_id"`
}
func NewAnthropicDriver(httpClient *http.Client) *AnthropicDriver {
return &AnthropicDriver{
httpClient: httpClient,
}
}
func (d *AnthropicDriver) ListAccounts(ctx context.Context) ([]AccountRecord, error) {
var (
records []AccountRecord
afterID string
)
for range maxPaginationPages {
resp, err := d.fetchUsers(ctx, afterID)
if err != nil {
return nil, err
}
for _, u := range resp.Data {
record := AccountRecord{
Email: u.Email,
FullName: u.Name,
Role: anthropicRole(u.Role),
IsAdmin: u.Role == "admin",
ExternalID: u.ID,
MFAStatus: coredata.MFAStatusUnknown,
AuthMethod: coredata.AccessEntryAuthMethodUnknown,
AccountType: coredata.AccessEntryAccountTypeUser,
}
// added_at is an RFC 3339 datetime string; ignore parse
// failures rather than dropping the record.
if u.AddedAt != "" {
if t, err := time.Parse(time.RFC3339, u.AddedAt); err == nil {
record.CreatedAt = &t
}
}
if record.Email != "" {
records = append(records, record)
}
}
if !resp.HasMore || resp.LastID == "" {
return records, nil
}
afterID = resp.LastID
}
return nil, fmt.Errorf("cannot list all anthropic accounts: %w", ErrPaginationLimitReached)
}
func (d *AnthropicDriver) fetchUsers(ctx context.Context, afterID string) (*anthropicUsersResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, anthropicUsersEndpoint, nil)
if err != nil {
return nil, fmt.Errorf("cannot create anthropic users request: %w", err)
}
q := req.URL.Query()
q.Set("limit", "100")
if afterID != "" {
q.Set("after_id", afterID)
}
req.URL.RawQuery = q.Encode()
req.Header.Set("Accept", "application/json")
req.Header.Set("anthropic-version", anthropicAPIVersion)
httpResp, err := d.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("cannot execute anthropic users request: %w", err)
}
defer func() {
_ = httpResp.Body.Close()
}()
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
return nil, fmt.Errorf("cannot fetch anthropic users: unexpected status %d", httpResp.StatusCode)
}
var resp anthropicUsersResponse
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("cannot decode anthropic users response: %w", err)
}
return &resp, nil
}
func anthropicRole(role string) string {
switch role {
case "admin":
return "Admin"
case "billing":
return "Billing"
case "developer":
return "Developer"
case "claude_code_user":
return "Claude Code User"
case "user":
return "User"
default:
return role
}
}