Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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 @@ -85,6 +87,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