Skip to content

Commit 85e3bc5

Browse files
google-labs-jules[bot]pintohutch
authored andcommitted
feat: add the --grafana-api-token-filepath flag to the datasource-syncer
This allows you to specify the Grafana API token through a file, which is more secure than passing it as a command-line argument.
1 parent 7d1b2b1 commit 85e3bc5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

cmd/datasource-syncer/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var (
4343

4444
grafanaAPIToken = flag.String("grafana-api-token", "",
4545
"grafana-api-token used to access Grafana. Can be created using: https://grafana.com/docs/grafana/latest/administration/service-accounts/#create-a-service-account-in-grafana")
46+
grafanaAPITokenFilepath = flag.String("grafana-api-token-filepath", "",
47+
"filepath to a file containing the grafana-api-token used to access Grafana.")
4648

4749
grafanaEndpoint = flag.String("grafana-api-endpoint", "", "grafana-api-endpoint is the endpoint of the Grafana instance that contains the data sources to update.")
4850

@@ -76,11 +78,27 @@ func main() {
7678
os.Exit(1)
7779
}
7880

81+
if *grafanaAPIToken != "" && *grafanaAPITokenFilepath != "" {
82+
//nolint:errcheck
83+
level.Error(logger).Log("msg", "at most one of --grafana-api-token and --grafana-api-token-filepath must be set")
84+
os.Exit(1)
85+
}
86+
87+
if *grafanaAPITokenFilepath != "" {
88+
b, err := os.ReadFile(*grafanaAPITokenFilepath)
89+
if err != nil {
90+
//nolint:errcheck
91+
level.Error(logger).Log("msg", "could not read grafana-api-token-filepath", "err", err)
92+
os.Exit(1)
93+
}
94+
*grafanaAPIToken = string(b)
95+
}
96+
7997
if *grafanaAPIToken == "" {
8098
envToken := os.Getenv("GRAFANA_SERVICE_ACCOUNT_TOKEN")
8199
if envToken == "" {
82100
//nolint:errcheck
83-
level.Error(logger).Log("msg", "--grafana-api-token or the environment variable GRAFANA_SERVICE_ACCOUNT_TOKEN must be set")
101+
level.Error(logger).Log("msg", "at most one of --grafana-api-token, --grafana-api-token-filepath, or the environment variable GRAFANA_SERVICE_ACCOUNT_TOKEN must be set")
84102
os.Exit(1)
85103
}
86104
grafanaAPIToken = &envToken

0 commit comments

Comments
 (0)