Skip to content

Onboarding a Team

When several teams share one proxy, give each team a separate identity: a name, a token, and the path patterns its agents may register. A team here is a group that runs its agents.

  • Impact. The proxy reads identities only at startup, so adding one restarts it, and every team's agents reconnect. A proxy that can't read the new identity doesn't start, and while it's down, no team's paths are scraped.
  • Check. Before the restart, confirm the proxy will find the new token, and that no other identity has it (step 3).
  • Change. Make a token (step 1), add the identity (step 2), restart the proxy (step 4), and give the team its token (step 5).
  • Verify. The proxy's log lists the new identity as it starts, and the dashboard shows the team's agents and paths (step 6).
  • Go back. Put back the config step 2 copies, or roll back the Helm release, and every team's agents reconnect (If It Doesn't Come Back).

1. Choose a Prefix and Make a Token

Agree with the team on a prefix that every one of its paths will start with, such as team_b_. Then generate its token into a file that only you can read:

(umask 077 && openssl rand -hex 32 > team-b-token)

The commands below read the token from team-b-token, so run them in the same directory. Make a new token for each team: the proxy refuses to start when two identities, or an identity and the shared proxy.agentToken, have the same one.

2. Add the Identity

This step, and steps 3 and 4, which change the proxy, have a tab for each way it runs, as in the Quick Start. Choosing one opens the same tab in every step. The JAR tabs are for a proxy run by the systemd unit in Running as a Service, and each says what changes for one you run by hand with nohup, as the Quick Start does.

The identity goes in the proxy's config. Its token goes in an environment variable that the config reads, so the config holds no token. Homebrew's service passes the proxy no variables, so there the token goes in the config.

The identity's pattern, team_b_*, lets the team's agents register any path that starts with team_b_: * matches any run of characters, ? exactly one, and any other character only itself, case included. A pattern is one segment, as a path is, so it may start with one / and has no other; see the Security page's path patterns for the rest.

These steps use the systemd unit in Running as a Service, with the config in /etc/prometheus-proxy. For a proxy you run by hand with nohup, as in the Quick Start, keep ./proxy.conf and ./tokens.env in the directory you start it from, and leave out sudo and systemctl edit: step 4 starts it with the variables in ./tokens.env.

First keep a copy of the config the proxy runs with now, to go back to:

sudo cp /etc/prometheus-proxy/proxy.conf /etc/prometheus-proxy/proxy.conf.before-team-b

Add the identity, with its token read from a variable:

proxy.auth = [
  // ...the identities already there
  {
    name = team-b
    token = ${TEAM_B_TOKEN}
    paths = ["team_b_*"]
  }
]

Set TEAM_B_TOKEN in the file the unit reads its environment from, beside the other identities' variables. The steps below use /etc/prometheus-proxy/tokens.env, which only root can read:

sudo touch /etc/prometheus-proxy/tokens.env
sudo chmod 600 /etc/prometheus-proxy/tokens.env
echo "TEAM_B_TOKEN=$(cat team-b-token)" |
  sudo tee -a /etc/prometheus-proxy/tokens.env > /dev/null

If the unit doesn't name that file yet, run sudo systemctl edit prometheus-proxy, add these lines, and save:

[Service]
EnvironmentFile=/etc/prometheus-proxy/tokens.env

brew services starts the proxy without your shell's environment, and the formula's service sets no variables. So the token goes in the service's config, which you make readable only by you. Then keep a copy to go back to:

CONF="$(brew --prefix)/etc/prometheus-proxy.conf"
chmod 600 "$CONF"
cp "$CONF" "$CONF.before-team-b"
# The token, to write in the identity:
cat team-b-token

Add the identity to $(brew --prefix)/etc/prometheus-proxy.conf, with the token in quotes:

proxy.auth = [
  // ...the identities already there
  {
    name = team-b
    token = "<the token>"
    paths = ["team_b_*"]
  }
]

The container reads the proxy's config from a file it mounts, named by PROXY_CONFIG, as in TLS with Docker. It gets its environment from an env file, named by --env-file or Compose's env_file:. The steps below use ./proxy.conf and ./proxy.env, in the directory you run docker from.

Keep a copy of the config, to go back to, and make the env file readable only by you:

cp proxy.conf proxy.conf.before-team-b
touch proxy.env && chmod 600 proxy.env

Add the identity to ./proxy.conf, with its token read from a variable:

proxy.auth = [
  // ...the identities already there
  {
    name = team-b
    token = ${TEAM_B_TOKEN}
    paths = ["team_b_*"]
  }
]

Then add the variable to the env file:

echo "TEAM_B_TOKEN=$(cat team-b-token)" >> proxy.env

Docker reads the env file only when it creates the container, so docker restart doesn't pass the new variable. Step 4 creates the container again.

Put the token in a separate Secret, in the proxy's namespace:

kubectl -n monitoring create secret generic proxy-auth-team-b \
  --from-literal=token="$(cat team-b-token)"

Then add the identity to the values file the proxy's release was installed with, and its variable, read from that Secret. helm upgrade replaces config and env whole, so each keeps every team's entry:

# proxy-values.yaml
config: |
  proxy {
    auth = [
      { name = "team-a", token = ${TEAM_A_TOKEN}, paths = ["team_a_*"] }
      { name = "team-b", token = ${TEAM_B_TOKEN}, paths = ["team_b_*"] }
    ]
  }
env:
  - name: TEAM_A_TOKEN
    valueFrom: { secretKeyRef: { name: proxy-auth-team-a, key: token } }
  - name: TEAM_B_TOKEN
    valueFrom: { secretKeyRef: { name: proxy-auth-team-b, key: token } }

The chart renders config into a ConfigMap, which anyone who can read the namespace's ConfigMaps can read, so the token stays in the Secret. To keep the whole config in a Secret instead, tokens and all, name that Secret in the chart's existingSecret.

3. Check Before You Restart

Check that the proxy, once restarted, will find a token for the new identity, and that no other identity has it:

With the systemd unit:

# Names tokens.env:
systemctl show prometheus-proxy --property=EnvironmentFiles
# 1: set, and not empty:
sudo grep -c '^TEAM_B_TOKEN=.' /etc/prometheus-proxy/tokens.env
# Nothing: no two tokens alike:
sudo cut -d= -f2- /etc/prometheus-proxy/tokens.env | sort | uniq -d

The last check covers the tokens in that file. Compare a token set anywhere else, such as proxy.agentToken in the config, by hand. For a proxy you run by hand, skip the first check, and run the others on your file.

CONF="$(brew --prefix)/etc/prometheus-proxy.conf"
# 1: written once:
grep -cF "$(cat team-b-token)" "$CONF"
# Nothing: no two tokens alike:
grep -Eo '[0-9a-f]{64}' "$CONF" | sort | uniq -d

The last check finds tokens made as step 1 makes them, 64 hexadecimal characters. Compare any other by hand.

# 1: set, and not empty:
grep -c '^TEAM_B_TOKEN=.' proxy.env
# Nothing: no two tokens alike:
grep '_TOKEN=' proxy.env | cut -d= -f2- | sort | uniq -d

The env file can hold other settings, so the last check compares only the variables that end in _TOKEN, the shared token's AGENT_TOKEN among them. Compare a token set anywhere else by hand.

# The Secret the env entry names:
kubectl -n monitoring get secret proxy-auth-team-b
# The revision to go back to:
helm history prometheus-proxy --namespace monitoring

The chart stops the old pod before it starts the new one, and the new pod can't start while a Secret or key that its env names is missing. So check each name in env against the Secrets.

4. Restart the Proxy

Every team's agents lose their connection, and a scrape that lands in between fails. Each agent reconnects on its own within seconds of the proxy being back, trying every agent.internal.reconnectPauseSecs (3 seconds by default), and registers its paths again.

With the systemd unit:

sudo systemctl restart prometheus-proxy
journalctl -u prometheus-proxy -f       # follow its log
Run by hand

A proxy run with nohup, as in the Quick Start's Keep it running, gets its variables from the shell that starts it. Stop it, wait for it to exit, and start it again with the variables in tokens.env:

kill "$(cat proxy.pid)"
# Wait until it has exited and freed its ports:
while kill -0 "$(cat proxy.pid)" 2>/dev/null; do sleep 1; done
# Start it with the file's variables, in a subshell, so they stay out of your shell:
(set -a && . ./tokens.env &&
  exec nohup java -jar prometheus-proxy.jar --config proxy.conf > proxy.log 2>&1) &
echo $! > proxy.pid
tail -f proxy.log   # follow its log
brew services restart prometheus-proxy
tail -f "$(brew --prefix)/var/log/prometheus-proxy.log"   # follow its log

Remove the container and create it again, so it gets the new variable. The command below gives it the ports, the config, and the env file. Add any other option you started it with, such as an --env for TLS:

# The image it runs now, to run again:
IMAGE=$(docker inspect --format '{{.Config.Image}}' prometheus-proxy)
docker rm --force prometheus-proxy
docker run --detach --name prometheus-proxy --restart unless-stopped \
  -p 50051:50051 -p 127.0.0.1:8080:8080 \
  --mount type=bind,source="$(pwd)"/proxy.conf,target=/app/proxy.conf \
  --env PROXY_CONFIG=proxy.conf --env-file proxy.env \
  "$IMAGE"
docker logs --follow prometheus-proxy   # follow its log

With Compose, whose service mounts the config and names the env file in env_file:, recreate the service, proxy here:

docker compose up --detach --force-recreate proxy
docker compose logs --follow proxy   # follow its log
helm upgrade prometheus-proxy ./charts/prometheus-proxy --namespace monitoring --reuse-values -f proxy-values.yaml
kubectl -n monitoring logs deploy/prometheus-proxy -f      # follow its log

The log lists the identities the proxy read, then says it's up:

The proxy's log
Per-agent auth enabled with identities: team-a, team-b
All Proxy services healthy

If It Doesn't Come Back

A proxy that rejects its config logs why, after Invalid configuration:, and exits with status 1. Under systemd, Homebrew's service, Docker's restart policy, or Kubernetes, it starts again and fails the same way. Find the reason in its log, which step 4 follows:

The log says What's wrong
Could not resolve substitution to a value: ${TEAM_B_TOKEN} The variable isn't in the proxy's environment
proxy.auth identity 'team-b' has an empty token The variable is set, but empty
proxy.auth identities share a token: team-a, team-b Another identity has the same token, where legacy-agent-token is the shared proxy.agentToken
Duplicate proxy.auth identity names: team-b Another identity has the name
proxy.auth identity 'team-b' has path pattern 'team_b/*', which no path can match A pattern has a / after its first character

In Kubernetes, a missing Secret or key stops the pod before the proxy runs, so there's no log: kubectl -n monitoring get pods shows the pod in CreateContainerConfigError.

Put the proxy back as it was, and every team's agents reconnect to it. Then correct the change, and make it again from step 2:

With the systemd unit:

sudo cp /etc/prometheus-proxy/proxy.conf.before-team-b /etc/prometheus-proxy/proxy.conf
sudo systemctl restart prometheus-proxy

For a proxy you run by hand, copy the config back the same way, then start the proxy again as in step 4.

CONF="$(brew --prefix)/etc/prometheus-proxy.conf"
cp "$CONF.before-team-b" "$CONF"
brew services restart prometheus-proxy
cp proxy.conf.before-team-b proxy.conf
docker restart prometheus-proxy

The container mounts the file, so a restart reads the copy. The variable the container gained stays, unused. With Compose, run docker compose restart proxy.

helm rollback prometheus-proxy --namespace monitoring   # back to the revision before the upgrade

5. Give the Team Its Token

Send the token the way your organization shares secrets, then delete team-b-token. The team's agents present the token as AGENT_TOKEN, --agent_token, or agent.agentToken. They name their paths with the prefix, as team_b_orders, and keep their other settings, such as the proxy's address.

Unlike the steps before, these tabs are for the team's agents, which needn't run the way the proxy does, so each is a place an agent can take its token from:

The token comes from the agent's environment, through a substitution, so the config holds no token:

agent {
  agentToken = ${TEAM_B_TOKEN}
  pathConfigs = [
    { name = "Orders", path = team_b_orders, url = "http://orders.internal:8080/metrics" }
  ]
}

AGENT_TOKEN takes precedence over the config file's token, and the paths stay in the config file. Here the team keeps the token in a separate file, team-b-token:

# agent.conf lists the agent's paths, each starting with team_b_
AGENT_TOKEN="$(cat team-b-token)" java -jar prometheus-agent.jar --config agent.conf

An agent's container takes it the same way, from --env or an env file.

Put the token in a Secret in the agent's namespace, which the agent's chart reads into AGENT_TOKEN. Here the team keeps the token in a separate file, team-b-token:

kubectl -n team-b create secret generic agent-token \
  --from-literal=token="$(cat team-b-token)"
# agent-values.yaml
proxy:
  hostname: proxy.example.com:50051
agentToken:
  existingSecret: agent-token   # its key is token, the chart's default
config: |
  agent {
    pathConfigs: [
      { name: "Orders", path: team_b_orders, url: "http://orders.team-b.svc.cluster.local:8080/metrics" }
    ]
  }

Tell the team, too, whether the proxy shows relayed agent dashboards on its screens page. With proxy.dashboard.agentScreens on, each agent's relayed dashboard appears there, with its paths, target URLs, scrapes, and activity. Everyone who can sign in to the proxy's dashboard sees it, other teams included, unless the agent sets agent.dashboard.showOnProxy = false (see The Dashboard on a Shared Proxy).

6. Confirm the Team's Agents Are In

With the proxy's dashboard on, three of its views show the team in place:

  • Settings lists team-b and its patterns under Agent authentication.
  • Agents shows each of the team's agents, with team-b as its identity.
  • Paths, searched with ?q=team-b, shows the paths the team's agents registered.

A path outside the team's patterns appears in the Activity view as rejected, with the reason, and on the team's agent dashboard, where it's on, as refused.

With the dashboard off, the proxy's log tells the same. It logs each path it adds, so the team's prefix finds them:

The proxy's log
Added path /team_b_orders for agentId 12

A path outside the team's patterns, and an agent whose token the proxy doesn't know, are logged at WARN:

The proxy's log
Agent identity 'team-b' denied registration of path /orders
Refused an agent from 10.0.4.17 (UNAUTHENTICATED): it sent an unknown agent token

With the proxy's metrics on, proxy_path_rejections_total{cause="not_authorized"} and proxy_agent_auth_failures_total{reason="unknown_token"} count them.

Taking a Team's Access Away

Remove the team's identity from the config, and with Helm, its entry in env. Then restart the proxy as in step 4, with the same check before and the same way back. From then on the proxy refuses the team's agents with UNAUTHENTICATED, and their paths leave it.

Remove the team's variable, or delete its Secret, only once the restarted proxy is up: the config you'd go back to still names it. With Homebrew, the token goes with the identity.