268 lines
10 KiB
Go
268 lines
10 KiB
Go
package aibridged
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
"storj.io/drpc/drpcerr"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/aibridge"
|
|
"github.com/coder/coder/v2/aibridge/routing"
|
|
agplaibridge "github.com/coder/coder/v2/coderd/aibridge"
|
|
"github.com/coder/coder/v2/coderd/aibridged/proto"
|
|
)
|
|
|
|
var _ http.Handler = &Server{}
|
|
|
|
var (
|
|
ErrNoAuthKey = xerrors.New("no authentication key provided")
|
|
ErrConnect = xerrors.New("could not connect to coderd")
|
|
ErrUnauthorized = xerrors.New("unauthorized")
|
|
ErrAcquireRequestHandler = xerrors.New("failed to acquire request handler")
|
|
ErrAuthorizationCheck = xerrors.New("internal server error checking authorization")
|
|
ErrBudgetCheck = xerrors.New("internal server error checking user AI budget")
|
|
)
|
|
|
|
// ServeHTTP is the entrypoint for requests which will be intercepted by AI Bridge.
|
|
// This function will validate that the given API key may be used to perform the request.
|
|
//
|
|
// An [aibridge.RequestBridge] instance is acquired from a pool based on the API key's
|
|
// owner (referred to as the "initiator"); this instance is responsible for the
|
|
// AI Bridge-specific handling of the request.
|
|
//
|
|
// A [DRPCClient] is provided to the [aibridge.RequestBridge] instance so that data can
|
|
// be passed up to a [DRPCServer] for persistence.
|
|
func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
logger := s.logger.With(
|
|
slog.F("method", r.Method),
|
|
slog.F("path", r.URL.Path),
|
|
)
|
|
|
|
// Extract and strip proxy request ID for cross-service log
|
|
// correlation. Absent for direct requests not routed through
|
|
// aibridgeproxyd.
|
|
if proxyReqID := r.Header.Get(agplaibridge.HeaderCoderRequestID); proxyReqID != "" {
|
|
// Inject into context so downstream loggers include it.
|
|
ctx = slog.With(ctx, slog.F("aibridgeproxy_id", proxyReqID))
|
|
logger = logger.With(slog.F("aibridgeproxy_id", proxyReqID))
|
|
}
|
|
r.Header.Del(agplaibridge.HeaderCoderRequestID)
|
|
|
|
byok := agplaibridge.IsBYOK(r.Header)
|
|
authMode := "centralized"
|
|
if byok {
|
|
authMode = "byok"
|
|
}
|
|
|
|
// When the request arrived via the in-process transport, the caller
|
|
// has placed a delegated API key ID on the context. We trust that the
|
|
// caller already established the user's identity and only validate
|
|
// liveness; the caller does not have (and cannot send) the key secret.
|
|
// Delegation is orthogonal to BYOK: a delegated request still carries
|
|
// the user's own LLM credentials in Authorization/X-Api-Key when BYOK
|
|
// is in effect.
|
|
var (
|
|
authReq *proto.IsAuthorizedRequest
|
|
)
|
|
|
|
delegatedID, delegated := agplaibridge.DelegatedAPIKeyIDFromContext(ctx)
|
|
|
|
key := strings.TrimSpace(agplaibridge.ExtractAuthToken(r.Header))
|
|
|
|
// When a BYOK header is present, a key is ALWAYS required.
|
|
// Delegated auth only requires a key when using BYOK.
|
|
if key == "" && !delegated {
|
|
// Some clients (e.g. Claude) send a HEAD request
|
|
// without credentials to check connectivity.
|
|
if r.Method == http.MethodHead {
|
|
logger.Info(ctx, "unauthenticated HEAD request")
|
|
} else {
|
|
logger.Warn(ctx, "no auth key provided")
|
|
}
|
|
http.Error(rw, ErrNoAuthKey.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if delegated {
|
|
authReq = &proto.IsAuthorizedRequest{KeyId: delegatedID}
|
|
if attr, ok := agplaibridge.DelegatedAttributionFromContext(ctx); ok && attr.DelegatedOrganizationID != uuid.Nil {
|
|
authReq.DelegatedOrganizationId = attr.DelegatedOrganizationID.String()
|
|
}
|
|
} else {
|
|
authReq = &proto.IsAuthorizedRequest{Key: key}
|
|
}
|
|
|
|
// Strip every header that may carry the Coder token so it is never
|
|
// forwarded to upstream providers. Runs for both header-auth and
|
|
// delegated requests: a delegated caller may forward the user's BYOK
|
|
// headers, and we still want to scrub any Coder-specific credentials
|
|
// that may have leaked through. After stripping, the aibridge library
|
|
// can treat the request as a normal LLM API call with no
|
|
// Coder-specific information.
|
|
if byok {
|
|
// In BYOK mode the Coder token is in X-Coder-AI-Governance-Token;
|
|
// Authorization and X-Api-Key carry the user's own LLM
|
|
// credentials and must be preserved.
|
|
r.Header.Del(agplaibridge.HeaderCoderToken)
|
|
} else {
|
|
// In centralized mode the Coder token may be in Authorization
|
|
// (the documented path) or X-Api-Key (legacy clients that set
|
|
// ANTHROPIC_API_KEY to their Coder token). Both are stripped.
|
|
r.Header.Del("Authorization")
|
|
r.Header.Del("X-Api-Key")
|
|
}
|
|
|
|
client, err := s.Client(ctx)
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to connect to coderd", slog.Error(err))
|
|
http.Error(rw, ErrConnect.Error(), http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
|
|
// Attach auth attributes used by all log lines below. "source" is the
|
|
// transport origin (e.g., "agents" for in-process callers, empty for
|
|
// network callers); "auth_delegated" distinguishes header-based from
|
|
// context-delegated authentication.
|
|
logger = logger.With(
|
|
slog.F("source", string(agplaibridge.SourceFromContext(ctx))),
|
|
slog.F("auth_mode", authMode),
|
|
slog.F("auth_delegated", delegated),
|
|
)
|
|
|
|
resp, err := client.IsAuthorized(ctx, authReq)
|
|
if err != nil {
|
|
// coderd marks its own failures, such as a database error, so they are
|
|
// not reported as a rejected request. Every other error, including any
|
|
// from a coderd that does not mark its failures, stays a 403.
|
|
if drpcerr.Code(err) == proto.ErrCodeInternal {
|
|
logger.Warn(ctx, "key authorization check failed internally", slog.Error(err))
|
|
http.Error(rw, ErrAuthorizationCheck.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
logger.Warn(ctx, "key authorization check failed", slog.Error(err))
|
|
http.Error(rw, ErrUnauthorized.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
id, err := uuid.Parse(resp.GetOwnerId())
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to parse user ID", slog.Error(err), slog.F("id", resp.GetOwnerId()))
|
|
http.Error(rw, ErrUnauthorized.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
logger = logger.With(slog.F("user_id", id))
|
|
|
|
// Direct and delegated carriers are mutually exclusive and request-scoped.
|
|
// Delegated requests carry attribution stamped by the in-process caller
|
|
// (chatd); direct requests carry workspace attribution parsed from the
|
|
// server-minted token name by IsAuthorized.
|
|
var attribution agplaibridge.Attribution
|
|
if delegated {
|
|
attribution, _ = agplaibridge.DelegatedAttributionFromContext(ctx)
|
|
} else {
|
|
attribution, err = attributionFromAuthorization(resp)
|
|
if err != nil {
|
|
logger.Warn(ctx, "invalid authorization attribution", slog.Error(err))
|
|
http.Error(rw, ErrUnauthorized.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
}
|
|
ctx = agplaibridge.WithAttribution(ctx, attribution)
|
|
|
|
// coderd resolves the organization once, in IsAuthorized. The budget check
|
|
// and the interception record both reuse it. A request without one is
|
|
// rejected so it never falls back to site-wide budget and cost.
|
|
org, err := requestOrganizationFromAuthorization(resp)
|
|
if err != nil {
|
|
logger.Warn(ctx, "invalid authorization organization", slog.Error(err))
|
|
http.Error(rw, ErrUnauthorized.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
ctx = agplaibridge.WithRequestOrganization(ctx, org)
|
|
logger = logger.With(slog.F("organization_id", org.ID), slog.F("organization_source", org.Source))
|
|
|
|
budgetResp, err := client.IsBudgetExceeded(ctx, &proto.IsBudgetExceededRequest{
|
|
UserId: id.String(),
|
|
OrganizationId: org.ID.String(),
|
|
})
|
|
if err != nil {
|
|
logger.Warn(ctx, "user AI budget check failed", slog.Error(err))
|
|
http.Error(rw, ErrBudgetCheck.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if budgetResp.GetExceeded() {
|
|
routing.WriteBudgetExceededError(rw, r, fmt.Sprintf(
|
|
"AI budget of US$%.2f exceeded. Please contact an administrator for more details.",
|
|
float64(budgetResp.GetSpendLimitMicros())/1_000_000,
|
|
))
|
|
return
|
|
}
|
|
|
|
// Attach only the verified identity. Key secrets remain outside the
|
|
// request context.
|
|
ctx = aibridge.AsActor(ctx, aibridge.Actor{
|
|
ID: id,
|
|
APIKeyID: resp.GetApiKeyId(),
|
|
Username: resp.GetUsername(),
|
|
Email: resp.GetEmail(),
|
|
})
|
|
r = r.WithContext(ctx)
|
|
|
|
handler, err := s.GetRequestHandler(ctx, Request{
|
|
SessionKey: key,
|
|
APIKeyID: resp.ApiKeyId,
|
|
InitiatorID: id,
|
|
})
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to acquire request handler", slog.Error(err))
|
|
http.Error(rw, ErrAcquireRequestHandler.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
handler.ServeHTTP(rw, r)
|
|
}
|
|
|
|
// attributionFromAuthorization extracts the workspace ID from the
|
|
// IsAuthorizedResponse. The proto carries only workspace_id for attribution;
|
|
// organization and workspace name are not returned.
|
|
func attributionFromAuthorization(resp *proto.IsAuthorizedResponse) (agplaibridge.Attribution, error) {
|
|
if resp.GetWorkspaceId() == "" {
|
|
return agplaibridge.Attribution{}, nil
|
|
}
|
|
workspaceID, err := uuid.Parse(resp.GetWorkspaceId())
|
|
if err != nil {
|
|
return agplaibridge.Attribution{}, xerrors.Errorf("parse workspace ID: %w", err)
|
|
}
|
|
return agplaibridge.Attribution{WorkspaceID: workspaceID}, nil
|
|
}
|
|
|
|
// requestOrganizationFromAuthorization extracts the organization coderd
|
|
// resolved for the request. A missing ID or source, or a malformed ID, is an
|
|
// error. coderd at v1.6 or later always sends one, and an older coderd rejects
|
|
// this gateway's version, so a missing organization is never expected. The
|
|
// source value itself is not checked, so a newer coderd can add sources
|
|
// without breaking this gateway.
|
|
func requestOrganizationFromAuthorization(resp *proto.IsAuthorizedResponse) (agplaibridge.RequestOrganization, error) {
|
|
rawID, source := resp.GetOrganizationId(), resp.GetOrganizationSource()
|
|
switch {
|
|
case rawID == "":
|
|
return agplaibridge.RequestOrganization{}, xerrors.New("no organization ID")
|
|
case source == "":
|
|
return agplaibridge.RequestOrganization{}, xerrors.New("organization ID set without organization source")
|
|
}
|
|
id, err := uuid.Parse(rawID)
|
|
if err != nil {
|
|
return agplaibridge.RequestOrganization{}, xerrors.Errorf("parse organization ID: %w", err)
|
|
}
|
|
if id == uuid.Nil {
|
|
return agplaibridge.RequestOrganization{}, xerrors.New("organization ID is the nil UUID")
|
|
}
|
|
return agplaibridge.RequestOrganization{ID: id, Source: source}, nil
|
|
}
|