Skip to content

Commit

Permalink
added API key auth
Browse files Browse the repository at this point in the history
  • Loading branch information
francojreyes committed Mar 26, 2024
1 parent 3b17593 commit e0d7a1c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ POSTGRES_DB=postgres
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
HASURAGRES_PORT=8000
API_KEYS=my_key
13 changes: 13 additions & 0 deletions app/helpers/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from typing import Annotated

from dotenv import load_dotenv
from fastapi import Header, HTTPException

load_dotenv()
API_KEYS = list(filter(None, os.environ.get("API_KEYS", "").split(";")))


def validate_api_key(x_api_key: Annotated[str, Header()] = ""):
if x_api_key not in API_KEYS:
raise HTTPException(status_code=401, detail="X-API-Key invalid or missing")
17 changes: 11 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import psycopg2
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from psycopg2 import Error
from psycopg2.extensions import connection, cursor
from pydantic import BaseModel, Field

from helpers.auth import validate_api_key
from helpers.hasura import track_table


Expand All @@ -20,6 +21,7 @@ class Metadata(BaseModel):
sql_down: str # SQL to tear DOWN a table (should be the opp. of up)
columns: list[str] # list of column names that require insertion
write_mode: Literal['append', 'overwrite'] = Field('overwrite', description='mode in which to write to the database')
dryrun: bool = Field(False, description='if true, does not commit changes - useful for testing')


conn: connection = None
Expand Down Expand Up @@ -139,7 +141,7 @@ def execute_delete(metadata: Metadata, payload: list[Any]):
cur.execute(cmd, (values,))


@app.post("/insert")
@app.post("/insert", dependencies=[Depends(validate_api_key)])
def insert(metadata: Metadata, payload: list[Any]):
try:
created = create_table(metadata)
Expand All @@ -166,11 +168,14 @@ def insert(metadata: Metadata, payload: list[Any]):
conn.rollback()
raise HTTPException(status_code=400, detail=err_msg)

conn.commit()
if not metadata.dryrun:
conn.commit()

# Run Hasura actions - must be done after transaction committed otherwise Hasura won't see the table
if created:
track_table(metadata.table_name.lower())
# Run Hasura actions - must be done after transaction committed otherwise Hasura won't see the table
if created:
track_table(metadata.table_name.lower())
else:
conn.rollback()

return {}

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
HASURA_GRAPHQL_HOST: ${HASURA_GRAPHQL_HOST}
HASURA_GRAPHQL_PORT: ${HASURA_GRAPHQL_PORT}
HASURAGRES_PORT: ${HASURAGRES_PORT}
API_KEYS: ${API_KEYS}
depends_on:
- graphql-engine
- postgres
Expand Down

0 comments on commit e0d7a1c

Please sign in to comment.