From 7be5732a3a6679120b0f01bd1eb1207194f57f5e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 20 Jan 2024 15:18:26 -0800 Subject: [PATCH] New: Option to disable Email encryption Closes #6380 --- .../Migration/201_email_encryptionFixture.cs | 142 ++++++++++++++++++ .../EmailSettingsValidatorFixture.cs | 2 +- .../Migration/201_email_encryption.cs | 50 ++++++ src/NzbDrone.Core/Localization/Core/en.json | 4 +- .../Notifications/Email/Email.cs | 19 +-- .../Notifications/Email/EmailSettings.cs | 11 +- 6 files changed, 212 insertions(+), 16 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs new file mode 100644 index 000000000..7a6cee1ca --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs @@ -0,0 +1,142 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Notifications.Email; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class email_encryptionFixture : MigrationTest + { + [Test] + public void should_convert_do_not_require_encryption_to_auto() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Notifications").Row(new + { + OnGrab = true, + OnDownload = true, + OnUpgrade = true, + OnHealthIssue = true, + IncludeHealthWarnings = true, + OnRename = true, + Name = "Mail Sonarr", + Implementation = "Email", + Tags = "[]", + Settings = new EmailSettings200 + { + Server = "smtp.gmail.com", + Port = 563, + To = new List { "dont@email.me" }, + RequireEncryption = false + }.ToJson(), + ConfigContract = "EmailSettings" + }); + }); + + var items = db.Query("SELECT * FROM \"Notifications\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Email"); + items.First().ConfigContract.Should().Be("EmailSettings"); + items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Preferred); + } + + [Test] + public void should_convert_require_encryption_to_always() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Notifications").Row(new + { + OnGrab = true, + OnDownload = true, + OnUpgrade = true, + OnHealthIssue = true, + IncludeHealthWarnings = true, + OnRename = true, + Name = "Mail Sonarr", + Implementation = "Email", + Tags = "[]", + Settings = new EmailSettings200 + { + Server = "smtp.gmail.com", + Port = 563, + To = new List { "dont@email.me" }, + RequireEncryption = true + }.ToJson(), + ConfigContract = "EmailSettings" + }); + }); + + var items = db.Query("SELECT * FROM \"Notifications\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Email"); + items.First().ConfigContract.Should().Be("EmailSettings"); + items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Always); + } + } + + public class NotificationDefinition201 + { + public int Id { get; set; } + public string Implementation { get; set; } + public string ConfigContract { get; set; } + public EmailSettings201 Settings { get; set; } + public string Name { get; set; } + public bool OnGrab { get; set; } + public bool OnDownload { get; set; } + public bool OnUpgrade { get; set; } + public bool OnRename { get; set; } + public bool OnSeriesDelete { get; set; } + public bool OnEpisodeFileDelete { get; set; } + public bool OnEpisodeFileDeleteForUpgrade { get; set; } + public bool OnHealthIssue { get; set; } + public bool OnApplicationUpdate { get; set; } + public bool OnManualInteractionRequired { get; set; } + public bool OnSeriesAdd { get; set; } + public bool OnHealthRestored { get; set; } + public bool SupportsOnGrab { get; set; } + public bool SupportsOnDownload { get; set; } + public bool SupportsOnUpgrade { get; set; } + public bool SupportsOnRename { get; set; } + public bool SupportsOnSeriesDelete { get; set; } + public bool SupportsOnEpisodeFileDelete { get; set; } + public bool SupportsOnEpisodeFileDeleteForUpgrade { get; set; } + public bool SupportsOnHealthIssue { get; set; } + public bool IncludeHealthWarnings { get; set; } + public List Tags { get; set; } + } + + public class EmailSettings200 + { + public string Server { get; set; } + public int Port { get; set; } + public bool RequireEncryption { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public IEnumerable To { get; set; } + public IEnumerable Cc { get; set; } + public IEnumerable Bcc { get; set; } + } + + public class EmailSettings201 + { + public string Server { get; set; } + public int Port { get; set; } + public int UseEncryption { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public IEnumerable To { get; set; } + public IEnumerable Cc { get; set; } + public IEnumerable Bcc { get; set; } + } +} diff --git a/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs index 35aa38fdf..68d5d5e2f 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs @@ -25,7 +25,7 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests _emailSettings = Builder.CreateNew() .With(s => s.Server = "someserver") .With(s => s.Port = 567) - .With(s => s.RequireEncryption = true) + .With(s => s.UseEncryption = (int)EmailEncryptionType.Always) .With(s => s.From = "dont@email.me") .With(s => s.To = new string[] { "dont@email.me" }) .Build(); diff --git a/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs b/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs new file mode 100644 index 000000000..93ee7a962 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Data; +using Dapper; +using FluentMigrator; +using Newtonsoft.Json.Linq; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(201)] + public class email_encryption : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(ChangeEncryption); + } + + private void ChangeEncryption(IDbConnection conn, IDbTransaction tran) + { + var updated = new List(); + using (var getEmailCmd = conn.CreateCommand()) + { + getEmailCmd.Transaction = tran; + getEmailCmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Notifications\" WHERE \"Implementation\" = 'Email'"; + + using (var reader = getEmailCmd.ExecuteReader()) + { + while (reader.Read()) + { + var id = reader.GetInt32(0); + var settings = Json.Deserialize(reader.GetString(1)); + + settings["useEncryption"] = settings["requireEncryption"].ToObject() ? 1 : 0; + settings["requireEncryption"] = null; + + updated.Add(new + { + Settings = settings.ToJson(), + Id = id + }); + } + } + } + + var updateSql = $"UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id"; + conn.Execute(updateSql, updated, transaction: tran); + } + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index c437f4973..3a0b392e7 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -1258,10 +1258,10 @@ "NotificationsEmailSettingsName": "Email", "NotificationsEmailSettingsRecipientAddress": "Recipient Address(es)", "NotificationsEmailSettingsRecipientAddressHelpText": "Comma separated list of email recipients", - "NotificationsEmailSettingsRequireEncryption": "Require Encryption", - "NotificationsEmailSettingsRequireEncryptionHelpText": "Require SSL (Port 465 only) or StartTLS (any other port)", "NotificationsEmailSettingsServer": "Server", "NotificationsEmailSettingsServerHelpText": "Hostname or IP of Email server", + "NotificationsEmailSettingsUseEncryption": "Use Encryption", + "NotificationsEmailSettingsUseEncryptionHelpText": "Whether to prefer using encryption if configured on the server, to always use encryption via SSL (Port 465 only) or StartTLS (any other port) or to never use encryption", "NotificationsEmbySettingsSendNotifications": "Send Notifications", "NotificationsEmbySettingsSendNotificationsHelpText": "Have MediaBrowser send notifications to configured providers", "NotificationsEmbySettingsUpdateLibraryHelpText": "Update Library on Import, Rename, or Delete?", diff --git a/src/NzbDrone.Core/Notifications/Email/Email.cs b/src/NzbDrone.Core/Notifications/Email/Email.cs index f8d3646ad..941754c34 100644 --- a/src/NzbDrone.Core/Notifications/Email/Email.cs +++ b/src/NzbDrone.Core/Notifications/Email/Email.cs @@ -134,19 +134,16 @@ namespace NzbDrone.Core.Notifications.Email using var client = new SmtpClient(); client.Timeout = 10000; - var serverOption = SecureSocketOptions.Auto; + var useEncyption = (EmailEncryptionType)settings.UseEncryption; - if (settings.RequireEncryption) + var serverOption = useEncyption switch { - if (settings.Port == 465) - { - serverOption = SecureSocketOptions.SslOnConnect; - } - else - { - serverOption = SecureSocketOptions.StartTls; - } - } + EmailEncryptionType.Always => settings.Port == 465 + ? SecureSocketOptions.SslOnConnect + : SecureSocketOptions.StartTls, + EmailEncryptionType.Never => SecureSocketOptions.None, + _ => SecureSocketOptions.Auto + }; client.ServerCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError; diff --git a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs index 69ffaa60f..f6b23caad 100644 --- a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs +++ b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs @@ -45,8 +45,8 @@ namespace NzbDrone.Core.Notifications.Email [FieldDefinition(1, Label = "Port")] public int Port { get; set; } - [FieldDefinition(2, Label = "NotificationsEmailSettingsRequireEncryption", HelpText = "NotificationsEmailSettingsRequireEncryptionHelpText", Type = FieldType.Checkbox)] - public bool RequireEncryption { get; set; } + [FieldDefinition(2, Label = "NotificationsEmailSettingsUseEncryption", HelpText = "NotificationsEmailSettingsUseEncryptionHelpText", Type = FieldType.Select, SelectOptions = typeof(EmailEncryptionType))] + public int UseEncryption { get; set; } [FieldDefinition(3, Label = "Username", Privacy = PrivacyLevel.UserName)] public string Username { get; set; } @@ -71,4 +71,11 @@ namespace NzbDrone.Core.Notifications.Email return new NzbDroneValidationResult(Validator.Validate(this)); } } + + public enum EmailEncryptionType + { + Preferred = 0, + Always = 1, + Never = 2 + } }