Filters out sensitive setting data while exporting
This commit is contained in:
parent
bafb325880
commit
59da2c23a8
@ -148,7 +148,8 @@ public sealed class DataContext : DbContext, IDataContext
|
||||
/// Exports this database to a JSON file.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to the JSON file.</param>
|
||||
public async Task ExportAsync(string path)
|
||||
/// <param name="includeSensitiveData">When false, exclude sensitive data from export.</param>
|
||||
public async Task ExportAsync(string path, bool includeSensitiveData = false)
|
||||
{
|
||||
var jsonSettings = new JsonSerializerOptions
|
||||
{
|
||||
@ -156,11 +157,34 @@ public sealed class DataContext : DbContext, IDataContext
|
||||
Converters = { new JsonUniqueIdConverter() },
|
||||
};
|
||||
|
||||
// Maintained list of sensitive data to be removed from the export:
|
||||
var sensitiveSettingCodes = new HashSet<string>
|
||||
{
|
||||
SettingNames.DEEPL_API_KEY,
|
||||
};
|
||||
|
||||
// A local filter function to remove sensitive data from the export.
|
||||
// Removing just the sensitive values instead of the entire setting.
|
||||
IEnumerable<JsonSetting> FilterSensitiveSettings(IEnumerable<JsonSetting> settings)
|
||||
{
|
||||
foreach (var setting in settings)
|
||||
{
|
||||
if (sensitiveSettingCodes!.Contains(setting.Code))
|
||||
yield return new JsonSetting(setting.UniqueId, setting.Code, string.Empty, 0, false, Guid.Empty);
|
||||
else
|
||||
yield return setting;
|
||||
}
|
||||
}
|
||||
|
||||
await using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await JsonSerializer.SerializeAsync(fileStream,
|
||||
new JsonData
|
||||
{
|
||||
Settings = this.Settings.OrderBy(n => n.UniqueId).Select(n => n.ToJsonSetting()).ToList(),
|
||||
Settings = includeSensitiveData ?
|
||||
// Include all settings, including sensitive data:
|
||||
this.Settings.OrderBy(n => n.UniqueId).Select(n => n.ToJsonSetting()).ToList() :
|
||||
// Exclude sensitive data:
|
||||
FilterSensitiveSettings(this.Settings.OrderBy(n => n.UniqueId).Select(n => n.ToJsonSetting()).AsEnumerable()).ToList(),
|
||||
Sections = this.Sections.OrderBy(n => n.UniqueId).Select(n => n.ToJsonSection()).ToList(),
|
||||
TextElements = this.TextElements.OrderBy(n => n.UniqueId).Select(n => n.ToJsonTextElement()).ToList(),
|
||||
Translations = this.Translations.OrderBy(n => n.UniqueId).Select(n => n.ToJsonTranslation()).ToList(),
|
||||
|
@ -12,5 +12,5 @@ public interface IDataContext
|
||||
|
||||
public DbSet<Translation> Translations { get; set; }
|
||||
|
||||
public Task ExportAsync(string path);
|
||||
public Task ExportAsync(string path, bool includeSensitiveData = false);
|
||||
}
|
@ -26,7 +26,13 @@ public static class ExportProcessor
|
||||
{
|
||||
await EXPORT_SEMAPHORE_EXPORTING.WaitAsync();
|
||||
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
||||
await db.ExportAsync(Environment.ExpandEnvironmentVariables(Path.Join(await AppSettings.GetAutoExportDestinationPath(), await AppSettings.GetAutoExportFilename())));
|
||||
await db.ExportAsync(
|
||||
path: Environment.ExpandEnvironmentVariables(
|
||||
Path.Join(await AppSettings.GetAutoExportDestinationPath(),
|
||||
await AppSettings.GetAutoExportFilename())
|
||||
),
|
||||
includeSensitiveData: await AppSettings.GetAutoExportSensitiveData()
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -2,5 +2,5 @@
|
||||
|
||||
public static class Version
|
||||
{
|
||||
public static string Text => $"v0.8.0 (2023-01-22), .NET {Environment.Version}";
|
||||
public static string Text => $"v0.8.1 (2023-01-24), .NET {Environment.Version}";
|
||||
}
|
Loading…
Reference in New Issue
Block a user