Grafana Datasource With Custom CA Certificate
Today, I had to figure out how to format a Grafana datasource with a custom CA literally for the 99th time. How can this be so hard? Also, it doesn’t help that there is conflicting information on how to format certs in yaml even in the Grafana community threads. That’s always the first page I find about the topic. The second is the stackoverflow post on how to break strings in yaml which sends you down a totally wrong path.
The error messages you get from the Grafana server logs are also not very helpful, given you can access them:
Failed to call resource" error="Get \"https:///tnglab.fritz.box/prometheus...": x509: certificate signed by unknown authority |
Well, thanks for nothing!
So, once and for all! This is the way:
|
This example also includes basic auth. You’re welcome.
Common formatting errors
Just some errors I made myself over and over again.
It’s tlsCACert
, not tlsCaCert
No, it’s not strict CamelCase!
Use the pipe for multiline input
Yes, it’s easy to forget. But start the certificate line like this: tlsCACert: |
Don’t use the greater than symbol
If you use the greater than symbol to start the certificate, all newlines will be removed. That’s not necessary and may not work.
Don’t try modifiers after the pipe
Like 2
, -
, etc… Just don’t!
Don’t use quotes instead of the pipe
They will not preserve the newlines.
Use the correct indentation
Use the same indentation method and depth throughout the file. The certificate starts one line below the tlsCACert: |
and is indented one level more than the tlsCACert
.
The certificate goes into the secureJsonData
block
Yes, I know, for some reason the switch to turn the certificate check off tlsSkipVerify
goes into the jsonData
block. But the tlsCACert
goes into the secureJsonData
block.
Don’t forget the API version
apiVersion: 1
or newer if appropriate must be at the top of the file.
Don’t mix LF/CRLF
Or just don’t use Windows.
Use the correct certificate
Sometimes it helps to display the certificate you are trying to use to notice that it’s the wrong one. Display your certificate details like this:
openssl x509 -noout -text -in path/to/certificate/file.crt |
Don’t forget to switch it on
Use tlsAuthWithCACert: true
in the jsonData
block, so that Grafana will evaluate your CA certificate.
Use the correct URL with https
prefix
That should go without saying. Also, I should have found it sooner.
Wrap
I hope this will help … me! When I have to write the next Grafana datasource file.