-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TLS client verify tests Tests to make sure TLS client certificates works. * Also allow TLS 1.3 * Tweak to how client certificates taken in for Windows * Format fixes * Security docs * Debugging CI * Test workaround * Reverted CI debugging changes
- Loading branch information
Showing
14 changed files
with
162 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Security | ||
|
||
NATS has a lot of [security features](https://docs.nats.io/nats-concepts/security) and .NET V2 client supports them all. | ||
All you need to do is to pass your credentials to the connection. | ||
|
||
```csharp | ||
var opts = NatsOpts.Default with | ||
{ | ||
AuthOpts = NatsAuthOpts.Default with | ||
{ | ||
Username = "bob", | ||
Password = "s3cr3t", | ||
}, | ||
}; | ||
|
||
await using var nats = new NatsConnection(opts); | ||
``` | ||
|
||
See also [user authentication tests](https://github.com/nats-io/nats.net.v2/blob/main/tests/NATS.Client.Core.Tests/NatsConnectionTest.Auth.cs) for more examples. | ||
|
||
## Implicit TLS Connections | ||
|
||
As of NATS server version 2.10.4 and later, the server supports implicit TLS connections. | ||
This means that the client can connect to the server using the default port of 4222 and the server will automatically upgrade the connection to TLS. | ||
This is useful for environments where TLS is required by default. | ||
|
||
```csharp | ||
var opts = NatsOpts.Default with | ||
{ | ||
TlsOpts = new NatsTlsOpts | ||
{ | ||
Mode = TlsMode.Implicit, | ||
}, | ||
}; | ||
|
||
await using var nats = new NatsConnection(opts); | ||
``` | ||
|
||
## Mutual TLS Connections | ||
|
||
The [server can require TLS certificates from a client](https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_intro/tls_mutual_auth) to validate | ||
the client certificate matches a known or trusted CA and to provide authentication. | ||
|
||
You can set the TLS options to use your client certificates when connecting to a server which requires TLS Mutual authentication. | ||
|
||
```csharp | ||
var opts = NatsOpts.Default with | ||
{ | ||
TlsOpts = new NatsTlsOpts | ||
{ | ||
CertFile = "path/to/cert.pem", | ||
KeyFile = "path/to/key.pem", | ||
CaFile = "path/to/ca.pem", | ||
}, | ||
}; | ||
|
||
await using var nats = new NatsConnection(opts); | ||
``` | ||
|
||
### Intermediate CA Certificates | ||
|
||
When connecting using intermediate CA certificates, it might noy be possible to validate the client certificate and the TLS handshake may fail. | ||
|
||
Unfortunately, for .NET client applications it isn't possible to pass additional intermediate certificates and the only | ||
solution is to add the certificates to the certificate store manually. | ||
|
||
See also: | ||
https://learn.microsoft.com/en-us/dotnet/core/extensions/sslstream-troubleshooting#intermediate-certificates-are-not-sent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace NATS.Client.Core.Tests; | ||
|
||
public class TlsClientTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public TlsClientTest(ITestOutputHelper output) => _output = output; | ||
|
||
[Fact] | ||
public async Task Client_connect_using_certificate() | ||
{ | ||
await using var server = NatsServer.Start( | ||
new NullOutputHelper(), | ||
new NatsServerOptsBuilder() | ||
.UseTransport(TransportType.Tls, tlsVerify: true) | ||
.Build()); | ||
|
||
var clientOpts = server.ClientOpts(NatsOpts.Default with { Name = "tls-test-client" }); | ||
await using var nats = new NatsConnection(clientOpts); | ||
await nats.ConnectAsync(); | ||
var rtt = await nats.PingAsync(); | ||
Assert.True(rtt > TimeSpan.Zero); | ||
} | ||
|
||
[Fact] | ||
public async Task Client_cannot_connect_without_certificate() | ||
{ | ||
await using var server = NatsServer.Start( | ||
new NullOutputHelper(), | ||
new NatsServerOptsBuilder() | ||
.UseTransport(TransportType.Tls, tlsVerify: true) | ||
.Build()); | ||
|
||
var clientOpts = server.ClientOpts(NatsOpts.Default); | ||
clientOpts = clientOpts with { TlsOpts = clientOpts.TlsOpts with { CertFile = null, KeyFile = null } }; | ||
await using var nats = new NatsConnection(clientOpts); | ||
|
||
var exceptionTask = Assert.ThrowsAsync<NatsException>(async () => await nats.ConnectAsync()); | ||
|
||
// TODO: On Linux failed mTLS connection hangs. | ||
// In this scenario _sslStream.AuthenticateAsClientAsync() is not throwing exception on Linux | ||
// which is causing the connection to hang. So if the serer is configured to verify the client | ||
// and the client does not provide a certificate, the connection will hang on Linux. | ||
await Task.WhenAny(exceptionTask, Task.Delay(3000)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace NATS.Client.Testing.Failground; | ||
namespace NATS.Client.Testing.Failground; | ||
|
||
public interface ITest | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters