Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
Shell
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123#!/bin/bash# Enable direct MongoDB access on silo (with security)
set -e
GREEN='\033[0;32m'YELLOW='\033[1;33m'BLUE='\033[0;36m'RED='\033[0;31m'NC='\033[0m'
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"echo -e "${BLUE}║ Enable Direct MongoDB Access on Silo ║${NC}"echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"echo ""
# Get current public IPCURRENT_IP=$(curl -s ifconfig.me)echo -e "${YELLOW}Your current public IP: $CURRENT_IP${NC}"echo ""
# Ask for confirmationecho -e "${YELLOW}This will:${NC}"echo " 1. Configure MongoDB to bind to 0.0.0.0 (all interfaces)"echo " 2. Set up firewall to ONLY allow your IP ($CURRENT_IP)"echo " 3. Keep authentication enabled (passwords still required)"echo ""echo -e "${RED}⚠️ MongoDB will be accessible from the internet (but only from your IP)${NC}"echo ""read -p "Continue? [y/N] " -n 1 -rechoif [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 0fi
# SSH into silo and configuressh -i aesthetic-computer-vault/home/.ssh/id_rsa root@silo.aesthetic.computer bash << EOFset -e
echo "Configuring MongoDB to bind to all interfaces..."
# Backup configcp /etc/mongod.conf /etc/mongod.conf.backup.before-direct-access
# Update bindIp to 0.0.0.0if grep -q "bindIp:" /etc/mongod.conf; then sed -i 's/bindIp:.*/bindIp: 0.0.0.0/' /etc/mongod.conf echo "✓ Updated bindIp to 0.0.0.0"else # Add bindIp if not present sed -i '/^net:/a\ bindIp: 0.0.0.0' /etc/mongod.conf echo "✓ Added bindIp: 0.0.0.0"fi
# Enable UFW if not already enabledif ! ufw status | grep -q "Status: active"; then echo "Enabling UFW firewall..." # Allow SSH first so we don't lock ourselves out! ufw allow 22/tcp ufw allow 443/tcp ufw allow 80/tcp ufw --force enable echo "✓ UFW enabled"fi
# Allow MongoDB from specific IP onlyecho "Adding firewall rule for MongoDB..."ufw allow from $CURRENT_IP to any port 27017 comment 'MongoDB access from trusted IP'
echo "✓ Firewall rule added"
# Show firewall statusecho ""echo "Current UFW rules:"ufw status numbered
# Restart MongoDBecho ""echo "Restarting MongoDB..."systemctl restart mongodsleep 3
# Check if MongoDB is listening on all interfacesecho ""echo "MongoDB listening on:"netstat -tlnp | grep 27017 || ss -tlnp | grep 27017
echo ""echo "✓ MongoDB is now accessible from $CURRENT_IP"
EOF
echo ""echo -e "${GREEN}╔════════════════════════════════════════════════════════════╗${NC}"echo -e "${GREEN}║ SUCCESS! ║${NC}"echo -e "${GREEN}╚════════════════════════════════════════════════════════════╝${NC}"echo ""echo -e "${YELLOW}Direct Connection String:${NC}"echo ""
source aesthetic-computer-vault/silo/.envDIRECT_CONN_STRING=$(echo "$MONGODB_CONNECTION_STRING" | sed 's/localhost:27018/silo.aesthetic.computer:27017/')
echo "$DIRECT_CONN_STRING"echo ""echo -e "${BLUE}Test the connection:${NC}"echo "node -e \""echo " const { MongoClient } = require('mongodb');"echo " const client = new MongoClient('$DIRECT_CONN_STRING');"echo " client.connect()"echo " .then(() => client.db().admin().ping())"echo " .then(() => { console.log('✓ Direct connection works!'); return client.close(); })"echo " .catch(err => console.error('✗ Failed:', err.message));"echo "\""echo ""echo -e "${YELLOW}Security Notes:${NC}"echo " • Only your IP ($CURRENT_IP) can access MongoDB"echo " • Authentication is still required (passwords)"echo " • To add more IPs: sudo ufw allow from NEW_IP to any port 27017"echo " • To disable: sudo ufw delete allow from $CURRENT_IP to any port 27017"echo ""