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

MS SQL data source: Disable SSL certificate verification doesn't work #24589

Closed
jericvizmanos opened this issue May 12, 2020 · 8 comments · Fixed by #31865
Closed

MS SQL data source: Disable SSL certificate verification doesn't work #24589

jericvizmanos opened this issue May 12, 2020 · 8 comments · Fixed by #31865
Labels
datasource/MSSQL Microsoft SQL Server Data Source help wanted type/bug
Milestone

Comments

@jericvizmanos
Copy link

What would you like to be added:
Disable SSL certificate verification for MS SQL Server Datasource

Why is this needed:
Encryption for MS SQL Server datasource has been added recently on #13629

While most data source allow disabling TLS verification, MS SQL datasource does not. Currently, there is no way to provide the CA Certificate in the datasource's configuration resulting in an error.

Error:
TLS Handshake failed: x509: certificate signed by unknown authority
Screen Shot 2020-05-12 at 9 53 04 AM

@marefr
Copy link
Member

marefr commented May 12, 2020

Have you tried configured encrypt to disable?

@marefr
Copy link
Member

marefr commented May 12, 2020

Also what Grafana version are you using? That feature was implemented quite long time ago and not recently.

@jericvizmanos
Copy link
Author

Hi @marefr - Thanks for checking this out! Yup. If encrypt = false, it goes through properly. However, when you set encrypt = true, it throws the error. Currently, there's no way to attach or place the certificate.

It's for Grafana v6.7.3.

@ivanahuckova ivanahuckova added datasource/MSSQL Microsoft SQL Server Data Source needs investigation for unconfirmed bugs. use type/bug for confirmed bugs, even if they "need" more investigating labels May 13, 2020
@ivanahuckova ivanahuckova changed the title Disable SSL certificate verification MS SQL data source: Disable SSL certificate verification doesn't work May 13, 2020
@aknuds1 aknuds1 added this to Inbox in Backend Platform Backlog via automation May 21, 2020
@marefr marefr removed this from Inbox in Backend Platform Backlog Jun 11, 2020
@marefr marefr added help wanted type/bug and removed needs investigation for unconfirmed bugs. use type/bug for confirmed bugs, even if they "need" more investigating labels Jun 11, 2020
@fifofonix
Copy link

Are we sure that encryption works at all for SQLServer connections?

We are getting the same TLS handshake error despite using a properly signed certificate.

We are using Grafana v6.5.2 (742d165) deployed in docker connecting to SQL Server 2017 (RTM) - 14.0.1000.169 (X64).

Unencrypted connection work fine. And we are able to establish encrypted connections to SQLServer via other tools, e.g. SQLServerMgmtStudio without the need to explicitly trust the certificate (meaning it is validated via machine-configured trusted CAs) and also DBeaver (although because this is Java we need to specify a trust bundle).

Furthermore from the Grafana docker container command line we are able to execute openssl handshakes to other TLS-secured resources with the same CA chain as our SQLServer certificate. And, we are able to complete openssl handshakes to the SQLServer itself - although I should note that this is a bizarre handshake as it shows no cipher selection - but I believe this is because Microsoft does something strange with its secure connections - schannel?

Without a connection-specific means to specify a trusted CA, my assumption is that the docker container's trust chain will be used, but perhaps as OP suggests there may need to be an explicit way to pass in a trusted CA if this is not respected?

@marefr
Copy link
Member

marefr commented Jun 30, 2020

I would suggest have a look at readme/issues at library we're using for connecting to mssql to figure out what's possible missing in Grafana: https://github.com/denisenkom/go-mssqldb#connection-parameters-and-dsn

@fifofonix
Copy link

fifofonix commented Jul 1, 2020

Thanks @marefr. I cloned the repo and was able to run a few tests on my Mac. I have two observations. For the purposes of these I'm presuming that grafana builds a DSN string behind the scenes in the sqlserver:// format that this library supports.

  • Passing TrustServerCertificate=true will solve OP's issue
% export SQLSERVER_DSN=sqlserver://<hostname>?user+id=DOMAIN\\<username>\&encrypt=true\&password=<URL escaped password>\&TrustServerCertificate=true
% go test # passes
  • (Go) Certificate Handling

It seems Go has its own way of specifying trusted platform CAs and on a Mac this does NOT involve using those in KeyChain. For this reason in my tests I received same error as OP when using a validly signed cert on SQLServer. However, the library does support specifying CA certs explicitly and tests of this work:

% export SQLSERVER_DSN=sqlserver://<hostname>?user+id=DOMAIN\\<username>\&encrypt=true\&password=<URL escaped password>\&certificate=/a/local/ca/cert.pem
% go test # passes

Whether the right longer term solution for Grafana is to allow the cert to be explicitly specified via UI or configuration, or to have users modify their local platform installation to include certs in a Go-supported location is up for debate. Regardless, an update to SQLServer Data Source documentation in the short term, so it is understood that encryption with certificate verification requires that certificates for trusted CAs must be in Go-supported locations, may be appropriate.

Testing with a minimal alpine docker container I have validated that the go library will use /etc/ssl/cert.pem for its certificates if it is present, and if not use /etc/ssl/certs/ca-certificates.crt. Even if your CA root of trusts are set correctly in ca-certificates you will get the error message OP posted. In the base alpine Go docker image it is perhaps for this reason that /etc/ssl/cert.pem is symbolically linked to ca-certificates.crt thereby avoiding confusion. This is not the case in grafana/grafana:6.5.2.

If you want to bake a missing CA cert into your own grafana docker image, and have them picked up by the go library the following additions to your dockerfile might do the trick (works for me):

...
USER root
RUN rm /etc/ssl/cert.pem
RUN ln -s /etc/ssl/certs/ca-certificates.crt /etc/ssl/cert.pem
ADD ./<your-ca-cert> /usr/local/share/ca-certificates
RUN update-ca-certificates
USER 472
...

@marefr
Copy link
Member

marefr commented Sep 9, 2020

@fifofonix sorry for the late response. Thanks for great investigation and writing this up, appreciate it. I would suggest you to open a PR if your interesting in getting this feature implemented. Similar functionality have already been added to the Postgres data source so should be pretty straight forward (I think) implementing this looking at the postgres implementation:
image

Postgres frontend code and backend code for reference.

Let me know if you have any questions/concerns

@mortenaa
Copy link
Contributor

Hi, we had this same issue when connecting to a Mssql server with a self signed certificate, so I had a go at fixing it.
#31865

@marefr marefr added this to the 8.4.0 milestone Jan 26, 2022
marefr added a commit that referenced this issue Jan 26, 2022
…31865)

Fixes #24589

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
ryantxu pushed a commit that referenced this issue Jan 26, 2022
…31865)

Fixes #24589

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
ryantxu pushed a commit that referenced this issue Jan 26, 2022
…31865)

Fixes #24589

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datasource/MSSQL Microsoft SQL Server Data Source help wanted type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants