Skip to content

Commit 28c9ac1

Browse files
committed
Encryption updated and added
1 parent e4db961 commit 28c9ac1

File tree

23 files changed

+370
-6
lines changed

23 files changed

+370
-6
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,6 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
# Ecryption key
353+
.salt

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Bootstrap/PowerCommandsConfiguration.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ configuration:
2020
name: "Azure Key Vault"
2121
component: "PainKiller.AzureKeyVault.dll"
2222
checksum: "e6d2d6cb64863e9dc68a9602f83bcfde"
23+
myExampleCommand:
24+
name: "My Example Command"
25+
component: "PainKiller.PowerCommands.MyExampleCommands.dll"
26+
checksum: "e6d2d6cb64863e9dc68a9602f83bcfde"

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Configuration/ConfigurationManager.cs

+44
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,48 @@ public static class ConfigurationManager
1717
.Build();
1818
return deserializer.Deserialize<YamlContainer<T>>(yamlContent) ?? new YamlContainer<T>();
1919
}
20+
21+
public static void Update<T>(T configuration, string inputFileName = "") where T : new()
22+
{
23+
var fileName = string.IsNullOrEmpty(inputFileName) ? $"{typeof(T).Name}.yaml".GetSafePathRegardlessHowApplicationStarted() : inputFileName;
24+
25+
var yamlContainer = new YamlContainer<T> { Configuration = configuration, Version = "1.0" };
26+
var serializer = new SerializerBuilder()
27+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
28+
.Build();
29+
var yamlData = serializer.Serialize(yamlContainer);
30+
File.WriteAllText(fileName, yamlData);
31+
}
32+
33+
public static string CreateConfiguration<T>(T item, string fileName = "") where T : new()
34+
{
35+
if (item is not null)
36+
{
37+
var yamlContainer = new YamlContainer<T> {Configuration = item, Version = "1.0"};
38+
var serializer = new SerializerBuilder()
39+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
40+
.Build();
41+
var yamlData = serializer.Serialize(yamlContainer);
42+
return yamlData;
43+
}
44+
return "--- item is null and can not be serialized ---";
45+
}
46+
47+
public static YamlContainer<T> GetAppDataConfiguration<T>(T defaultIfMissing, string inputFileName = "") where T : new()
48+
{
49+
var directory = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{nameof(PowerCommands)}";
50+
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
51+
var fileName = Path.Combine(directory, inputFileName);
52+
if (!File.Exists(fileName))
53+
{
54+
var yaml = CreateConfiguration(defaultIfMissing, fileName);
55+
File.WriteAllText(fileName, yaml);
56+
}
57+
58+
var yamlContent = File.ReadAllText(fileName);
59+
var deserializer = new DeserializerBuilder()
60+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
61+
.Build();
62+
return deserializer.Deserialize<YamlContainer<T>>(yamlContent) ?? new YamlContainer<T>();
63+
}
2064
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace PainKiller.PowerCommands.Configuration.DomainObjects;
2+
3+
public class EncryptionConfiguration
4+
{
5+
public string ShareSecretEnvironmentKey { get; set; }
6+
public string ShareSecretSalt { get; set; }
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PainKiller.PowerCommands.Configuration.DomainObjects;
2+
3+
public class SecurityConfiguration
4+
{
5+
public EncryptionConfiguration Encryption { get; set; }
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PainKiller.PowerCommands.Configuration.Enums;
2+
3+
public enum ConfigurationFiles
4+
{
5+
Security
6+
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Configuration/PainKiller.PowerCommands.Configuration.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<Folder Include="Extensions\" />
15+
<Folder Include="Enums\" />
1516
</ItemGroup>
1617

1718
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Reflection;
2+
using PainKiller.PowerCommands.Shared.Contracts;
3+
using PainKiller.PowerCommands.Shared.DomainObjects.Core;
4+
5+
namespace PainKiller.PowerCommands.Core.BaseClasses;
6+
7+
public abstract class CommandBase : IConsoleCommand
8+
{
9+
10+
protected CommandBase(string identifier)
11+
{
12+
Identifier = identifier;
13+
}
14+
public string Identifier { get; }
15+
16+
17+
18+
public virtual RunResult Run(string input)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
public virtual Task<RunResult> RunAsync(string input)
23+
{
24+
throw new NotImplementedException();
25+
}
26+
protected string RootPath()
27+
{
28+
var retVal = Assembly.GetEntryAssembly()?.Location ?? "";
29+
retVal = retVal.Replace($"{Assembly.GetEntryAssembly()?.GetName(false).Name}.dll", "");
30+
return retVal;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using PainKiller.PowerCommands.Configuration;
2+
using PainKiller.PowerCommands.Configuration.DomainObjects;
3+
using PainKiller.PowerCommands.Configuration.Enums;
4+
using PainKiller.PowerCommands.Security.Contracts;
5+
using PainKiller.PowerCommands.Security.Managers;
6+
7+
namespace PainKiller.PowerCommands.Core.BaseClasses;
8+
9+
public class EncryptionCommandBase : CommandBase
10+
{
11+
public EncryptionCommandBase(string identifier) : base(identifier)
12+
{
13+
SecurityConfiguration1 = ConfigurationManager.GetAppDataConfiguration(new SecurityConfiguration { Encryption = new EncryptionConfiguration { ShareSecretEnvironmentKey = nameof(IEncryptionManager), ShareSecretSalt = "-- salt --" } }, $"{ConfigurationFiles.Security}.yaml").Configuration;
14+
Salt = SecurityConfiguration1.Encryption.ShareSecretSalt;
15+
SharedSecret = Environment.GetEnvironmentVariable(SecurityConfiguration1.Encryption.ShareSecretEnvironmentKey, EnvironmentVariableTarget.User) ?? string.Empty;
16+
}
17+
protected SecurityConfiguration SecurityConfiguration1 { get; }
18+
protected string SharedSecret { get; }
19+
protected string Salt { get; }
20+
21+
protected string EncryptString(string plainText)
22+
{
23+
var encryptionManager = new AESEncryptionManager(Salt);
24+
var retVal = encryptionManager.EncryptString(plainText, SharedSecret);
25+
return retVal;
26+
}
27+
28+
protected string DecryptString(string plainText)
29+
{
30+
var encryptionManager = new AESEncryptionManager(Salt);
31+
var retVal = encryptionManager.DecryptString(plainText, SharedSecret);
32+
return retVal;
33+
}
34+
protected string GetEnvironmentVariable(string variableName, bool decrypt = false)
35+
{
36+
var retVal = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.User) ?? "";
37+
if (decrypt) retVal = DecryptString(retVal);
38+
return retVal;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using PainKiller.PowerCommands.Core.BaseClasses;
2+
using PainKiller.PowerCommands.Shared.DomainObjects.Core;
3+
using PainKiller.PowerCommands.Shared.Enums;
4+
5+
namespace PainKiller.PowerCommands.Core.Commands;
6+
7+
public class PluginCommand : CommandBase
8+
{
9+
public PluginCommand(string identifier) : base(identifier) { }
10+
11+
public override RunResult Run(string input)
12+
{
13+
//Hur kommer man åt PowerCommandsConfiguration här???
14+
return new RunResult {Status = RunResultStatus.Ok, Output = ""};
15+
}
16+
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Core/Managers/PluginManager.cs

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using PainKiller.PowerCommands.Core.Extensions;
1+
using System.Text;
2+
using PainKiller.PowerCommands.Configuration;
3+
using PainKiller.PowerCommands.Core.Extensions;
24
using PainKiller.PowerCommands.Security.DomainObjects;
35
using PainKiller.PowerCommands.Security.Extensions;
46
using PainKiller.PowerCommands.Shared.DomainObjects.Configuration;
57

68
namespace PainKiller.PowerCommands.Core.Managers;
79

8-
public class PluginManager<TConfiguration> where TConfiguration : BaseCommandsConfiguration
10+
public class PluginManager<TConfiguration> where TConfiguration : BasicCommandsConfiguration
911
{
1012
private readonly TConfiguration _configuration;
1113

@@ -36,8 +38,30 @@ public bool ValidateConfigurationWithPlugins()
3638
var fileCheckSum = new FileChecksum(instance.Component);
3739
var validateCheckSum = fileCheckSum.CompareFileChecksum(instance.Checksum);
3840
if(!validateCheckSum) retVal = false;
39-
if(_configuration.ShowDiagnosticInformation) Console.WriteLine($"{instance?.Name ?? "null"} Checksum {fileCheckSum.Mde5Hash} {validateCheckSum}");
41+
if(_configuration.ShowDiagnosticInformation) Console.WriteLine($"{instance.Name ?? "null"} Checksum {fileCheckSum.Mde5Hash} {validateCheckSum}");
4042
}
4143
return retVal;
4244
}
45+
46+
protected string AutofixConfigurationPlugins<T>(T configuration) where T : BasicCommandsConfiguration, new()
47+
{
48+
var retVal = new StringBuilder();
49+
var components = typeof(TConfiguration).GetPropertiesOfT<BaseComponentConfiguration>();
50+
51+
foreach (var propertyInfo in components)
52+
{
53+
if (propertyInfo.GetValue(_configuration) is not BaseComponentConfiguration instance) continue;
54+
var fileCheckSum = new FileChecksum(instance.Component);
55+
var validateCheckSum = fileCheckSum.CompareFileChecksum(instance.Checksum);
56+
if (_configuration.ShowDiagnosticInformation) Console.WriteLine($"{instance.Name ?? "null"} Checksum {fileCheckSum.Mde5Hash} {validateCheckSum}");
57+
if (!validateCheckSum)
58+
{
59+
propertyInfo.SetValue(instance, fileCheckSum.Mde5Hash);
60+
ConfigurationManager.Update(configuration);
61+
retVal.AppendLine($"{instance.Name ?? "null"} Checksum {fileCheckSum.Mde5Hash} {validateCheckSum} FIXED");
62+
}
63+
}
64+
return retVal.ToString();
65+
}
66+
4367
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Core/PainKiller.PowerCommands.Core.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
<ItemGroup>
1010
<Folder Include="Extensions\" />
1111
<Folder Include="Managers\" />
12+
<Folder Include="BaseClasses\" />
13+
<Folder Include="Commands\" />
1214
</ItemGroup>
1315

1416
<ItemGroup>
17+
<ProjectReference Include="..\PainKiller.PowerCommands.Configuration\PainKiller.PowerCommands.Configuration.csproj" />
1518
<ProjectReference Include="..\PainKiller.PowerCommands.Security\PainKiller.PowerCommands.Security.csproj" />
1619
<ProjectReference Include="..\PainKiller.PowerCommands.Shared\PainKiller.PowerCommands.Shared.csproj" />
1720
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using PainKiller.PowerCommands.Core.BaseClasses;
2+
using PainKiller.PowerCommands.Shared.DomainObjects.Core;
3+
using PainKiller.PowerCommands.Shared.Enums;
4+
5+
namespace PainKiller.PowerCommands.MyExampleCommands.Commands;
6+
7+
public class EncryptionCommand : EncryptionCommandBase
8+
{
9+
public EncryptionCommand(string identifier) : base(identifier) { }
10+
11+
public override RunResult Run(string input)
12+
{
13+
var decrypt = DecryptString(input);
14+
Console.WriteLine(decrypt);
15+
return new RunResult {Status = RunResultStatus.Ok};
16+
}
17+
18+
19+
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.MyExampleCommands/Configuration/PowerCommandsConfiguration.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PainKiller.PowerCommands.MyExampleCommands.Configuration;
66

7-
public class PowerCommandsConfiguration : BaseCommandsConfiguration
7+
public class PowerCommandsConfiguration : BasicCommandsConfiguration
88
{
99
private static PowerCommandsConfiguration? _instance;
1010
public static PowerCommandsConfiguration? Instance
@@ -18,5 +18,6 @@ public static PowerCommandsConfiguration? Instance
1818
}
1919
}
2020
public KeyVaultConfig KeyVault { get; set; } = new();
21+
public BaseComponentConfiguration MyExampleCommand { get; set; } = new();
2122

2223
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.MyExampleCommands/PainKiller.PowerCommands.MyExampleCommands.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<ProjectReference Include="..\PainKiller.AzureKeyVault\PainKiller.AzureKeyVault.csproj" />
1515
<ProjectReference Include="..\PainKiller.PowerCommands.Configuration\PainKiller.PowerCommands.Configuration.csproj" />
16+
<ProjectReference Include="..\PainKiller.PowerCommands.Core\PainKiller.PowerCommands.Core.csproj" />
1617
<ProjectReference Include="..\PainKiller.PowerCommands.Shared\PainKiller.PowerCommands.Shared.csproj" />
1718
</ItemGroup>
1819

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
using PainKiller.PowerCommands.Bootstrap;
2+
using PainKiller.PowerCommands.MyExampleCommands.Commands;
3+
using PainKiller.PowerCommands.Shared.DomainObjects.Core;
4+
using PainKiller.PowerCommands.Shared.Enums;
25

36
try
47
{
58
Startup.Initialize();
9+
var command = new EncryptionCommand("babar");
10+
11+
var runCommand = new RunResult{Status = RunResultStatus.Ok};
12+
while (runCommand.Status != RunResultStatus.Quit) runCommand = command.Run(Console.ReadLine());
13+
614
}
715
catch { Console.WriteLine("Critical error, program could not start, check the log for more details"); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PainKiller.PowerCommands.Security.Contracts;
2+
3+
public interface IEncryptionManager
4+
{
5+
string EncryptString(string plainText, string sharedSecret);
6+
string DecryptString(string cipherText, string sharedSecret);
7+
string GetRandomSalt();
8+
}

0 commit comments

Comments
 (0)