11using PainKiller . PowerCommands . Security . Services ;
22
3- namespace PainKiller . PowerCommands . Core . Commands
3+ namespace PainKiller . PowerCommands . Core . Commands ;
4+
5+ [ PowerCommandDesign ( description : "Get, creates, removes or view secrets, first you need to configure your encryption key with initialize argument" ,
6+ options : "create|initialize|configuration|remove|salt" ,
7+ disableProxyOutput : true ,
8+ example : "//View all declared secrets|secret|secret --create \" mycommand-pass\" |secret --remove \" mycommand-pass\" |//Initialize your machine with a new encryption key (stops if this is already done)|secret --initialize" ) ]
9+ public class SecretCommand : CommandBase < CommandsConfiguration >
410{
5- [ PowerCommandDesign ( description : "Get, creates, removes or view secrets, first you need to configure your encryption key with initialize argument" ,
6- options : "create|initialize|configuration|remove|salt" ,
7- disableProxyOutput : true ,
8- example : "//View all declared secrets|secret|secret --create \" mycommand-pass\" |secret --remove \" mycommand-pass\" |//Initialize your machine with a new encryption key (stops if this is already done)|secret --initialize" ) ]
9- public class SecretCommand : CommandBase < CommandsConfiguration >
11+ public SecretCommand ( string identifier , CommandsConfiguration configuration ) : base ( identifier , configuration ) { }
12+ public override RunResult Run ( )
1013 {
11- public SecretCommand ( string identifier , CommandsConfiguration configuration ) : base ( identifier , configuration ) { }
12- public override RunResult Run ( )
13- {
14- if ( Input . HasOption ( "initialize" ) ) return Init ( ) ;
15- if ( Input . HasOption ( "" ) ) return CheckEncryptConfiguration ( ) ;
16- if ( Input . HasOption ( "salt" ) ) return Salt ( ) ;
17- if ( Input . HasOption ( "create" ) ) return Create ( ) ;
18- if ( Input . HasOption ( "remove" ) ) return Remove ( ) ;
19- if ( ( Input . Arguments . Length + Input . Quotes . Length < 2 ) && Input . Arguments . Length > 0 ) throw new MissingFieldException ( "Two parameters must be provided" ) ;
20- if ( Input . Arguments . Length == 0 || Input . Arguments [ 0 ] == "view" ) return List ( ) ;
14+ if ( Input . HasOption ( "initialize" ) ) return Init ( ) ;
15+ if ( Input . HasOption ( "" ) ) return CheckEncryptConfiguration ( ) ;
16+ if ( Input . HasOption ( "salt" ) ) return Salt ( ) ;
17+ if ( Input . HasOption ( "create" ) ) return Create ( ) ;
18+ if ( Input . HasOption ( "remove" ) ) return Remove ( ) ;
19+ if ( ( Input . Arguments . Length + Input . Quotes . Length < 2 ) && Input . Arguments . Length > 0 ) throw new MissingFieldException ( "Two parameters must be provided" ) ;
20+ if ( Input . Arguments . Length == 0 || Input . Arguments [ 0 ] == "view" ) return List ( ) ;
2121
22- return BadParameterError ( "No matching parameter" ) ;
23- }
24- private RunResult CheckEncryptConfiguration ( )
22+ return BadParameterError ( "No matching parameter" ) ;
23+ }
24+ private RunResult CheckEncryptConfiguration ( )
25+ {
26+ try
2527 {
26- try
27- {
28- var encryptedString = EncryptionService . Service . EncryptString ( "Encryption is setup properly" ) ;
29- var decryptedString = EncryptionService . Service . DecryptString ( encryptedString ) ;
30- WriteLine ( encryptedString ) ;
31- WriteLine ( decryptedString ) ;
32- }
33- catch
34- {
35- Console . WriteLine ( "" ) ;
36- WriteError ( "Encryption is not configured properly" ) ;
37- }
38- return Ok ( ) ;
28+ var encryptedString = EncryptionService . Service . EncryptString ( "Encryption is setup properly" ) ;
29+ var decryptedString = EncryptionService . Service . DecryptString ( encryptedString ) ;
30+ WriteLine ( encryptedString ) ;
31+ WriteLine ( decryptedString ) ;
3932 }
40- private RunResult Salt ( )
33+ catch
4134 {
42- Console . WriteLine ( IEncryptionService . GetRandomSalt ( ) ) ;
43- return Ok ( ) ;
35+ Console . WriteLine ( "" ) ;
36+ WriteError ( "Encryption is not configured properly" ) ;
4437 }
38+ return Ok ( ) ;
39+ }
40+ private RunResult Salt ( )
41+ {
42+ Console . WriteLine ( IEncryptionService . GetRandomSalt ( ) ) ;
43+ return Ok ( ) ;
44+ }
4545
46- private RunResult Init ( )
47- {
48- var firstHalf = IEncryptionService . GetRandomSalt ( ) ; ;
49- var secondHalf = IEncryptionService . GetRandomSalt ( ) ; ;
50- Environment . SetEnvironmentVariable ( "_encryptionManager" , firstHalf , EnvironmentVariableTarget . User ) ;
51- var securityConfig = new SecurityConfiguration { Encryption = new EncryptionConfiguration { SharedSecretEnvironmentKey = "_encryptionManager" , SharedSecretSalt = secondHalf } } ;
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.") ;
55- return Ok ( ) ;
56- }
46+ private RunResult Init ( )
47+ {
48+ var firstHalf = IEncryptionService . GetRandomSalt ( ) ;
49+ var secondHalf = IEncryptionService . GetRandomSalt ( ) ;
50+ Environment . SetEnvironmentVariable ( "_encryptionManager" , firstHalf , EnvironmentVariableTarget . User ) ;
51+ var securityConfig = new SecurityConfiguration { Encryption = new EncryptionConfiguration { SharedSecretEnvironmentKey = "_encryptionManager" , SharedSecretSalt = secondHalf } } ;
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.") ;
55+ return Ok ( ) ;
56+ }
5757
58- private RunResult List ( )
59- {
60- if ( Configuration . Secret . Secrets == null ) return Ok ( ) ;
61- foreach ( var secret in Configuration . Secret . Secrets ) ConsoleService . Service . WriteObjectDescription ( $ "{ GetType ( ) . Name } ", secret . Name , $ "{ string . Join ( ',' , secret . Options . Keys ) } ") ;
62- return Ok ( ) ;
63- }
64- private RunResult Create ( )
65- {
66- var name = Input . SingleQuote ;
67- var password = DialogService . SecretPromptDialog ( "Enter secret:" ) ;
68- if ( string . IsNullOrEmpty ( password ) ) return BadParameterError ( "Passwords do not match" ) ;
58+ private RunResult List ( )
59+ {
60+ foreach ( var secret in Configuration . Secret . Secrets ) ConsoleService . Service . WriteObjectDescription ( $ "{ GetType ( ) . Name } ", secret . Name , $ "{ string . Join ( ',' , secret . Options . Keys ) } ") ;
61+ return Ok ( ) ;
62+ }
63+ private RunResult Create ( )
64+ {
65+ var name = Input . SingleQuote ;
66+ var password = DialogService . SecretPromptDialog ( "Enter secret:" ) ;
67+ if ( string . IsNullOrEmpty ( password ) ) return BadParameterError ( "Passwords do not match" ) ;
6968
70- var secret = new SecretItemConfiguration { Name = name } ;
71- var val = SecretService . Service . SetSecret ( name , password , secret . Options , EncryptionService . Service . EncryptString ) ;
69+ var secret = new SecretItemConfiguration { Name = name } ;
70+ var val = SecretService . Service . SetSecret ( name , password , secret . Options , EncryptionService . Service . EncryptString ) ;
7271
73- Configuration . Secret ??= new ( ) ;
74- Configuration . Secret . Secrets ??= new ( ) ;
75- Configuration . Secret . Secrets . Add ( secret ) ;
76- ConfigurationService . Service . SaveChanges ( Configuration ) ;
77- Console . WriteLine ( ) ;
78- WriteHeadLine ( "New secret created and stored in configuration file" ) ;
79- ConsoleService . Service . WriteObjectDescription ( $ "{ GetType ( ) . Name } ", name , val ) ;
72+ Configuration . Secret . Secrets . Add ( secret ) ;
73+ ConfigurationService . Service . SaveChanges ( Configuration ) ;
74+ Console . WriteLine ( ) ;
75+ WriteHeadLine ( "New secret created and stored in configuration file" ) ;
76+ ConsoleService . Service . WriteObjectDescription ( $ "{ GetType ( ) . Name } ", name , val ) ;
8077
81- return Ok ( ) ;
82- }
83- private RunResult Remove ( )
84- {
85- var name = Input . SingleQuote ;
78+ return Ok ( ) ;
79+ }
80+ private RunResult Remove ( )
81+ {
82+ var name = Input . SingleQuote ;
8683
87- var secret = Configuration . Secret . Secrets . FirstOrDefault ( s => s . Name . ToLower ( ) == name . ToLower ( ) ) ;
88- if ( secret == null ) return BadParameterError ( $ "No secret with name \" { name } \" found.") ;
84+ var secret = Configuration . Secret . Secrets . FirstOrDefault ( s => s . Name . ToLower ( ) == name . ToLower ( ) ) ;
85+ if ( secret == null ) return BadParameterError ( $ "No secret with name \" { name } \" found.") ;
8986
90- Configuration . Secret . Secrets . Remove ( secret ) ;
91- ConfigurationService . Service . SaveChanges ( Configuration ) ;
87+ Configuration . Secret . Secrets . Remove ( secret ) ;
88+ ConfigurationService . Service . SaveChanges ( Configuration ) ;
9289
93- WriteHeadLine ( "Secret removed from configuration file\n Manually remove the secret key from environment variables or vault depending on how they are stored." ) ;
94- return Ok ( ) ;
95- }
90+ WriteHeadLine ( "Secret removed from configuration file\n Manually remove the secret key from environment variables or vault depending on how they are stored." ) ;
91+ return Ok ( ) ;
9692 }
9793}
0 commit comments