Skip to content

Commit 012b95b

Browse files
committed
[fix] SSL connections
[del[] SSL redundant options [add] connection detailed error description
1 parent 159d16c commit 012b95b

4 files changed

Lines changed: 15 additions & 40 deletions

File tree

src/EtcdTerminal.App/Screens/InstanceSelectionScreen.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public sealed class InstanceSelectionScreen(IConnectionConfigRepository _configR
2121
private const string EnterInstanceName = "Enter instance name:";
2222
private const string EnterConnStr = "Enter connection string:";
2323
private const string DefaultConnStr = "http://localhost:2379";
24-
private const string UseSsl = "Use SSL?";
2524
private const string EnterUsername = "Enter username (optional, leave empty for none):";
2625
private const string EnterPassword = "Enter password:";
2726
private const string Connecting = "Connecting...";
@@ -58,11 +57,7 @@ await AnsiConsole.Status()
5857
.StartAsync(Connecting, async ctx =>
5958
{
6059
await _etcdClient.ConnectAsync(selected);
61-
62-
var healthy = await _etcdClient.PingAsync();
63-
64-
if (!healthy)
65-
throw new Exception("Server did not respond.");
60+
await _etcdClient.PingAsync();
6661
});
6762

6863
AnsiConsole.MarkupLine(ConnectedSuccess);
@@ -71,9 +66,8 @@ await AnsiConsole.Status()
7166
}
7267
catch (Exception ex)
7368
{
74-
AnsiConsole.MarkupLine($"[red]Failed to connect: {ex.Message}[/]");
69+
AnsiConsole.MarkupLine($"[red]Failed to connect:[/] {ex.Message}");
7570
AnsiConsole.WriteLine();
76-
7771
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);
7872
Console.ReadKey(true);
7973
}
@@ -154,11 +148,6 @@ private void AddInstanceInteractive()
154148
return;
155149
}
156150

157-
var useSsl = Prompt.Confirm(UseSsl);
158-
159-
if (useSsl is null)
160-
return;
161-
162151
var username = Prompt.Ask(EnterUsername);
163152

164153
if (username is null)
@@ -178,7 +167,6 @@ private void AddInstanceInteractive()
178167
{
179168
Name = name,
180169
ConnectionString = connectionString,
181-
UseSsl = useSsl.Value,
182170
Username = string.IsNullOrEmpty(username) ? null : username,
183171
Password = string.IsNullOrEmpty(password) ? null : password
184172
};

src/EtcdTerminal.Infrastructure/Configuration/JsonBasedConfigRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public IReadOnlyList<EtcdConnectionConfig> LoadInstances()
4040
{
4141
Name = i.GetProperty("Name").GetString() ?? string.Empty,
4242
ConnectionString = i.GetProperty("ConnectionString").GetString() ?? string.Empty,
43-
UseSsl = i.GetProperty("UseSsl").GetBoolean(),
4443
Username = i.TryGetProperty("Username", out var u) ? u.GetString() : null,
4544
Password = i.TryGetProperty("Password", out var p) ? p.GetString() : null,
4645
};
@@ -102,7 +101,6 @@ private void SaveInstances(List<EtcdConnectionConfig> instances)
102101
{
103102
i.Name,
104103
i.ConnectionString,
105-
i.UseSsl,
106104
i.Username,
107105
i.Password
108106
})

src/EtcdTerminal.Infrastructure/Etcd/DotnetEtcdBasedClient.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using EtcdTerminal.Models;
66
using Google.Protobuf;
77
using Grpc.Core;
8+
using Grpc.Net.Client;
89
using Mvccpb;
910

1011
namespace EtcdTerminal.Infrastructure.Etcd;
@@ -20,41 +21,30 @@ public Task ConnectAsync(EtcdConnectionConfig config, CancellationToken ct = def
2021
Disconnect();
2122

2223
var connectionString = config.ConnectionString;
24+
var useSsl = connectionString.StartsWith("https", StringComparison.OrdinalIgnoreCase);
2325

24-
if (config.IsAuthenticationEnabled)
26+
void configureChannel(GrpcChannelOptions options)
2527
{
26-
_client = new EtcdClient(
27-
connectionString,
28-
config.Username,
29-
config.Password);
30-
}
31-
else
32-
{
33-
_client = new EtcdClient(connectionString,
34-
configureChannelOptions: options =>
35-
{
36-
if (!config.UseSsl)
37-
options.Credentials = ChannelCredentials.Insecure;
38-
});
28+
options.Credentials = useSsl
29+
? ChannelCredentials.SecureSsl
30+
: ChannelCredentials.Insecure;
3931
}
4032

33+
_client = config.IsAuthenticationEnabled
34+
? new EtcdClient(connectionString, config.Username!, config.Password!,
35+
configureChannelOptions: configureChannel)
36+
: new EtcdClient(connectionString, configureChannelOptions: configureChannel);
37+
4138
return Task.CompletedTask;
4239
}
4340

4441
public async Task<bool> PingAsync(CancellationToken ct = default)
4542
{
4643
if (_client is null) return false;
4744

48-
try
49-
{
50-
await _client!.GetAsync("\0", cancellationToken: ct);
45+
await _client!.GetAsync("\0", cancellationToken: ct);
5146

52-
return true;
53-
}
54-
catch
55-
{
56-
return false;
57-
}
47+
return true;
5848
}
5949

6050
public Task DisconnectAsync()

src/EtcdTerminal/Models/EtcdConnectionConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public sealed class EtcdConnectionConfig
44
{
55
public string Name { get; init; } = string.Empty;
66
public string ConnectionString { get; init; } = string.Empty;
7-
public bool UseSsl { get; init; }
87
public string? Username { get; init; }
98
public string? Password { get; init; }
109
public bool IsAuthenticationEnabled => !string.IsNullOrEmpty(Username);

0 commit comments

Comments
 (0)