azukaar / Cosmos-Server

☁️ The Most Secure and Easy Selfhosted Home Server. Take control of your data and privacy without sacrificing security and stability (Authentication, anti-DDOS, anti-bot)
https://cosmos-cloud.io
Other
3.65k stars 130 forks source link

[FEAT]: Protect, or recover, or warn when out-of-storage #215

Closed Lyamc closed 8 months ago

Lyamc commented 8 months ago

Feature Description

There are cases, whether accidental (user error) or malicious (compromised upload server) that can result in running of of storage space.

If the /var/lib/cosmos/cosmos.config.json was modified afterwards, the file can become truncated:

          "SmartShield": {
            "Enabled": true,
            "PolicyStrictness": 0,
            "PerUserTimeBudget": 0,
            "PerU

In such a case, the container will have an error and then restart, endlessly.

2024/03/12 23:24:04 [INFO] Starting...
2024/03/12 23:24:04 [INFO] Using config file: /config/cosmos.config.json
2024/03/12 23:24:04 [FATAL] Reading Config File: unexpected EOF : unexpected EOF

Ideally, the space should be allocated before the write to prevent that, but supposing it does happen, having documentation to recover from backup like:

cat << 'EOF' | tee restore-cosmos.sh
#!/bin/bash

cosmosdir="/var/lib/cosmos"
tempdir="/tmp/cosmos"

unzip -o "$cosmosdir/cosmos-backup.zip" -d "$tempdir" 

cmp -s "$tempdir/cosmos.config.json" "$cosmosdir/cosmos.config.json" && echo "The backup and current config files are identical." && exit 0

echo "<----------------------- Backup Config ---------------------- | ---------------------- Current Config ----------------------->"
diff -y --suppress-common-lines "$tempdir/cosmos.config.json" "$cosmosdir/cosmos.config.json"
echo "<----------------------- Backup Config ---------------------- | ---------------------- Current Config ----------------------->"

read -p "Do you want to replace the current config with the backup? (y/N): " answer

case "${answer}" in
  [yY]|[yY][eE][sS]) cp "$tempdir/cosmos.config.json" "$cosmosdir/cosmos.config.json"; echo "Configuration restored from backup."  ;;
  *) echo "No changes made." ;;
esac

EOF
chmod +x restore-cosmos.sh
./restore-cosmos.sh

or a way for the container to either start (with the webpage complaining about storage), or exit with information about how to resolve the issue.

azukaar commented 8 months ago

Cosmos actually has a notification when you run out of storage already