The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
BOT-1918: use the AWS default credentials chain for keyless Bedrock (#80512)
Andrei Simionescuf1ea6b4ad5fd
diff --git a/frontend/src/metabase-types/api/llm.ts b/frontend/src/metabase-types/api/llm.tsindex 95e1f2f7e99..a4b8174f1e0 100644--- a/frontend/src/metabase-types/api/llm.ts+++ b/frontend/src/metabase-types/api/llm.ts@@ -78,6 +78,7 @@ export interface LlmProviderType {default_model: string | null;models: LlmModel[];required_any: string[][];+ requires: Record<string, string[]>;fields: LlmProviderField[];}diff --git a/frontend/src/metabase-types/api/mocks/llm.ts b/frontend/src/metabase-types/api/mocks/llm.tsindex 47dd5bd316b..a0ae6657983 100644--- a/frontend/src/metabase-types/api/mocks/llm.ts+++ b/frontend/src/metabase-types/api/mocks/llm.ts@@ -28,6 +28,7 @@ export const createMockLlmProviderType = (default_model: null,models: [],required_any: [],+ requires: {},fields: [createMockLlmProviderField()],...opts,});diff --git a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsxindex 48bacc85240..a139591c64f 100644--- a/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AIProviderSettingsSection.unit.spec.tsx@@ -98,18 +98,26 @@ const AZURE_TYPE = createMockLlmProviderType({const BEDROCK_TYPE = createMockLlmProviderType({type: "bedrock",label: "Amazon Bedrock",+ requires: {+ "access-key-id": ["secret-access-key"],+ "secret-access-key": ["access-key-id"],+ "session-token": ["access-key-id", "secret-access-key"],+ },fields: [createMockLlmProviderField({key: "access-key-id",label: "Access key ID",type: "password",- required: true,+ required: false,+ help: "Leave the keys blank to authenticate with the AWS default credentials chain (IRSA, EKS Pod Identity, or instance profile).",+ docs_url:+ "https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html",}),createMockLlmProviderField({key: "secret-access-key",label: "Secret access key",type: "password",- required: true,+ required: false,}),createMockLlmProviderField({key: "region",@@ -533,6 +541,75 @@ describe("AIProviderSettingsSection", () => {expect(within(modal).getByLabelText("Session token")).not.toBeVisible();});+ it("connects with the whole Bedrock key pair or with none of it", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Amazon Bedrock" }),+ );++ expect(within(modal).getByText(/Leave the keys blank/)).toBeInTheDocument();+ expect(+ within(modal).getByRole("link", { name: "Where do I find this?" }),+ ).toBeInTheDocument();+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeEnabled();++ await userEvent.type(+ within(modal).getByLabelText(/Access key ID/),+ "AKIA123",+ );+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeDisabled();++ await userEvent.type(+ within(modal).getByLabelText(/Secret access key/),+ "secret123",+ );+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeEnabled();++ await userEvent.clear(within(modal).getByLabelText(/Access key ID/));+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeDisabled();+ });++ it("connects with a Bedrock session token only alongside the key pair", async () => {+ await setup();++ const modal = await openAddProviderModal();+ await userEvent.click(+ within(modal).getByRole("button", { name: "Amazon Bedrock" }),+ );+ await userEvent.click(+ within(modal).getByRole("button", { name: /Advanced settings/ }),+ );+ await userEvent.type(+ within(modal).getByLabelText(/Session token/),+ "FwoG-token",+ );+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeDisabled();++ await userEvent.type(+ within(modal).getByLabelText(/Access key ID/),+ "AKIA123",+ );+ await userEvent.type(+ within(modal).getByLabelText(/Secret access key/),+ "secret123",+ );+ expect(+ within(modal).getByRole("button", { name: "Connect" }),+ ).toBeEnabled();+ });+it("opens advanced settings for a connection that already customized one", async () => {await setup({connections: [diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsxindex 622e04559ac..685017f9cbf 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConfigFields.tsx@@ -71,12 +71,19 @@ function ProviderConfigField({}) {const focusProps = autoFocus ? { autoFocus, "data-autofocus": true } : {};const externalDocsUrl = field.docs_url;- const description = externalDocsUrl ? (- <ExternalLink href={externalDocsUrl}>- {t`Where do I find this?`}- </ExternalLink>- ) : (- field.help && <Text size="sm">{field.help}</Text>+ const description = (field.help || externalDocsUrl) && (+ <>+ {field.help && (+ <Text component="span" display="block" size="sm">+ {field.help}+ </Text>+ )}+ {externalDocsUrl && (+ <ExternalLink href={externalDocsUrl}>+ {t`Where do I find this?`}+ </ExternalLink>+ )}+ </>);if (field.type === "segmented") {diff --git a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsx b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsxindex d1809c90e0d..f7fc30fb0d7 100644--- a/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsx+++ b/frontend/src/metabase/metabot/components/AIProviderConfigurationForm/ProviderConnectionForm.tsx@@ -150,7 +150,9 @@ export function ProviderConnectionForm({// A required field the registry gives a default is already satisfied — the form shows that value pre-selected,// and the backend fills it in for a connection that never touched it. A type with alternative credential// groups (Google: a service account key, or an OAuth token with a project ID) additionally needs one group- // filled in full; its fields are individually optional because either group will do.+ // filled in full; its fields are individually optional because either group will do. A type with paired+ // credential groups (Bedrock: the AWS key pair) needs each filled in full or left empty in full, and a+ // dependent field (Bedrock: the session token) only counts filled when the fields it requires are too.const hasValue = (key: string) => (config[key] ?? "").trim() !== "";const isComplete =providerType != null &&@@ -163,7 +165,10 @@ export function ProviderConnectionForm({).every((field) => hasValue(field.key)) &&(providerType.required_any.length === 0 ||- providerType.required_any.some((group) => group.every(hasValue)));+ providerType.required_any.some((group) => group.every(hasValue))) &&+ Object.entries(providerType.requires).every(+ ([key, deps]) => !hasValue(key) || deps.every(hasValue),+ );const handleSave = async () => {if (!providerType) {diff --git a/mage/resources/token_scanner/token_whitelist.txt b/mage/resources/token_scanner/token_whitelist.txtindex 4a08bc40976..f9e5f1d75ae 100644--- a/mage/resources/token_scanner/token_whitelist.txt+++ b/mage/resources/token_scanner/token_whitelist.txt@@ -28,3 +28,4 @@ AKIAIOSFODNN7EXAMPLEAKIAOVERRIDEOVERRID1AKIANEWNEWNEWNEWNEW1AKIAOLDOLDOLDOLDOLD1+AKIACHAINCHAINCHAIN1diff --git a/src/metabase/llm/api/provider.clj b/src/metabase/llm/api/provider.cljindex 19716b9a5a0..7bb46dc7cb1 100644--- a/src/metabase/llm/api/provider.clj+++ b/src/metabase/llm/api/provider.clj@@ -52,6 +52,9 @@[:models [:sequential [:map [:id :string] [:display_name :string]]]];; alternative credential groups: the connection is complete when one group is filled in full[:required_any [:sequential [:sequential :string]]]+ ;; paired credential groups: each must be filled in full or left empty in full+ ;; per-field dependencies: a field may be filled only when the fields it names are too+ [:requires [:map-of :string [:sequential :string]]][:fields [:sequential field-response-schema]]])(def ^:private connection-response-schema@@ -107,7 +110,7 @@show-when (assoc :show_when {:field (name (:field show-when)) :value (:value show-when)})))(defn- provider-type-response- [{:keys [type label managed? singleton? default-model required-any fields]}]+ [{:keys [type label managed? singleton? default-model required-any requires fields]}]{:type type:label (str label):managed (boolean managed?)@@ -116,6 +119,7 @@:default_model default-model:models (mapv #(select-keys % [:id :display_name]) (llm.provider/fixed-models type)):required_any (mapv #(mapv name %) required-any)+ :requires (into {} (map (fn [[k deps]] [(name k) (mapv name deps)])) requires):fields (mapv field-response fields)})(defn- connection-responsediff --git a/src/metabase/llm/provider.clj b/src/metabase/llm/provider.cljindex 76201de5967..5b679d69a25 100644--- a/src/metabase/llm/provider.clj+++ b/src/metabase/llm/provider.clj@@ -19,6 +19,7 @@(:require[clojure.string :as str][metabase.llm.settings :as llm.settings]+ [metabase.premium-features.core :as premium-features][metabase.settings.core :as setting][metabase.util :as u][metabase.util.i18n :refer [deferred-tru tru]]@@ -271,16 +272,25 @@:label (deferred-tru "Amazon Bedrock"):default-model "anthropic.claude-opus-4-8":mini-model "anthropic.claude-haiku-4-5"- :fields [{:key :access-key-id- :label (deferred-tru "Access key ID")- :type :password- :required? true- :placeholder "AKIA..."- :docs-url "https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html"}- {:key :secret-access-key- :label (deferred-tru "Secret access key")- :type :password- :required? true}+ ;; Both keys together select explicit credentials, neither selects the AWS default credentials chain, and one+ ;; without the other authenticates nothing. A session token only extends the pair.+ :requires {:access-key-id [:secret-access-key]+ :secret-access-key [:access-key-id]+ :session-token [:access-key-id :secret-access-key]}+ :fields [{:key :access-key-id+ :label (deferred-tru "Access key ID")+ :type :password+ :placeholder "AKIA..."+ :help (deferred-tru "Leave the keys blank to authenticate with the AWS default credentials chain (IRSA, EKS Pod Identity, or instance profile).")+ ;; The default chain resolves the instance's own AWS identity, which on Metabase Cloud belongs+ ;; to the operator rather than the tenant, so hosted deployments must bring explicit keys.+ :hosted-required? true+ :hosted-help (deferred-tru "On Metabase Cloud, Bedrock always authenticates with your own AWS keys.")+ :docs-url "https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html"}+ {:key :secret-access-key+ :label (deferred-tru "Secret access key")+ :type :password+ :hosted-required? true}{:key :region:label (deferred-tru "Region"):type :select@@ -323,15 +333,35 @@(def ^:private provider-type-by-name(into {} (map (juxt :type identity)) provider-type-registry))+(defn- hosted-provider-type+ "`entry` adjusted for a hosted deployment: a field's `:hosted-required?` makes it required and its `:hosted-help`+ replaces its `:help`. Everything downstream (validation, completeness, the connection form) reads the entry+ through [[provider-type]], so this is the one place hosted policy is applied."+ [entry]+ (update entry :fields+ (partial mapv (fn [{:keys [hosted-help hosted-required?] :as field}]+ (cond-> field+ hosted-help (assoc :help hosted-help)+ hosted-required? (assoc :required? true))))))++(defn hosted?+ "Whether hosted credential policy applies. [[premium-features/is-hosted?]] reads the `:hosting` license feature,+ which reads as absent while the token service is unreachable, so an indeterminate token status counts as hosted+ too: on Cloud a keyless Bedrock connection would otherwise sign as the operator."+ []+ (or (premium-features/is-hosted?)+ (nil? (premium-features/canonically-has-feature? :hosting))))+(defn provider-type"The registry entry for `type-name`, or nil when it is not a known provider type."[type-name]- (get provider-type-by-name type-name))+ (when-let [entry (get provider-type-by-name type-name)]+ (cond-> entry (hosted?) hosted-provider-type)))(defn provider-types"Every registered provider type."[]- provider-type-registry)+ (mapv (comp provider-type :type) provider-type-registry))(defn managed-type?"Whether `type-name` is the Metabase-managed provider, which authenticates with the instance token through the LLM@@ -428,8 +458,8 @@[type-name config](let [{:keys [required-any fields]} (provider-type type-name)label-for (into {} (map (juxt :key :label)) fields)- carried? (fn [group] (every? #(u/trimmed-string (get config %)) group))]- (when (and (seq required-any) (not-any? carried? required-any))+ group-carried? (fn [group] (every? #(u/trimmed-string (get config %)) group))]+ (when (and (seq required-any) (not-any? group-carried? required-any))(throw (ex-info (tru "{0} needs one of: {1}."type-name(str/join (str " " (tru "or") " ")@@ -437,17 +467,34 @@required-any))){:status-code 400 :required-any required-any})))))+(defn- validate-requires!+ "Throw a 400 when `config` carries a field without the fields its `:requires` entry names: for Bedrock, half an+ access key pair, or a session token without the pair."+ [type-name config]+ (let [{:keys [requires fields]} (provider-type type-name)+ label-for (into {} (map (juxt :key :label)) fields)+ carried? #(u/trimmed-string (get config %))]+ (doseq [[field-key deps] requires]+ (when (and (carried? field-key) (not-every? carried? deps))+ (throw (ex-info (tru "{0} takes {1} only together with {2}."+ type-name+ (str (label-for field-key))+ (str/join " + " (map (comp str label-for) deps)))+ {:status-code 400 :requires {field-key deps}}))))))+(defn validate-config!"Check a connection's `:config` against its provider type's field descriptors: required fields are present, fieldsthat declare a `:prefix` start with it, `:options` values are among the options, per-field `:validate` hooks pass,- and one of the type's `:required-any` credential groups is carried. Throws a 400 on the first problem."+ one of the type's `:required-any` credential groups is carried, and no field is carried without the fields its+ `:requires` entry names. Throws a 400 on the first problem."[type-name config](when-not (provider-type type-name)(throw (ex-info (tru "Unknown provider type {0}." (pr-str type-name)){:status-code 400 :type type-name})))(doseq [field (:fields (provider-type type-name))](validate-field! type-name field config))- (validate-required-any! type-name config))+ (validate-required-any! type-name config)+ (validate-requires! type-name config))(defn credentials-complete?"Whether `config` carries the credentials a request needs.@@ -455,24 +502,31 @@A required field the registry gives a `:default` counts as carried: [[with-field-defaults]] supplies it when theconnection is resolved, so leaving it untouched is the admin accepting the value its form showed. A type with`:required-any` groups additionally needs one of them carried in full — Google's fields are individually optional- because either credential will do, which without the groups would make an empty config count as complete.+ because either credential will do, which without the groups would make an empty config count as complete. A field+ with a `:requires` entry needs the fields it names: Bedrock's key pair is optional because a keyless connection+ signs with the AWS default credentials chain, but half a pair authenticates nothing.[[model-fields]] are exempt: they name what to call, not what authenticates the call, and a connection canlegitimately take its model from the `connection-key/model` reference instead — which is where an Azuredeployment configured before the connection list existed still lives. [[validate-config!]] still requires themof anything saved through the API, so only the environment and a hand-written `llm-providers` can omit them."[type-name config]- (let [{:keys [fields required-any]} (provider-type type-name)- model-keys (set (model-fields type-name))]+ (let [{:keys [fields required-any requires]} (provider-type type-name)+ model-keys (set (model-fields type-name))+ carried? #(u/trimmed-string (get config %))](and (every? (fn [{:keys [key required? default]}](or (not required?)default(contains? model-keys key)- (u/trimmed-string (get config key))))+ (carried? key)))fields)(or (empty? required-any)- (boolean (some (fn [group] (every? #(u/trimmed-string (get config %)) group))- required-any))))))+ (boolean (some (fn [group] (every? carried? group))+ required-any)))+ (every? (fn [[field-key deps]]+ (or (not (carried? field-key))+ (every? carried? deps)))+ requires))))(defn config-complete?"Whether a connection of `type-name` can make requests: [[credentials-complete?]], or for the Metabase-managed@@ -532,10 +586,12 @@:model-family {:setting :llm-azure-model-family}:deployment-name {:setting :llm-azure-deployment-name}}}"bedrock" {:type "bedrock"+ ;; the region counts as a credential here: with no key pair the AWS default credentials chain+ ;; signs the requests, so the region alone brings a usable connection into existence:settings {:access-key-id {:setting :llm-bedrock-access-key-id :credential? true}:secret-access-key {:setting :llm-bedrock-secret-access-key :credential? true}:session-token {:setting :llm-bedrock-session-token}- :region {:setting :llm-bedrock-region}}}+ :region {:setting :llm-bedrock-region :credential? true}}}"vllm" {:type "vllm";; the base URL is the credential here, unlike Azure's: a server started without --api-key takes;; no key, so the URL alone brings a usable connection into existencediff --git a/src/metabase/llm/settings.clj b/src/metabase/llm/settings.cljindex 4e116975838..cbc0d9256cb 100644--- a/src/metabase/llm/settings.clj+++ b/src/metabase/llm/settings.clj@@ -333,7 +333,7 @@;;; ----------------------------------------------- Amazon Bedrock ----------------------------------------------(defsetting llm-bedrock-access-key-id- (deferred-tru "The AWS Access Key ID for Amazon Bedrock.")+ (deferred-tru "The AWS Access Key ID for Amazon Bedrock. On a self-hosted Metabase, leave unset together with the secret access key to authenticate with the AWS default credentials chain (IRSA, EKS Pod Identity, or instance profile); on Metabase Cloud both keys are required."):sensitive? true:visibility :settings-manager:export? false@@ -342,7 +342,7 @@:doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")(defsetting llm-bedrock-secret-access-key- (deferred-tru "The AWS Secret Access Key for Amazon Bedrock.")+ (deferred-tru "The AWS Secret Access Key for Amazon Bedrock. On a self-hosted Metabase, leave unset together with the access key ID to authenticate with the AWS default credentials chain (IRSA, EKS Pod Identity, or instance profile); on Metabase Cloud both keys are required."):sensitive? true:visibility :settings-manager:export? false@@ -367,7 +367,7 @@:export? false:getter (connection-field-getter :llm-bedrock-region):setter (connection-field-setter :llm-bedrock-region)- :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection.")+ :doc "Backed by the bedrock connection in the admin AI settings provider list: reads and writes go through the llm-providers connection list, and a value set by this environment variable shadows this one field of that connection. On a self-hosted Metabase, setting only the region enables Bedrock with the AWS default credentials chain, with no access keys configured.");;; ----------------------------------------------- Microsoft Azure ---------------------------------------------diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex c8613bf208f..a6c26a6b26d 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -157,7 +157,7 @@(defn list-models"List available models for a provider using its configured credentials, or `:credentials` in `opts`.The shape of the credentials map varies by provider: API-key providers take `{:api-key ...}`, while Bedrock takes- AWS key material and region (see [[bedrock/list-models]])."+ optional AWS key material and region (see [[bedrock/list-models]])."([provider]((resolve-model-lister provider)))([provider opts]diff --git a/src/metabase/metabot/self/bedrock.clj b/src/metabase/metabot/self/bedrock.cljindex 5a7cf1717a7..511b264654f 100644--- a/src/metabase/metabot/self/bedrock.clj+++ b/src/metabase/metabot/self/bedrock.clj@@ -13,7 +13,9 @@[[openai/openai-request-body]] + [[openai/openai->aisdk-chunks-xf]]; the vendor prefix on themodel id (e.g. `anthropic.claude-haiku-4-5`, `openai.gpt-5.5`) selects the API family.- Requests are authenticated with AWS Signature Version 4 computed from the `llm-bedrock-*` settings:+ Requests are authenticated with AWS Signature Version 4 computed from the connection's access key pair, or, on a+ self-hosted Metabase with no pair configured, from the AWS SDK default credentials chain (IRSA, EKS Pod Identity,+ instance profile). Metabase Cloud always requires the pair:https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html"(:require[clojure.string :as str]@@ -26,11 +28,14 @@[metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json]+ [metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.o11y :refer [with-span]])(:import(java.net URI)(java.util.function Consumer)+ (software.amazon.awssdk.auth.credentials DefaultCredentialsProvider)+ (software.amazon.awssdk.core.exception SdkClientException SdkException)(software.amazon.awssdk.http ContentStreamProvider SdkHttpMethod SdkHttpRequest SdkHttpRequest$Builder)(software.amazon.awssdk.http.auth.aws.signer AwsV4HttpSigner)(software.amazon.awssdk.http.auth.spi.signer SignRequest$Builder SignedRequest)@@ -40,11 +45,54 @@;;; ------------------------------------------ AWS Signature Version 4 ------------------------------------------+(defn- chain-empty-ex+ "The chain ran out of providers. `AwsCredentialsProviderChain` catches what each provider throws and reports them+ together once none is left, so an unconfigured process and a broken IRSA trust policy both arrive here. The AWS+ text names roles and endpoints, so it goes to the log and the admin gets pointed at it."+ [cause]+ (log/warnf "AWS Bedrock default credentials chain resolved nothing: %s" (ex-message cause))+ (ex-info (tru "AWS Bedrock got no credentials from the AWS default credentials chain. See the Metabase logs for what each provider reported.")+ {:api-error true+ :error-code :api-key-missing+ :status-code 403}+ cause))++(defn- chain-failed-ex+ "A provider that resolved before threw on its own rather than being folded into the chain's report: an STS+ rejection refreshing a web identity, a throttled container-credentials refresh."+ [cause]+ (log/warnf "AWS Bedrock default credentials chain failed: %s" (ex-message cause))+ (ex-info (tru "AWS Bedrock could not refresh its AWS credentials. See the Metabase logs for what AWS reported.")+ {:api-error true+ :error-code :credentials-unavailable+ :status-code 403}+ cause))++(defn- chain-credentials+ "Credentials resolved from the AWS SDK default provider chain+ (https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials-chain.html). The SDK's shared+ chain caches what it resolves, so short-lived role credentials rotate without a restart."+ ^AwsCredentialsIdentity []+ (.resolveCredentials (DefaultCredentialsProvider/create)))+(defn- aws-identity- "An AWS credentials identity, carrying the session token when one is present."- [{:keys [access-key-id secret-access-key session-token]}]- (if session-token+ "The AWS credentials identity to sign with: the connection's access key pair, carrying the session token when one+ is present, or, on a self-hosted Metabase with no pair configured, whatever the default credentials chain+ resolves."+ ^AwsCredentialsIdentity [{:keys [access-key-id secret-access-key session-token]}]+ (cond+ (not (and access-key-id secret-access-key))+ (try+ (chain-credentials)+ (catch SdkClientException e+ (throw (chain-empty-ex e)))+ (catch SdkException e+ (throw (chain-failed-ex e))))++ session-token(AwsSessionCredentialsIdentity/create access-key-id secret-access-key session-token)++ :else(AwsCredentialsIdentity/create access-key-id secret-access-key)))(defn- unsigned-request@@ -100,8 +148,20 @@(throw (invalid-region-ex region)))region)-(defn- missing-credentials-ex []- (ex-info (tru "AWS Bedrock credentials are not configured")+(defn- token-without-pair-ex []+ (ex-info (tru "AWS Bedrock cannot use a session token without its access key pair")+ {:api-error true+ :error-code :api-key-missing+ :status-code 403}))++(defn- hosted-keyless-ex []+ (ex-info (tru "AWS Bedrock on Metabase Cloud requires an access key pair")+ {:api-error true+ :error-code :api-key-missing+ :status-code 403}))++(defn- incomplete-credentials-ex []+ (ex-info (tru "AWS Bedrock needs both an access key ID and a secret access key, or neither to use the default credentials chain"){:api-error true:error-code :api-key-missing:status-code 403}))@@ -113,11 +173,25 @@(defn- ensure-credentials"Validate the credentials of the connection serving this request.- Throws when the access key pair is incomplete or the region is unknown; the region defaults to us-east-1."+ Self-hosted, no access key pair at all is fine, signing falls back to the AWS default credentials chain; on a+ hosted deployment, or one whose license cannot be checked, the chain would resolve the operator's own identity,+ so the pair is required. Half a pair throws, as does a session token without the pair and an unknown region; the+ region defaults to us-east-1."[credentials]- (when-not (llm.provider/config-complete? "bedrock" credentials)- (throw (missing-credentials-ex)))- (update credentials :region #(validate-region (or (not-empty %) "us-east-1"))))+ (let [key-id (u/trimmed-string (:access-key-id credentials))+ secret (u/trimmed-string (:secret-access-key credentials))+ token (u/trimmed-string (:session-token credentials))]+ (when (not= (some? key-id) (some? secret))+ (throw (incomplete-credentials-ex)))+ (when (and token (not key-id))+ (throw (token-without-pair-ex)))+ (when (and (not key-id) (llm.provider/hosted?))+ (throw (hosted-keyless-ex)))+ (-> credentials+ (u/assoc-dissoc :access-key-id key-id)+ (u/assoc-dissoc :secret-access-key secret)+ (u/assoc-dissoc :session-token token)+ (update :region #(validate-region (or (not-empty %) "us-east-1"))))))(defn- bedrock-error-msg"Canonical, status-specific Bedrock error message."@@ -206,9 +280,10 @@(defn list-models"List the Bedrock models supported by this adapter (see [[supported-models]]).- No-arg uses the `llm-bedrock-*` settings. The opts map supports `:credentials`, a map of `:access-key-id`,- `:secret-access-key`, `:region`, and (for temporary credentials) `:session-token`, plus `:ai-proxy?`,- which is not supported for Bedrock and throws when true."+ The opts map supports `:credentials`, a map of `:access-key-id`, `:secret-access-key`, `:region`, and (for+ temporary credentials) `:session-token`, plus `:ai-proxy?`, which is not supported for Bedrock and throws when+ true. On a self-hosted Metabase with no access key pair, requests are signed with whatever the AWS default+ credentials chain resolves; Metabase Cloud requires the pair."([] (list-models {}))([opts]{:models (->> (list-all-models opts)@@ -245,8 +320,9 @@(mu/defn bedrock-raw"Perform a streaming request to the Bedrock mantle endpoint.- Opts map takes `:credentials` from the connection serving this request — `:access-key-id`, `:secret-access-key`,- `:region`, and (for temporary credentials) `:session-token` — and throws when they are missing.+ Opts map takes `:credentials` from the connection serving this request: `:access-key-id`, `:secret-access-key`,+ `:region`, and (for temporary credentials) `:session-token`. On a self-hosted Metabase with no access key pair,+ requests are signed with whatever the AWS default credentials chain resolves; Metabase Cloud requires the pair.`:ai-proxy?` is not supported for Bedrock and throws when true."[{:keys [model input tools credentials ai-proxy?] :as opts:or {model default-model}} :- core/LLMRequestOpts]diff --git a/test/metabase/llm/api/provider_test.clj b/test/metabase/llm/api/provider_test.cljindex b13f67a0d09..74d617e6348 100644--- a/test/metabase/llm/api/provider_test.clj+++ b/test/metabase/llm/api/provider_test.clj@@ -2,6 +2,7 @@(:require[clj-http.client :as http][clojure.test :refer [deftest is testing use-fixtures]]+ [medley.core :as m][metabase.llm.api.provider :as llm.api.provider][metabase.llm.provider :as llm.provider][metabase.metabot.self :as metabot.self]@@ -110,7 +111,12 @@(filter #(= "bedrock" (:type %)))first:fields- (into {} (map (juxt :key :advanced))))))))))+ (into {} (map (juxt :key :advanced)))))))+ (testing "each Bedrock key travels as requiring the other, which the form uses to gate half a pair"+ (is (= {:access-key-id ["secret-access-key"]+ :secret-access-key ["access-key-id"]+ :session-token ["access-key-id" "secret-access-key"]}+ (:requires (m/find-first #(= "bedrock" (:type %)) types))))))))(deftest provider-types-google-fields-test(testing "Google's credentials hang off the authentication method it is asked for, and its models are a fixed list"@@ -149,6 +155,16 @@(is (= [["service-account-key"] ["oauth-access-token" "project-id"]](:required_any google)))))))+(deftest provider-types-hosted-bedrock-test+ (testing "the listed Bedrock entry carries hosted policy, so the form asks for the keys the backend will demand"+ (mt/with-premium-features #{:hosting}+ (let [bedrock (m/find-first #(= "bedrock" (:type %))+ (mt/user-http-request :crowberto :get 200 "llm/provider-types"))]+ (is (= {"access-key-id" true "secret-access-key" true "region" false "session-token" false}+ (->> bedrock :fields (into {} (map (juxt :key :required))))))+ (is (= "On Metabase Cloud, Bedrock always authenticates with your own AWS keys."+ (:help (m/find-first #(= "access-key-id" (:key %)) (:fields bedrock)))))))))+(deftest provider-types-managed-availability-test(letfn [(managed [types] (->> types (filter #(= "metabase" (:type %))) first))](testing "the managed provider leads the list when the LLM proxy is configured"diff --git a/test/metabase/llm/provider_test.clj b/test/metabase/llm/provider_test.cljindex c93a4147416..f370abb8bb0 100644--- a/test/metabase/llm/provider_test.clj+++ b/test/metabase/llm/provider_test.clj@@ -2,8 +2,10 @@(:require[clojure.string :as str][clojure.test :refer [deftest is testing use-fixtures]]+ [medley.core :as m][metabase.llm.provider :as llm.provider][metabase.llm.settings :as llm.settings]+ [metabase.premium-features.core :as premium-features][metabase.settings.core :as setting][metabase.test :as mt][metabase.test.fixtures :as fixtures]))@@ -184,6 +186,86 @@(is (nil? (llm.provider/validate-config! "google" {:oauth-access-token "ya29.token":project-id "my-project"})))))+(deftest validate-config!-key-pair-test+ (testing "a bedrock key is taken only together with the other, so half a pair never reaches the signer"+ (is (nil? (llm.provider/validate-config! "bedrock" {})))+ (is (nil? (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"bedrock takes Access key ID only together with Secret access key"+ (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"bedrock takes Access key ID only together with Secret access key"+ (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key " "})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"bedrock takes Secret access key only together with Access key ID"+ (llm.provider/validate-config! "bedrock" {:secret-access-key "test-secret"})))))++(deftest config-complete?-requires-test+ (testing "a session token rides along with the bedrock pair, never alone"+ (is (false? (llm.provider/config-complete? "bedrock" {:session-token "FwoGZXIvYXdzEXAMPLE"})))+ (is (true? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"+ :session-token "FwoGZXIvYXdzEXAMPLE"})))))++(deftest validate-config!-requires-test+ (testing "a dependent field is taken only together with the fields it requires"+ (is (nil? (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"+ :session-token "FwoGZXIvYXdzEXAMPLE"})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"bedrock takes Session token only together with Access key ID \+ Secret access key"+ (llm.provider/validate-config! "bedrock" {:session-token "FwoGZXIvYXdzEXAMPLE"})))))++(deftest hosted-bedrock-requires-key-pair-test+ (testing "on a hosted deployment keyless bedrock is neither valid nor complete, since the default chain would sign as the operator"+ (mt/with-premium-features #{:hosting}+ (is (false? (llm.provider/config-complete? "bedrock" {})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Access key ID is required for bedrock"+ (llm.provider/validate-config! "bedrock" {})))+ (testing "the pair is a mandatory set, so the form marks each of its fields required"+ (is (= {:access-key-id true :secret-access-key true :region false :session-token false}+ (->> (llm.provider/provider-type "bedrock")+ :fields+ (into {} (map (juxt :key (comp boolean :required?))))))))+ (testing "an explicit customer pair stays valid"+ (is (nil? (llm.provider/validate-config! "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"})))+ (is (true? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "test-secret"})))))))++(deftest indeterminate-hosting-bedrock-requires-key-pair-test+ (testing "a token status the token service could not confirm counts as hosted, since the chain would sign as the+ operator on a Cloud instance that cannot reach it"+ (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature? (constantly nil)]+ (is (true? (llm.provider/hosted?)))+ (is (false? (llm.provider/config-complete? "bedrock" {})))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Access key ID is required for bedrock"+ (llm.provider/validate-config! "bedrock" {})))))+ (testing "a token the service did answer for leaves a self-hosted deployment keyless"+ (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature? (constantly false)]+ (is (false? (llm.provider/hosted?)))+ (is (nil? (llm.provider/validate-config! "bedrock" {}))))))++(deftest provider-types-carry-hosted-policy-test+ (testing "enumeration applies hosted policy too, so the connection form sees the same requirements as validation"+ (mt/with-premium-features #{:hosting}+ (let [bedrock (m/find-first #(= "bedrock" (:type %)) (llm.provider/provider-types))+ key-field (m/find-first #(= :access-key-id (:key %)) (:fields bedrock))]+ (is (true? (:required? key-field)))+ (is (= "On Metabase Cloud, Bedrock always authenticates with your own AWS keys."+ (str (:help key-field))))))+ (testing "and leaves the self-hosted entry alone"+ (let [key-field (->> (llm.provider/provider-types)+ (m/find-first #(= "bedrock" (:type %)))+ :fields+ (m/find-first #(= :access-key-id (:key %))))]+ (is (nil? (:required? key-field)))+ (is (str/starts-with? (str (:help key-field)) "Leave the keys blank"))))))+(deftest validate-config!-field-validator-test(testing "a field's own validator runs on a non-blank value"(is (thrown-with-msg?@@ -268,13 +350,16 @@(is (false? (llm.provider/config-complete? "vllm" {:api-key "local-dev-key"})))(is (false? (llm.provider/config-complete? "vllm" {:base-url " "})))(is (false? (llm.provider/config-complete? "vllm" nil))))- (testing "bedrock needs both AWS keys, and neither the region nor the session token"+ (testing "bedrock takes both AWS keys or none: a keyless connection signs with the AWS default credentials chain"(is (true? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE":secret-access-key "test-secret"})))- (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"})))- (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"- :secret-access-key ""})))- (is (false? (llm.provider/config-complete? "bedrock" {:secret-access-key "test-secret"}))))+ (is (true? (llm.provider/config-complete? "bedrock" {})))+ (is (true? (llm.provider/config-complete? "bedrock" nil)))+ (testing "but half a pair authenticates nothing and is not complete"+ (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"})))+ (is (false? (llm.provider/config-complete? "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key ""})))+ (is (false? (llm.provider/config-complete? "bedrock" {:secret-access-key "test-secret"})))))(testing "the managed type carries no credentials and is complete exactly when the LLM proxy is configured"(mt/with-premium-features #{:metabase-ai-managed}(mt/with-temporary-setting-values [llm-proxy-base-url "https://proxy.example.com"]@@ -347,6 +432,23 @@(testing "and a key on its own does not: it authenticates nothing without a server to send it to"(mt/with-temp-env-var-value! [mb-llm-vllm-api-key "local-dev-key"](is (= [] (llm.provider/connections)))))))+ (testing "Bedrock's region is a credential too: a keyless connection signs with the AWS default chain, so it alone synthesizes one"+ (mt/with-temporary-setting-values [llm-providers []]+ (mt/with-temp-env-var-value! [mb-llm-bedrock-region "eu-central-1"]+ (is (= [{:key "bedrock"+ :type "bedrock"+ :name "Amazon Bedrock"+ :source :env+ :env-vars #{"MB_LLM_BEDROCK_REGION"}+ :env-fields #{:region}+ :config {:region "eu-central-1"}}]+ (llm.provider/connections)))+ (is (true? (llm.provider/connection-usable? "bedrock"))))+ (testing "and a lone key synthesizes a connection that is not usable: half a pair authenticates nothing"+ (mt/with-temp-env-var-value! [mb-llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"]+ (is (=? [{:key "bedrock" :config {:access-key-id "AKIAIOSFODNN7EXAMPLE"}}]+ (llm.provider/connections)))+ (is (false? (llm.provider/connection-usable? "bedrock")))))))(testing "a metabase/ reference pinned by the environment synthesizes the managed connection it names"(mt/with-temporary-setting-values [llm-providers []](mt/with-temp-env-var-value! [mb-llm-metabot-provider "metabase/anthropic/claude-sonnet-4-6"]diff --git a/test/metabase/llm/startup_test.clj b/test/metabase/llm/startup_test.cljindex e3ae7144fd1..2b32790c895 100644--- a/test/metabase/llm/startup_test.clj+++ b/test/metabase/llm/startup_test.clj@@ -19,7 +19,8 @@(fn [feature](case feature:metabot-v3 metabot-v3?- :metabase-ai-managed managed-ai?))+ :metabase-ai-managed managed-ai?+ false))metabot.settings/llm-metabot-configured? (constantly (boolean configured?))](thunk))))diff --git a/test/metabase/metabot/self/bedrock_test.clj b/test/metabase/metabot/self/bedrock_test.cljindex 9a346bfda34..ce864dd5322 100644--- a/test/metabase/metabot/self/bedrock_test.clj+++ b/test/metabase/metabot/self/bedrock_test.clj@@ -7,8 +7,12 @@[metabase.metabot.self.bedrock :as bedrock][metabase.metabot.self.core :as self.core][metabase.metabot.self.debug :as debug]+ [metabase.premium-features.core :as premium-features][metabase.test :as mt]- [metabase.util.json :as json]))+ [metabase.util.json :as json])+ (:import+ (software.amazon.awssdk.core.exception SdkClientException SdkException)+ (software.amazon.awssdk.identity.spi AwsSessionCredentialsIdentity)))(set! *warn-on-reflection* true)@@ -70,19 +74,100 @@(is (= {:models [{:id "anthropic.claude-sonnet-5" :display_name "Claude Sonnet 5"}]}(bedrock/list-models {:credentials credentials}))))))-(deftest list-models-missing-credentials-test- (testing "a connection with no credentials fails rather than picking up the single-provider settings"+(deftest list-models-missing-credentials-uses-default-chain-test+ (testing "a connection with no credentials signs with the AWS default chain rather than picking up the single-provider settings"(mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id "AKIAIOSFODNN7EXAMPLE"llm.settings/llm-bedrock-secret-access-key "wJalrXUtnFEMI"]+ (let [captured (atom nil)]+ (mt/with-dynamic-fn-redefs [bedrock/chain-credentials+ (constantly (AwsSessionCredentialsIdentity/create+ "AKIACHAINCHAINCHAIN1" "chain-secret" "chain-token"))]+ (with-redefs [http/request (fn [req] (reset! captured req) {:body {:data fake-catalog}})]+ (is (=? {:models [{:id "anthropic.claude-fable-5"}+ {:id "anthropic.claude-haiku-4-5"}+ {:id "anthropic.claude-opus-4-8"}+ {:id "openai.gpt-5.4"}+ {:id "openai.gpt-5.5"}]}+ (bedrock/list-models)))+ (is (=? {:url "https://bedrock-mantle.us-east-1.api.aws/v1/models"+ :headers {"Authorization" #".*Credential=AKIACHAINCHAINCHAIN1/.*"+ "X-Amz-Security-Token" "chain-token"}}+ @captured))))))))++(deftest list-models-empty-default-chain-test+ (testing "no configured credentials and nothing in the default chain surface a provider-friendly error"+ (mt/with-dynamic-fn-redefs [bedrock/chain-credentials+ (fn [] (throw (SdkClientException/create "Unable to load credentials")))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"got no credentials from the AWS default credentials chain"+ (bedrock/list-models))))))++(deftest list-models-chain-refresh-failure-test+ (testing "a refresh that throws on its own, rather than through the chain's own report, is not called a missing+ key pair, which would send an operator off to create long-lived keys"+ (mt/with-dynamic-fn-redefs [bedrock/chain-credentials+ (fn [] (throw (-> (SdkException/builder)+ (.message "User: arn:aws:sts::123456789012:assumed-role/x is not authorized")+ (.build))))](is (thrown-with-msg?clojure.lang.ExceptionInfo- #"AWS Bedrock credentials are not configured"- (bedrock/list-models)))))- (deftest list-models-requires-both-keys-test+ #"could not refresh its AWS credentials"+ (bedrock/list-models)))+ (is (= :credentials-unavailable+ (try (bedrock/list-models)+ (catch clojure.lang.ExceptionInfo e (:error-code (ex-data e)))))))))++(deftest list-models-session-token-without-pair-test+ (testing "a session token without its key pair throws instead of silently signing as the ambient identity"(is (thrown-with-msg?clojure.lang.ExceptionInfo- #"AWS Bedrock credentials are not configured"- (bedrock/list-models)))))+ #"session token without its access key pair"+ (bedrock/list-models {:credentials {:session-token "FwoGZXIvYXdzEXAMPLE"}})))))++(deftest list-models-hosted-keyless-rejected-test+ (testing "a hosted deployment rejects a keyless connection before the credentials chain is touched"+ (mt/with-premium-features #{:hosting}+ (let [chain-calls (atom 0)+ requests (atom 0)]+ (mt/with-dynamic-fn-redefs [bedrock/chain-credentials (fn [] (swap! chain-calls inc) nil)]+ (with-redefs [http/request (fn [_] (swap! requests inc) {:body {:data fake-catalog}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Metabase Cloud requires an access key pair"+ (bedrock/list-models)))+ (is (zero? @chain-calls))+ (is (zero? @requests))))))))++(deftest list-models-unconfirmed-hosting-keyless-rejected-test+ (testing "a token status the token service could not confirm is treated as hosted, so a Cloud instance that+ cannot reach it does not fall back to the operator identity"+ (mt/with-dynamic-fn-redefs [premium-features/canonically-has-feature? (constantly nil)]+ (let [chain-calls (atom 0)+ requests (atom 0)]+ (mt/with-dynamic-fn-redefs [bedrock/chain-credentials (fn [] (swap! chain-calls inc) nil)]+ (with-redefs [http/request (fn [_] (swap! requests inc) {:body {:data fake-catalog}})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Metabase Cloud requires an access key pair"+ (bedrock/list-models)))+ (is (zero? @chain-calls))+ (is (zero? @requests))))))))++(deftest list-models-hosted-explicit-pair-works-test+ (testing "a hosted deployment still signs with an explicit customer key pair"+ (mt/with-premium-features #{:hosting}+ (let [captured (atom nil)]+ (with-redefs [http/request (fn [req] (reset! captured req) {:body {:data fake-catalog}})]+ (is (=? {:models [{:id "anthropic.claude-fable-5"}+ {:id "anthropic.claude-haiku-4-5"}+ {:id "anthropic.claude-opus-4-8"}+ {:id "openai.gpt-5.4"}+ {:id "openai.gpt-5.5"}]}+ (bedrock/list-models {:credentials {:access-key-id "AKIAIOSFODNN7EXAMPLE"+ :secret-access-key "wJalrXUtnFEMI"}})))+ (is (=? {:headers {"Authorization" #".*Credential=AKIAIOSFODNN7EXAMPLE/.*"}}+ @captured)))))))(deftest list-models-accepts-credentials-override-test(mt/with-temporary-setting-values [llm.settings/llm-bedrock-access-key-id nil@@ -105,10 +190,10 @@@captured)))))))(deftest list-models-credentials-override-must-be-complete-test- (testing "an override missing the secret access key throws without falling back to saved settings"+ (testing "an override missing the secret access key throws without falling back to saved settings or the chain"(is (thrown-with-msg?clojure.lang.ExceptionInfo- #"AWS Bedrock credentials are not configured"+ #"AWS Bedrock needs both an access key ID and a secret access key"(bedrock/list-models {:credentials {:access-key-id "AKIAOVERRIDEOVERRID1"}})))))(deftest list-models-credentials-override-region-validated-testdiff --git a/test/metabase/metabot/settings_test.clj b/test/metabase/metabot/settings_test.cljindex 2bfca0e56f9..dc4183d8cbd 100644--- a/test/metabase/metabot/settings_test.clj+++ b/test/metabase/metabot/settings_test.clj@@ -156,6 +156,10 @@(testing "returns true when bedrock has both the access key ID and secret access key"(with-connections [(connection "bedrock" "bedrock" {:access-key-id "AKIAIOSFODNN7EXAMPLE":secret-access-key "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"})]+ (with-selected-model "bedrock/anthropic.claude-haiku-4-5"+ (is (true? (metabot.settings/llm-metabot-configured?))))))+ (testing "returns true with no keys at all: requests sign with the AWS default credentials chain"+ (with-connections [(connection "bedrock" "bedrock")](with-selected-model "bedrock/anthropic.claude-haiku-4-5"(is (true? (metabot.settings/llm-metabot-configured?)))))))