From cbff5ef53bbee9c8329dfbf93338e0b33db0fa2c Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Thu, 1 Mar 2018 16:25:30 -0800 Subject: [PATCH] extract GCP project ID from creds file, remove from config Signed-off-by: Steve Kriss --- docs/config-definition.md | 5 +--- docs/gcp-config.md | 2 +- examples/gcp/00-ark-config.yaml | 2 -- pkg/cloudprovider/gcp/block_store.go | 35 ++++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/docs/config-definition.md b/docs/config-definition.md index a05724ade..00d01836f 100644 --- a/docs/config-definition.md +++ b/docs/config-definition.md @@ -86,9 +86,7 @@ No parameters required. #### persistentVolumeProvider/config -| Key | Type | Default | Meaning | -| --- | --- | --- | --- | -| `project` | string | Required Field | *Example*: "project-example-3jsn23"

See the [Project ID documentation][4] for details. | +No parameters required. ### Azure @@ -107,7 +105,6 @@ No parameters required. [1]: #gcp [2]: #azure [3]: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions -[4]: https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects [5]: https://azure.microsoft.com/en-us/regions/ [6]: #parameter-reference [7]: #main-config-parameters diff --git a/docs/gcp-config.md b/docs/gcp-config.md index be450ac34..a48310b3f 100644 --- a/docs/gcp-config.md +++ b/docs/gcp-config.md @@ -82,7 +82,7 @@ Specify the following values in the example files: * In file `examples/gcp/00-ark-config.yaml`: - * Replace `` and ``. See the [Config definition][7] for details. + * Replace ``. See the [Config definition][7] for details. * In file `examples/common/10-deployment.yaml`: diff --git a/examples/gcp/00-ark-config.yaml b/examples/gcp/00-ark-config.yaml index 28538aef8..647cb3cd2 100644 --- a/examples/gcp/00-ark-config.yaml +++ b/examples/gcp/00-ark-config.yaml @@ -20,8 +20,6 @@ metadata: name: default persistentVolumeProvider: name: gcp - config: - project: backupStorageProvider: name: gcp bucket: diff --git a/pkg/cloudprovider/gcp/block_store.go b/pkg/cloudprovider/gcp/block_store.go index 9433433bb..3d5a57da1 100644 --- a/pkg/cloudprovider/gcp/block_store.go +++ b/pkg/cloudprovider/gcp/block_store.go @@ -17,6 +17,10 @@ limitations under the License. package gcp import ( + "encoding/json" + "io/ioutil" + "os" + "github.com/pkg/errors" uuid "github.com/satori/go.uuid" "golang.org/x/oauth2" @@ -41,10 +45,9 @@ func NewBlockStore() cloudprovider.BlockStore { } func (b *blockStore) Init(config map[string]string) error { - project := config[projectKey] - - if project == "" { - return errors.Errorf("missing %s in gcp configuration", projectKey) + project, err := extractProjectFromCreds() + if err != nil { + return err } client, err := google.DefaultClient(oauth2.NoContext, compute.ComputeScope) @@ -57,7 +60,7 @@ func (b *blockStore) Init(config map[string]string) error { return errors.WithStack(err) } - // validate project + // validate connection res, err := gce.Projects.Get(project).Do() if err != nil { return errors.WithStack(err) @@ -73,6 +76,28 @@ func (b *blockStore) Init(config map[string]string) error { return nil } +func extractProjectFromCreds() (string, error) { + credsBytes, err := ioutil.ReadFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")) + if err != nil { + return "", errors.WithStack(err) + } + + type credentials struct { + ProjectID string `json:"project_id"` + } + + var creds credentials + if err := json.Unmarshal(credsBytes, &creds); err != nil { + return "", errors.WithStack(err) + } + + if creds.ProjectID == "" { + return "", errors.New("cannot fetch project_id from GCP credentials file") + } + + return creds.ProjectID, nil +} + func (b *blockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ string, iops *int64) (volumeID string, err error) { res, err := b.gce.Snapshots.Get(b.project, snapshotID).Do() if err != nil {