JDBC connection to Cloud SQL PostgreSQL is failing - postgresql

I am trying to connect Cloud SQL Postgres using JDBC Using SSL without Certificate Validation.
The option I passed to the JDBC connection is "?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"
The connection fails with the below error:
[Permission denied] PSQLException: FATAL: connection requires a valid
client certificate
It seems the connect string options "sslfactory=org.postgresql.ssl.NonValidatingFactory" is not considered.
Can you please advise how to connect SSL Cloud SQL PostgreSQL without validating SSL certificate?

The server is demanding a client certificate to prove the identity of the client. This is an unusual setting, but if that is how the server is configured (via pg_hba.conf) you can't make it change its mind from the client side, other than perhaps switching to a connection method that matches a different pg_hba.conf line. But of course you can't do that unless another suitable pg_hba.conf entry exists, which is not under the client's control.
Talk to whoever is in charge of the server about either changing the configuration, or granting you a client cert.
There is no such thing as a non-validating client cert. It would be pointless: if you don't want to validate the client cert you just wouldn't demand one. If you just want encryption without validation, then the usual server cert (without validation) is entirely sufficient to do that.

Related

So how does this SSL/Secure established?

Verified AZ pg require_secure_transport = "on"
Without any ssl and no SET PGSSLMODE=require, following command in Windows' cmd succeeded:
pg_restore --format=custom -d "port=5432 host=mypg.postgres.database.azure.com user=myuser dbname=mydb" my.dump
Checking AZ's Log Analytics workspace shows
connection authorized: user=myuser database=mydb
application_name=pg_restore SSL enabled (protocol=TLSv1.3,
cipher=TLS_AES_256_GCM_SHA384, bits=256, compression=off)
So how does this SSL/Secure established or it's enabled but operation isn't on SSL channel?
pg_restore is based on libpq, and follows its conventions in this area. An unset sslmode has the default behavior of 'prefer'. It first attempts to set up an SSL connection, then tries without SSL only if that first attempt fails. So 'prefer' is identical to 'require' provided the first attempt does succeed. If you want to prove that your servers setting require_secure_transport = "on" is doing something, you should try setting sslmode=disable on the client. Then you should see a failure driven by that mismatch between client and server.
In your comment you mention sslcert and sslkey. Those are used only for client certificate authentication. This is optional, unusual, and as far as I know is not even supported by Azure's hosted PostgreSQL. SSL is generally done only with a server certificate, not with a client certificate (for example, pretty much all of the web over https uses server certs only, it isn't just PostgreSQL).
By what is unusual about libpq (in the world of SSL) is that by default, the server's cert does not need to be verified. By default, the cert is just used as a way to negotiate Diffie-Hellman-like key exchange that protects you from eavesdropping but not from impersonation. If you want to verify that the server you connect to is the right one, you would need to set sslmode=verify-full, as well as configure sslrootcert. This is all controlled by the client. While the server can insist that the client use SSL, it has no way to insist that the client actually verify the server's certificate.

How do i connect from FluentNhibernate to postgres using ssl authentication

We were able to establish a connection with regular (username and password)
now we want to connect via ssl.
I'm trying to connect to postgresql server with nhibernate (v4.0) using ssl certificate.
I'm getting error message "connection requires a valid client certificate.
Where I need to save the key on the server for the nhibernate to use it.
or how to specify the path for the cert and the key.
The error Is in postgres, but the nhibernate doesn't bring the certificate. how do I configure nhibernate to provide the certificate? please help..
I'v tried adding sslkey, sslcert paramaters to the connection string
with no luck.
var config = Flunety.Configure().Datebase(PostgresSQLConfiguration.Standart.ConnectionString(x=>x.FromConnectionStringWithKey(ConnectionString)) ….
connectionString = "Server=server;Port=5432;User Id=username;sslMode=require;
FATAL 28000 connection requires a valid client certificate.
Assuming you're using Npgsql, see this section in the docs: the NpgsqlConnection type exposes two callbacks for SSL/TLS operation, which it simply forwards to the internally-used SslStream.
I'm not sure if/how NHibernate exposes these, but it should be possible to get (or provide) an instance of NpgsqlConnection on which you can set these callbacks.

How secure npgsql connection with ssl option

i use npgsql version 2270 and i would to secure my connection passing db password NOT in cleartext and with capability to use SSL connection on a preconfigured postgresql server. My doubt is about how set the connection string with npgsql.
I tryed adding ssl=true sslmode=prefer to conn string but i have not idea about how to verify if it work.
Is sufficient for secure data transit? is string correct?
Is needed to activate ssl also on server is right?
How can i verify if data transit on ssl mode ? Thanks all.
First, SSL isn't really about encrypting your password - it's about encrypting the entire stream. If you use PostgreSQL's md5 authentication method (see your pg_hba.conf), then passwords are never transferred in cleartext regardless of whether you're using SSL or not.
To use SSL, it's better to set SSL Mode to require rather than prefer: the latter will attempt to establish an SSL connection, but will fallback to non-SSL if the server doesn't support it. require will fail the connection attempt if SSL could not be established.
Regardless, yes, you need to set up SSL on PostgreSQL, including a certificate and a private key - you can read about this in the documentation.
If the aim is only to avoid sending passwords in cleartext, using md5 (or sha256 in more recent versions) is the simplest way.

Npgsql 3.0.3 error with Power BI Desktop

I'm receiving the following error when connecting to an AWS Postgres database that requires SSL. I recently upgraded from npgsql 2.3.2 (which was buggy) to 3.0.3 which won't connect. Any suggestions would be appreciated.
DataSource.Error: TlsClientStream.ClientAlertException:
CertificateUnknown: Server certificate was not accepted. Chain status:
A certificate chain could not be built to a trusted root authority. .
at TlsClientStream.TlsClientStream.ParseCertificateMessage(Byte[] buf,
Int32& pos) at
TlsClientStream.TlsClientStream.TraverseHandshakeMessages() at
TlsClientStream.TlsClientStream.GetInitialHandshakeMessages(Boolean
allowApplicationData) at
TlsClientStream.TlsClientStream.PerformInitialHandshake(String
hostName, X509CertificateCollection clientCertificates,
RemoteCertificateValidationCallback
remoteCertificateValidationCallback, Boolean
checkCertificateRevocation) Details:
DataSourceKind=PostgreSQL
I was able to fix the issue by installing the Amazon RDS public certificate on my machine. Once I did this, I was able to connect.
Steps I followed:
Download the AWS RDS public certificate 1
Create a .crt file from the .pem file downloaded. Sample instructions
here 2
Install the certificate (.crt file) on the machine. 3
Connect!
The docs from npgsql give the solution as changing the default trust server certificate of 'false' to 'true' in the connection string.
Unfortunately, neither Excel (AFAIK) nor Power BI will allow you to edit the connection string. So if you are unable to get the SSL certificate from the DB admin (as suggested in another answer), or the SSL cert has a different server name to the name you connect to (in my case an IP address), there is not much that can be done.
I can see two ways of fixing this. Either Shay & co from npgsql (who are doing an excellent job btw) provide some way for users to change the default settings for the connection string parameters. Or Microsoft allows users to send keywords in the connection dialog of Power BI (and Excel).
Npgsql 2.x didn’t perform validation on the server’s certificate by default, so self-signed certificate were accepted. The new default is to perform validation, which is probably why your connection is failing. Specify the Trust Server Certificate connection string parameter to get back previous behavior.
You can read more on the Npgsql security doc page, note also that this change is mentioned in our migration notes.
I had the same issue connecting PowerBI to a locally hosted PostgreSQL server and it turned out to be easy to solve if you can get the right information. Recent Npgsql versions will only connect over SSL if it trusts the certificate of the server. As a Windows application PowerBI uses the windows certificate store to decide what to trust. If you can get the SSL cert for the PostgreSQL server (or the CA cert used to sign that one) then tell Windows to trust that certificate, PowerBI will trust it too.
In the configuration folder for the PostgreSQL server there is a postgresql.conf file, search it for ssl settings, there is one with the location of the ssl cert. Note NOT the key file which contains the private key, only the cert file which contains the public key. copy it or its content to the machine running PowerBI and import using Run | mmc | Add Plugin... Certificates (Google it)
Look at the server name once you imported the cert and connect from PowerBI using the same server name (so the cert matches the connection). That solved the problem for me. If PostgreSQL is configured to insist on a SSL connection you might have to do the same for a ODBC connection too.
Its not best way but worked for me since if u dont need encryption for security reason.
Go to Postgres config file on your DB server and go from
ssl = true
to
ssl = false
Then open your power bi desktop File-> Options and settings -> Data source settings -> then in global you will have saved your connection press Edit Permissions and uncheck "ENCRYPT CONNECTIONS"
Then it will work
WARNING: THIS IS NOT RECOMMENDED IF YOUR DB IS OPEN TO PUBLIC.
Regards,
Davlik

PostgreSQL : SSL with TCP connections

I am trying to configure SSL communication for PostgreSQL.
I have a self-signed cerificate and I am following these instructions as specified in URL "http://www.postgresql.org/docs/9.2/static/ssl-tcp.html".
When I configure SSL with a self signed certificate, as per the documentation the server will prompt for a (private key's password) password when it start-up.
My PostgreSQL is installed as a windows service.So, I want my service to start without any user intervention. Is there a way to configure private key password so that my database service starts up normally.
This would be a bad idea even if it were possible. You get very little additional security by putting both the key and the passphrase where the server process can see it. This adds a false sense of security and a greater chance of bad things happening.
If your tool won't give you a passphraseless key, use a different tool.