Skip to content

Commit 0aea822

Browse files
committed
enhance: remote ssh private key validation
1 parent ef20c17 commit 0aea822

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/ViewModels/AddRemote.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations;
2+
using System.IO;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -29,13 +30,18 @@ public string Url
2930
public bool UseSSH
3031
{
3132
get => _useSSH;
32-
set => SetProperty(ref _useSSH, value);
33+
set
34+
{
35+
if (SetProperty(ref _useSSH, value))
36+
ValidateProperty(_sshkey, nameof(SSHKey));
37+
}
3338
}
3439

40+
[CustomValidation(typeof(AddRemote), nameof(ValidateSSHKey))]
3541
public string SSHKey
3642
{
37-
get;
38-
set;
43+
get => _sshkey;
44+
set => SetProperty(ref _sshkey, value, true);
3945
}
4046

4147
public AddRemote(Repository repo)
@@ -71,6 +77,20 @@ public static ValidationResult ValidateRemoteURL(string url, ValidationContext c
7177
return ValidationResult.Success;
7278
}
7379

80+
public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx)
81+
{
82+
if (ctx.ObjectInstance is AddRemote add && add._useSSH)
83+
{
84+
if (string.IsNullOrEmpty(sshkey))
85+
return new ValidationResult("SSH private key is required");
86+
87+
if (!File.Exists(sshkey))
88+
return new ValidationResult("Given SSH private key can NOT be found!");
89+
}
90+
91+
return ValidationResult.Success;
92+
}
93+
7494
public override Task<bool> Sure()
7595
{
7696
_repo.SetWatcherEnabled(false);
@@ -84,11 +104,8 @@ public override Task<bool> Sure()
84104
SetProgressDescription("Fetching from added remote ...");
85105
new Commands.Fetch(_repo.FullPath, _name, true, SetProgressDescription).Exec();
86106

87-
if (_useSSH)
88-
{
89-
SetProgressDescription("Post processing ...");
90-
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", SSHKey);
91-
}
107+
SetProgressDescription("Post processing ...");
108+
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null);
92109
}
93110
CallUIThread(() =>
94111
{
@@ -103,5 +120,6 @@ public override Task<bool> Sure()
103120
private string _name = string.Empty;
104121
private string _url = string.Empty;
105122
private bool _useSSH = false;
123+
private string _sshkey = string.Empty;
106124
}
107125
}

src/ViewModels/EditRemote.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations;
2+
using System.IO;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -29,13 +30,18 @@ public string Url
2930
public bool UseSSH
3031
{
3132
get => _useSSH;
32-
set => SetProperty(ref _useSSH, value);
33+
set
34+
{
35+
if (SetProperty(ref _useSSH, value))
36+
ValidateProperty(_sshkey, nameof(SSHKey));
37+
}
3338
}
3439

40+
[CustomValidation(typeof(EditRemote), nameof(ValidateSSHKey))]
3541
public string SSHKey
3642
{
37-
get;
38-
set;
43+
get => _sshkey;
44+
set => SetProperty(ref _sshkey, value, true);
3945
}
4046

4147
public EditRemote(Repository repo, Models.Remote remote)
@@ -85,6 +91,20 @@ public static ValidationResult ValidateRemoteURL(string url, ValidationContext c
8591
return ValidationResult.Success;
8692
}
8793

94+
public static ValidationResult ValidateSSHKey(string sshkey, ValidationContext ctx)
95+
{
96+
if (ctx.ObjectInstance is EditRemote edit && edit.UseSSH)
97+
{
98+
if (string.IsNullOrEmpty(sshkey))
99+
return new ValidationResult("SSH private key is required");
100+
101+
if (!File.Exists(sshkey))
102+
return new ValidationResult("Given SSH private key can NOT be found!");
103+
}
104+
105+
return ValidationResult.Success;
106+
}
107+
88108
public override Task<bool> Sure()
89109
{
90110
_repo.SetWatcherEnabled(false);
@@ -106,11 +126,8 @@ public override Task<bool> Sure()
106126
_remote.URL = _url;
107127
}
108128

109-
if (_useSSH)
110-
{
111-
SetProgressDescription("Post processing ...");
112-
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", SSHKey);
113-
}
129+
SetProgressDescription("Post processing ...");
130+
new Commands.Config(_repo.FullPath).Set($"remote.{_name}.sshkey", _useSSH ? SSHKey : null);
114131

115132
CallUIThread(() => _repo.SetWatcherEnabled(true));
116133
return true;
@@ -122,5 +139,6 @@ public override Task<bool> Sure()
122139
private string _name = string.Empty;
123140
private string _url = string.Empty;
124141
private bool _useSSH = false;
142+
private string _sshkey = string.Empty;
125143
}
126144
}

0 commit comments

Comments
 (0)