| | | 1 | | using ArturRios.Data.Relational.Core.DependencyInjection; |
| | | 2 | | using ArturRios.Util.WebApi.Configuration; |
| | | 3 | | using ArturRios.Util.WebApi.Security.Extensions; |
| | | 4 | | using FluentValidation; |
| | | 5 | | using Serilog; |
| | | 6 | | |
| | | 7 | | namespace ArturRios.Heimdall.WebApi; |
| | | 8 | | |
| | | 9 | | internal static class DatabaseConnectionGuard |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Set to <c>true</c> to refuse start-up when the database connection does not require TLS, |
| | | 13 | | /// rather than warning. Opt-in: see <see cref="WarnIfDatabaseConnectionIsNotEncrypted" />. |
| | | 14 | | /// </summary> |
| | | 15 | | private const string RequireDatabaseTlsVariable = "HEIMDALL_DATA_REQUIRE_TLS"; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Warns when the database connection string does not require TLS (NFR-25, GDPR Art. 32, |
| | | 19 | | /// LGPD Art. 46). |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// <para> |
| | | 23 | | /// Everything this database holds is personal data, and Npgsql's default |
| | | 24 | | /// <c>SSL Mode</c> is <c>Prefer</c> — which silently falls back to an unencrypted |
| | | 25 | | /// connection when the server does not offer TLS. A deployment can therefore be sending |
| | | 26 | | /// credentials and addresses in clear text across the network while looking correctly |
| | | 27 | | /// configured, which is exactly the failure worth a start-up warning. |
| | | 28 | | /// </para> |
| | | 29 | | /// <para> |
| | | 30 | | /// It warns by default rather than refusing to start, which is a deliberate departure |
| | | 31 | | /// from how this codebase treats other security configuration — TH-23's email control |
| | | 32 | | /// fails start-up in Production. The difference is that failing by default would take |
| | | 33 | | /// down a working deployment on upgrade over a setting the operator may not control |
| | | 34 | | /// directly. |
| | | 35 | | /// </para> |
| | | 36 | | /// <para> |
| | | 37 | | /// Setting <c>HEIMDALL_DATA_REQUIRE_TLS=true</c> turns the warning into a refusal. It is |
| | | 38 | | /// opt-in rather than the default for that reason, and it is the right setting for any |
| | | 39 | | /// deployment that has confirmed TLS is in place: from that point, a connection string |
| | | 40 | | /// that does not ask for encryption is a misconfiguration rather than a known state, and |
| | | 41 | | /// starting anyway would be starting in the one condition the check exists to prevent. |
| | | 42 | | /// </para> |
| | | 43 | | /// </remarks> |
| | | 44 | | internal static void WarnIfDatabaseConnectionIsNotEncrypted() |
| | | 45 | | { |
| | 973 | 46 | | var connectionString = Environment.GetEnvironmentVariable("HEIMDALL_DATA_CONNECTIONSTRING"); |
| | | 47 | | |
| | 973 | 48 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | | 49 | | { |
| | 0 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 973 | 53 | | var required = string.Equals( |
| | 973 | 54 | | Environment.GetEnvironmentVariable(RequireDatabaseTlsVariable), |
| | 973 | 55 | | "true", |
| | 973 | 56 | | StringComparison.OrdinalIgnoreCase); |
| | | 57 | | |
| | 973 | 58 | | var requiresTls = connectionString |
| | 973 | 59 | | .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| | 4865 | 60 | | .Where(part => part.Contains('=')) |
| | 4865 | 61 | | .Select(part => part.Split('=', 2)) |
| | 4865 | 62 | | .Where(pair => pair[0].Replace(" ", string.Empty) |
| | 4865 | 63 | | .Equals("SSLMode", StringComparison.OrdinalIgnoreCase)) |
| | 973 | 64 | | .Any(pair => pair[1].Trim() is "Require" or "VerifyCA" or "VerifyFull"); |
| | | 65 | | |
| | 973 | 66 | | if (requiresTls) |
| | | 67 | | { |
| | 0 | 68 | | return; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | const string explanation = |
| | | 72 | | "The database connection does not require TLS. Npgsql defaults to SSL Mode=Prefer, " + |
| | | 73 | | "which falls back to an unencrypted connection — every credential and address this " + |
| | | 74 | | "API stores would cross the network in clear text. Set SSL Mode=Require or stronger"; |
| | | 75 | | |
| | 973 | 76 | | if (required) |
| | | 77 | | { |
| | | 78 | | // Opted in, so the operator has said TLS is in place: a connection string that does not |
| | | 79 | | // ask for it is then a misconfiguration to fail on rather than warn about. |
| | 0 | 80 | | throw new InvalidOperationException( |
| | 0 | 81 | | $"{explanation}. {RequireDatabaseTlsVariable} is set, so this is refused rather than warned about."); |
| | | 82 | | } |
| | | 83 | | |
| | 973 | 84 | | Log.Warning("{Explanation}", explanation); |
| | 973 | 85 | | } |
| | | 86 | | } |