Skip to content

Commit

Permalink
feat: allow the use of an ENV variable to set the API key if the Para…
Browse files Browse the repository at this point in the history
…meterStore isn't used. (#40)
  • Loading branch information
knechtionscoding authored Dec 6, 2024
1 parent 25b3cfb commit 241d5c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**

#### 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.
1. Open the AWS Management Console and navigate to the Systems Manager service.
Expand All @@ -86,6 +88,10 @@ Please follow the steps below to deploy the Bedrock Proxy APIs into your AWS acc
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.

#### 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**

1. Sign in to AWS Management Console, switch to the region to deploy the CloudFormation Stack to.
Expand Down
7 changes: 5 additions & 2 deletions src/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@

import boto3
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

api_key_param = os.environ.get("API_KEY_PARAM_NAME")
api_key_env = os.environ.get("API_KEY")
if api_key_param:
ssm = boto3.client("ssm")
api_key = ssm.get_parameter(Name=api_key_param, WithDecryption=True)["Parameter"][
"Value"
]
elif api_key_env:
api_key = api_key_env
else:
api_key = DEFAULT_API_KEYS

security = HTTPBearer()


def api_key_auth(
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
):
if credentials.credentials != api_key:
raise HTTPException(
Expand Down

0 comments on commit 241d5c0

Please sign in to comment.