Filters out sensitive setting data while exporting
This commit is contained in:
4 files changed
+36
-6
No files matched your search
@@ -143,24 +143,48 @@ public sealed class DataContext : DbContext, IDataContext
|
||||
bool TranslateManual,
|
||||
JsonUniqueId TextElementUniqueId
|
||||
);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
{
|
||||
WriteIndented = true,
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user