From 0593b64674aab6828aee3ad05bea55fd0dc2476f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Lu=CC=88dtke?= Date: Wed, 30 May 2018 20:20:21 +0200 Subject: [PATCH] Fix gcloud naming inconsistency; parse projectID (#425) This commit fixes the naming inconsistency of Google Cloud DNS being named `googlecloud` in the source and `gcloud` in the user interface. Furthermore, improving my previous commit, I realized that the project ID is stored in the service account file. We can save one environment variable when using service account file (e.g. in automation applications). --- providers/dns/dns_providers.go | 4 +-- .../{googlecloud => gcloud}/googlecloud.go | 25 +++++++++++++------ .../googlecloud_test.go | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) rename providers/dns/{googlecloud => gcloud}/googlecloud.go (90%) rename providers/dns/{googlecloud => gcloud}/googlecloud_test.go (99%) diff --git a/providers/dns/dns_providers.go b/providers/dns/dns_providers.go index 83693d20..d0002861 100644 --- a/providers/dns/dns_providers.go +++ b/providers/dns/dns_providers.go @@ -20,9 +20,9 @@ import ( "github.com/xenolf/lego/providers/dns/fastdns" "github.com/xenolf/lego/providers/dns/gandi" "github.com/xenolf/lego/providers/dns/gandiv5" + "github.com/xenolf/lego/providers/dns/gcloud" "github.com/xenolf/lego/providers/dns/glesys" "github.com/xenolf/lego/providers/dns/godaddy" - "github.com/xenolf/lego/providers/dns/googlecloud" "github.com/xenolf/lego/providers/dns/lightsail" "github.com/xenolf/lego/providers/dns/linode" "github.com/xenolf/lego/providers/dns/namecheap" @@ -75,7 +75,7 @@ func NewDNSChallengeProviderByName(name string) (acme.ChallengeProvider, error) case "glesys": provider, err = glesys.NewDNSProvider() case "gcloud": - provider, err = googlecloud.NewDNSProvider() + provider, err = gcloud.NewDNSProvider() case "godaddy": provider, err = godaddy.NewDNSProvider() case "lightsail": diff --git a/providers/dns/googlecloud/googlecloud.go b/providers/dns/gcloud/googlecloud.go similarity index 90% rename from providers/dns/googlecloud/googlecloud.go rename to providers/dns/gcloud/googlecloud.go index dc46152f..4a938709 100644 --- a/providers/dns/googlecloud/googlecloud.go +++ b/providers/dns/gcloud/googlecloud.go @@ -1,8 +1,9 @@ -// Package googlecloud implements a DNS provider for solving the DNS-01 +// Package gcloud implements a DNS provider for solving the DNS-01 // challenge using Google Cloud DNS. -package googlecloud +package gcloud import ( + "encoding/json" "fmt" "io/ioutil" "os" @@ -27,10 +28,10 @@ type DNSProvider struct { // A Service Account file can be passed in the environment variable: // GCE_SERVICE_ACCOUNT_FILE func NewDNSProvider() (*DNSProvider, error) { - project := os.Getenv("GCE_PROJECT") if saFile, ok := os.LookupEnv("GCE_SERVICE_ACCOUNT_FILE"); ok { - return NewDNSProviderServiceAccount(project, saFile) + return NewDNSProviderServiceAccount(saFile) } + project := os.Getenv("GCE_PROJECT") return NewDNSProviderCredentials(project) } @@ -57,10 +58,7 @@ func NewDNSProviderCredentials(project string) (*DNSProvider, error) { // NewDNSProviderServiceAccount uses the supplied service account JSON file to // return a DNSProvider instance configured for Google Cloud DNS. -func NewDNSProviderServiceAccount(project string, saFile string) (*DNSProvider, error) { - if project == "" { - return nil, fmt.Errorf("Google Cloud project name missing") - } +func NewDNSProviderServiceAccount(saFile string) (*DNSProvider, error) { if saFile == "" { return nil, fmt.Errorf("Google Cloud Service Account file missing") } @@ -69,6 +67,17 @@ func NewDNSProviderServiceAccount(project string, saFile string) (*DNSProvider, if err != nil { return nil, fmt.Errorf("Unable to read Service Account file: %v", err) } + + // read project id from service account file + var datJSON struct { + ProjectID string `json:"project_id"` + } + err = json.Unmarshal(dat, &datJSON) + if err != nil || datJSON.ProjectID == "" { + return nil, fmt.Errorf("Project ID not found in Google Cloud Service Account file") + } + project := datJSON.ProjectID + conf, err := google.JWTConfigFromJSON(dat, dns.NdevClouddnsReadwriteScope) if err != nil { return nil, fmt.Errorf("Unable to acquire config: %v", err) diff --git a/providers/dns/googlecloud/googlecloud_test.go b/providers/dns/gcloud/googlecloud_test.go similarity index 99% rename from providers/dns/googlecloud/googlecloud_test.go rename to providers/dns/gcloud/googlecloud_test.go index 70b0e730..ea171e2a 100644 --- a/providers/dns/googlecloud/googlecloud_test.go +++ b/providers/dns/gcloud/googlecloud_test.go @@ -1,4 +1,4 @@ -package googlecloud +package gcloud import ( "os"