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:

---
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: https://tnglab.fritz.box/prometheus
orgId: 1
isDefault: true
editable: false
access: proxy
basicAuth: true
basicAuthUser: promuser
jsonData:
tlsAuthWithCACert: true
secureJsonData:
basicAuthPassword: change-me
tlsCACert: |
-----BEGIN CERTIFICATE-----
MIIG7jCCBNagAwIBAgIBDzANBgkqhkiG9w0BAQsFADB5MRAwDgYDVQQKEwdSb290
IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB
IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA
Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO
BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi
MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ
ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ
8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6
zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y
fk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AOCAgEAR5zXs6IX01JTt7Rq3b+bNRUhbO9vGBMggczo7R0q
Ih1kdhS6WzcrDoO6PkpuRg0L3qM7YQB6pw2V+ubzF7xl4C0HWltfzPTbzAHdJtja
JQw7QaBlmAYpN2CLB6Jeg8q/1Xpgdw/+IP1GRwdg7xUpReUA482l4MH1kf0W0ad9
4SuIfNWQHcdLApmno/SUh1bpZyeWrMnlhkGNDKMxCCQXQ360TwFHc8dfEAaq5ry6
cZzm1oetrkSviE2qofxvv1VFiQ+9TX3/zkECCsUB/EjPM0lxFBmu9T5Ih+Eqns9i
vmrEIQDv9tNyJHuLsDNqbUBal7OoiPZnXk9LH+qb+pLf1ofv5noy5vX2a5OKebHe
+0Ex/A7e+G/HuOjVNqhZ9j5Nispfq9zNyOHGWD8ofj8DHwB50L1Xh5H+EbIoga/h
JCQnRtxWkHP699T1JpLFYwapgplivF4TFv4fqp0nHTKC1x9gGrIgvuYJl1txIKmx
XdfJzgscMzqpabhtHOMXOiwQBpWzyJkofF/w55e0LttZDBkEsilV/vW0CJsPs3eN
aQF+iMWscGOkgLFlWsAS3HwyiYLNJo26aqyWPaIdc8E4ck7Sk08WrFrHIK3EHr4n
1FZwmLpFAvucKqgl0hr+2jypyh5puA3KksHF3CsUzjMUvzxMhykh9zrMxQAHLBVr
Gwc=
-----END CERTIFICATE-----
...

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.