feat: allow the use of an ENV variable to set the API key if the ParameterStore isn't used. (#40)

This commit is contained in:
Hans Knecht
2024-12-06 07:32:06 +01:00
committed by GitHub
parent 25b3cfb146
commit 241d5c0f3e
2 changed files with 11 additions and 2 deletions

View File

@@ -72,6 +72,8 @@ Please follow the steps below to deploy the Bedrock Proxy APIs into your AWS acc
**Step 1: Create your own custom API key (Optional)** **Step 1: Create your own custom API key (Optional)**
#### Store API Key in ParameterStore
> **Note:** This step is to use any string (without spaces) you like to create a custom API Key (credential) that will be used to access the proxy API later. This key does not have to match your actual OpenAI key, and you don't need to have an OpenAI API key. It is recommended that you take this step and ensure that you keep the key safe and private. > **Note:** This step is to use any string (without spaces) you like to create a custom API Key (credential) that will be used to access the proxy API later. This key does not have to match your actual OpenAI key, and you don't need to have an OpenAI API key. It is recommended that you take this step and ensure that you keep the key safe and private.
1. Open the AWS Management Console and navigate to the Systems Manager service. 1. Open the AWS Management Console and navigate to the Systems Manager service.
@@ -86,6 +88,10 @@ Please follow the steps below to deploy the Bedrock Proxy APIs into your AWS acc
5. Click "Create parameter". 5. Click "Create parameter".
6. Make a note of the parameter name you used (e.g., "BedrockProxyAPIKey"). You'll need this in the next step. 6. Make a note of the parameter name you used (e.g., "BedrockProxyAPIKey"). You'll need this in the next step.
#### Store API Key in ENV variable
1. Provide an ENV variable to the container named: `API_KEY` with the API key value.
**Step 2: Deploy the CloudFormation stack** **Step 2: Deploy the CloudFormation stack**
1. Sign in to AWS Management Console, switch to the region to deploy the CloudFormation Stack to. 1. Sign in to AWS Management Console, switch to the region to deploy the CloudFormation Stack to.

View File

@@ -3,16 +3,19 @@ from typing import Annotated
import boto3 import boto3
from fastapi import Depends, HTTPException, status from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from api.setting import DEFAULT_API_KEYS from api.setting import DEFAULT_API_KEYS
api_key_param = os.environ.get("API_KEY_PARAM_NAME") api_key_param = os.environ.get("API_KEY_PARAM_NAME")
api_key_env = os.environ.get("API_KEY")
if api_key_param: if api_key_param:
ssm = boto3.client("ssm") ssm = boto3.client("ssm")
api_key = ssm.get_parameter(Name=api_key_param, WithDecryption=True)["Parameter"][ api_key = ssm.get_parameter(Name=api_key_param, WithDecryption=True)["Parameter"][
"Value" "Value"
] ]
elif api_key_env:
api_key = api_key_env
else: else:
api_key = DEFAULT_API_KEYS api_key = DEFAULT_API_KEYS
@@ -20,7 +23,7 @@ security = HTTPBearer()
def api_key_auth( def api_key_auth(
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)] credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
): ):
if credentials.credentials != api_key: if credentials.credentials != api_key:
raise HTTPException( raise HTTPException(