Imagestore: Fallback to application default credentials when no key file is specified for GCS (#25948)

The external image storage for GCS creates the JWT Token from a credentials file, 
but if your Grafana server runs under a GCE instance with a service account on it, 
you can use that instead (you don't have to manage/secure the credentials file).

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
Labesse Kévin
2020-07-06 15:02:58 +02:00
committed by GitHub
co-authored by Arve Knudsen Emil Tullstedt
parent 44dff6fdd0
commit 8e7a88faff
2 changed files with 24 additions and 13 deletions
+23 -12
View File
@@ -43,20 +43,31 @@ func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string,
fileName += pngExt
key := path.Join(u.path, fileName)
u.log.Debug("Opening key file ", u.keyFile)
data, err := ioutil.ReadFile(u.keyFile)
if err != nil {
return "", err
var client *http.Client
if u.keyFile != "" {
u.log.Debug("Opening key file ", u.keyFile)
data, err := ioutil.ReadFile(u.keyFile)
if err != nil {
return "", err
}
u.log.Debug("Creating JWT conf")
conf, err := google.JWTConfigFromJSON(data, tokenUrl)
if err != nil {
return "", err
}
u.log.Debug("Creating HTTP client")
client = conf.Client(ctx)
} else {
u.log.Debug("Key file is empty, trying to use application default credentials")
client, err = google.DefaultClient(ctx)
if err != nil {
return "", err
}
}
u.log.Debug("Creating JWT conf")
conf, err := google.JWTConfigFromJSON(data, tokenUrl)
if err != nil {
return "", err
}
u.log.Debug("Creating HTTP client")
client := conf.Client(ctx)
err = u.uploadFile(client, imageDiskPath, key)
if err != nil {
return "", err