1package provider
2
3// Amazon Bedrock Converse-Stream client.
4//
5// Endpoint: POST https://bedrock-runtime.{region}.amazonaws.com/model/{id}/converse-stream
6//
7// Auth: two paths supported.
8//
9// - **Bearer token** via AWS_BEARER_TOKEN_BEDROCK. Modern, simple,
10// `bedrock:CallWithBearerToken` IAM permission required.
11// - **SigV4 signing** via AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY
12// (+ optional AWS_SESSION_TOKEN for temporary credentials, e.g. SSO
13// or IRSA-issued). Hand-rolled here so we don't pull in aws-sdk-go-v2.
14//
15// If both are present, bearer wins (simpler / fewer moving parts).
16//
17// Wire format: AWS event-stream binary framing. Each message is:
18//
19// [4-byte total length BE]
20// [4-byte headers length BE]
21// [4-byte CRC32 of the 8-byte prelude]
22// [headers ...] each = [1B name-length][name][1B value-type][2B value-length][value]
23// [payload ...] payload-length = total - headers - 4 (trailer) - 12 (prelude+prelude-crc)
24// [4-byte CRC32 of message bytes minus trailer]
25//
26// We only consume the stream — never produce it — so we don't validate
27// CRCs (the TLS+TCP transport already protects in-flight bytes). We do
28// validate length-bounds to avoid OOB reads on a hostile / corrupted
29// stream.
30//
31// Event types we care about (from `:event-type` header):
32// - messageStart { role: "assistant" }
33// - contentBlockStart { contentBlockIndex, start: { toolUse: { toolUseId, name } } }
34// - contentBlockDelta { contentBlockIndex, delta: { text?, toolUse?: { input } } }
35// - contentBlockStop { contentBlockIndex }
36// - messageStop { stopReason }
37// - metadata { usage: { inputTokens, outputTokens } }
38
39import (
40 "bytes"
41 "context"
42 "crypto/hmac"
43 "crypto/sha256"
44 "encoding/binary"
45 "encoding/hex"
46 "encoding/json"
47 "fmt"
48 "io"
49 "net/http"
50 "net/url"
51 "os"
52 "sort"
53 "strings"
54 "time"
55)
56
57type bedrockClient struct {
58 // Auth mode is determined at construction time. Exactly one of
59 // bearerToken or sigv4 is populated.
60 bearerToken string
61 sigv4 *bedrockSigV4Creds
62
63 region string
64 baseURL string
65 http *http.Client
66}
67
68// bedrockSigV4Creds holds the IAM credentials used to sign each request.
69type bedrockSigV4Creds struct {
70 accessKeyID string
71 secretAccessKey string
72 sessionToken string // optional; STS / SSO / IRSA temp creds
73}
74
75// NewBedrockClient returns a Bedrock client.
76//
77// Auth resolution (first match wins):
78//
79// 1. apiKey == real bearer-ish string (not "<aws>") -> bearer route.
80// 2. AWS_BEARER_TOKEN_BEDROCK env var -> bearer route.
81// 3. AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY (+ optional
82// AWS_SESSION_TOKEN) -> SigV4 route.
83// 4. AWS_PROFILE -> read ~/.aws/credentials, take that profile's keys.
84//
85// region defaults to us-east-1 unless AWS_REGION / AWS_DEFAULT_REGION is
86// set or the baseURL embeds a region.
87func NewBedrockClient(apiKey, baseURL string) Client {
88 region := os.Getenv("AWS_REGION")
89 if region == "" {
90 region = os.Getenv("AWS_DEFAULT_REGION")
91 }
92 if region == "" {
93 region = "us-east-1"
94 }
95 if baseURL == "" {
96 baseURL = "https://bedrock-runtime." + region + ".amazonaws.com"
97 }
98 c := &bedrockClient{
99 region: region,
100 baseURL: strings.TrimRight(baseURL, "/"),
101 http: &http.Client{Timeout: 0},
102 }
103
104 // Bearer route.
105 token := apiKey
106 if token == "" || token == "<aws>" {
107 token = os.Getenv("AWS_BEARER_TOKEN_BEDROCK")
108 }
109 if token != "" && token != "<aws>" {
110 c.bearerToken = token
111 return c
112 }
113
114 // SigV4 route: env vars first.
115 ak := os.Getenv("AWS_ACCESS_KEY_ID")
116 sk := os.Getenv("AWS_SECRET_ACCESS_KEY")
117 st := os.Getenv("AWS_SESSION_TOKEN")
118 if ak != "" && sk != "" {
119 c.sigv4 = &bedrockSigV4Creds{accessKeyID: ak, secretAccessKey: sk, sessionToken: st}
120 return c
121 }
122
123 // SigV4 route: ~/.aws/credentials via AWS_PROFILE.
124 if profile := os.Getenv("AWS_PROFILE"); profile != "" {
125 if creds, err := readAWSCredentialsFile(profile); err == nil {
126 c.sigv4 = creds
127 return c
128 }
129 }
130
131 return &unimplementedClient{
132 name: "amazon-bedrock",
133 hint: "no Bedrock credentials found (set AWS_BEARER_TOKEN_BEDROCK, AWS_ACCESS_KEY_ID+AWS_SECRET_ACCESS_KEY, or AWS_PROFILE)",
134 }
135}
136
137// readAWSCredentialsFile parses ~/.aws/credentials and returns the
138// access-key/secret-key (and optional session-token) for the named
139// profile. The file is an INI-like format with `[profile]` headers.
140func readAWSCredentialsFile(profile string) (*bedrockSigV4Creds, error) {
141 home, err := os.UserHomeDir()
142 if err != nil {
143 return nil, err
144 }
145 b, err := os.ReadFile(home + "/.aws/credentials")
146 if err != nil {
147 return nil, err
148 }
149 var current string
150 creds := map[string]map[string]string{}
151 for _, line := range strings.Split(string(b), "\n") {
152 line = strings.TrimSpace(line)
153 if line == "" || strings.HasPrefix(line, "#") {
154 continue
155 }
156 if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
157 current = strings.TrimSpace(line[1 : len(line)-1])
158 creds[current] = map[string]string{}
159 continue
160 }
161 if current == "" {
162 continue
163 }
164 eq := strings.IndexByte(line, '=')
165 if eq < 0 {
166 continue
167 }
168 k := strings.TrimSpace(line[:eq])
169 v := strings.TrimSpace(line[eq+1:])
170 creds[current][k] = v
171 }
172 p, ok := creds[profile]
173 if !ok {
174 return nil, fmt.Errorf("aws profile %q not found in ~/.aws/credentials", profile)
175 }
176 ak := p["aws_access_key_id"]
177 sk := p["aws_secret_access_key"]
178 if ak == "" || sk == "" {
179 return nil, fmt.Errorf("aws profile %q missing aws_access_key_id or aws_secret_access_key", profile)
180 }
181 return &bedrockSigV4Creds{
182 accessKeyID: ak,
183 secretAccessKey: sk,
184 sessionToken: p["aws_session_token"],
185 }, nil
186}
187
188func (c *bedrockClient) Name() string { return "amazon-bedrock" }
189
190// ---- request building ----
191
192type bedrockMessage struct {
193 Role string `json:"role"`
194 Content []map[string]interface{} `json:"content"`
195}
196
197type bedrockToolSpec struct {
198 ToolSpec struct {
199 Name string `json:"name"`
200 Description string `json:"description,omitempty"`
201 InputSchema struct {
202 JSON json.RawMessage `json:"json"`
203 } `json:"inputSchema"`
204 } `json:"toolSpec"`
205}
206
207type bedrockRequest struct {
208 Messages []bedrockMessage `json:"messages"`
209 System []map[string]string `json:"system,omitempty"`
210 InferenceConfig struct {
211 MaxTokens int `json:"maxTokens,omitempty"`
212 Temperature *float32 `json:"temperature,omitempty"`
213 } `json:"inferenceConfig,omitempty"`
214 ToolConfig *struct {
215 Tools []bedrockToolSpec `json:"tools"`
216 } `json:"toolConfig,omitempty"`
217}
218
219func (c *bedrockClient) buildRequest(req Request) (*bedrockRequest, error) {
220 out := &bedrockRequest{}
221 if req.System != "" {
222 out.System = []map[string]string{{"text": req.System}}
223 }
224 out.InferenceConfig.Temperature = req.Temperature
225 out.InferenceConfig.MaxTokens = req.MaxTokens
226 if out.InferenceConfig.MaxTokens == 0 {
227 out.InferenceConfig.MaxTokens = 4096
228 }
229 for _, m := range req.Messages {
230 role := string(m.Role)
231 if role == "tool" {
232 role = "user"
233 }
234 bm := bedrockMessage{Role: role}
235 for _, c := range m.Content {
236 switch v := c.(type) {
237 case TextBlock:
238 bm.Content = append(bm.Content, map[string]interface{}{"text": v.Text})
239 case ToolCallBlock:
240 var input map[string]interface{}
241 _ = json.Unmarshal(v.Arguments, &input)
242 if input == nil {
243 input = map[string]interface{}{}
244 }
245 bm.Content = append(bm.Content, map[string]interface{}{
246 "toolUse": map[string]interface{}{
247 "toolUseId": v.ID, "name": v.Name, "input": input,
248 },
249 })
250 case ToolResultBlock:
251 var resultContent []map[string]interface{}
252 for _, inner := range v.Content {
253 if tb, ok := inner.(TextBlock); ok {
254 resultContent = append(resultContent, map[string]interface{}{"text": tb.Text})
255 }
256 }
257 status := "success"
258 if v.IsError {
259 status = "error"
260 }
261 bm.Content = append(bm.Content, map[string]interface{}{
262 "toolResult": map[string]interface{}{
263 "toolUseId": v.CallID,
264 "content": resultContent,
265 "status": status,
266 },
267 })
268 }
269 }
270 out.Messages = append(out.Messages, bm)
271 }
272 if len(req.Tools) > 0 {
273 tc := struct {
274 Tools []bedrockToolSpec `json:"tools"`
275 }{}
276 for _, t := range req.Tools {
277 var ts bedrockToolSpec
278 ts.ToolSpec.Name = t.Name
279 ts.ToolSpec.Description = t.Description
280 ts.ToolSpec.InputSchema.JSON = t.Schema
281 tc.Tools = append(tc.Tools, ts)
282 }
283 out.ToolConfig = &tc
284 }
285 return out, nil
286}
287
288// resolveBedrockInferenceProfileID maps a bare foundation-model ID to
289// its region-matched cross-region inference-profile ID.
290//
291// Several newer Bedrock models (Anthropic Claude 4.x, DeepSeek, etc.)
292// cannot be invoked with on-demand throughput by their plain
293// foundation-model ID; Bedrock returns HTTP 400 demanding "the ID or
294// ARN of an inference profile that contains this model". The profile
295// ID is the same model ID with a geographic prefix (us/eu/apac/...).
296//
297// We only rewrite IDs that (a) lack an existing geo prefix and (b)
298// belong to a model family that requires a profile. IDs that already
299// carry a prefix (e.g. "eu.anthropic...", "global.anthropic...") or
300// fully-qualified ARNs are returned unchanged, so explicit user
301// choices and custom application inference profiles still work.
302func resolveBedrockInferenceProfileID(modelID, region string) string {
303 if modelID == "" {
304 return modelID
305 }
306 // ARNs are already inference-profile references; leave untouched.
307 if strings.HasPrefix(modelID, "arn:") {
308 return modelID
309 }
310 // Already geo-prefixed (us. / eu. / apac. / ap. / us-gov. / global.)?
311 if bedrockHasGeoPrefix(modelID) {
312 return modelID
313 }
314 if !bedrockRequiresInferenceProfile(modelID) {
315 return modelID
316 }
317 prefix := bedrockGeoPrefixForRegion(region)
318 if prefix == "" {
319 return modelID
320 }
321 return prefix + "." + modelID
322}
323
324// bedrockGeoPrefixes are the cross-region inference-profile geo
325// prefixes Bedrock uses. A model ID that starts with one of these
326// (followed by a dot) is already a profile reference.
327var bedrockGeoPrefixes = []string{"us-gov", "us", "eu", "apac", "ap", "global", "au"}
328
329func bedrockHasGeoPrefix(modelID string) bool {
330 for _, p := range bedrockGeoPrefixes {
331 if strings.HasPrefix(modelID, p+".") {
332 return true
333 }
334 }
335 return false
336}
337
338// bedrockRequiresInferenceProfile reports whether a bare
339// foundation-model ID is one of the families AWS only exposes through
340// a cross-region inference profile for on-demand throughput.
341func bedrockRequiresInferenceProfile(modelID string) bool {
342 switch {
343 case strings.HasPrefix(modelID, "anthropic.claude-"):
344 return true
345 case strings.HasPrefix(modelID, "deepseek."):
346 return true
347 default:
348 return false
349 }
350}
351
352// bedrockGeoPrefixForRegion maps an AWS region to the geo prefix used
353// by its cross-region inference profiles. Returns "" when the region
354// has no known mapping, in which case the model ID is left unchanged.
355func bedrockGeoPrefixForRegion(region string) string {
356 switch {
357 case region == "":
358 return "us"
359 case strings.HasPrefix(region, "us-gov-"):
360 return "us-gov"
361 case strings.HasPrefix(region, "us-"):
362 return "us"
363 case strings.HasPrefix(region, "eu-"):
364 return "eu"
365 case strings.HasPrefix(region, "ap-"):
366 return "apac"
367 case strings.HasPrefix(region, "ca-"):
368 return "us"
369 case strings.HasPrefix(region, "sa-"):
370 return "us"
371 default:
372 return "us"
373 }
374}
375
376func (c *bedrockClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
377 wire, err := c.buildRequest(req)
378 if err != nil {
379 return nil, err
380 }
381 body, err := json.Marshal(wire)
382 if err != nil {
383 return nil, err
384 }
385 modelID := resolveBedrockInferenceProfileID(req.Model, c.region)
386 url := c.baseURL + "/model/" + modelID + "/converse-stream"
387 httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
388 if err != nil {
389 return nil, err
390 }
391 httpReq.Header.Set("content-type", "application/json")
392 httpReq.Header.Set("accept", "application/vnd.amazon.eventstream")
393 if c.bearerToken != "" {
394 httpReq.Header.Set("authorization", "Bearer "+c.bearerToken)
395 } else if c.sigv4 != nil {
396 if err := signSigV4(httpReq, body, "bedrock", c.region, c.sigv4, time.Now().UTC()); err != nil {
397 return nil, fmt.Errorf("bedrock: sigv4 sign: %w", err)
398 }
399 } else {
400 return nil, fmt.Errorf("bedrock: no auth configured")
401 }
402 resp, err := c.http.Do(httpReq)
403 if err != nil {
404 return nil, fmt.Errorf("bedrock: %w", err)
405 }
406 if resp.StatusCode != http.StatusOK {
407 b, _ := io.ReadAll(resp.Body)
408 resp.Body.Close()
409 msg := strings.TrimSpace(string(b))
410 // A 403 on the bearer route is almost always a region mismatch:
411 // short-term Bedrock API keys are scoped to the region of the
412 // console session that minted them, but zot defaults to
413 // us-east-1. Surface the resolved region and the fix so the user
414 // is not left guessing why a freshly-copied key is "invalid".
415 if resp.StatusCode == http.StatusForbidden && c.bearerToken != "" {
416 return nil, fmt.Errorf(
417 "bedrock: http 403 (region=%s): %s\nhint: Bedrock API keys are region-scoped. If your key was created in another region, set AWS_REGION (e.g. AWS_REGION=eu-central-1) or pass --base-url https://bedrock-runtime.<region>.amazonaws.com",
418 c.region, msg)
419 }
420 return nil, fmt.Errorf("bedrock: http %d: %s", resp.StatusCode, msg)
421 }
422 out := make(chan Event, 16)
423 go c.runStream(ctx, resp, req, out)
424 return out, nil
425}
426
427func (c *bedrockClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
428 defer close(out)
429 defer resp.Body.Close()
430
431 out <- EventStart{Provider: "amazon-bedrock", Model: req.Model}
432
433 // State accumulated across deltas.
434 contentBlocks := map[int]*bedrockBlockState{}
435 stop := StopEnd
436 finalMsg := Message{Role: RoleAssistant, Time: time.Now()}
437 var usage Usage
438
439 for {
440 if ctx.Err() != nil {
441 stop = StopAborted
442 break
443 }
444 evt, err := readEventStreamMessage(resp.Body)
445 if err != nil {
446 if err == io.EOF {
447 break
448 }
449 out <- EventDone{Stop: StopError, Err: err, Message: finalMsg}
450 return
451 }
452 eventType := evt.headerString(":event-type")
453 messageType := evt.headerString(":message-type")
454 if messageType == "exception" {
455 out <- EventDone{Stop: StopError, Err: fmt.Errorf("bedrock exception (%s): %s", evt.headerString(":exception-type"), string(evt.payload)), Message: finalMsg}
456 return
457 }
458 switch eventType {
459 case "messageStart":
460 // nothing to do; role is always "assistant"
461 case "contentBlockStart":
462 var d struct {
463 ContentBlockIndex int `json:"contentBlockIndex"`
464 Start struct {
465 ToolUse *struct {
466 ToolUseID string `json:"toolUseId"`
467 Name string `json:"name"`
468 } `json:"toolUse"`
469 } `json:"start"`
470 }
471 if err := json.Unmarshal(evt.payload, &d); err != nil {
472 continue
473 }
474 st := &bedrockBlockState{}
475 contentBlocks[d.ContentBlockIndex] = st
476 if d.Start.ToolUse != nil {
477 st.isToolUse = true
478 st.toolID = d.Start.ToolUse.ToolUseID
479 st.toolName = d.Start.ToolUse.Name
480 out <- EventToolStart{ID: st.toolID, Name: st.toolName}
481 }
482 case "contentBlockDelta":
483 var d struct {
484 ContentBlockIndex int `json:"contentBlockIndex"`
485 Delta struct {
486 Text string `json:"text"`
487 ToolUse *struct {
488 Input string `json:"input"`
489 } `json:"toolUse"`
490 } `json:"delta"`
491 }
492 if err := json.Unmarshal(evt.payload, &d); err != nil {
493 continue
494 }
495 st := contentBlocks[d.ContentBlockIndex]
496 if st == nil {
497 continue
498 }
499 if d.Delta.Text != "" {
500 st.text.WriteString(d.Delta.Text)
501 out <- EventTextDelta{Delta: d.Delta.Text}
502 }
503 if d.Delta.ToolUse != nil && d.Delta.ToolUse.Input != "" {
504 st.toolArgs.WriteString(d.Delta.ToolUse.Input)
505 out <- EventToolArgs{ID: st.toolID, Delta: d.Delta.ToolUse.Input}
506 }
507 case "contentBlockStop":
508 var d struct {
509 ContentBlockIndex int `json:"contentBlockIndex"`
510 }
511 if err := json.Unmarshal(evt.payload, &d); err != nil {
512 continue
513 }
514 st := contentBlocks[d.ContentBlockIndex]
515 if st == nil {
516 continue
517 }
518 if st.isToolUse {
519 args := st.toolArgs.String()
520 if args == "" {
521 args = "{}"
522 }
523 finalMsg.Content = append(finalMsg.Content, ToolCallBlock{
524 ID: st.toolID, Name: st.toolName, Arguments: json.RawMessage(args),
525 })
526 out <- EventToolEnd{ID: st.toolID}
527 } else if st.text.Len() > 0 {
528 finalMsg.Content = append(finalMsg.Content, TextBlock{Text: st.text.String()})
529 }
530 case "messageStop":
531 var d struct {
532 StopReason string `json:"stopReason"`
533 }
534 _ = json.Unmarshal(evt.payload, &d)
535 switch d.StopReason {
536 case "tool_use":
537 stop = StopToolUse
538 case "end_turn":
539 stop = StopEnd
540 case "max_tokens":
541 stop = StopLength
542 default:
543 stop = StopEnd
544 }
545 case "metadata":
546 var d struct {
547 Usage struct {
548 InputTokens int `json:"inputTokens"`
549 OutputTokens int `json:"outputTokens"`
550 CacheReadInputTokens int `json:"cacheReadInputTokens"`
551 CacheWriteInputTokens int `json:"cacheWriteInputTokens"`
552 } `json:"usage"`
553 }
554 if err := json.Unmarshal(evt.payload, &d); err == nil {
555 usage.InputTokens = d.Usage.InputTokens
556 usage.OutputTokens = d.Usage.OutputTokens
557 usage.CacheReadTokens = d.Usage.CacheReadInputTokens
558 usage.CacheWriteTokens = d.Usage.CacheWriteInputTokens
559 if m, err := FindModel("amazon-bedrock", req.Model); err == nil {
560 usage.CostUSD = ComputeCost(m, usage)
561 }
562 out <- EventUsage{Usage: usage}
563 }
564 }
565 }
566 out <- EventDone{Stop: stop, Message: finalMsg}
567}
568
569type bedrockBlockState struct {
570 isToolUse bool
571 toolID string
572 toolName string
573 toolArgs strings.Builder
574 text strings.Builder
575}
576
577// ---- event-stream binary framing parser ----
578
579type eventStreamMessage struct {
580 headers map[string]string
581 payload []byte
582}
583
584func (m *eventStreamMessage) headerString(name string) string { return m.headers[name] }
585
586// readEventStreamMessage reads one event-stream message from r. Returns
587// io.EOF when the stream is finished cleanly.
588func readEventStreamMessage(r io.Reader) (*eventStreamMessage, error) {
589 var prelude [8]byte
590 if _, err := io.ReadFull(r, prelude[:]); err != nil {
591 return nil, err
592 }
593 totalLen := binary.BigEndian.Uint32(prelude[0:4])
594 headersLen := binary.BigEndian.Uint32(prelude[4:8])
595 if totalLen < 16 || headersLen > totalLen-16 {
596 return nil, fmt.Errorf("bedrock: malformed event-stream prelude (total=%d headers=%d)", totalLen, headersLen)
597 }
598 // Skip prelude CRC (4 bytes); we don't validate.
599 var preludeCRC [4]byte
600 if _, err := io.ReadFull(r, preludeCRC[:]); err != nil {
601 return nil, err
602 }
603 headersBuf := make([]byte, headersLen)
604 if _, err := io.ReadFull(r, headersBuf); err != nil {
605 return nil, err
606 }
607 payloadLen := int(totalLen) - 16 - int(headersLen)
608 payload := make([]byte, payloadLen)
609 if _, err := io.ReadFull(r, payload); err != nil {
610 return nil, err
611 }
612 var trailer [4]byte
613 if _, err := io.ReadFull(r, trailer[:]); err != nil {
614 return nil, err
615 }
616 // Parse headers.
617 hdrs := map[string]string{}
618 i := 0
619 for i < len(headersBuf) {
620 if i+1 > len(headersBuf) {
621 break
622 }
623 nameLen := int(headersBuf[i])
624 i++
625 if i+nameLen > len(headersBuf) {
626 return nil, fmt.Errorf("bedrock: header name overflow")
627 }
628 name := string(headersBuf[i : i+nameLen])
629 i += nameLen
630 if i+1 > len(headersBuf) {
631 return nil, fmt.Errorf("bedrock: header value-type missing")
632 }
633 valueType := headersBuf[i]
634 i++
635 // Type 7 = string with 2-byte length. Type 6 = byte-array similar.
636 // Other types (bool/int/long/timestamp/uuid) we treat as opaque
637 // strings since bedrock's event-type headers are always type 7.
638 switch valueType {
639 case 7, 6:
640 if i+2 > len(headersBuf) {
641 return nil, fmt.Errorf("bedrock: header value length missing")
642 }
643 vLen := int(binary.BigEndian.Uint16(headersBuf[i : i+2]))
644 i += 2
645 if i+vLen > len(headersBuf) {
646 return nil, fmt.Errorf("bedrock: header value overflow")
647 }
648 hdrs[name] = string(headersBuf[i : i+vLen])
649 i += vLen
650 case 0: // bool true
651 hdrs[name] = "true"
652 case 1: // bool false
653 hdrs[name] = "false"
654 case 2: // byte
655 if i+1 > len(headersBuf) {
656 return nil, fmt.Errorf("bedrock: header byte missing")
657 }
658 i++
659 case 3: // int16
660 if i+2 > len(headersBuf) {
661 return nil, fmt.Errorf("bedrock: header int16 missing")
662 }
663 i += 2
664 case 4: // int32
665 if i+4 > len(headersBuf) {
666 return nil, fmt.Errorf("bedrock: header int32 missing")
667 }
668 i += 4
669 case 5: // int64
670 if i+8 > len(headersBuf) {
671 return nil, fmt.Errorf("bedrock: header int64 missing")
672 }
673 i += 8
674 case 8: // timestamp (int64)
675 if i+8 > len(headersBuf) {
676 return nil, fmt.Errorf("bedrock: header timestamp missing")
677 }
678 i += 8
679 case 9: // uuid (16 bytes)
680 if i+16 > len(headersBuf) {
681 return nil, fmt.Errorf("bedrock: header uuid missing")
682 }
683 i += 16
684 default:
685 return nil, fmt.Errorf("bedrock: unknown header value-type %d", valueType)
686 }
687 }
688 return &eventStreamMessage{headers: hdrs, payload: payload}, nil
689}
690
691// ---- SigV4 signing ----
692//
693// AWS Signature Version 4. Implemented from scratch so we don't pull in
694// aws-sdk-go-v2 (large transitive dep tree just for one signer). The
695// algorithm is documented at:
696// https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
697//
698// Steps:
699//
700// 1. Build the canonical request: method + path + canonical query +
701// canonical headers + signed headers + payload-hash.
702// 2. Build the string-to-sign: algorithm + amz-date + credential scope +
703// sha256(canonical request).
704// 3. Derive the signing key by chaining HMAC-SHA256:
705// kSecret -> kDate -> kRegion -> kService -> kSigning
706// 4. signature = HMAC-SHA256(kSigning, stringToSign)
707// 5. Authorization header: AWS4-HMAC-SHA256 Credential=..., SignedHeaders=..., Signature=...
708//
709// Payload hash is sha256 of the JSON body. For Bedrock's streaming
710// endpoint this works because the request body is fully buffered before
711// the call — we don't need the streaming-payload variant.
712
713func sigHMAC(key, msg []byte) []byte {
714 m := hmac.New(sha256.New, key)
715 m.Write(msg)
716 return m.Sum(nil)
717}
718
719func sigSHA256Hex(b []byte) string {
720 h := sha256.Sum256(b)
721 return hex.EncodeToString(h[:])
722}
723
724func signSigV4(req *http.Request, payload []byte, service, region string, creds *bedrockSigV4Creds, now time.Time) error {
725 amzDate := now.Format("20060102T150405Z")
726 shortDate := now.Format("20060102")
727 credentialScope := shortDate + "/" + region + "/" + service + "/aws4_request"
728
729 // Required headers.
730 req.Header.Set("host", req.URL.Host)
731 req.Header.Set("x-amz-date", amzDate)
732 if creds.sessionToken != "" {
733 req.Header.Set("x-amz-security-token", creds.sessionToken)
734 }
735 payloadHash := sigSHA256Hex(payload)
736 req.Header.Set("x-amz-content-sha256", payloadHash)
737
738 // Canonical headers: lowercase name, trimmed value, sorted by name,
739 // each on its own line ending with \n.
740 var names []string
741 values := map[string]string{}
742 for k, vs := range req.Header {
743 lower := strings.ToLower(k)
744 // Skip authorization (we're computing it) and any
745 // connection-specific headers.
746 if lower == "authorization" {
747 continue
748 }
749 names = append(names, lower)
750 values[lower] = strings.Join(vs, ",")
751 }
752 // host is special — it's not in req.Header by default; set above.
753 sort.Strings(names)
754 var canonHdrs strings.Builder
755 for _, n := range names {
756 canonHdrs.WriteString(n)
757 canonHdrs.WriteByte(':')
758 canonHdrs.WriteString(strings.TrimSpace(values[n]))
759 canonHdrs.WriteByte('\n')
760 }
761 signedHeaders := strings.Join(names, ";")
762
763 // Canonical query string: keys sorted by name, each key and value
764 // URI-escaped (RFC 3986).
765 q := req.URL.Query()
766 var qKeys []string
767 for k := range q {
768 qKeys = append(qKeys, k)
769 }
770 sort.Strings(qKeys)
771 var canonQuery strings.Builder
772 for i, k := range qKeys {
773 if i > 0 {
774 canonQuery.WriteByte('&')
775 }
776 canonQuery.WriteString(url.QueryEscape(k))
777 canonQuery.WriteByte('=')
778 canonQuery.WriteString(url.QueryEscape(q.Get(k)))
779 }
780
781 // Canonical request.
782 canonReq := req.Method + "\n" +
783 canonicalSigV4Path(req.URL.Path) + "\n" +
784 canonQuery.String() + "\n" +
785 canonHdrs.String() + "\n" +
786 signedHeaders + "\n" +
787 payloadHash
788
789 // String to sign.
790 stringToSign := "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n" + sigSHA256Hex([]byte(canonReq))
791
792 // Signing key.
793 kDate := sigHMAC([]byte("AWS4"+creds.secretAccessKey), []byte(shortDate))
794 kRegion := sigHMAC(kDate, []byte(region))
795 kService := sigHMAC(kRegion, []byte(service))
796 kSigning := sigHMAC(kService, []byte("aws4_request"))
797
798 // Signature.
799 signature := hex.EncodeToString(sigHMAC(kSigning, []byte(stringToSign)))
800
801 // Authorization header.
802 auth := "AWS4-HMAC-SHA256 " +
803 "Credential=" + creds.accessKeyID + "/" + credentialScope + ", " +
804 "SignedHeaders=" + signedHeaders + ", " +
805 "Signature=" + signature
806 req.Header.Set("Authorization", auth)
807 return nil
808}
809
810// canonicalSigV4Path returns the path with each segment URI-encoded
811// according to SigV4 rules (everything except unreserved chars
812// A-Za-z0-9-_.~ and / between segments). Bedrock model IDs contain
813// dots, colons, and slashes; the colons must be percent-encoded.
814func canonicalSigV4Path(p string) string {
815 if p == "" {
816 return "/"
817 }
818 segments := strings.Split(p, "/")
819 for i, s := range segments {
820 segments[i] = sigV4EncodeSegment(s)
821 }
822 return strings.Join(segments, "/")
823}
824
825func sigV4EncodeSegment(s string) string {
826 var b strings.Builder
827 for i := 0; i < len(s); i++ {
828 c := s[i]
829 // Unreserved per RFC 3986.
830 if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
831 c == '-' || c == '_' || c == '.' || c == '~' {
832 b.WriteByte(c)
833 continue
834 }
835 fmt.Fprintf(&b, "%%%02X", c)
836 }
837 return b.String()
838}