From a3349e23c96223a18280357509f9e03e103c4281 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Mon, 7 Jun 2021 16:12:20 +0200 Subject: [PATCH] Fix IKeyManager Now correctly delete key folder while avoiding issues --- .../Services/ApplicationsManager.cs | 6 +++--- Akari.Prototype.Server/Services/IKeyManager.cs | 3 ++- Akari.Prototype.Server/Services/KeyManager.cs | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Akari.Prototype.Server/Services/ApplicationsManager.cs b/Akari.Prototype.Server/Services/ApplicationsManager.cs index 4c44c18..d1cbb08 100644 --- a/Akari.Prototype.Server/Services/ApplicationsManager.cs +++ b/Akari.Prototype.Server/Services/ApplicationsManager.cs @@ -110,7 +110,7 @@ namespace Akari.Prototype.Server.Services public bool Remove(string applicationName) { - if (!_applications.ContainsKey(applicationName)) + if (!_applications.TryGetValue(applicationName, out var application)) { _logger.LogDebug($"Can't remove non existing application: {applicationName}"); @@ -118,9 +118,9 @@ namespace Akari.Prototype.Server.Services } // Clear keys - _keyManager.Clear(applicationName); + _keyManager.Clear(application); - _applications.Remove(applicationName); + _applications.Remove(application.Name); SaveApplications(); diff --git a/Akari.Prototype.Server/Services/IKeyManager.cs b/Akari.Prototype.Server/Services/IKeyManager.cs index fbe4961..e5f82c5 100644 --- a/Akari.Prototype.Server/Services/IKeyManager.cs +++ b/Akari.Prototype.Server/Services/IKeyManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Threading.Tasks; +using Akari.Prototype.Server.Models; namespace Akari.Prototype.Server.Services { @@ -10,7 +11,7 @@ namespace Akari.Prototype.Server.Services { public bool AddFingerprint(string applicationName, string fingerprintName, AesGcm fingerprintKey); - public void Clear(string applicationName); + public void Clear(Application application); public bool Create(string applicationName); diff --git a/Akari.Prototype.Server/Services/KeyManager.cs b/Akari.Prototype.Server/Services/KeyManager.cs index dc04e39..99dd3ca 100644 --- a/Akari.Prototype.Server/Services/KeyManager.cs +++ b/Akari.Prototype.Server/Services/KeyManager.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Security.Cryptography; using System.Threading.Tasks; +using Akari.Prototype.Server.Models; using Akari.Prototype.Server.Utils; using Microsoft.Extensions.Logging; @@ -58,12 +59,19 @@ namespace Akari.Prototype.Server.Services return true; } - public void Clear(string applicationName) + public void Clear(Application application) { - _logger.LogDebug($"Deleted keys for {applicationName}"); + // Do it safely to avoid deleting unwanted files + File.Delete(GetMasterKeyPath(application.Name)); - _logger.LogDebug($"Going to delete {GetKeyDirectoryPath(applicationName)}"); - //Directory.Delete(GetKeyDirectoryPath(applicationName)); + foreach (var fingerprint in application.Fingerprints) + { + File.Delete(GetKeyPath(application.Name, fingerprint)); + } + + Directory.Delete(GetKeyDirectoryPath(application.Name), recursive: false); + + _logger.LogDebug($"Deleted keys for {application.Name}"); } public bool Create(string applicationName)