Skip to content

Commit

Permalink
Accept non-trusted certificates option (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszgolebiowski and Tomasz Gołębiowski authored Sep 27, 2024
1 parent 8063e27 commit 779619c
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Cody.Core/Settings/IUserSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface IUserSettingsService
string AccessToken { get; set; }
string ServerEndpoint { get; set; }
string CodySettings { get; set; }
bool AcceptNonTrustedCert { get; set; }


event EventHandler AuthorizationDetailsChanged;
}
}
10 changes: 10 additions & 0 deletions src/Cody.Core/Settings/UserSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,15 @@ public string CodySettings
get => GetOrDefault(nameof(CodySettings), string.Empty);
set => Set(nameof(CodySettings), value);
}

public bool AcceptNonTrustedCert
{
get
{
var value = GetOrDefault(nameof(AcceptNonTrustedCert), false.ToString());
return bool.Parse(value);
}
set => Set(nameof(AcceptNonTrustedCert), value.ToString());
}
}
}
14 changes: 13 additions & 1 deletion src/Cody.UI/Controls/Options/GeneralOptionsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
Expand All @@ -42,10 +43,19 @@
Name="ConfigurationsTextBox"
/>

<CheckBox
Name="AcceptNonTrustedCertCheckBox"
Grid.Row="2"
Grid.Column="1"
Margin="0 5 0 0"
IsChecked="{Binding AcceptNonTrustedCert, Mode=TwoWay}"
Content="Accept non-trusted certificates (requires restart)"
/>

<!--Get Help-->
<Button
Margin="0 5 0 0"
Grid.Row="2"
Grid.Row="3"
Grid.Column="1"
Height="25"
Width="100"
Expand All @@ -54,6 +64,8 @@
Command="{Binding ActivateBetaCommand}"
/>



<!--Sourcegraph URL-->
<Label
Grid.Row="0"
Expand Down
1 change: 1 addition & 0 deletions src/Cody.UI/Controls/Options/GeneralOptionsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void ForceBindingsUpdate()
// This is a workaround to get bindings updated. The second solution is to use NotifyPropertyChange for every TextBox in the Xaml, but current solution is a little more "clean" - everything is clearly visible in a one place.
SourcegraphUrlTextBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
ConfigurationsTextBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
AcceptNonTrustedCertCheckBox.GetBindingExpression(CheckBox.IsCheckedProperty)?.UpdateSource();
}
}
}
13 changes: 13 additions & 0 deletions src/Cody.UI/ViewModels/GeneralOptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,18 @@ public string SourcegraphUrl
}
}

private bool _acceptNonTrustedCert;
public bool AcceptNonTrustedCert
{
get => _acceptNonTrustedCert;
set
{
if (SetProperty(ref _acceptNonTrustedCert, value))
{
_logger.Debug($"AcceptNonTrustedCert set:{SourcegraphUrl}");
}
}
}

}
}
3 changes: 3 additions & 0 deletions src/Cody.VisualStudio/Client/AgentClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cody.Core.Agent;
using Microsoft.VisualStudio.Shell.FindAllReferences;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,6 +21,8 @@ public class AgentClientOptions
/// </summary>
public int RemoteAgentPort { get; set; } = 3113;

public bool AcceptNonTrustedCertificates { get; set; } = false;

public string AgentDirectory { get; set; }

public List<object> CallbackHandlers { get; set; } = new List<object>();
Expand Down
4 changes: 4 additions & 0 deletions src/Cody.VisualStudio/Client/AgentProcessConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void Connect(AgentClientOptions options)
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.EnableRaisingEvents = true;

if (options.AcceptNonTrustedCertificates)
process.StartInfo.EnvironmentVariables["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";

process.Exited += OnProcessExited;
process.ErrorDataReceived += OnErrorDataReceived;

Expand Down
32 changes: 32 additions & 0 deletions src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using Task = System.Threading.Tasks.Task;
using Microsoft.VisualStudio.TaskStatusCenter;
using SolutionEvents = Microsoft.VisualStudio.Shell.Events.SolutionEvents;
using System.Net;

namespace Cody.VisualStudio
{
Expand Down Expand Up @@ -286,6 +287,7 @@ private void PrepareAgentConfiguration()
RestartAgentOnFailure = true,
ConnectToRemoteAgent = devPort != null,
RemoteAgentPort = portNumber,
AcceptNonTrustedCertificates = UserSettingsService.AcceptNonTrustedCert,
Debug = true
};

Expand Down Expand Up @@ -376,6 +378,7 @@ private void InitializeAgent()
{
try
{
await TestServerConnection(UserSettingsService.ServerEndpoint);
AgentClient.Start();
var clientConfig = GetClientInfo();
Expand Down Expand Up @@ -449,6 +452,35 @@ private void OnAfterCloseSolution(object sender, EventArgs e)
}
}

private async Task TestServerConnection(string serverAddress)
{
if (string.IsNullOrWhiteSpace(serverAddress)) return;

using (WebClient webClient = new WebClient())
{
try
{
await webClient.DownloadStringTaskAsync(serverAddress);
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.TrustFailure)
{
if (!UserSettingsService.AcceptNonTrustedCert)
{
AgentLogger.Warn("SSL certificate problem: self-signed certificate in servers certificate chain. " +
"Consider using 'Accept non-trusted certificates' option in Cody settings window.");
}
else
{
AgentLogger.Warn("SSL certificate problem.");
}
}
catch
{
AgentLogger.Warn($"Connection problem with '{serverAddress}'");
}
}
}


private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Cody.VisualStudio/Options/GeneralOptionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ protected override void OnActivate(CancelEventArgs e)

var codyConfig = _settingsService.CodySettings;
var sourcegraphUrl = _settingsService.ServerEndpoint;
var acceptNonTrustedCert = _settingsService.AcceptNonTrustedCert;

_generalOptionsViewModel.CodyConfigurations = codyConfig;
_generalOptionsViewModel.SourcegraphUrl = sourcegraphUrl;
_generalOptionsViewModel.AcceptNonTrustedCert = acceptNonTrustedCert;

_logger.Debug($"Is canceled:{e.Cancel}");

Expand All @@ -74,9 +76,11 @@ protected override void OnApply(PageApplyEventArgs args)

var codyConfig = _generalOptionsViewModel.CodyConfigurations;
var sourcegraphUrl = _generalOptionsViewModel.SourcegraphUrl;
var acceptNonTrustedCert = _generalOptionsViewModel.AcceptNonTrustedCert;

_settingsService.CodySettings = codyConfig;
_settingsService.ServerEndpoint = sourcegraphUrl;
_settingsService.AcceptNonTrustedCert = acceptNonTrustedCert;
}

protected override void OnDeactivate(CancelEventArgs e)
Expand All @@ -97,6 +101,7 @@ public override void ResetSettings()
{
_settingsService.CodySettings = string.Empty;
_settingsService.ServerEndpoint = string.Empty;
_settingsService.AcceptNonTrustedCert = false;

base.ResetSettings();
}
Expand Down

0 comments on commit 779619c

Please sign in to comment.