## Description Every Coder Agents chat belongs to an organization, but its AI Gateway requests don't carry it. AI Gateway instead uses the organization of the user's cost control effective group, which can be a different one. So a chat in org A can have its budget and cost attributed to org B. This PR makes Coder Agents send the chat's organization with its delegated attribution, so AI Gateway can use the chat's organization for the whole request. AI Gateway starts using it in the next PR in this stack, so behaviour doesn't change here. ## Changes - Add `DelegatedOrganizationID` to `aibridge.Attribution`, and fix the `WorkspaceID` comment, which applies to workspace session tokens too. - Set it from the chat in `newModel` when routing Coder Agents requests through AI Gateway. - Extend `TestNewModelAttributionImmutablePerRequest` to check the organization. Related to AIGOV-713 > [!NOTE] > Generated by Coder Agents on behalf of @ssncferreira.
141 lines
5.7 KiB
Go
141 lines
5.7 KiB
Go
package aibridge
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Attribution carries contextual per-request attribution data for a request.
|
|
type Attribution struct {
|
|
// WorkspaceID is the workspace the request comes from, or uuid.Nil when
|
|
// unknown.
|
|
WorkspaceID uuid.UUID
|
|
// DelegatedOrganizationID is the organization a delegated in-process caller
|
|
// acts in. Currently only chatd sets it, to the chat's organization. It is
|
|
// uuid.Nil for every other request.
|
|
DelegatedOrganizationID uuid.UUID
|
|
}
|
|
|
|
type attributionCtxKey struct{}
|
|
|
|
// WithAttribution returns a copy of ctx carrying the trusted Attribution.
|
|
// Attribution MUST ONLY set by authentication or delegated in-process code,
|
|
// and NEVER by client-provided HTTP headers.
|
|
func WithAttribution(ctx context.Context, attr Attribution) context.Context {
|
|
return context.WithValue(ctx, attributionCtxKey{}, attr)
|
|
}
|
|
|
|
// AttributionFromContext returns the Attribution attached by [WithAttribution]
|
|
// and whether a non-zero WorkspaceID was present.
|
|
func AttributionFromContext(ctx context.Context) (Attribution, bool) {
|
|
attr, ok := ctx.Value(attributionCtxKey{}).(Attribution)
|
|
return attr, ok && attr.WorkspaceID != uuid.Nil
|
|
}
|
|
|
|
// RequestOrganization is the organization coderd resolved for a request in
|
|
// IsAuthorized. It is kept apart from [Attribution] because coderd computes it,
|
|
// while delegated callers supply Attribution.
|
|
type RequestOrganization struct {
|
|
ID uuid.UUID
|
|
// Source records how ID was chosen. It holds a
|
|
// database.AIBridgeInterceptionOrganizationSource value.
|
|
Source string
|
|
}
|
|
|
|
type requestOrganizationCtxKey struct{}
|
|
|
|
// WithRequestOrganization returns a copy of ctx carrying the organization
|
|
// coderd resolved for the request. It MUST ONLY be set from the IsAuthorized
|
|
// response, and NEVER from client-provided HTTP headers.
|
|
func WithRequestOrganization(ctx context.Context, org RequestOrganization) context.Context {
|
|
return context.WithValue(ctx, requestOrganizationCtxKey{}, org)
|
|
}
|
|
|
|
// RequestOrganizationFromContext returns the organization attached by
|
|
// [WithRequestOrganization] and whether a non-zero ID was present.
|
|
func RequestOrganizationFromContext(ctx context.Context) (RequestOrganization, bool) {
|
|
org, ok := ctx.Value(requestOrganizationCtxKey{}).(RequestOrganization)
|
|
return org, ok && org.ID != uuid.Nil
|
|
}
|
|
|
|
// Source identifies the call site that asked aibridge for a transport. It is
|
|
// attached to the request context so downstream handlers and logs can attribute
|
|
// traffic without changing behavior based on the value.
|
|
type Source string
|
|
|
|
// SourceAgents is chatd traffic originating from a Coder agent.
|
|
const SourceAgents Source = "agents"
|
|
|
|
type sourceCtxKey struct{}
|
|
|
|
// WithSource returns a copy of ctx carrying the given Source. Use this on the
|
|
// request context before invoking a downstream handler so [SourceFromContext]
|
|
// can recover it for logging.
|
|
func WithSource(ctx context.Context, src Source) context.Context {
|
|
return context.WithValue(ctx, sourceCtxKey{}, src)
|
|
}
|
|
|
|
// SourceFromContext returns the Source attached by [WithSource], or the empty
|
|
// string when no Source is set.
|
|
func SourceFromContext(ctx context.Context) Source {
|
|
src, _ := ctx.Value(sourceCtxKey{}).(Source)
|
|
return src
|
|
}
|
|
|
|
type (
|
|
deletedAPIKeyIDCtxKey struct{}
|
|
delegatedAttributionCtxKey struct{}
|
|
)
|
|
|
|
// WithDelegatedAPIKeyID returns a copy of ctx carrying an API key ID on whose
|
|
// behalf the request is being made. The in-process aibridge transport requires
|
|
// this on every RoundTrip and rejects calls whose context lacks it.
|
|
//
|
|
// The caller is responsible for having established that the user owning this
|
|
// key authorized the request: aibridged validates only that the key exists,
|
|
// has not expired, and belongs to a non-deleted, non-system user. It does not
|
|
// verify the key secret, because the caller never has it.
|
|
func WithDelegatedAPIKeyID(ctx context.Context, id string) context.Context {
|
|
return context.WithValue(ctx, deletedAPIKeyIDCtxKey{}, id)
|
|
}
|
|
|
|
func WithDelegatedAttribution(ctx context.Context, attr Attribution) context.Context {
|
|
return context.WithValue(ctx, delegatedAttributionCtxKey{}, attr)
|
|
}
|
|
|
|
// DelegatedAPIKeyIDFromContext returns the API key ID attached by
|
|
// [WithDelegatedAPIKeyID] and whether a non-empty value was set.
|
|
func DelegatedAPIKeyIDFromContext(ctx context.Context) (string, bool) {
|
|
id, ok := ctx.Value(deletedAPIKeyIDCtxKey{}).(string)
|
|
return id, ok && id != ""
|
|
}
|
|
|
|
// DelegatedAttributionFromContext returns the trusted attribution attached to
|
|
// the delegated request. Its boolean reports whether delegated authentication,
|
|
// rather than attribution itself, was present.
|
|
func DelegatedAttributionFromContext(ctx context.Context) (Attribution, bool) {
|
|
attr, ok := ctx.Value(delegatedAttributionCtxKey{}).(Attribution)
|
|
return attr, ok
|
|
}
|
|
|
|
// TransportFactory returns an [http.RoundTripper] that dispatches an aibridge
|
|
// request in-process for a given provider instance name.
|
|
//
|
|
// Implementations live in coderd/aibridged. coderd registers an in-process
|
|
// factory on coderd.API.AIBridgeTransportFactory at startup so callers route
|
|
// traffic through the daemon without going through the gated HTTP route.
|
|
//
|
|
// The returned RoundTripper is responsible for adapting the caller's request
|
|
// to the aibridge daemon's mount path: callers hand it an upstream-shaped
|
|
// request and the transport rewrites URL.Path to "/api/v2/ai-gateway/<name>/..."
|
|
// before dispatching. Routing keys on the provider's instance name so callers
|
|
// can use the same string the proxy daemon and the bridge mount use.
|
|
//
|
|
// Source is informational: implementations must not gate on it. It is attached
|
|
// to the request context so handlers can include it in logs and metrics.
|
|
type TransportFactory interface {
|
|
TransportFor(providerName string, source Source) (http.RoundTripper, error)
|
|
}
|