Backup notturno: le impostazioni senza il token del bot Telegram (#32)

Il backup logico scrive la tabella settings in chiaro in /config/backup, e con lei il token del bot
Telegram. Ora settings esce con value - 'TelegramBotToken', come già fa lo strumento dei dati; la chat
resta, perché non è un segreto. Dopo un ripristino da quei file il token va riscritto: lo dicono README
e architettura. Una prova su PostgreSQL controlla che il file non contenga il token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-24 18:23:41 +01:00
1 parent ceca0013b8
commit f92b8d08f0
4 files changed
+38 -3

No files matched your search

+2 -1
View File
@@ -129,7 +129,8 @@ campiona per esito, e la legenda lo dice. I file restano in `/config/esportazion
tue puntate, previsioni, prodotti, rischio, impostazioni.
- **Backup**: ogni notte le tabelle di riepilogo in `/config/backup/AAAA-MM-GG/*.csv.gz` (14 giorni). Le
puntate di tutti (`bids`, la parte grossa) si affidano al **backup di Unraid del container PostgreSQL**
(`/mnt/cache/appdata/postgresql18`): tienilo attivo.
(`/mnt/cache/appdata/postgresql18`): tienilo attivo. Le impostazioni escono senza il token del bot
Telegram: dopo un ripristino da questi file va riscritto.
- **Registri**: `/config/registri/app-AAAA-MM-GG.log` e `aste-AAAA-MM-GG.log`, 90 giorni.
## Aggiornare
+2 -1
View File
@@ -307,7 +307,8 @@ riepilogo, non si toccano mai. Oltre il 90% del tetto parte un avviso Telegram.
### Backup e registri
Dopo la manutenzione, `COPY … TO STDOUT` delle tabelle non partizionate più `my_bids` e
`auction_forecasts` in `/config/backup/AAAA-MM-GG/*.csv.gz`, conservati 14 giorni. Il grosso
`auction_forecasts` in `/config/backup/AAAA-MM-GG/*.csv.gz`, conservati 14 giorni; `settings` esce senza
`TelegramBotToken` (`Maintenance.BackupCopySql`), perché i file stanno in chiaro. Il grosso
(`bids`) si affida al backup del container PostgreSQL. I registri su file (`app-*.log`, `aste-*.log`)
si cancellano dopo `LogRetentionDays` (90). La copia passa da `Data/BulkCopy` (file `.tmp` rinominato
alla fine, righe esatte, impronta SHA-256).
+9 -1
View File
@@ -40,6 +40,14 @@ public sealed class Maintenance
"risk_ledger", "products", "watched_products", "product_stats", "promo_redemptions", "chests"
};
/// <summary>
/// Il COPY del backup di una tabella. Da <c>settings</c> esce senza il token del bot Telegram: il backup sta in
/// chiaro in <c>/config/backup</c> e un segreto non ci deve finire (dopo un ripristino il token si riscrive).
/// </summary>
internal static string BackupCopySql(string table) => table == "settings"
? "COPY (SELECT id, value - 'TelegramBotToken' AS value, updated_at FROM settings) TO STDOUT (FORMAT csv, HEADER)"
: $"COPY {table} TO STDOUT (FORMAT csv, HEADER)";
public long? LastSizeBytes { get; private set; }
public long CapBytes => (long)(Math.Max(0.001, _settings().DataCapGb) * 1024 * 1024 * 1024);
public DateTime? LastRunUtc { get; private set; }
@@ -161,7 +169,7 @@ public sealed class Maintenance
{
foreach (var table in BackupTables)
{
var copied = await BulkCopy.ToFileAsync(conn, $"COPY {table} TO STDOUT (FORMAT csv, HEADER)",
var copied = await BulkCopy.ToFileAsync(conn, BackupCopySql(table),
Path.Combine(folder, table + ".csv.gz"), ct: ct).ConfigureAwait(false);
total += copied.Bytes;
}
+25
View File
@@ -279,6 +279,31 @@ public class ArchiveDownloaderTests(DatabaseFixture fx)
Assert.True((bool)(await none.ExecuteScalarAsync())!);
}
[DbFact]
public async Task Il_backup_notturno_delle_impostazioni_non_contiene_il_token_di_telegram()
{
var db = fx.Db!;
await db.ExecuteAsync("INSERT INTO settings(id, value) VALUES (1, '{\"TelegramBotToken\": \"123:segreto\", \"TelegramChatId\": \"42\", \"MaxBids\": 5}') " +
"ON CONFLICT (id) DO UPDATE SET value = excluded.value");
var file = Path.Combine(Path.GetTempPath(), "mimante-backup-" + Guid.NewGuid().ToString("N") + ".csv");
try
{
await using (var conn = await db.Writer.OpenConnectionAsync())
await BulkCopy.ToFileAsync(conn, Maintenance.BackupCopySql("settings"), file);
var text = await File.ReadAllTextAsync(file);
Assert.StartsWith("id,value,updated_at", text);
Assert.DoesNotContain("segreto", text);
Assert.DoesNotContain("TelegramBotToken", text);
Assert.Contains("MaxBids", text);
Assert.Contains("TelegramChatId", text);
}
finally
{
File.Delete(file);
}
Assert.Equal("COPY bids TO STDOUT (FORMAT csv, HEADER)", Maintenance.BackupCopySql("bids"));
}
[DbFact]
public async Task La_connessione_dello_strumento_non_puo_scrivere()
{