Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing certificate causes The collection already contains item with same key 'net.transport' exception #1405

Closed
bgrainger opened this issue Dec 1, 2023 · 1 comment
Assignees
Labels

Comments

@bgrainger
Copy link
Member

Software versions
MySqlConnector version: 2.3.1
Server type (MySQL, MariaDB, Aurora, etc.) and version: mysql:8.2.0
.NET version: 8.0

Describe the bug

Attempt to connect to a MySQL server using a SSL certificate but specify an invalid path to the client certificate file. MySqlConnector will throw InvalidOperationException: The collection already contains item with same key 'net.transport'. It should throw a FileNotFoundException or some other more self-explanatory error.

Exception

[INFO]  ConnectionPool  Creating new connection pool 1 for Server=localhost;Port=3306;User ID=root;SSL Mode=VerifyFull;SSL Cert=C:\client-cert.pem;SSL Key=C:\DoesNotExist\client-key.pem;SSL CA=C:\ssl-ca-cert.pem
[TRACE]  ConnectionPool  Pool 1 waiting for an available session
[TRACE]  MySqlConnection  Created new session 1.1
[DEBUG]  ConnectionPool  Pool 1 has no pooled session available; created new session 1.1
[TRACE]  MySqlConnection  Session 1.1 connecting to IP address 127.0.0.1 (1 of 2) for host name localhost (1 of 1)
[TRACE]  MySqlConnection  Session 1.1 connected to IP address 127.0.0.1 for host name localhost with local port 61277
[TRACE]  MySqlConnection  Session 1.1 server sent auth plugin name caching_sha2_password
[DEBUG]  MySqlConnection  Session 1.1 made connection; server version 8.2.0; connection ID 28; supports: compression False, attributes True, deprecate EOF True, cached metadata False, SSL True, session track True, pipelining True, query attributes True
[TRACE]  MySqlConnection  Session 1.1 initializing TLS connection
[WARN]  MySqlConnection  Session 1.1 failed negotiating TLS; falling back to TLS 1.1
System.IO.FileNotFoundException: Could not find file 'C:\DoesNotExist\client-key.pem'.
File name: 'C:\DoesNotExist\client-key.pem'
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.File.ReadAllText(String path, Encoding encoding)
   at System.Security.Cryptography.X509Certificates.X509Certificate2.CreateFromPemFile(String certPemFilePath, String keyPemFilePath)
   at MySqlConnector.Core.ServerSession.<>c__DisplayClass114_0.<InitSslAsync>g__LoadCertificate|2(String sslKeyFile, String sslCertificateFile) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ServerSession.cs:line 1532
   at MySqlConnector.Core.ServerSession.InitSslAsync(ProtocolCapabilities serverCapabilities, ConnectionSettings cs, MySqlConnection connection, SslProtocols sslProtocols, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ServerSession.cs:line 1302
   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, MySqlConnection connection, Int64 startingTimestamp, ILoadBalancer loadBalancer, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ServerSession.cs:line 542
[TRACE]  MySqlConnection  Session 1.1 sending QUIT command
[DEBUG]  MySqlConnection  Session 1.1 closing stream/socket

InvalidOperationException: "The collection already contains item with same key 'net.transport''"
   at System.Diagnostics.ActivityTagsCollection.Add(String key, Object value)
   at MySqlConnector.Core.ServerSession.OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ServerSession.cs:line 989
   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, MySqlConnection connection, Int64 startingTimestamp, ILoadBalancer loadBalancer, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ServerSession.cs:line 444
   at MySqlConnector.Core.ConnectionPool.ConnectSessionAsync(MySqlConnection connection, Action`4 logMessage, Int64 startingTimestamp, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ConnectionPool.cs:line 428
   at MySqlConnector.Core.ConnectionPool.ConnectSessionAsync(MySqlConnection connection, Action`4 logMessage, Int64 startingTimestamp, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ConnectionPool.cs:line 433
   at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, Int64 startingTimestamp, Int32 timeoutMilliseconds, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ConnectionPool.cs:line 113
   at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, Int64 startingTimestamp, Int32 timeoutMilliseconds, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\Core\ConnectionPool.cs:line 146
   at MySqlConnector.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Int64 startingTimestamp, Activity activity, Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\MySqlConnection.cs:line 919
   at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\Code\MySqlConnector\src\MySqlConnector\MySqlConnection.cs:line 419
   at MySqlConnector.MySqlConnection.Open() in C:\Code\MySqlConnector\src\MySqlConnector\MySqlConnection.cs:line 381
   at UserQuery.Main(), line 21

**Code sample**

```csharp
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder()
{
	Server = "localhost",
	Port = 3306,
	UserID = "root",
	Password = "pass",
	SslMode = MySqlSslMode.VerifyFull,
	SslCa = @"C:\ssl-ca-cert.pem",
	SslCert = @"C:\client-cert.pem",
	SslKey = @"C:\DoesNotExist\client-key.pem",
};

using var connection = new MySqlConnection(csb.ConnectionString);
connection.Open();

Expected behavior
It should throw a FileNotFoundException or some other more self-explanatory error.

Additional context
Possibly related to #1074.

@bgrainger
Copy link
Member Author

Fixed in 2.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant