diff ServerMonitor/Objects/Server.cs @ 5:b6fe203af9d5

Private key passwords and validation
author Brad Greco <brad@bgreco.net>
date Thu, 28 Feb 2019 21:19:32 -0500
parents 3142e52cbe69
children c1dffaac66fa
line wrap: on
line diff
--- a/ServerMonitor/Objects/Server.cs	Sun Feb 10 20:51:26 2019 -0500
+++ b/ServerMonitor/Objects/Server.cs	Thu Feb 28 21:19:32 2019 -0500
@@ -6,6 +6,7 @@
 using System.ComponentModel;
 using Renci.SshNet;
 using System.Runtime.Serialization;
+using System.Xml.Serialization;
 
 namespace ServerMonitorApp
 {
@@ -21,6 +22,7 @@
         private SshClient _sshClient;
         private bool _enabled = true;
         private byte[] passwordHash;
+        private PrivateKeyFile _privateKeyFile;
 
         public event EventHandler CheckModified;
         public event EventHandler EnabledChanged;
@@ -70,17 +72,51 @@
             set
             {
                 passwordHash = ProtectedData.Protect(Encoding.UTF8.GetBytes(value),
-                                 Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy
-                                 DataProtectionScope.CurrentUser);
+                                    Encoding.UTF8.GetBytes("Server".Reverse().ToString()), // Minor obfuscation of additional entropy
+                                    DataProtectionScope.CurrentUser);
             }
         }
 
+        [XmlIgnore]
+        public PrivateKeyFile PrivateKeyFile
+        {
+            get { return _privateKeyFile; }
+            set
+            {
+                _privateKeyFile = value;
+                if (LoginType == LoginType.PrivateKey)
+                {
+                    if (_privateKeyFile == null)
+                    {
+                        KeyStatus = KeyStatus.Closed;
+                        Enabled = false;
+                    }
+                    else
+                    {
+                        if (!KeyStatus.In(KeyStatus.Open, KeyStatus.Closed))
+                            Enabled = true;
+                        KeyStatus = KeyStatus.Open;
+                    }
+                }
+            }
+        }
+
+        public KeyStatus KeyStatus { get; set; }
+
         public bool Enabled
         {
             get { return _enabled; }
-            set { _enabled = value; EnabledChanged?.Invoke(this, new EventArgs()); }
+            set
+            {
+                if (LoginType == LoginType.PrivateKey && PrivateKeyFile == null && value == true)
+                    return;
+                _enabled = value;
+                EnabledChanged?.Invoke(this, new EventArgs());
+            }
         }
 
+        //public bool WaitingForUser { get; set; }
+
         public CheckStatus Status => !Enabled ? CheckStatus.Disabled : Checks
             .Where(c => c.Enabled)
             .Select(c => c.LastRunStatus)
@@ -93,12 +129,7 @@
             {
                 if (_sshClient == null)
                 {
-                    AuthenticationMethod auth = null;
-                    if (LoginType == LoginType.Password)
-                        auth = new PasswordAuthenticationMethod(Username, Password);
-                    else
-                        auth = new PrivateKeyAuthenticationMethod(Username, new PrivateKeyFile(KeyFile));
-                    ConnectionInfo info = new ConnectionInfo(Host, Port, Username, auth);
+                    ConnectionInfo info = new ConnectionInfo(Host, Port, Username, GetAuthentication());
                     _sshClient = new SshClient(info);
                 }
                 return _sshClient;
@@ -156,10 +187,28 @@
                 && Checks.Count == 0;
         }
 
+        private AuthenticationMethod GetAuthentication()
+        {
+            if (LoginType == LoginType.Password)
+                return new PasswordAuthenticationMethod(Username, Password);
+            else
+                return new PrivateKeyAuthenticationMethod(Username, PrivateKeyFile);
+        }
+
         private void InvalidateSshConnection()
         {
             _sshClient?.Dispose();
             _sshClient = null;
         }
     }
+
+    public enum KeyStatus
+    {
+        Closed,
+        Open,
+        NotAccessible,
+        NeedPassword,
+    }
+
+
 }