-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbedrock.go
More file actions
543 lines (477 loc) · 17.8 KB
/
Copy pathbedrock.go
File metadata and controls
543 lines (477 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"regexp"
"strings"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream"
"github.com/google/uuid"
)
// validModelID validates Bedrock model IDs to prevent SSRF.
// Allows alphanumeric, dots, hyphens, underscores, and optional :version suffix.
var validModelID = regexp.MustCompile(`^[a-zA-Z0-9._-]+(:[0-9]+)?$`)
// bedrockMaxRequestBody is the max request body size for Bedrock requests (16 MB).
const bedrockMaxRequestBody = 16 << 20
// bedrockMaxBuffer is the max buffer size for observability decode (4 MB).
const bedrockMaxBuffer = 4 << 20
// bedrockMaxConcurrent is the max concurrent Bedrock requests.
const bedrockMaxConcurrent = 5
// bedrockMaxErrorBody is the max error response body to read (1 MB).
const bedrockMaxErrorBody = 1 << 20
// LimitedWriter stops writing after N bytes. NEVER returns an error —
// this is critical because io.TeeReader propagates Write errors to io.Copy,
// which would break the client stream.
type LimitedWriter struct {
W io.Writer
N int64
Overflow bool
}
func (lw *LimitedWriter) Write(p []byte) (int, error) {
if lw.Overflow {
return len(p), nil
}
if int64(len(p)) > lw.N {
lw.Overflow = true
return len(p), nil
}
n, _ := lw.W.Write(p)
lw.N -= int64(n)
return len(p), nil
}
// extractModelID extracts and validates the model ID from a Bedrock URL path.
// Path format: /model/{modelId}/invoke or /model/{modelId}/invoke-with-response-stream
func extractModelID(path string) (string, error) {
trimmed := strings.TrimPrefix(path, "/model/")
if trimmed == path {
return "", fmt.Errorf("path does not start with /model/")
}
parts := strings.SplitN(trimmed, "/", 2)
if len(parts) == 0 || parts[0] == "" {
return "", fmt.Errorf("empty model ID in path %q", path)
}
modelID := parts[0]
if !validModelID.MatchString(modelID) {
return "", fmt.Errorf("invalid model ID: %q", modelID)
}
return modelID, nil
}
// isBedrockStreaming returns true if the path ends with invoke-with-response-stream.
func isBedrockStreaming(path string) bool {
return strings.HasSuffix(path, "/invoke-with-response-stream")
}
// decodeBedrockEventstream decodes a complete Bedrock eventstream response buffer
// into normalized StreamChunks (with "data: " prefix for parser compatibility).
// Returns any successfully decoded chunks even if the buffer is truncated.
func decodeBedrockEventstream(buf []byte) ([]StreamChunk, error) {
if len(buf) == 0 {
return nil, nil
}
decoder := eventstream.NewDecoder()
reader := bytes.NewReader(buf)
var chunks []StreamChunk
var lastErr error
for reader.Len() > 0 {
msg, err := decoder.Decode(reader, nil)
if err != nil {
lastErr = fmt.Errorf("eventstream decode: %w", err)
break
}
// Parse the frame payload: {"bytes": "<base64>", "p": "<padding>"}
var payload struct {
Bytes string `json:"bytes"`
}
if err := json.Unmarshal(msg.Payload, &payload); err != nil {
// Skip frames with non-JSON payload (e.g., exception frames)
continue
}
if payload.Bytes == "" {
continue
}
// Base64-decode to get the Anthropic event JSON
decoded, err := base64.StdEncoding.DecodeString(payload.Bytes)
if err != nil {
// Try URL-safe encoding as fallback
decoded, err = base64.URLEncoding.DecodeString(payload.Bytes)
if err != nil {
lastErr = fmt.Errorf("base64 decode: %w", err)
continue
}
}
// Prepend "data: " for compatibility with ParseStreamingResponse
chunks = append(chunks, StreamChunk{
Raw: "data: " + string(decoded),
Timestamp: time.Now(),
})
}
return chunks, lastErr
}
// bedrockState holds per-proxy Bedrock resources initialized at startup.
type bedrockState struct {
region string
client *http.Client
semaphore chan struct{}
decodeErrors int64 // atomic counter
}
// initBedrock initializes Bedrock resources. Returns nil if Bedrock is not configured.
// The proxy holds no AWS credentials: per-request auth is a real Bedrock Bearer key
// resolved from the client's opaque nonce, so no AWS config is loaded here.
func initBedrock(region string) (*bedrockState, error) {
if region == "" {
return nil, nil
}
if err := ValidateBedrockRegion(region); err != nil {
return nil, err
}
return &bedrockState{
region: region,
client: &http.Client{
Transport: &http.Transport{
DisableCompression: true,
ResponseHeaderTimeout: 300 * time.Second,
ForceAttemptHTTP2: true,
},
Timeout: 0,
},
semaphore: make(chan struct{}, bedrockMaxConcurrent),
}, nil
}
// serveBedrock handles Bedrock pass-through requests. The proxy resolves the
// client's opaque nonce to a real Bedrock Bearer key, forwards to Bedrock with
// that key, streams the response to the client, and decodes the eventstream for
// observability after the stream completes.
func (p *Proxy) serveBedrock(w http.ResponseWriter, r *http.Request) {
p.serveBedrockForPath(w, r, r.URL.Path, RunAttribution{})
}
func (p *Proxy) serveBedrockForPath(w http.ResponseWriter, r *http.Request, innerPath string, attribution RunAttribution) {
if p.bedrock == nil {
http.Error(w, "Bedrock not configured", http.StatusServiceUnavailable)
return
}
if p.tokenSub == nil {
http.Error(w, "token substitution not configured", http.StatusServiceUnavailable)
return
}
startTime := time.Now()
// Extract and validate model ID
modelID, err := extractModelID(innerPath)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
streaming := isBedrockStreaming(innerPath)
// Acquire concurrency semaphore
select {
case p.bedrock.semaphore <- struct{}{}:
defer func() { <-p.bedrock.semaphore }()
case <-r.Context().Done():
http.Error(w, "request cancelled", http.StatusServiceUnavailable)
return
}
// Read request body (capped)
var reqBody []byte
if r.Body != nil {
reqBody, err = io.ReadAll(io.LimitReader(r.Body, bedrockMaxRequestBody))
if err != nil {
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
r.Body.Close()
}
// Use provider=anthropic — Bedrock payloads use the Anthropic JSON format
provider := "anthropic"
upstream := fmt.Sprintf("bedrock-runtime.%s.amazonaws.com", p.bedrock.region)
// Resolve the client's opaque nonce to a real short-lived Bedrock Bearer key.
// Fail closed: no upstream call on resolution failure.
nonce, _ := readClientToken(r.Header)
resolved, status, _ := p.tokenSub.Resolve(r.Context(), ResolveContext{
APIToken: nonce,
ClientHost: r.RemoteAddr,
Provider: provider,
ProviderURL: upstream,
})
if status != 0 {
http.Error(w, "api token substitution failed", status)
return
}
if runAttributionProjectMismatch(attribution.RunID, resolved.Project) {
http.Error(w, "run attribution project mismatch", http.StatusForbidden)
return
}
// Session tracking and logging setup
var sessionID string
var seq int
var isNewSession bool
var requestID string
var patternState *PatternState
// Bedrock paths are always conversation endpoints
shouldLog := p.logger != nil
if shouldLog {
requestID = uuid.New().String()
if logger, ok := p.logger.(requestLogContextLogger); ok {
ctx := RequestLogContext{
Transport: "bedrock",
ModelOverride: modelID,
ClientFPHash: resolved.ClientFPHash,
Project: resolved.Project,
ProviderRoute: "aws-bedrock",
WireAPI: "messages",
}
if attribution.RunID != "" {
ctx.RunID = attribution.RunID
ctx.ResolvedRunID = resolved.RunID
} else {
ctx.LegacyRunID = resolved.RunID
}
logger.SetRequestLogContext(requestID, ctx)
defer logger.ClearRequestLogContext(requestID)
}
if p.sessionManager != nil {
sessionID, seq, isNewSession, err = p.sessionManager.GetOrCreateSession(reqBody, provider, upstream, r.Header, innerPath)
if err != nil {
sessionID = p.generateSessionID()
seq = 1
isNewSession = true
}
if p.eventEmitter != nil {
patternState, _ = p.sessionManager.LoadPatternState(sessionID)
if patternState == nil {
patternState = &PatternState{PendingToolIDs: make(map[string]string)}
}
errorRecovered := patternState.LastWasError
hadError := p.processToolResultsAndEmitEvents(reqBody, sessionID, provider, patternState)
patternState.LastWasError = hadError
patternState.TurnCount++
p.eventEmitter.EmitTurnStart(sessionID, provider, p.machineID, patternState.TurnCount, errorRecovered)
}
} else {
sessionID = p.generateSessionID()
seq = 1
isNewSession = true
}
if isNewSession {
p.logger.LogSessionStart(sessionID, provider, upstream)
}
p.logger.LogRequest(sessionID, provider, seq, r.Method, innerPath, r.Header, reqBody, requestID)
}
// Build upstream URL — path stays the same since CC sends the Bedrock path format
upstreamURL := fmt.Sprintf("https://%s%s", upstream, innerPath)
// Create the upstream request
proxyReq, err := http.NewRequestWithContext(r.Context(), r.Method, upstreamURL, bytes.NewReader(reqBody))
if err != nil {
if shouldLog {
// Pair the already-logged request line: no response bytes existed,
// so this is bookkeeping (status 0), not money.
timing := ResponseTiming{TotalMs: time.Since(startTime).Milliseconds()}
p.logger.LogResponse(sessionID, provider, seq, 0, nil, nil, nil, timing, requestID, ResponseCapture{
Path: innerPath,
Termination: TerminationUpstreamUnreachable,
TerminationError: err.Error(),
})
}
http.Error(w, "failed to create request", http.StatusInternalServerError)
return
}
// Whitelist headers — only copy Content-Type and Accept; the client's opaque
// nonce never rides upstream alongside the resolved Bearer key.
if ct := r.Header.Get("Content-Type"); ct != "" {
proxyReq.Header.Set("Content-Type", ct)
}
if accept := r.Header.Get("Accept"); accept != "" {
proxyReq.Header.Set("Accept", accept)
}
proxyReq.Header.Set("Authorization", "Bearer "+resolved.Token)
// Send to Bedrock
resp, err := p.bedrock.client.Do(proxyReq)
if err != nil {
if shouldLog {
// Pair the already-logged request line: no response bytes existed,
// so this is bookkeeping (status 0), not money.
timing := ResponseTiming{TotalMs: time.Since(startTime).Milliseconds()}
p.logger.LogResponse(sessionID, provider, seq, 0, nil, nil, nil, timing, requestID, ResponseCapture{
Path: innerPath,
Termination: TerminationUpstreamUnreachable,
TerminationError: err.Error(),
})
}
http.Error(w, "upstream request failed: "+err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
// Non-200: forward error directly, skip eventstream decode
if resp.StatusCode != http.StatusOK {
errBody, _ := io.ReadAll(io.LimitReader(resp.Body, bedrockMaxErrorBody))
if shouldLog {
timing := ResponseTiming{
TTFBMs: time.Since(startTime).Milliseconds(),
TotalMs: time.Since(startTime).Milliseconds(),
}
p.logger.LogResponse(sessionID, provider, seq, resp.StatusCode, resp.Header, errBody, nil, timing, requestID, ResponseCapture{Path: innerPath, Termination: TerminationEOF})
}
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
w.Write(errBody)
return
}
if streaming {
p.serveBedrockStreaming(w, resp, startTime, modelID, upstream, provider, sessionID, seq, reqBody, requestID, innerPath, patternState, shouldLog)
} else {
p.serveBedrockNonStreaming(w, resp, startTime, modelID, upstream, provider, sessionID, seq, reqBody, requestID, innerPath, patternState, shouldLog)
}
}
func (p *Proxy) serveBedrockDiscoveryForPath(w http.ResponseWriter, r *http.Request, innerPath string, attribution RunAttribution) {
if p.bedrock == nil {
http.Error(w, "Bedrock not configured", http.StatusServiceUnavailable)
return
}
if p.tokenSub == nil {
http.Error(w, "token substitution not configured", http.StatusServiceUnavailable)
return
}
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if innerPath != "/inference-profiles" {
http.Error(w, "unknown run attribution route", http.StatusBadRequest)
return
}
provider := "anthropic"
upstream := fmt.Sprintf("bedrock-runtime.%s.amazonaws.com", p.bedrock.region)
nonce, _ := readClientToken(r.Header)
resolved, status, _ := p.tokenSub.Resolve(r.Context(), ResolveContext{
APIToken: nonce,
ClientHost: r.RemoteAddr,
Provider: provider,
ProviderURL: upstream,
})
if status != 0 {
http.Error(w, "api token substitution failed", status)
return
}
if runAttributionProjectMismatch(attribution.RunID, resolved.Project) {
http.Error(w, "run attribution project mismatch", http.StatusForbidden)
return
}
upstreamURL := fmt.Sprintf("https://%s%s", upstream, innerPath)
if rawQuery := r.URL.RawQuery; rawQuery != "" {
upstreamURL += "?" + rawQuery
}
proxyReq, err := http.NewRequestWithContext(r.Context(), http.MethodGet, upstreamURL, nil)
if err != nil {
http.Error(w, "failed to create request", http.StatusInternalServerError)
return
}
if ct := r.Header.Get("Content-Type"); ct != "" {
proxyReq.Header.Set("Content-Type", ct)
}
if accept := r.Header.Get("Accept"); accept != "" {
proxyReq.Header.Set("Accept", accept)
}
proxyReq.Header.Set("Authorization", "Bearer "+resolved.Token)
resp, err := p.bedrock.client.Do(proxyReq)
if err != nil {
http.Error(w, "upstream request failed: "+err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
log.Printf("bedrock discovery response copy failed: %v", err)
}
}
// serveBedrockStreaming handles streaming Bedrock responses using buffer-then-decode.
func (p *Proxy) serveBedrockStreaming(w http.ResponseWriter, resp *http.Response, startTime time.Time, modelID, upstream, provider, sessionID string, seq int, reqBody []byte, requestID, path string, patternState *PatternState, shouldLog bool) {
// Set up TeeReader with LimitedWriter for observability buffer
observeBuf := bytes.NewBuffer(make([]byte, 0, bedrockMaxBuffer))
limitedW := &LimitedWriter{W: observeBuf, N: int64(bedrockMaxBuffer)}
tee := io.TeeReader(resp.Body, limitedW)
// Forward headers and status
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
// Stream raw bytes to client — this is the critical path
_, copyErr := io.Copy(w, tee)
// Critical path done — CC has the full response.
// Now decode the buffered copy for observability.
ttfb := time.Since(startTime)
totalTime := time.Since(startTime)
if limitedW.Overflow {
log.Printf("WARNING: Bedrock response exceeded %d byte buffer, observability decode skipped (model=%s session=%s)", bedrockMaxBuffer, modelID, sessionID)
atomic.AddInt64(&p.bedrock.decodeErrors, 1)
}
if copyErr != nil {
log.Printf("WARNING: Bedrock stream copy error: %v (model=%s session=%s)", copyErr, modelID, sessionID)
}
// Decode eventstream frames from buffer
var chunks []StreamChunk
if !limitedW.Overflow && observeBuf.Len() > 0 {
var decodeErr error
chunks, decodeErr = decodeBedrockEventstream(observeBuf.Bytes())
if decodeErr != nil {
log.Printf("WARNING: Bedrock decode error: %v (model=%s session=%s, decoded %d chunks before error)", decodeErr, modelID, sessionID, len(chunks))
atomic.AddInt64(&p.bedrock.decodeErrors, 1)
}
}
termination := TerminationEOF
if copyErr != nil {
termination = TerminationUpstreamError
if resp.Request != nil && resp.Request.Context().Err() != nil {
// The outbound request rides the inbound request's context, so a
// canceled context means the client hung up on us.
termination = TerminationClientCancel
}
}
if shouldLog {
timing := ResponseTiming{
TTFBMs: ttfb.Milliseconds(),
TotalMs: totalTime.Milliseconds(),
}
capture := ResponseCapture{Path: path, Termination: termination}
if copyErr != nil {
capture.TerminationError = copyErr.Error()
}
p.logger.LogResponse(sessionID, provider, seq, resp.StatusCode, resp.Header, nil, chunks, timing, requestID, capture)
// Emit agent observability events; clean-EOF-only, mirroring streamResponse.
if termination == TerminationEOF && p.eventEmitter != nil && patternState != nil && p.sessionManager != nil && len(chunks) > 0 {
parsed := ParseStreamingResponse(chunks)
emitResponseEvents(p.eventEmitter, p.sessionManager, sessionID, provider, p.machineID, patternState, parsed.Content, parsed.Usage, parsed.StopReason, resp.StatusCode, "")
}
}
}
// serveBedrockNonStreaming handles non-streaming Bedrock responses (/invoke).
func (p *Proxy) serveBedrockNonStreaming(w http.ResponseWriter, resp *http.Response, startTime time.Time, modelID, upstream, provider, sessionID string, seq int, reqBody []byte, requestID, path string, patternState *PatternState, shouldLog bool) {
respBody, err := io.ReadAll(io.LimitReader(resp.Body, bedrockMaxRequestBody))
totalTime := time.Since(startTime)
if err != nil {
if shouldLog {
timing := ResponseTiming{
TTFBMs: totalTime.Milliseconds(),
TotalMs: totalTime.Milliseconds(),
}
p.logger.LogResponse(sessionID, provider, seq, resp.StatusCode, resp.Header, nil, nil, timing, requestID, ResponseCapture{Path: path, Termination: TerminationUpstreamError, TerminationError: err.Error()})
}
http.Error(w, "failed to read response body", http.StatusBadGateway)
return
}
if shouldLog {
timing := ResponseTiming{
TTFBMs: totalTime.Milliseconds(),
TotalMs: totalTime.Milliseconds(),
}
p.logger.LogResponse(sessionID, provider, seq, resp.StatusCode, resp.Header, respBody, nil, timing, requestID, ResponseCapture{Path: path, Termination: TerminationEOF})
if p.eventEmitter != nil && patternState != nil {
parsed := ParseResponseBody(string(respBody), upstream)
p.processResponseAndEmitEvents(parsed, sessionID, provider, patternState, resp.StatusCode, string(respBody))
}
}
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}