Skip to content

Commit

Permalink
Changes related to suggestions from review
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelvds committed Jan 23, 2024
1 parent a58571d commit f13409a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
31 changes: 18 additions & 13 deletions src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,20 @@ internal void SetAlternateNames(string text)
}
}
}

private void MapFieldsWithAttribute<T>(Func<object?, object?> action)
private void MapFieldsWithAttribute<T>(Func<object?, string, object?> action)
{
var errors = new List<Exception>();

foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
var value = field.GetValue(this);
var name = field.Name;

if (field.GetCustomAttributes(typeof(T)).Any())
if (field.GetCustomAttributes(typeof(T), false).Any())
{
try
{
field.SetValue(this, action(value));
field.SetValue(this, action(value, name));
}
catch (Exception e)
{
Expand All @@ -297,20 +297,26 @@ private void MapFieldsWithAttribute<T>(Func<object?, object?> action)
throw new AggregateException(errors);
}
}

public void Encrypt(IDataProtector dataProtector)
{
MapFieldsWithAttribute<EncryptAttribute>(value =>
MapFieldsWithAttribute<EncryptAttribute>((value, name) =>
{
if (value is null) { return null; }
if (value is null) return null;

return dataProtector.Protect((string)value);
try
{
return dataProtector.Protect((string)value);
}
catch (Exception e)
{
Log.Warning($"Failed to encrypt value of configuration field {name}. Consider using the 'config set' command to set the field's value.", name);
return null;
}
});
}

public void Decrypt(IDataProtector dataProtector)
{
MapFieldsWithAttribute<EncryptAttribute>((value) =>
MapFieldsWithAttribute<EncryptAttribute>((value, name) =>
{
try
{
Expand All @@ -319,7 +325,7 @@ public void Decrypt(IDataProtector dataProtector)
}
catch (Exception e)
{
Log.Warning($"Failed to decrypt field: {e.Message}. Consider using 'config set' to reset.");
Log.Warning("Failed to decrypt value of configuration field {name}. Consider using the 'config set' command to set the fields value.", name);
return null;
}
});
Expand All @@ -335,7 +341,6 @@ public void Decrypt(IDataProtector dataProtector)
return null;
}
}

public record LauncherConfigurationInCashBoxConfiguration
{
[JsonPropertyName("launcher")]
Expand All @@ -356,4 +361,4 @@ public record LauncherConfigurationInCashBoxConfiguration
return configuration;
}
}
}
}
46 changes: 24 additions & 22 deletions src/fiskaltrust.Launcher/Commands/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,38 +289,40 @@ public static async Task<ECDiffieHellman> LoadCurve(Guid cashboxId, string acces
var dataProtector = DataProtectionExtensions.Create(accessToken, useFallback: useFallback).CreateProtector(CashBoxConfigurationExt.DATA_PROTECTION_DATA_PURPOSE);
var clientEcdhPath = Path.Combine(serviceFolder, $"client-{cashboxId}.ecdh");

try
if (File.Exists(clientEcdhPath))
{
if (File.Exists(clientEcdhPath))
try
{
var protectedData = await File.ReadAllTextAsync(clientEcdhPath);
try
{
return ECDiffieHellmanExt.Deserialize(dataProtector.Unprotect(protectedData));
}
catch (Exception ex)
{
Log.Warning($"Could not decrypt ECDH curve, regenerating a new one. Error: {ex.Message}");
// Handle failed decryption here if necessary, e.g., by deleting the existing file
// File.Delete(clientEcdhPath);
}
return ECDiffieHellmanExt.Deserialize(dataProtector.Unprotect(await File.ReadAllTextAsync(clientEcdhPath)));
}
catch (Exception e)
{
Log.Warning($"Error loading or decrypting ECDH curve: {e.Message}. Regenerating new curve.");
}
}
catch (Exception ex)

// Handling offline client ECDH path
const string offlineClientEcdhPath = "/client.ecdh";
if (!dryRun && useOffline && File.Exists(offlineClientEcdhPath))
{
Log.Warning($"Error reading ECDH curve from file: {ex.Message}. Regenerating new curve.");
var clientEcdh = ECDiffieHellmanExt.Deserialize(await File.ReadAllTextAsync(offlineClientEcdhPath));
try
{
File.Delete(offlineClientEcdhPath);
}
catch { }

return clientEcdh;
}

// Rest of the method for regenerating curve if not loaded or in case of error
ECDiffieHellman clientEcdh = CashboxConfigEncryption.CreateCurve();
// Regenerating the curve if it's not loaded or in case of an error
var newClientEcdh = CashboxConfigEncryption.CreateCurve();
if (!dryRun)
{
var serializedCurve = clientEcdh.Serialize();
var protectedCurve = dataProtector.Protect(serializedCurve);
await File.WriteAllTextAsync(clientEcdhPath, protectedCurve);
await File.WriteAllTextAsync(clientEcdhPath, dataProtector.Protect(newClientEcdh.Serialize()));
}

return clientEcdh;
return newClientEcdh;
}
}
}
}
2 changes: 1 addition & 1 deletion src/fiskaltrust.Launcher/Commands/DoctorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro

if (clientEcdh is null)
{
Log.Warning("Failed to load ECDH curve. Unable to proceed with Doctor checks.");
Log.Warning("Failed to load ECDH curve. Skipping some related doctor checks.");
}
else
{
Expand Down

0 comments on commit f13409a

Please sign in to comment.