Skip to content
Snippets Groups Projects
Unverified Commit dd2756b5 authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

add `sessionToken` validation connection auth for AWSbedrock (#2554)

parent 2c9cb28d
No related branches found
No related tags found
No related merge requests found
import { ArrowSquareOut, Info } from "@phosphor-icons/react"; import { ArrowSquareOut, Info } from "@phosphor-icons/react";
import { AWS_REGIONS } from "./regions"; import { AWS_REGIONS } from "./regions";
import { useState } from "react";
export default function AwsBedrockLLMOptions({ settings }) { export default function AwsBedrockLLMOptions({ settings }) {
const [useSessionToken, setUseSessionToken] = useState(
settings?.AwsBedrockLLMConnectionMethod === "sessionToken"
);
return ( return (
<div className="w-full flex flex-col"> <div className="w-full flex flex-col">
{!settings?.credentialsOnly && ( {!settings?.credentialsOnly && (
...@@ -24,6 +29,43 @@ export default function AwsBedrockLLMOptions({ settings }) { ...@@ -24,6 +29,43 @@ export default function AwsBedrockLLMOptions({ settings }) {
</div> </div>
)} )}
<div className="flex flex-col gap-y-2">
<input
type="hidden"
name="AwsBedrockLLMConnectionMethod"
value={useSessionToken ? "sessionToken" : "iam"}
/>
<div className="flex flex-col w-full">
<label className="text-white text-sm font-semibold block mb-3">
Use session token
</label>
<p className="text-white/50 text-sm">
Select the method to authenticate with AWS Bedrock.
</p>
</div>
<div className="flex items-center justify-start gap-x-4 bg-zinc-900 p-2.5 rounded-lg w-fit">
<span
className={`text-sm ${!useSessionToken ? "text-white" : "text-white/50"}`}
>
IAM
</span>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={useSessionToken}
onChange={(e) => setUseSessionToken(e.target.checked)}
/>
<div className="w-11 h-6 bg-zinc-700 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-primary-button"></div>
</label>
<span
className={`text-sm ${useSessionToken ? "text-white" : "text-white/50"}`}
>
Session Token
</span>
</div>
</div>
<div className="w-full flex items-center gap-[36px] my-1.5"> <div className="w-full flex items-center gap-[36px] my-1.5">
<div className="flex flex-col w-60"> <div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-3"> <label className="text-white text-sm font-semibold block mb-3">
...@@ -59,6 +101,25 @@ export default function AwsBedrockLLMOptions({ settings }) { ...@@ -59,6 +101,25 @@ export default function AwsBedrockLLMOptions({ settings }) {
spellCheck={false} spellCheck={false}
/> />
</div> </div>
{useSessionToken && (
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-3">
AWS Bedrock Session Token
</label>
<input
type="password"
name="AwsBedrockLLMSessionToken"
className="border-none bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
placeholder="AWS Bedrock Session Token"
defaultValue={
settings?.AwsBedrockLLMSessionToken ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
)}
<div className="flex flex-col w-60"> <div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-3"> <label className="text-white text-sm font-semibold block mb-3">
AWS region AWS region
......
...@@ -505,8 +505,11 @@ const SystemSettings = { ...@@ -505,8 +505,11 @@ const SystemSettings = {
GenericOpenAiKey: !!process.env.GENERIC_OPEN_AI_API_KEY, GenericOpenAiKey: !!process.env.GENERIC_OPEN_AI_API_KEY,
GenericOpenAiMaxTokens: process.env.GENERIC_OPEN_AI_MAX_TOKENS, GenericOpenAiMaxTokens: process.env.GENERIC_OPEN_AI_MAX_TOKENS,
AwsBedrockLLMConnectionMethod:
process.env.AWS_BEDROCK_LLM_CONNECTION_METHOD || "iam",
AwsBedrockLLMAccessKeyId: !!process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID, AwsBedrockLLMAccessKeyId: !!process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID,
AwsBedrockLLMAccessKey: !!process.env.AWS_BEDROCK_LLM_ACCESS_KEY, AwsBedrockLLMAccessKey: !!process.env.AWS_BEDROCK_LLM_ACCESS_KEY,
AwsBedrockLLMSessionToken: !!process.env.AWS_BEDROCK_LLM_SESSION_TOKEN,
AwsBedrockLLMRegion: process.env.AWS_BEDROCK_LLM_REGION, AwsBedrockLLMRegion: process.env.AWS_BEDROCK_LLM_REGION,
AwsBedrockLLMModel: process.env.AWS_BEDROCK_LLM_MODEL_PREFERENCE, AwsBedrockLLMModel: process.env.AWS_BEDROCK_LLM_MODEL_PREFERENCE,
AwsBedrockLLMTokenLimit: process.env.AWS_BEDROCK_LLM_MODEL_TOKEN_LIMIT, AwsBedrockLLMTokenLimit: process.env.AWS_BEDROCK_LLM_MODEL_TOKEN_LIMIT,
......
...@@ -31,6 +31,14 @@ class AWSBedrockLLM { ...@@ -31,6 +31,14 @@ class AWSBedrockLLM {
if (!process.env.AWS_BEDROCK_LLM_REGION) if (!process.env.AWS_BEDROCK_LLM_REGION)
throw new Error("No AWS Bedrock LLM region was set."); throw new Error("No AWS Bedrock LLM region was set.");
if (
process.env.AWS_BEDROCK_LLM_CONNECTION_METHOD === "sessionToken" &&
!process.env.AWS_BEDROCK_LLM_SESSION_TOKEN
)
throw new Error(
"No AWS Bedrock LLM session token was set while using session token as the authentication method."
);
this.model = this.model =
modelPreference || process.env.AWS_BEDROCK_LLM_MODEL_PREFERENCE; modelPreference || process.env.AWS_BEDROCK_LLM_MODEL_PREFERENCE;
this.limits = { this.limits = {
...@@ -41,6 +49,20 @@ class AWSBedrockLLM { ...@@ -41,6 +49,20 @@ class AWSBedrockLLM {
this.embedder = embedder ?? new NativeEmbedder(); this.embedder = embedder ?? new NativeEmbedder();
this.defaultTemp = 0.7; this.defaultTemp = 0.7;
this.#log(
`Loaded with model: ${this.model}. Will communicate with AWS Bedrock using ${this.authMethod} authentication.`
);
}
/**
* Get the authentication method for the AWS Bedrock LLM.
* There are only two valid values for this setting - anything else will default to "iam".
* @returns {"iam"|"sessionToken"}
*/
get authMethod() {
const method = process.env.AWS_BEDROCK_LLM_CONNECTION_METHOD || "iam";
if (!["iam", "sessionToken"].includes(method)) return "iam";
return method;
} }
#bedrockClient({ temperature = 0.7 }) { #bedrockClient({ temperature = 0.7 }) {
...@@ -51,6 +73,9 @@ class AWSBedrockLLM { ...@@ -51,6 +73,9 @@ class AWSBedrockLLM {
credentials: { credentials: {
accessKeyId: process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID, accessKeyId: process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_BEDROCK_LLM_ACCESS_KEY, secretAccessKey: process.env.AWS_BEDROCK_LLM_ACCESS_KEY,
...(this.authMethod === "sessionToken"
? { sessionToken: process.env.AWS_BEDROCK_LLM_SESSION_TOKEN }
: {}),
}, },
temperature, temperature,
}); });
......
...@@ -22,6 +22,11 @@ class AWSBedrockProvider extends InheritMultiple([Provider, UnTooled]) { ...@@ -22,6 +22,11 @@ class AWSBedrockProvider extends InheritMultiple([Provider, UnTooled]) {
credentials: { credentials: {
accessKeyId: process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID, accessKeyId: process.env.AWS_BEDROCK_LLM_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_BEDROCK_LLM_ACCESS_KEY, secretAccessKey: process.env.AWS_BEDROCK_LLM_ACCESS_KEY,
// If we're using a session token, we need to pass it in as a credential
// otherwise we must omit it so it does not conflict if using IAM auth
...(this.authMethod === "sessionToken"
? { sessionToken: process.env.AWS_BEDROCK_LLM_SESSION_TOKEN }
: {}),
}, },
model, model,
}); });
...@@ -31,6 +36,17 @@ class AWSBedrockProvider extends InheritMultiple([Provider, UnTooled]) { ...@@ -31,6 +36,17 @@ class AWSBedrockProvider extends InheritMultiple([Provider, UnTooled]) {
this.verbose = true; this.verbose = true;
} }
/**
* Get the authentication method for the AWS Bedrock LLM.
* There are only two valid values for this setting - anything else will default to "iam".
* @returns {"iam"|"sessionToken"}
*/
get authMethod() {
const method = process.env.AWS_BEDROCK_LLM_CONNECTION_METHOD || "iam";
if (!["iam", "sessionToken"].includes(method)) return "iam";
return method;
}
get client() { get client() {
return this._client; return this._client;
} }
......
...@@ -213,6 +213,13 @@ const KEY_MAPPING = { ...@@ -213,6 +213,13 @@ const KEY_MAPPING = {
}, },
// AWS Bedrock LLM InferenceSettings // AWS Bedrock LLM InferenceSettings
AwsBedrockLLMConnectionMethod: {
envKey: "AWS_BEDROCK_LLM_CONNECTION_METHOD",
checks: [
(input) =>
["iam", "sessionToken"].includes(input) ? null : "Invalid value",
],
},
AwsBedrockLLMAccessKeyId: { AwsBedrockLLMAccessKeyId: {
envKey: "AWS_BEDROCK_LLM_ACCESS_KEY_ID", envKey: "AWS_BEDROCK_LLM_ACCESS_KEY_ID",
checks: [isNotEmpty], checks: [isNotEmpty],
...@@ -221,6 +228,10 @@ const KEY_MAPPING = { ...@@ -221,6 +228,10 @@ const KEY_MAPPING = {
envKey: "AWS_BEDROCK_LLM_ACCESS_KEY", envKey: "AWS_BEDROCK_LLM_ACCESS_KEY",
checks: [isNotEmpty], checks: [isNotEmpty],
}, },
AwsBedrockLLMSessionToken: {
envKey: "AWS_BEDROCK_LLM_SESSION_TOKEN",
checks: [],
},
AwsBedrockLLMRegion: { AwsBedrockLLMRegion: {
envKey: "AWS_BEDROCK_LLM_REGION", envKey: "AWS_BEDROCK_LLM_REGION",
checks: [isNotEmpty], checks: [isNotEmpty],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment