Skip to content

Commit

Permalink
create a network policy restricting access to backend, specify cors o…
Browse files Browse the repository at this point in the history
…rigin for backend
  • Loading branch information
liberty-rising committed Jan 10, 2024
1 parent 396771c commit 833e0b0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from database.database_manager import DatabaseManager
from models.base import Base
Expand All @@ -11,6 +12,7 @@
from routes.organization_routes import organization_router
from routes.table_routes import table_router
from routes.user_routes import user_router
from settings import APP_ENV
from startup import run_startup_routines
from utils.utils import get_app_logger

Expand All @@ -20,6 +22,19 @@

app = FastAPI()

if APP_ENV == "prod":
origins = ["https://docshow.ai"]
else:
origins = ["https://127.0.0.1"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


async def startup_event():
run_startup_routines()
Expand Down
44 changes: 44 additions & 0 deletions k8s/backend-network-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-network-policy
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
- Egress
ingress:
- from:
# Allow traffic from the Ingress controller namespace
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
# # Also allow traffic from frontend pods (if needed)
# - podSelector:
# matchLabels:
# app: frontend
- ports:
- protocol: TCP
port: 8000
egress:
- to:
# Allow access to DNS service
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
- to:
# Allow specific egress to PostgreSQL database (consider specifying a specific CIDR if possible)
- ipBlock:
cidr: 0.0.0.0/0 # Update this if a more specific CIDR block can be used
ports:
- protocol: TCP
port: 80 # For outbound web traffic
- protocol: TCP
port: 25060 # For PostgreSQL database

0 comments on commit 833e0b0

Please sign in to comment.