JuliusBrussee/caveman · error

bedrock: invalid API key

Error message

bedrock: invalid API key

What it means

SanitizeAndMapHeaders validates the credential when authKind is "bedrock_api_key": the key must be non-empty and must not contain CR or LF characters, since it is sent verbatim as an x-api-key or Authorization: Bearer header. An empty/whitespace-only key, or one containing embedded newlines, is rejected to prevent malformed or header-injection credentials reaching Bedrock.

Solutions

  1. Trim the credential key and remove any embedded newlines before storing it (e.g. strings.TrimSpace and strip \r\n)
  2. Verify the Bedrock API key was copied completely and is a single-line value
  3. If you actually intend IAM SigV4 auth, set AuthKind to "aws_access_keys" with an accessKeyId:secretAccessKey[:sessionToken] value instead of bedrock_api_key

Example fix

// before
credential := providers.Credential{AuthKind: "bedrock_api_key", Key: keyFromEnv + "\n"}
// after
credential := providers.Credential{AuthKind: "bedrock_api_key", Key: strings.TrimSpace(strings.ReplaceAll(keyFromEnv, "\n", ""))}
Defensive patterns

Strategy: validation

Validate before calling

func validBedrockAPIKey(key string) bool {
	key = strings.TrimSpace(key)
	return key != "" && !strings.ContainsAny(key, "\r\n")
}
if !validBedrockAPIKey(cred.Key) { return errors.New("bedrock API key must be a single non-empty line") }

Prevention

When it happens

Trigger: Credential.AuthKind is explicitly "bedrock_api_key" and either credential.Key is empty or trims to empty, or credential.Key contains a \r or \n character.

Common situations: Storing the API key in a config file or env var with a trailing newline that was not trimmed; pasting a multi-line value or a key plus notes into a secret store; leaving the Key field empty while setting AuthKind to bedrock_api_key; YAML/env interpolation pulling in blank values.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/241107bade81dda9. Report an issue: GitHub.

Appendix: source

Thrown at proxy/providers/bedrock/signing.go:66

	// Before the signature below covers these headers: a caller must not be able
	// to nominate a signed header (x-amz-date, x-amz-security-token) — or the
	// Authorization header itself — for removal after this hop builds it.
	providers.RemoveConnectionHeaders(out, req.Header)
	if out.Get("content-type") == "" {
		out.Set("content-type", "application/json")
	}

	authKind, err := credentialAuthKind(credential)
	if err != nil {
		return nil, err
	}
	if credential.Scheme == "sigv4" && authKind != "aws_access_keys" {
		return nil, ErrSigV4Configuration
	}
	if authKind == "bedrock_api_key" {
		key := strings.TrimSpace(credential.Key)
		if key == "" || strings.ContainsAny(key, "\r\n") {
			return nil, fmt.Errorf("bedrock: invalid API key")
		}
		if mantle {
			out.Set("x-api-key", key)
		} else {
			out.Set("Authorization", "Bearer "+key)
		}
		return out, nil
	}

	creds, err := parseAWSCredentials(credential.Key)
	if err != nil {
		if credential.Scheme == "sigv4" {
			return nil, ErrSigV4Configuration
		}
		return nil, err
	}

	// Sign against the SAME URL the proxy will forward to. The caller passes the

View on GitHub (pinned to 3ee70a1026)