From 7d296719769d02fcd5c145d2f30aebfc43bc6467 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 26 Dec 2024 16:24:28 +0800 Subject: [PATCH 1/5] Return setting value if value is not encrypted --- .../Volo/Abp/Settings/SettingEncryptionService.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs index 45a3362d30..6fffb2c93c 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs @@ -38,9 +38,15 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep { return StringEncryptionService.Decrypt(encryptedValue); } + catch (FormatException) + { + // not an encrypted value, return the original value + return encryptedValue; + } catch (Exception e) { Logger.LogException(e); + return string.Empty; } } From 0d10a22181f2ad4e4fc0ac56170e9bc50d720b60 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 26 Dec 2024 16:52:12 +0800 Subject: [PATCH 2/5] Add ReturnOrginalValueIfDecryptFailed to --- .../Volo/Abp/Settings/AbpSettingOptions.cs | 3 +++ .../Abp/Settings/SettingEncryptionService.cs | 16 ++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs index c8a06736b6..1d2f775e9d 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs @@ -11,10 +11,13 @@ public class AbpSettingOptions public HashSet DeletedSettings { get; } + public bool ReturnOrginalValueIfDecryptFailed { get; set; } + public AbpSettingOptions() { DefinitionProviders = new TypeList(); ValueProviders = new TypeList(); DeletedSettings = new HashSet(); + ReturnOrginalValueIfDecryptFailed = true; } } diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs index 6fffb2c93c..2076785f74 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs @@ -1,6 +1,7 @@ using System; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Encryption; @@ -10,10 +11,12 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep { protected IStringEncryptionService StringEncryptionService { get; } public ILogger Logger { get; set; } + protected IOptions Options { get; } - public SettingEncryptionService(IStringEncryptionService stringEncryptionService) + public SettingEncryptionService(IStringEncryptionService stringEncryptionService, IOptions options) { StringEncryptionService = stringEncryptionService; + Options = options; Logger = NullLogger.Instance; } @@ -38,13 +41,14 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep { return StringEncryptionService.Decrypt(encryptedValue); } - catch (FormatException) - { - // not an encrypted value, return the original value - return encryptedValue; - } catch (Exception e) { + if (Options.Value.ReturnOrginalValueIfDecryptFailed) + { + Logger.LogWarning(e, "Failed to decrypt the setting: {0}. Returning the original value...", settingDefinition.Name); + return encryptedValue; + } + Logger.LogException(e); return string.Empty; From a5df4855675c02b051cdfc017b5d8fcb1fbd4d4b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 26 Dec 2024 16:55:29 +0800 Subject: [PATCH 3/5] Update AbpSettingOptions.cs --- .../Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs index 1d2f775e9d..f4cba00fe9 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs @@ -11,6 +11,9 @@ public class AbpSettingOptions public HashSet DeletedSettings { get; } + /// + /// If set to true, returns the original value if the decryption fails. Otherwise, returns null. + /// public bool ReturnOrginalValueIfDecryptFailed { get; set; } public AbpSettingOptions() From b3ae5dd86cd3273180902389025286b9fb8ee29d Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 26 Dec 2024 16:56:15 +0800 Subject: [PATCH 4/5] Update AbpSettingOptions.cs --- .../Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs index f4cba00fe9..25a8fc254e 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs @@ -12,7 +12,7 @@ public class AbpSettingOptions public HashSet DeletedSettings { get; } /// - /// If set to true, returns the original value if the decryption fails. Otherwise, returns null. + /// If set to true, returns the original value if the decryption fails. Otherwise, returns empty. /// public bool ReturnOrginalValueIfDecryptFailed { get; set; } From 457cddd6cf399b6b633702e33851e58b3b822c3b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 26 Dec 2024 17:20:16 +0800 Subject: [PATCH 5/5] update comment --- .../Volo/Abp/Settings/AbpSettingOptions.cs | 7 ++++--- .../Volo/Abp/Settings/SettingEncryptionService.cs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs index 25a8fc254e..e4bbb4fe00 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs @@ -12,15 +12,16 @@ public class AbpSettingOptions public HashSet DeletedSettings { get; } /// - /// If set to true, returns the original value if the decryption fails. Otherwise, returns empty. + /// Default: true. + /// This is useful when you change of an existing setting definition to true and don't want to lose the original value. /// - public bool ReturnOrginalValueIfDecryptFailed { get; set; } + public bool ReturnOriginalValueIfDecryptFailed { get; set; } public AbpSettingOptions() { DefinitionProviders = new TypeList(); ValueProviders = new TypeList(); DeletedSettings = new HashSet(); - ReturnOrginalValueIfDecryptFailed = true; + ReturnOriginalValueIfDecryptFailed = true; } } diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs index 2076785f74..2af6b7b4af 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs @@ -43,7 +43,7 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep } catch (Exception e) { - if (Options.Value.ReturnOrginalValueIfDecryptFailed) + if (Options.Value.ReturnOriginalValueIfDecryptFailed) { Logger.LogWarning(e, "Failed to decrypt the setting: {0}. Returning the original value...", settingDefinition.Name); return encryptedValue;