Skip to content

Commit e6e4602

Browse files
authored
Merge pull request #21744 from abpframework/secretvalues
Add an option to return value if decrypt failed
2 parents 89bc837 + 457cddd commit e6e4602

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

framework/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ public class AbpSettingOptions
1111

1212
public HashSet<string> DeletedSettings { get; }
1313

14+
/// <summary>
15+
/// Default: true.
16+
/// This is useful when you change <see cref="SettingDefinition.IsEncrypted"/> of an existing setting definition to true and don't want to lose the original value.
17+
/// </summary>
18+
public bool ReturnOriginalValueIfDecryptFailed { get; set; }
19+
1420
public AbpSettingOptions()
1521
{
1622
DefinitionProviders = new TypeList<ISettingDefinitionProvider>();
1723
ValueProviders = new TypeList<ISettingValueProvider>();
1824
DeletedSettings = new HashSet<string>();
25+
ReturnOriginalValueIfDecryptFailed = true;
1926
}
2027
}

framework/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingEncryptionService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Microsoft.Extensions.Logging;
33
using Microsoft.Extensions.Logging.Abstractions;
4+
using Microsoft.Extensions.Options;
45
using Volo.Abp.DependencyInjection;
56
using Volo.Abp.Security.Encryption;
67

@@ -10,10 +11,12 @@ public class SettingEncryptionService : ISettingEncryptionService, ITransientDep
1011
{
1112
protected IStringEncryptionService StringEncryptionService { get; }
1213
public ILogger<SettingEncryptionService> Logger { get; set; }
14+
protected IOptions<AbpSettingOptions> Options { get; }
1315

14-
public SettingEncryptionService(IStringEncryptionService stringEncryptionService)
16+
public SettingEncryptionService(IStringEncryptionService stringEncryptionService, IOptions<AbpSettingOptions> options)
1517
{
1618
StringEncryptionService = stringEncryptionService;
19+
Options = options;
1720
Logger = NullLogger<SettingEncryptionService>.Instance;
1821
}
1922

@@ -40,7 +43,14 @@ public SettingEncryptionService(IStringEncryptionService stringEncryptionService
4043
}
4144
catch (Exception e)
4245
{
46+
if (Options.Value.ReturnOriginalValueIfDecryptFailed)
47+
{
48+
Logger.LogWarning(e, "Failed to decrypt the setting: {0}. Returning the original value...", settingDefinition.Name);
49+
return encryptedValue;
50+
}
51+
4352
Logger.LogException(e);
53+
4454
return string.Empty;
4555
}
4656
}

0 commit comments

Comments
 (0)