From 0aea8224991a5ece975ee99b63d6ada9a2643915 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 21 May 2024 11:52:30 +0800 Subject: [PATCH] enhance: remote ssh private key validation --- src/ViewModels/AddRemote.cs | 34 ++++++++++++++++++++++++++-------- src/ViewModels/EditRemote.cs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/ViewModels/AddRemote.cs b/src/ViewModels/AddRemote.cs index 6ec6f06c..84ca2326 100644 --- a/src/ViewModels/AddRemote.cs +++ b/src/ViewModels/AddRemote.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.IO; using System.Threading.Tasks; namespace SourceGit.ViewModels @@ -29,13 +30,18 @@ namespace SourceGit.ViewModels public bool UseSSH { get => _useSSH; - set => SetProperty(ref _useSSH, value); + set + { + if (SetProperty(ref _useSSH, value)) + ValidateProperty(_sshkey, nameof(SSHKey)); + } } + [CustomValidation(typeof(AddRemote), nameof(ValidateSSHKey))] public string SSHKey { - get; - set; + get => _sshkey; + set => SetProperty(ref _sshkey, value, true); } public AddRemote(Repository repo) @@ -71,6 +77,20 @@ namespace SourceGit.ViewModels return ValidationResult.Success; } + public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx) + { + if (ctx.ObjectInstance is AddRemote add && add._useSSH) + { + if (string.IsNullOrEmpty(sshkey)) + return new ValidationResult("SSH private key is required"); + + if (!File.Exists(sshkey)) + return new ValidationResult("Given SSH private key can NOT be found!"); + } + + return ValidationResult.Success; + } + public override Task Sure() { _repo.SetWatcherEnabled(false); @@ -84,11 +104,8 @@ namespace SourceGit.ViewModels SetProgressDescription("Fetching from added remote ..."); new Commands.Fetch(_repo.FullPath, _name, true, SetProgressDescription).Exec(); - if (_useSSH) - { - SetProgressDescription("Post processing ..."); - new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", SSHKey); - } + SetProgressDescription("Post processing ..."); + new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null); } CallUIThread(() => { @@ -103,5 +120,6 @@ namespace SourceGit.ViewModels private string _name = string.Empty; private string _url = string.Empty; private bool _useSSH = false; + private string _sshkey = string.Empty; } } diff --git a/src/ViewModels/EditRemote.cs b/src/ViewModels/EditRemote.cs index 91cf8749..5fd22d26 100644 --- a/src/ViewModels/EditRemote.cs +++ b/src/ViewModels/EditRemote.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.IO; using System.Threading.Tasks; namespace SourceGit.ViewModels @@ -29,13 +30,18 @@ namespace SourceGit.ViewModels public bool UseSSH { get => _useSSH; - set => SetProperty(ref _useSSH, value); + set + { + if (SetProperty(ref _useSSH, value)) + ValidateProperty(_sshkey, nameof(SSHKey)); + } } + [CustomValidation(typeof(EditRemote), nameof(ValidateSSHKey))] public string SSHKey { - get; - set; + get => _sshkey; + set => SetProperty(ref _sshkey, value, true); } public EditRemote(Repository repo, Models.Remote remote) @@ -85,6 +91,20 @@ namespace SourceGit.ViewModels return ValidationResult.Success; } + public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx) + { + if (ctx.ObjectInstance is EditRemote edit && edit.UseSSH) + { + if (string.IsNullOrEmpty(sshkey)) + return new ValidationResult("SSH private key is required"); + + if (!File.Exists(sshkey)) + return new ValidationResult("Given SSH private key can NOT be found!"); + } + + return ValidationResult.Success; + } + public override Task Sure() { _repo.SetWatcherEnabled(false); @@ -106,11 +126,8 @@ namespace SourceGit.ViewModels _remote.URL = _url; } - if (_useSSH) - { - SetProgressDescription("Post processing ..."); - new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", SSHKey); - } + SetProgressDescription("Post processing ..."); + new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null); CallUIThread(() => _repo.SetWatcherEnabled(true)); return true; @@ -122,5 +139,6 @@ namespace SourceGit.ViewModels private string _name = string.Empty; private string _url = string.Empty; private bool _useSSH = false; + private string _sshkey = string.Empty; } }