mithrandyr / SimplySql

PowerShell module for querying various SQL databases
MIT License
196 stars 31 forks source link

mysql connection with ssl #156

Open icabraja opened 2 months ago

icabraja commented 2 months ago

Hello! Is it possible to connect to a mysql server using only ssl without a password with this module?

mithrandyr commented 2 months ago

@icabraja theoretically any known way of connecting should be supported by using the -ConnectionString parameter. the other parameter sets exist to make connecting simpler without having to formulate a connection string, but for the non-common cases, that parameter exists. There is also the -Additional parameter that allows you specify additional connection parameters while using the simpler approach.

if you can provide me a connection string example, I can provide a suggestion on how to make the connection.

icabraja commented 2 months ago

Hello @mithrandyr! Here is an example using MySQL .NET connector $ConnectionString = "Server=$MySQLHost;Port=$MySQLPort;Uid=$MySQLUser;Database=$DatabaseName;SslMode=Required;SslCert=$CertificatePath;SslKey=$KeyPath"

$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection($ConnectionString)

Regards, Ivan

Edit:

If I pass a hash table to a -Additional parameter with ssl cert, key and user i get and error: SslCert and SslKey connection string options are not supported in netstandard2.0

Examlpe:

$ssl = @{ Uid = "ssluser" SslCert = "C:\ssl\client-cert.pem" SslKey = "C:\ssl\client-key.pem" } Open-MySqlConnection -ConnectionName "test" -Server "localhost" -Database "dashboard" -Port "3306" -SSLMode Required -Additional $ssl

I have also tried using the -ConnectionString parameter with above mentioned $ConnectionString and I get the same error. Unfortunately I couldn't find an answer. Btw, the above mentioned $ConnectionString works with MySQL .NET connector.

Regards, Ivan

mithrandyr commented 2 months ago

@icabraja - it looks like a limitation of the MySqlConnector library and .Net standard 2.0 implementations. https://github.com/mysql-net/MySqlConnector/blob/72ee684ed0e04e5f39d6e0757a1963e4aa36807f/src/MySqlConnector/Core/ServerSession.cs#L1516

Because I want SimplySql to work for both PS 5.1 and PS7+, I'm building against .NetStandard 2.0. I'd have to look at a different approach in order to handle this.

icabraja commented 2 months ago

Hello @mithrandyr!

Thank you for the info and your effort, I apricate it.

Is there maybe a way to use ODBC driver with existing DSN or to pass DSN connection values? I have tried to pass to -ConnectionString New-Object System.Data.Odbc.OdbcConnection("DSN=mysql;") where mysql is the name of already configured DSN connection, but i get and error : Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

Regards, Ivan

mithrandyr commented 2 months ago

@icabraja -- -ConnectionString always is just that... a connection string -- it can't be anything else. Now you could try just passing in "DSN=mysql" though I have no idea if that will work.

mithrandyr commented 1 week ago

@icabraja -- The next version (2.1.0, hopefully coming out soon) -- may fix the issue you are having...