Skip to content

Commit

Permalink
Fix: allow getting API token from environment
Browse files Browse the repository at this point in the history
Allow the API token to be obtained from the environment variable
SM_AGENT_API_TOKEN. If it's passed on the command line, that value takes
precedence. For backwards compatibility, in case the user decides to NOT
replace the existing configuration files (in Debian these are config
files, and the user has the option to NOT replace them during an upgrade
if they have edited them, and because of the way we have set this up,
they *have* edited them).

In the shipped systemd service file, replace passing the token on the
command line in favor of passing it on the environment.

Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
  • Loading branch information
mem committed Nov 29, 2022
1 parent 2c4c0ed commit d8dc7f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cmd/synthetic-monitoring-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func run(args []string, stdout io.Writer) error {
return nil
}

// If the token is provided on the command line, prefer that. Otherwise
// pull it from the environment variable SM_AGENT_API_TOKEN. If that's
// not available, fallback to API_TOKEN, which was the environment
// variable name previously used in the systemd unit files.
//
// Using API_TOKEN should be deprecated after March 1st, 2023.
*apiToken = stringFromEnv("API_TOKEN", stringFromEnv("SM_AGENT_API_TOKEN", *apiToken))

if *apiToken == "" {
return fmt.Errorf("invalid API token")
}
Expand Down Expand Up @@ -235,3 +243,11 @@ func newConnectionBackoff() *backoff.Backoff {
Jitter: true,
}
}

func stringFromEnv(name string, override string) string {
if override != "" {
return override
}

return os.Getenv(name)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Enter API token retrieved from grafana.com here
API_TOKEN='YOUR TOKEN HERE'
SM_AGENT_API_TOKEN='YOUR TOKEN HERE'

# Backend API server to connect to
API_SERVER='synthetic-monitoring-grpc.grafana.net:443'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Type=simple
User=root
Group=root
EnvironmentFile=/etc/synthetic-monitoring/synthetic-monitoring-agent.conf
ExecStart=/usr/bin/synthetic-monitoring-agent --api-token=${API_TOKEN} --api-server-address=${API_SERVER} --api-insecure=${API_INSECURE} --listen-address=${LISTEN_ADDRESS} --verbose=${VERBOSE} --debug=${DEBUG} --features=${FEATURES}
ExecStart=/usr/bin/synthetic-monitoring-agent --api-server-address=${API_SERVER} --api-insecure=${API_INSECURE} --listen-address=${LISTEN_ADDRESS} --verbose=${VERBOSE} --debug=${DEBUG} --features=${FEATURES}
Restart=always

[Install]
Expand Down

0 comments on commit d8dc7f9

Please sign in to comment.