|
| 1 | +# Database Security |
| 2 | + |
| 3 | +This document describes how to configure network security for the Azure Cosmos DB for MongoDB database to restrict access to authorized sources only. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +By default, anyone with the database connection string can access the database from anywhere. To improve security, we use Azure Cosmos DB's built-in firewall to restrict access to: |
| 8 | + |
| 9 | +1. **Azure App Service** — The production application |
| 10 | +2. **Whitelisted IP addresses** — Developers who need to run local scripts |
| 11 | + |
| 12 | +## Configuring the Cosmos DB Firewall |
| 13 | + |
| 14 | +### 1. Open Networking Settings |
| 15 | + |
| 16 | +1. Log in to the [Azure Portal](https://portal.azure.com) |
| 17 | +2. Navigate to your Cosmos DB account (e.g., `rds-db-kc`) |
| 18 | +3. In the left sidebar, go to **Settings** → **Networking** |
| 19 | + |
| 20 | +### 2. Configure Public Network Access |
| 21 | + |
| 22 | +1. Under **Public network access**, select **Selected networks** |
| 23 | +2. This disables open access and enables the firewall |
| 24 | + |
| 25 | +### 3. Allow Azure Services |
| 26 | + |
| 27 | +1. Check the box for **"Accept connections from within public Azure datacenters"** |
| 28 | +2. This allows your Azure App Service to connect to the database |
| 29 | +3. Note: This allows any Azure service to connect, not just yours. For stricter security, see [Private Endpoints](#advanced-private-endpoints) below. |
| 30 | + |
| 31 | +### 4. Add Developer IP Addresses |
| 32 | + |
| 33 | +For each developer who needs to run local scripts (e.g., `npm run super-admin`, `npm run locations`): |
| 34 | + |
| 35 | +1. Under **Firewall**, click **"Add my current IP"** or manually enter the IP address |
| 36 | +2. Click **Save** |
| 37 | + |
| 38 | +To find your public IP address: |
| 39 | + |
| 40 | +```bash |
| 41 | +curl ifconfig.me |
| 42 | +``` |
| 43 | + |
| 44 | +### 5. Save Changes |
| 45 | + |
| 46 | +Click **Save** at the top of the page. Changes may take a few minutes to propagate. |
| 47 | + |
| 48 | +## Running Local Scripts |
| 49 | + |
| 50 | +Scripts in `server/src/scripts/` require database access: |
| 51 | + |
| 52 | +- `superAdminCRUD.ts` — Manage super admin users |
| 53 | +- `locationCRUD.ts` — Manage locations |
| 54 | +- `generateSeeds.ts` — Generate seed data |
| 55 | +- `generateCoupons.ts` — Generate coupon codes |
| 56 | + |
| 57 | +Before running these scripts locally, ensure your IP address is whitelisted in the Cosmos DB firewall (see step 4 above). |
| 58 | + |
| 59 | +### Troubleshooting Connection Issues |
| 60 | + |
| 61 | +If you see connection timeout errors when running scripts: |
| 62 | + |
| 63 | +1. **Verify your IP is whitelisted:** |
| 64 | + - Run `curl ifconfig.me` to get your current public IP |
| 65 | + - Check that this IP is listed in the Cosmos DB firewall settings |
| 66 | + |
| 67 | +2. **IP address changed:** |
| 68 | + - Home/office IPs can change periodically |
| 69 | + - If you're on a new network (coffee shop, VPN, etc.), your IP will be different |
| 70 | + - Add the new IP to the firewall |
| 71 | + |
| 72 | +3. **Firewall propagation delay:** |
| 73 | + - After adding an IP, wait 1-2 minutes before retrying |
| 74 | + |
| 75 | +## Security Considerations |
| 76 | + |
| 77 | +### What This Protects Against |
| 78 | + |
| 79 | +- **Leaked connection strings** — Even if the `MONGO_URI` is exposed, attackers cannot connect without an allowed IP |
| 80 | +- **Unauthorized access** — Only explicitly whitelisted sources can reach the database |
| 81 | + |
| 82 | +### What This Does NOT Protect Against |
| 83 | + |
| 84 | +- **Compromised Azure services** — The "Accept Azure datacenters" option allows any Azure service to connect |
| 85 | +- **Compromised developer machines** — If a whitelisted machine is compromised, the attacker has database access |
| 86 | + |
| 87 | +### Best Practices |
| 88 | + |
| 89 | +1. **Minimize whitelisted IPs** — Only add IPs for developers who actively need script access |
| 90 | +2. **Review periodically** — Remove IPs for developers who no longer need access |
| 91 | +3. **Use environment-specific databases** — Use separate databases for development, staging, and production |
| 92 | +4. **Rotate credentials** — Periodically regenerate the Cosmos DB connection string and update the App Service configuration |
| 93 | + |
| 94 | +## Advanced: Private Endpoints |
| 95 | + |
| 96 | +For stricter security requirements (e.g., HIPAA compliance), consider using Azure Private Endpoints: |
| 97 | + |
| 98 | +1. **Private Endpoint** — Creates a private IP for Cosmos DB within your Virtual Network |
| 99 | +2. **VNet Integration** — Connect your App Service to the same Virtual Network |
| 100 | +3. **Azure VPN** — Developers connect via VPN to access the database |
| 101 | + |
| 102 | +This approach ensures the database is never exposed to the public internet. However, it requires additional Azure resources and configuration complexity. |
| 103 | + |
| 104 | +## Quick Reference |
| 105 | + |
| 106 | +| Task | Action | |
| 107 | +|------|--------| |
| 108 | +| Allow App Service | Enable "Accept connections from within public Azure datacenters" | |
| 109 | +| Allow developer | Add their public IP to the firewall | |
| 110 | +| Find your IP | Run `curl ifconfig.me` | |
| 111 | +| Remove access | Delete the IP from the firewall | |
0 commit comments