Skip to content

Commit ef4569b

Browse files
author
Harri Klingsten
committed
Finally fixed the Secret initialization problem! Added $ROAMING$ functionallity for bookmarks.
1 parent 70493cb commit ef4569b

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Configuration/ConfigurationService.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,12 @@ private ConfigurationService(){}
6464
/// <param name="defaultIfMissing"></param>
6565
/// <param name="inputFileName"></param>
6666
/// <returns></returns>
67-
public YamlContainer<T> GetAppDataConfiguration<T>(T defaultIfMissing, string inputFileName = "") where T : new()
67+
public YamlContainer<T> GetAppDataConfiguration<T>(string inputFileName = "") where T : new()
6868
{
6969
var directory = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\{nameof(PowerCommands)}";
7070
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
7171
var fileName = Path.Combine(directory, inputFileName);
72-
if (!File.Exists(fileName))
73-
{
74-
var yaml = CreateContent(defaultIfMissing);
75-
File.WriteAllText(fileName, yaml);
76-
}
72+
if (!File.Exists(fileName)) throw new FileNotFoundException($"Could not find file {fileName}");
7773
var yamlContent = File.ReadAllText(fileName);
7874
var deserializer = new DeserializerBuilder()
7975
.WithNamingConvention(CamelCaseNamingConvention.Instance)

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Configuration/Contracts/IConfigurationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public interface IConfigurationService
77
YamlContainer<T> Get<T>(string inputFileName = "") where T : new();
88
string SaveChanges<T>(T configuration, string inputFileName = "") where T : new();
99
void Create<T>(T configuration, string fullFileName) where T : new();
10-
YamlContainer<T> GetAppDataConfiguration<T>(T defaultIfMissing, string inputFileName = "") where T : new();
10+
YamlContainer<T> GetAppDataConfiguration<T>(string inputFileName = "") where T : new();
1111
}

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Core/Commands/CdCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[PowerCommandTest( tests: " ")]
44
[PowerCommandDesign(description: "Change or view the current working directory",
5-
options: "bookmark",
5+
options: "bookmark|roaming",
66
disableProxyOutput: true,
77
example: "//View current working directory|cd|//Traverse down one directory|//Change working directory|cd ..|cd \"C:\\ProgramData\"|//Set bookmark as the working directory using name|cd --bookmark program|//Set bookmark as the working directory using index|cd --bookmark 0|//Set first existing bookmark (if any) as working directory|cd --bookmark")]
88
public class CdCommand : CommandBase<CommandsConfiguration>, IWorkingDirectoryChangesListener
@@ -44,6 +44,10 @@ public override RunResult Run()
4444
{
4545
path = Configuration.Bookmark.Bookmarks.First().Path;
4646
}
47+
else if (HasOption("roaming"))
48+
{
49+
path = ConfigurationGlobals.ApplicationDataFolder;
50+
}
4751
else if (!string.IsNullOrEmpty(inputPath))
4852
{
4953
path = inputPath;
@@ -61,7 +65,7 @@ public override RunResult Run()
6165
if(!string.IsNullOrEmpty(dir)) paths.Add(dir);
6266
path = string.Join(Path.DirectorySeparatorChar, paths);
6367
}
64-
68+
if (path.Contains("$ROAMING$")) path = path.Replace("$ROAMING$", ConfigurationGlobals.ApplicationDataFolder);
6569
if (Directory.Exists(path)) WorkingDirectory = path;
6670
else WriteFailureLine($"[{path}] does not exist");
6771
ShowDirectories();

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Core/Commands/SecretCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ private RunResult Salt()
4545

4646
private RunResult Init()
4747
{
48-
var salt = IEncryptionService.GetRandomSalt();
49-
WriteLine($"{salt.Length}");
50-
var firstHalf = salt.Substring(0, 22);
51-
var secondHalf = salt.Substring(22, 22);
48+
var firstHalf = IEncryptionService.GetRandomSalt();;
49+
var secondHalf = IEncryptionService.GetRandomSalt();;
5250
Environment.SetEnvironmentVariable("_encryptionManager", firstHalf, EnvironmentVariableTarget.User);
5351
var securityConfig = new SecurityConfiguration { Encryption = new EncryptionConfiguration { SharedSecretEnvironmentKey = "_encryptionManager", SharedSecretSalt = secondHalf } };
54-
StorageService<SecurityConfiguration>.Service.StoreObject(securityConfig, ConfigurationGlobals.SecurityFileName);
52+
var fileName = Path.Combine(ConfigurationGlobals.ApplicationDataFolder, ConfigurationGlobals.SecurityFileName);
53+
ConfigurationService.Service.Create(securityConfig, fileName);
54+
WriteSuccessLine($"File {fileName} saved OK, you will need to restart the application before the changes take effect.");
5555
return Ok();
5656
}
5757

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Core/Services/EncryptionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class EncryptionService : IEncryptionService
44
{
55
private EncryptionService()
66
{
7-
var securityConfiguration = ConfigurationService.Service.GetAppDataConfiguration(new SecurityConfiguration { Encryption = new EncryptionConfiguration { SharedSecretEnvironmentKey = nameof(_encryptionManager), SharedSecretSalt = IEncryptionService.GetRandomSalt() } }, ConfigurationGlobals.SecurityFileName).Configuration;
7+
var securityConfiguration = ConfigurationService.Service.GetAppDataConfiguration<SecurityConfiguration>(ConfigurationGlobals.SecurityFileName).Configuration;
88
_salt = securityConfiguration.Encryption.SharedSecretSalt;
99
_sharedSecret = Environment.GetEnvironmentVariable(securityConfiguration.Encryption.SharedSecretEnvironmentKey, EnvironmentVariableTarget.User) ?? string.Empty;
1010
if (string.IsNullOrEmpty(_sharedSecret)) ConsoleService.Service.WriteWarning(nameof(EncryptionService),$"The environment variable [{securityConfiguration.Encryption.SharedSecretEnvironmentKey}] is missing, you should generate a salt with secret --salt\nAnd then create the environment variable with the salt as value with user scope, before you create secrets (otherwise you need to re-create them again)");

src/PainKiller.PowerCommands/PainKiller.PowerCommands.Bootstrap/Startup.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
using Microsoft.Extensions.Logging;
2+
using PainKiller.PowerCommands.Configuration;
3+
using PainKiller.PowerCommands.Configuration.DomainObjects;
24
using PainKiller.PowerCommands.Core.Managers;
35
using PainKiller.PowerCommands.Core.Services;
46
using PainKiller.PowerCommands.MyExampleCommands;
57
using PainKiller.PowerCommands.MyExampleCommands.Configuration;
8+
using PainKiller.PowerCommands.Shared.Contracts;
69

710
namespace PainKiller.PowerCommands.Bootstrap;
811
public static class Startup
912
{
1013
public static PowerCommandsManager ConfigureServices()
1114
{
15+
if (!Directory.Exists(ConfigurationGlobals.ApplicationDataFolder))
16+
{
17+
Directory.CreateDirectory(ConfigurationGlobals.ApplicationDataFolder);
18+
InitSecret();
19+
ConsoleService.Service.WriteSuccessLine(nameof(Startup), "\nFirst startup basic application configuration completed...");
20+
ConsoleService.Service.WriteSuccessLine(nameof(Startup), "You will need to restart the application before the changes take effect.");
21+
}
1222
var services = PowerCommandServices.Service;
1323

1424
services.Configuration.Environment.InitializeValues();
@@ -33,4 +43,14 @@ public static PowerCommandsManager ConfigureServices()
3343
ConsoleService.Service.WriteLine(nameof(Startup), "\nUse up or down key to cycle trough command history.", null);
3444
return new PowerCommandsManager(services);
3545
}
46+
47+
private static void InitSecret()
48+
{
49+
var firstHalf = IEncryptionService.GetRandomSalt();;
50+
var secondHalf = IEncryptionService.GetRandomSalt();;
51+
Environment.SetEnvironmentVariable("_encryptionManager", firstHalf, EnvironmentVariableTarget.User);
52+
var securityConfig = new SecurityConfiguration { Encryption = new EncryptionConfiguration { SharedSecretEnvironmentKey = "_encryptionManager", SharedSecretSalt = secondHalf } };
53+
var fileName = Path.Combine(ConfigurationGlobals.ApplicationDataFolder, ConfigurationGlobals.SecurityFileName);
54+
ConfigurationService.Service.Create(securityConfig, fileName);
55+
}
3656
}

0 commit comments

Comments
 (0)