Basic auth from environment variable

1,328 views
Skip to first unread message

Christian Oelsner

unread,
Apr 21, 2023, 5:49:42 PM4/21/23
to Prometheus Users
Hello,
I am running prometheus in Kubernetes using prometheus helm chart.

One of my targets require basic authentication in the for of an api key for "username" and an api secret for "password.
I have created them as a kubernetes secret and in the helm values in the env: section i have
- name: BASIC_USER
valueFrom:
secretKeyRef:
name: basic-auth
key: username
- name: BASIC_PW
valueFrom:
secretKeyRef:
name: basic-auth
key: password
When exec in to the pod and doing a printenv i can see that the vars are present, echo'ing them out also gives the expected values.

Now comes my challenge, my scrape job requires authentication, and i am trying to reference the variables like so:
basic_auth:
username: $BASIC_USER
password: $BASIC_PW

But for the life of me, i cant seem to get prometheus to pick it up in the scrape job.
Am i missing the obvious?

If it matters, i am scraping confluent cloud metrics.

Best regards
Christian Oelsner

Brian Candler

unread,
Apr 21, 2023, 6:43:16 PM4/21/23
to Prometheus Users
> But for the life of me, i cant seem to get prometheus to pick it up in the scrape job.
> Am i missing the obvious?

Environment-variable substitution is not performed in prometheus' config file.

You could expose the secret containing the password as a file, rather than an environment variable:

basic_auth:
  username: some_static_username
  password_file: /path/to/file

The username, though, still has to be included inline.

Since you're deploying via helm, there might be some way the helm chart lets you insert these values into the prometheus configuration.

Christian Oelsner

unread,
Apr 22, 2023, 3:00:04 PM4/22/23
to Prometheus Users
Hello Brian,
thank you for your input. I will give a try and see where that takes me :) 

Best regards
Christian Oelsner

Christian Oelsner

unread,
Apr 23, 2023, 4:36:46 PM4/23/23
to Prometheus Users
Hi again Brian,
That worked nicely for the password :)

Now i just need to figure out a way to something similar with username, as i know that my organization architects will be poiting fingers at me if i commit something to git with a username in it :)

Your help is as usual much appreciated :)

Best

Christian Oelsner

Christian Oelsner

unread,
Apr 23, 2023, 4:38:24 PM4/23/23
to Prometheus Users
Oh,

You mentioned that there might be a way to sort in the helm chart....
I am using the "official" helm chart, and while there might be a way to accomplish it, i have yet to find it.
I will keep digging aroung though :)

/Oelsner

Brian Candler

unread,
Apr 24, 2023, 3:16:52 AM4/24/23
to Prometheus Users
You must be getting that config snippet into prometheus via the helm chart somehow.  If you are just providing the snippet verbatim, then you could do some preprocessing of it. If there's no hook to do this at deployment time via the helm chart, then you could do it at runtime, e.g. via an init container.

Note that prometheus very recently (v2.43.0) added the ability to read additional scrape config files referenced from the main config file:

# Scrape config files specifies a list of globs. Scrape configs are read from # all matching files and appended to the list of scrape configs. scrape_config_files: [ - <filepath_glob> ... ]
Reply all
Reply to author
Forward
0 new messages