StorageDriver GCS: try google.DefaultTokenSource first

Signed-off-by: Arthur Baars <arthur@semmle.com>
This commit is contained in:
Arthur Baars 2016-01-15 11:47:26 +00:00
parent 05dc6404fd
commit 985c0d602f

View file

@ -31,7 +31,7 @@ func init() {
// Skip GCS storage driver tests if environment variable parameters are not provided // Skip GCS storage driver tests if environment variable parameters are not provided
skipGCS = func() string { skipGCS = func() string {
if bucket == "" || credentials == "" { if bucket == "" || credentials == "" {
return "The following environment variables must be set to enable these tests: REGISTRY_STORAGE_GCS_BUCKET, REGISTRY_STORAGE_GCS_CREDS" return "The following environment variables must be set to enable these tests: REGISTRY_STORAGE_GCS_BUCKET, GOOGLE_APPLICATION_CREDENTIALS"
} }
return "" return ""
} }
@ -45,30 +45,36 @@ func init() {
panic(err) panic(err)
} }
defer os.Remove(root) defer os.Remove(root)
var ts oauth2.TokenSource
var email string
var privateKey []byte
_, err = os.Stat(credentials) ts, err = google.DefaultTokenSource(ctx.Background(), storage.ScopeFullControl)
if err == nil {
jsonKey, err := ioutil.ReadFile(credentials)
if err != nil {
panic(fmt.Sprintf("Unable to read credentials from file : %s", err))
}
credentials = string(jsonKey)
}
// Assume that the file contents are within the environment variable since it exists
// but does not contain a valid file path
jwtConfig, err := google.JWTConfigFromJSON([]byte(credentials), storage.ScopeFullControl)
if err != nil { if err != nil {
panic(fmt.Sprintf("Error reading JWT config : %s", err)) // Assume that the file contents are within the environment variable since it exists
// but does not contain a valid file path
jwtConfig, err := google.JWTConfigFromJSON([]byte(credentials), storage.ScopeFullControl)
if err != nil {
panic(fmt.Sprintf("Error reading JWT config : %s", err))
}
email = jwtConfig.Email
privateKey = []byte(jwtConfig.PrivateKey)
if len(privateKey) == 0 {
panic("Error reading JWT config : missing private_key property")
}
if email == "" {
panic("Error reading JWT config : missing client_email property")
}
ts = jwtConfig.TokenSource(ctx.Background())
} }
gcsDriverConstructor = func(rootDirectory string) (storagedriver.StorageDriver, error) { gcsDriverConstructor = func(rootDirectory string) (storagedriver.StorageDriver, error) {
parameters := driverParameters{ parameters := driverParameters{
bucket: bucket, bucket: bucket,
rootDirectory: root, rootDirectory: root,
email: jwtConfig.Email, email: email,
privateKey: []byte(jwtConfig.PrivateKey), privateKey: privateKey,
client: oauth2.NewClient(ctx.Background(), jwtConfig.TokenSource(ctx.Background())), client: oauth2.NewClient(ctx.Background(), ts),
} }
return New(parameters) return New(parameters)